|
| 1 | +import {useEffect, useRef, useState} from 'react'; |
| 2 | +import Clipboard from '@libs/Clipboard'; |
| 3 | +import ROUTES from '@src/ROUTES'; |
| 4 | +import useEnvironment from './useEnvironment'; |
| 5 | + |
| 6 | +// Matches the success state duration in useThrottledButtonState used by ContextMenuItem |
| 7 | +const SHARE_FEEDBACK_DURATION_MS = 1800; |
| 8 | + |
| 9 | +// Matches the shouldDelay close timing in hideContextMenu (ReportActionContextMenu.ts) |
| 10 | +const MENU_CLOSE_DELAY_MS = 800; |
| 11 | + |
| 12 | +function useShareSavedSearch() { |
| 13 | + const {environmentURL} = useEnvironment(); |
| 14 | + const [copiedHash, setCopiedHash] = useState<number | null>(null); |
| 15 | + const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + return () => { |
| 19 | + clearTimeout(timeoutRef.current ?? undefined); |
| 20 | + }; |
| 21 | + }, []); |
| 22 | + |
| 23 | + const handleShare = (itemHash: number, itemQuery: string) => { |
| 24 | + const url = `${environmentURL}/${ROUTES.SEARCH_ROOT.getRoute({query: itemQuery})}`; |
| 25 | + Clipboard.setString(url); |
| 26 | + setCopiedHash(itemHash); |
| 27 | + |
| 28 | + if (timeoutRef.current !== null) { |
| 29 | + clearTimeout(timeoutRef.current); |
| 30 | + } |
| 31 | + timeoutRef.current = setTimeout(() => { |
| 32 | + setCopiedHash((prev) => (prev === itemHash ? null : prev)); |
| 33 | + timeoutRef.current = null; |
| 34 | + }, SHARE_FEEDBACK_DURATION_MS); |
| 35 | + }; |
| 36 | + |
| 37 | + return {copiedHash, handleShare}; |
| 38 | +} |
| 39 | + |
| 40 | +export {MENU_CLOSE_DELAY_MS}; |
| 41 | +export default useShareSavedSearch; |
0 commit comments