import React, { useMemo } from "react"; import { useSelectionContext, OptionalSelection, SelectionType, } from "../useSelection"; export type ContentSelector = ( selection: OptionalSelection, content: string ) => boolean; export const genericSelector = (type: SelectionType) => (selection: OptionalSelection, content: string): boolean => selection !== null && selection.type === type && selection.content === content; export const addressSelector: ContentSelector = genericSelector("address"); export const valueSelector: ContentSelector = genericSelector("value"); export const functionSigSelector: ContentSelector = genericSelector("functionSig"); type SelectionHighlighterProps = React.PropsWithChildren<{ myType: SelectionType; myContent: string; selector: ContentSelector; }>; const SelectionHighlighter: React.FC = ({ myType, myContent, selector, children, }) => { const [selection, setSelection] = useSelectionContext(); const [select, deselect] = useMemo(() => { const _select = () => { setSelection({ type: myType, content: myContent }); }; const _deselect = () => { setSelection(null); }; return [_select, _deselect]; }, [setSelection, myType, myContent]); return ( {children} ); }; type HighlighterBoxProps = { selected: boolean; select: () => void; deselect: () => void; }; const HighlighterBox: React.FC = React.memo( ({ selected, select, deselect, children }) => (
{children}
) ); export default SelectionHighlighter;