2021-10-22 18:50:31 +00:00
|
|
|
import React, { useMemo } from "react";
|
2021-07-14 07:24:28 +00:00
|
|
|
import { useSelectionContext } from "../useSelection";
|
|
|
|
|
2021-07-14 19:17:27 +00:00
|
|
|
type AddressHighlighterProps = React.PropsWithChildren<{
|
2021-07-14 07:24:28 +00:00
|
|
|
address: string;
|
2021-07-14 19:17:27 +00:00
|
|
|
}>;
|
2021-07-14 07:24:28 +00:00
|
|
|
|
|
|
|
const AddressHighlighter: React.FC<AddressHighlighterProps> = ({
|
|
|
|
address,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const [selection, setSelection] = useSelectionContext();
|
2021-10-22 18:50:31 +00:00
|
|
|
const [select, deselect] = useMemo(() => {
|
|
|
|
const _select = () => {
|
|
|
|
setSelection({ type: "address", content: address });
|
|
|
|
};
|
|
|
|
const _deselect = () => {
|
|
|
|
setSelection(null);
|
|
|
|
};
|
|
|
|
return [_select, _deselect];
|
|
|
|
}, [setSelection, address]);
|
2021-07-14 07:24:28 +00:00
|
|
|
|
|
|
|
return (
|
2021-10-27 18:49:15 +00:00
|
|
|
<AddressHighlighterImpl
|
|
|
|
selected={
|
2021-07-14 07:24:28 +00:00
|
|
|
selection !== null &&
|
|
|
|
selection.type === "address" &&
|
|
|
|
selection.content === address
|
2021-10-27 18:49:15 +00:00
|
|
|
}
|
|
|
|
select={select}
|
|
|
|
deselect={deselect}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</AddressHighlighterImpl>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
type _AddressHighlighterImplProps = {
|
|
|
|
selected: boolean;
|
|
|
|
select: () => void;
|
|
|
|
deselect: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const AddressHighlighterImpl: React.FC<_AddressHighlighterImplProps> =
|
|
|
|
React.memo(({ selected, select, deselect, children }) => (
|
|
|
|
<div
|
|
|
|
className={`border border-dashed rounded hover:bg-transparent hover:border-transparent px-1 truncate ${
|
|
|
|
selected ? "border-orange-400 bg-yellow-100" : "border-transparent"
|
2021-07-14 07:24:28 +00:00
|
|
|
}`}
|
|
|
|
onMouseEnter={select}
|
|
|
|
onMouseLeave={deselect}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
2021-10-27 18:49:15 +00:00
|
|
|
));
|
2021-07-14 07:24:28 +00:00
|
|
|
|
2021-10-27 18:49:15 +00:00
|
|
|
export default AddressHighlighter;
|