source dump of claude code
at main 41 lines 1.3 kB view raw
1import { useEffect, useRef } from 'react' 2import { getIsRemoteMode } from '../../bootstrap/state.js' 3import { 4 type Notification, 5 useNotifications, 6} from '../../context/notifications.js' 7import { logError } from '../../utils/log.js' 8 9type Result = Notification | Notification[] | null 10 11/** 12 * Fires notification(s) once on mount. Encapsulates the remote-mode gate and 13 * once-per-session ref guard that was hand-rolled across 10+ notifs/ hooks. 14 * 15 * The compute fn runs exactly once on first effect. Return null to skip, 16 * a Notification to fire one, or an array to fire several. Sync or async. 17 * Rejections are routed to logError. 18 */ 19export function useStartupNotification( 20 compute: () => Result | Promise<Result>, 21): void { 22 const { addNotification } = useNotifications() 23 const hasRunRef = useRef(false) 24 const computeRef = useRef(compute) 25 computeRef.current = compute 26 27 useEffect(() => { 28 if (getIsRemoteMode() || hasRunRef.current) return 29 hasRunRef.current = true 30 31 void Promise.resolve() 32 .then(() => computeRef.current()) 33 .then(result => { 34 if (!result) return 35 for (const n of Array.isArray(result) ? result : [result]) { 36 addNotification(n) 37 } 38 }) 39 .catch(logError) 40 }, [addNotification]) 41}