| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {CommitInfo, Hash} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {atom, useSetAtom} from 'jotai'; |
| b69ab31 | | | 11 | import {useEffect, useState} from 'react'; |
| b69ab31 | | | 12 | import {atomFamilyWeak} from './jotaiUtils'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | export const highlightedCommits = atom<Set<Hash>>(new Set<Hash>()); |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | export const isHighlightedCommit = atomFamilyWeak((hash: Hash) => |
| b69ab31 | | | 17 | atom(get => get(highlightedCommits).has(hash)), |
| b69ab31 | | | 18 | ); |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | export function HighlightCommitsWhileHovering({ |
| b69ab31 | | | 21 | toHighlight, |
| b69ab31 | | | 22 | children, |
| b69ab31 | | | 23 | ...rest |
| b69ab31 | | | 24 | }: { |
| b69ab31 | | | 25 | toHighlight: Array<CommitInfo | Hash>; |
| b69ab31 | | | 26 | children: React.ReactNode; |
| b69ab31 | | | 27 | } & React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>) { |
| b69ab31 | | | 28 | const setHighlighted = useSetAtom(highlightedCommits); |
| b69ab31 | | | 29 | const [isSourceOfHighlight, setIsSourceOfHighlight] = useState(false); |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | useEffect(() => { |
| b69ab31 | | | 32 | return () => { |
| b69ab31 | | | 33 | if (isSourceOfHighlight) { |
| b69ab31 | | | 34 | // if we started the highlight, make sure to unhighlight when unmounting |
| b69ab31 | | | 35 | setHighlighted(new Set()); |
| b69ab31 | | | 36 | } |
| b69ab31 | | | 37 | }; |
| b69ab31 | | | 38 | }, [isSourceOfHighlight, setHighlighted]); |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | return ( |
| b69ab31 | | | 41 | <div |
| b69ab31 | | | 42 | {...rest} |
| b69ab31 | | | 43 | onMouseOver={() => { |
| b69ab31 | | | 44 | setHighlighted( |
| b69ab31 | | | 45 | new Set( |
| b69ab31 | | | 46 | toHighlight.map(commitOrHash => |
| b69ab31 | | | 47 | typeof commitOrHash === 'string' ? commitOrHash : commitOrHash.hash, |
| b69ab31 | | | 48 | ), |
| b69ab31 | | | 49 | ), |
| b69ab31 | | | 50 | ); |
| b69ab31 | | | 51 | setIsSourceOfHighlight(true); |
| b69ab31 | | | 52 | }} |
| b69ab31 | | | 53 | onMouseOut={() => { |
| b69ab31 | | | 54 | setHighlighted(new Set()); |
| b69ab31 | | | 55 | setIsSourceOfHighlight(false); |
| b69ab31 | | | 56 | }}> |
| b69ab31 | | | 57 | {children} |
| b69ab31 | | | 58 | </div> |
| b69ab31 | | | 59 | ); |
| b69ab31 | | | 60 | } |