Skip to content

Commit

Permalink
fix: "Copied" notification hidden for web & desktop platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
siandreev committed Mar 13, 2024
1 parent 1546550 commit 0b65e1c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion apps/desktop/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export const Loader: FC = () => {
>
<AppContext.Provider value={context}>
<Content activeWallet={activeWallet} lock={lock} />
<CopyNotification />
<CopyNotification hideSimpleCopyNotifications />
<QrScanner />
</AppContext.Provider>
</AfterImportAction.Provider>
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export const Loader: FC = () => {
>
<AppContext.Provider value={context}>
<Content activeWallet={activeWallet} lock={lock} standalone={standalone} />
<CopyNotification />
<CopyNotification hideSimpleCopyNotifications />
<Suspense fallback={<></>}>
<QrScanner />
</Suspense>
Expand Down
92 changes: 52 additions & 40 deletions packages/uikit/src/components/CopyNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,59 @@ const Content = styled.div`
box-shadow: 0px 4px 16px rgba(0, 0, 0, 0.16);
`;

export const CopyNotification: FC = React.memo(() => {
const { t } = useTranslation();
const [isOpen, setOpen] = useState<boolean>(false);
const [text, setText] = useState<string>(t('copied'));
const sdk = useAppSdk();
export const CopyNotification: FC<{ hideSimpleCopyNotifications?: boolean }> = React.memo(
({ hideSimpleCopyNotifications }) => {
const { t } = useTranslation();
const [isOpen, setOpen] = useState<boolean>(false);
const [text, setText] = useState<string>(t('copied'));
const sdk = useAppSdk();

useEffect(() => {
let timer: NodeJS.Timeout | null = null;
const handler = (options: { method: 'copy'; id?: number | undefined; params: string }) => {
if (timer) {
clearTimeout(timer);
}
setText(options.params ?? t('copied'));
setOpen(true);
timer = setTimeout(() => {
setOpen(false);
}, 2000);
};
sdk.uiEvents.on('copy', handler);
return () => {
sdk.uiEvents.off('copy', handler);
};
}, []);
useEffect(() => {
let timer: NodeJS.Timeout | null = null;
const handler = (options: {
method: 'copy';
id?: number | undefined;
params: string;
}) => {
if (timer) {
clearTimeout(timer);
}

const nodeRef = useRef(null);
if (hideSimpleCopyNotifications && !options.params) {
return;
// hide 'Copy' notification
}

return (
<ReactPortal wrapperId="react-copy-modal">
<CSSTransition
in={isOpen}
timeout={{ enter: 0, exit: 300 }}
unmountOnExit
nodeRef={nodeRef}
>
<Message ref={nodeRef}>
<Content>
<Label2 onClick={() => sdk.copyToClipboard(text)}>{text}</Label2>
</Content>
</Message>
</CSSTransition>
</ReactPortal>
);
});
setText(options.params ?? t('copied'));
setOpen(true);
timer = setTimeout(() => {
setOpen(false);
}, 2000);
};
sdk.uiEvents.on('copy', handler);
return () => {
sdk.uiEvents.off('copy', handler);
};
}, [hideSimpleCopyNotifications]);

const nodeRef = useRef(null);

return (
<ReactPortal wrapperId="react-copy-modal">
<CSSTransition
in={isOpen}
timeout={{ enter: 0, exit: 300 }}
unmountOnExit
nodeRef={nodeRef}
>
<Message onClick={() => setOpen(false)} ref={nodeRef}>
<Content>
<Label2 onClick={() => sdk.copyToClipboard(text)}>{text}</Label2>
</Content>
</Message>
</CSSTransition>
</ReactPortal>
);
}
);
CopyNotification.displayName = 'CopyNotification';

0 comments on commit 0b65e1c

Please sign in to comment.