Skip to content

Commit 72c25d2

Browse files
feat: 🎸 Copy button for format Text & JSON (#99)
* feat: 🎸 Copy buton for format Text & JSON βœ… Closes: #98 * chore: restored .env.example * fix: πŸ› build fails Change use write() to writeText() to copy String to clipboard βœ… Closes: #99 --------- Co-authored-by: Leonardo Montini <[email protected]>
1 parent a355805 commit 72c25d2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

β€Žsrc/pages/stats/[login].tsxβ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useSession } from "next-auth/react";
66
import { useRouter } from "next/router";
77
import { useMemo, useState } from "react";
88
import CardSkeleton from "@/components/CardSkeleton";
9+
import { toast } from "react-toastify";
910

1011
const yearsRange = 4;
1112

@@ -126,6 +127,22 @@ export default function Stats() {
126127
return text;
127128
}
128129

130+
const copyToClipboard = (format: "text" | "json") => {
131+
let data = null;
132+
133+
if (format === "text") data = generateText();
134+
else data = JSON.stringify(repositories, null, 2);
135+
136+
navigator.clipboard
137+
.writeText(data)
138+
.then(() => {
139+
toast.success(`${format} copied to clipboard`);
140+
})
141+
.catch(() => {
142+
toast.error(`Failed to copy ${format} to clipboard`);
143+
});
144+
};
145+
129146
const formatRender = useMemo(() => {
130147
switch (format) {
131148
case "cards":
@@ -185,6 +202,12 @@ export default function Stats() {
185202
>
186203
Export as JSON
187204
</button>
205+
<button
206+
className="btn btn-primary p-2 m-1 rounded"
207+
onClick={() => copyToClipboard("json")}
208+
>
209+
Copy
210+
</button>
188211
<div className="p-2 m-1 text-xs overflow-x-auto sm:text-sm md:text-base lg:text-lg">
189212
<pre>{JSON.stringify(filteredRepositories, null, 2)}</pre>
190213
</div>
@@ -199,6 +222,12 @@ export default function Stats() {
199222
>
200223
Export as Text
201224
</button>
225+
<button
226+
className="btn btn-primary p-2 m-1 rounded"
227+
onClick={() => copyToClipboard("text")}
228+
>
229+
Copy
230+
</button>
202231
<div className="p-2 m-1 text-xs overflow-x-auto sm:text-sm md:text-base lg:text-lg">
203232
<pre>{generateText()}</pre>
204233
</div>

0 commit comments

Comments
Β (0)