1- import { useState } from 'react' ;
1+ import { useState , useRef , useEffect } from 'react' ;
22import { toPng } from 'html-to-image' ;
33import type { DashboardExportData } from '@/types/dashboard' ;
44
@@ -16,12 +16,24 @@ export function useShareActions(
1616) {
1717 const [ states , setStates ] = useState < Record < string , OptionState > > ( { } ) ;
1818
19+ const timeoutsRef = useRef < Record < string , ReturnType < typeof setTimeout > > > ( { } ) ;
20+
1921 const setOptionState = ( key : string , state : OptionState ) => {
2022 setStates ( ( prev ) => ( { ...prev , [ key ] : state } ) ) ;
2123 if ( state === 'success' || state === 'error' ) {
22- setTimeout ( ( ) => setStates ( ( prev ) => ( { ...prev , [ key ] : 'idle' } ) ) , 2500 ) ;
24+ if ( timeoutsRef . current [ key ] ) clearTimeout ( timeoutsRef . current [ key ] ) ;
25+ timeoutsRef . current [ key ] = setTimeout (
26+ ( ) => setStates ( ( prev ) => ( { ...prev , [ key ] : 'idle' } ) ) ,
27+ 2500
28+ ) ;
2329 }
2430 } ;
31+ useEffect ( ( ) => {
32+ const t = timeoutsRef . current ;
33+ return ( ) => {
34+ Object . values ( t ) . forEach ( clearTimeout ) ;
35+ } ;
36+ } , [ ] ) ;
2537
2638 const handleCopyLink = async ( ) => {
2739 setOptionState ( 'copy' , 'loading' ) ;
@@ -50,7 +62,11 @@ export function useShareActions(
5062 const handleReddit = ( ) => {
5163 const url = encodeURIComponent ( PROFILE_URL ( username ) ) ;
5264 const title = encodeURIComponent ( 'Check out my CommitPulse dashboard 🚀' ) ;
53- window . open ( `https://www.reddit.com/submit?url=${ url } &title=${ title } ` , '_blank' ) ;
65+ window . open (
66+ `https://www.reddit.com/submit?url=${ url } &title=${ title } ` ,
67+ '_blank' ,
68+ 'noopener,noreferrer'
69+ ) ;
5470 onClose ( ) ;
5571 } ;
5672
@@ -141,7 +157,9 @@ export function useShareActions(
141157
142158 const handleNativeShare = async ( ) => {
143159 if ( ! ( 'share' in navigator ) ) {
160+ setOptionState ( 'native' , 'loading' ) ;
144161 await handleCopyLink ( ) ;
162+ setOptionState ( 'native' , 'success' ) ;
145163 return ;
146164 }
147165 setOptionState ( 'native' , 'loading' ) ;
0 commit comments