Skip to content

Commit 064371e

Browse files
committed
add share button
1 parent 7a6340e commit 064371e

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44
- find edge cases
55
- styling
66
- search
7-
- share button

app/Header.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Image from "next/image";
22
import Link from "next/link";
3+
import { Suspense } from "react";
4+
import { ShareButton } from "./ShareButton";
35

46
export function Header() {
57
return (
@@ -34,6 +36,10 @@ export function Header() {
3436
d="M8 7h12m0 0l-4-4m4 4l-4 4m0 6H4m0 0l4 4m-4-4l4-4"
3537
/>
3638
</svg>
39+
40+
<Suspense>
41+
<ShareButton />
42+
</Suspense>
3743
</div>
3844
</header>
3945
);

app/ShareButton.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"use client";
2+
3+
import { useSearchParams } from "next/navigation";
4+
import { useState } from "react";
5+
6+
export function ShareButton() {
7+
const searchParams = useSearchParams();
8+
const [copied, setCopied] = useState(false);
9+
10+
const isVisible = searchParams.get("symbol1") && searchParams.get("symbol2");
11+
if (!isVisible) return null;
12+
13+
const handleShare = async () => {
14+
await navigator.clipboard.writeText(window.location.href);
15+
setCopied(true);
16+
setTimeout(() => setCopied(false), 2000);
17+
};
18+
19+
return (
20+
<button
21+
onClick={handleShare}
22+
className={`ml-auto flex items-center gap-2 rounded-md bg-un-blue px-3 py-1.5 text-sm font-medium text-white transition-all ${copied ? "cursor-default" : "hover:opacity-80"}`}
23+
>
24+
{copied ? (
25+
<>
26+
<svg
27+
className="h-4 w-4"
28+
fill="none"
29+
viewBox="0 0 24 24"
30+
stroke="currentColor"
31+
strokeWidth={2.5}
32+
>
33+
<path
34+
strokeLinecap="round"
35+
strokeLinejoin="round"
36+
d="M5 13l4 4L19 7"
37+
/>
38+
</svg>
39+
Link Copied!
40+
</>
41+
) : (
42+
<>
43+
<svg
44+
className="h-4 w-4"
45+
fill="none"
46+
viewBox="0 0 24 24"
47+
stroke="currentColor"
48+
strokeWidth={2}
49+
>
50+
<path
51+
strokeLinecap="round"
52+
strokeLinejoin="round"
53+
d="M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1"
54+
/>
55+
</svg>
56+
Share
57+
</>
58+
)}
59+
</button>
60+
);
61+
}

app/page.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ function HomeContent() {
2424
const [diffData, setDiffData] = useState<DiffResponse | null>(null);
2525
const [loading, setLoading] = useState(false);
2626
const [error, setError] = useState<string | null>(null);
27+
const [copied, setCopied] = useState(false);
28+
29+
const handleShare = async () => {
30+
await navigator.clipboard.writeText(window.location.href);
31+
setCopied(true);
32+
setTimeout(() => setCopied(false), 2000);
33+
};
2734

2835
const fetchDiff = async (sym1?: string, sym2?: string) => {
2936
const symbolA = sym1 || symbol1;

0 commit comments

Comments
 (0)