Skip to content

Commit

Permalink
feat: 🎸 Copy buton for format Text & JSON
Browse files Browse the repository at this point in the history
✅ Closes: #98
  • Loading branch information
K1ethoang committed Jan 29, 2024
1 parent 986e9db commit 0eda586
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
9 changes: 0 additions & 9 deletions .env.example

This file was deleted.

33 changes: 33 additions & 0 deletions src/pages/stats/[login].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useSession } from "next-auth/react";
import { useRouter } from "next/router";
import { useMemo, useState } from "react";
import CardSkeleton from "@/components/CardSkeleton";
import { toast } from "react-toastify";

const yearsRange = 4;

Expand Down Expand Up @@ -126,6 +127,26 @@ export default function Stats() {
return text;
}

const copyToClipboard = (format: "text" | "json") => {
let data = null;

if (format === "text") data = generateText();
else data = JSON.stringify(repositories, null, 2);

format = format.charAt(0).toUpperCase() + format.slice(1);

const blob = new Blob([data], { type: "text/plain" });

navigator.clipboard
.write([new ClipboardItem({ "text/plain": blob })])
.then(() => {
toast.success(`${format} copied to clipboard`);
})
.catch(() => {
toast.error(`Failed to copy ${format} to clipboard`);
});
};

const formatRender = useMemo(() => {
switch (format) {
case "cards":
Expand Down Expand Up @@ -185,6 +206,12 @@ export default function Stats() {
>
Export as JSON
</button>
<button
className="btn btn-primary p-2 m-1 rounded"
onClick={() => copyToClipboard("json")}
>
Copy
</button>
<div className="p-2 m-1 text-xs overflow-x-auto sm:text-sm md:text-base lg:text-lg">
<pre>{JSON.stringify(filteredRepositories, null, 2)}</pre>
</div>
Expand All @@ -199,6 +226,12 @@ export default function Stats() {
>
Export as Text
</button>
<button
className="btn btn-primary p-2 m-1 rounded"
onClick={() => copyToClipboard("text")}
>
Copy
</button>
<div className="p-2 m-1 text-xs overflow-x-auto sm:text-sm md:text-base lg:text-lg">
<pre>{generateText()}</pre>
</div>
Expand Down

0 comments on commit 0eda586

Please sign in to comment.