| 1 | import { useCallback } from "react"; |
| 2 | import { app } from "./bridge"; |
| 3 | import { asArray } from "./array"; |
| 4 | import type { HistoryMessage } from "./types"; |
| 5 | import type { SessionRef } from "./sessionRef"; |
| 6 | |
| 7 | export function useSessionCatalogActions( |
| 8 | requireIntent: (seq: number) => Promise<void>, |
| 9 | current: (seq: number) => boolean, |
| 10 | sync: (reset: boolean, guard: boolean, options: { navigationIntentSeq: number; surfacePolicy: "replace-surface" }) => Promise<string | undefined>, |
| 11 | invalidateCache: () => void, |
| 12 | ) { |
| 13 | const openCanonicalSession = useCallback(async (ref: SessionRef, navigationIntentSeq: number): Promise<void> => { |
| 14 | await requireIntent(navigationIntentSeq); |
| 15 | if (!current(navigationIntentSeq)) return; |
| 16 | await app.OpenSession(ref); |
| 17 | if (!current(navigationIntentSeq)) return; |
| 18 | // User navigation adopts the active surface; background ready events cannot. |
| 19 | await sync(true, false, { navigationIntentSeq, surfacePolicy: "replace-surface" }); |
| 20 | }, [requireIntent, current, sync]); |
| 21 | const previewSession = useCallback(async (path: string): Promise<HistoryMessage[]> => asArray<HistoryMessage>(await app.PreviewSession(path).catch(() => [])), []); |
| 22 | const deleteSession = useCallback((path: string) => app.DeleteSession(path).finally(() => invalidateCache()), [invalidateCache]); |
| 23 | const restoreSession = useCallback((path: string) => app.RestoreSession(path).finally(() => invalidateCache()), [invalidateCache]); |
| 24 | const purgeTrashedSession = useCallback((path: string) => app.PurgeTrashedSession(path).finally(() => invalidateCache()), [invalidateCache]); |
| 25 | const renameSession = useCallback((path: string, title: string) => app.RenameSession(path, title).catch(() => {}).finally(() => invalidateCache()), [invalidateCache]); |
| 26 | return { openCanonicalSession, previewSession, deleteSession, restoreSession, purgeTrashedSession, renameSession }; |
| 27 | } |
| 28 |