@@ -6,6 +6,7 @@ import { useSession } from "next-auth/react";
66import { useRouter } from "next/router" ;
77import { useMemo , useState } from "react" ;
88import CardSkeleton from "@/components/CardSkeleton" ;
9+ import { toast } from "react-toastify" ;
910
1011const 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