useStartupNotification.ts
hooks/notifs/useStartupNotification.ts
No strong subsystem tag
42
Lines
1278
Bytes
1
Exports
4
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with general runtime concerns. It contains 42 lines, 4 detected imports, and 1 detected exports.
Important relationships
- hooks/notifs/useAutoModeUnavailableNotification.ts
- hooks/notifs/useCanSwitchToExistingSubscription.tsx
- hooks/notifs/useDeprecationWarningNotification.tsx
- hooks/notifs/useFastModeNotification.tsx
- hooks/notifs/useIDEStatusIndicator.tsx
- hooks/notifs/useInstallMessages.tsx
- hooks/notifs/useLspInitializationNotification.tsx
- hooks/notifs/useMcpConnectivityStatus.tsx
Detected exports
useStartupNotification
Keywords
resultnotificationcomputecurrentusereflogerroraddnotificationhasrunrefcomputerefuseeffect
Detected imports
react../../bootstrap/state.js../../context/notifications.js../../utils/log.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { useEffect, useRef } from 'react'
import { getIsRemoteMode } from '../../bootstrap/state.js'
import {
type Notification,
useNotifications,
} from '../../context/notifications.js'
import { logError } from '../../utils/log.js'
type Result = Notification | Notification[] | null
/**
* Fires notification(s) once on mount. Encapsulates the remote-mode gate and
* once-per-session ref guard that was hand-rolled across 10+ notifs/ hooks.
*
* The compute fn runs exactly once on first effect. Return null to skip,
* a Notification to fire one, or an array to fire several. Sync or async.
* Rejections are routed to logError.
*/
export function useStartupNotification(
compute: () => Result | Promise<Result>,
): void {
const { addNotification } = useNotifications()
const hasRunRef = useRef(false)
const computeRef = useRef(compute)
computeRef.current = compute
useEffect(() => {
if (getIsRemoteMode() || hasRunRef.current) return
hasRunRef.current = true
void Promise.resolve()
.then(() => computeRef.current())
.then(result => {
if (!result) return
for (const n of Array.isArray(result) ? result : [result]) {
addNotification(n)
}
})
.catch(logError)
}, [addNotification])
}