From 2822509ff8d67a61674b9db042e18f48bb34c716 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Mon, 12 Jun 2023 22:06:15 +0600 Subject: [PATCH] refactor: copy button --- src/components/RoadCard/LongBadge.tsx | 19 ++++++++++++------- src/components/RoadCard/RoadCardPage.tsx | 23 +++++------------------ src/components/RoadCard/WideBadge.tsx | 14 +++++++++----- src/hooks/use-is-copied.ts | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 src/hooks/use-is-copied.ts diff --git a/src/components/RoadCard/LongBadge.tsx b/src/components/RoadCard/LongBadge.tsx index 82d727e8b..2cfff1b3e 100644 --- a/src/components/RoadCard/LongBadge.tsx +++ b/src/components/RoadCard/LongBadge.tsx @@ -1,6 +1,9 @@ +import { useIsCopied } from '../../hooks/use-is-copied'; import type { BadgeProps } from './RoadCardPage'; export function LongBadge({ badgeUrl }: BadgeProps) { + const { isCopied, handleCopy } = useIsCopied(); + return (
- - Copy Link + Download +
); diff --git a/src/components/RoadCard/RoadCardPage.tsx b/src/components/RoadCard/RoadCardPage.tsx index 6020c2afb..86efc5110 100644 --- a/src/components/RoadCard/RoadCardPage.tsx +++ b/src/components/RoadCard/RoadCardPage.tsx @@ -5,6 +5,7 @@ import { WideBadge } from './WideBadge'; import { LongBadge } from './LongBadge'; import CopyIcon from '../../icons/copy.svg'; +import { useIsCopied } from '../../hooks/use-is-copied'; export type BadgeProps = { badgeUrl: string; @@ -12,7 +13,8 @@ export type BadgeProps = { export function RoadCardPage() { const [selectedBadge, setSelectedBadge] = useState<'long' | 'wide'>('long'); - const [isCopied, setIsCopied] = useState(false); + const { isCopied, handleCopy } = useIsCopied(); + const token = Cookies.get(TOKEN_COOKIE_NAME); if (!token) { return null; @@ -36,21 +38,6 @@ export function RoadCardPage() { `.trim(); - const handleCopyEmbed = () => { - navigator.clipboard.writeText(textareaContent); - setIsCopied(true); - }; - - useEffect(() => { - let timeout: ReturnType; - if (isCopied) { - timeout = setTimeout(() => { - setIsCopied(false); - }, 2000); - } - return () => clearTimeout(timeout); - }, [isCopied]); - return ( <>
@@ -98,11 +85,11 @@ export function RoadCardPage() { {selectedBadge === 'wide' && }
-
+
Embed