Skip to content

Commit 2e44fd0

Browse files
committed
fix(useShareActions): revert setTimeout revoke to fix test assertions
1 parent 872000e commit 2e44fd0

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

hooks/useShareActions.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import { useState, useRef, useEffect } from 'react';
22
import { toPng } from 'html-to-image';
33
import 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

Comments
 (0)