diff --git a/components/post/CommentsSection.tsx b/components/post/CommentsSection.tsx index e1f39c4..9fab2d2 100644 --- a/components/post/CommentsSection.tsx +++ b/components/post/CommentsSection.tsx @@ -18,13 +18,23 @@ interface CommentsResponse { function CommentAnchorLink({ commentId }: { commentId: string }) { const [copied, setCopied] = useState(false); + const [failed, setFailed] = useState(false); - function copy() { + async function copy() { const url = `${window.location.origin}${window.location.pathname}#c-${commentId}`; - navigator.clipboard.writeText(url).then(() => { + if (!navigator.clipboard) { + setFailed(true); + setTimeout(() => setFailed(false), 1500); + return; + } + try { + await navigator.clipboard.writeText(url); setCopied(true); setTimeout(() => setCopied(false), 1500); - }); + } catch { + setFailed(true); + setTimeout(() => setFailed(false), 1500); + } } return ( @@ -33,7 +43,7 @@ function CommentAnchorLink({ commentId }: { commentId: string }) { className="font-mono text-[10px] text-text-tertiary opacity-0 transition-opacity group-hover:opacity-100 hover:text-text-secondary" title="Copy link to comment" > - {copied ? "Copied" : "#"} + {copied ? "Copied" : failed ? "Failed" : "#"} ); } diff --git a/components/post/CopyLinkButton.tsx b/components/post/CopyLinkButton.tsx index 8716988..b6482e4 100644 --- a/components/post/CopyLinkButton.tsx +++ b/components/post/CopyLinkButton.tsx @@ -5,19 +5,35 @@ import { getSiteUrl } from "@/lib/utils/urls"; export function CopyLinkButton({ caseNumber }: { caseNumber: string }) { const [copied, setCopied] = useState(false); + const [failed, setFailed] = useState(false); + + async function copy() { + if (!navigator.clipboard) { + setFailed(true); + setTimeout(() => setFailed(false), 2000); + return; + } + try { + await navigator.clipboard.writeText(`${getSiteUrl()}/case/${caseNumber}`); + setCopied(true); + setTimeout(() => setCopied(false), 2000); + } catch { + setFailed(true); + setTimeout(() => setFailed(false), 2000); + } + } + return (