The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // Shared across hook instances so sidebar, header and menu actions invalidate each other. |
| 2 | const currentActions = new Map<string, symbol>(); |
| 3 | const listeners = new Set<() => void>(); |
| 4 | |
| 5 | export function subscribe(listener: () => void) { |
| 6 | listeners.add(listener); |
| 7 | return () => { |
| 8 | listeners.delete(listener); |
| 9 | }; |
| 10 | } |
| 11 | |
| 12 | function notify() { |
| 13 | for (const listener of listeners) listener(); |
| 14 | } |
| 15 | |
| 16 | export function begin(kind: string, threadKey: string) { |
| 17 | const key = JSON.stringify([kind, threadKey]); |
| 18 | const token = Symbol(); |
| 19 | currentActions.set(key, token); |
| 20 | notify(); |
| 21 | const isCurrent = () => currentActions.get(key) === token; |
| 22 | return { |
| 23 | isCurrent, |
| 24 | finish: () => { |
| 25 | if (isCurrent()) { |
| 26 | currentActions.delete(key); |
| 27 | notify(); |
| 28 | } |
| 29 | }, |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | export function invalidate(kind: string, threadKey: string) { |
| 34 | currentActions.delete(JSON.stringify([kind, threadKey])); |
| 35 | notify(); |
| 36 | } |
| 37 | |
| 38 | export type ThreadUndoStep = |
| 39 | | { readonly action: "unsettle"; readonly restoreUnsettledAt?: string | null } |
| 40 | | { readonly action: "unsnooze" } |
| 41 | | { readonly action: "pin"; readonly orderKey?: string } |
| 42 | | { readonly action: "reorderActive"; readonly orderKey: string } |
| 43 | | { readonly action: "snooze"; readonly snoozedUntil: string }; |
| 44 | |
| 45 | export type ThreadUndoPlan = readonly [ThreadUndoStep, ...ThreadUndoStep[]]; |
| 46 | |
| 47 | export function planThreadUndo( |
| 48 | action: "settle" | "snooze" | "unpin", |
| 49 | thread?: { |
| 50 | readonly pinnedAt?: string | null; |
| 51 | readonly pinOrderKey?: string | null; |
| 52 | readonly activeOrderKey?: string | null; |
| 53 | readonly unsettledAt?: string | null; |
| 54 | readonly snoozedUntil?: string | null; |
| 55 | } | null, |
| 56 | ): ThreadUndoPlan { |
| 57 | if (action === "snooze") return [{ action: "unsnooze" }]; |
| 58 | const pin: ThreadUndoStep = { |
| 59 | action: "pin", |
| 60 | ...(thread?.pinOrderKey != null ? { orderKey: thread.pinOrderKey } : {}), |
| 61 | }; |
| 62 | // Pinning clears snooze on the server; restore it after restoring pin placement. |
| 63 | const snooze = |
| 64 | thread?.snoozedUntil != null |
| 65 | ? [{ action: "snooze" as const, snoozedUntil: thread.snoozedUntil }] |
| 66 | : []; |
| 67 | if (action === "unpin") return [pin, ...snooze]; |
| 68 | // Settling clears a manual active position; pinned threads are placed by their pin instead. |
| 69 | const placement: ThreadUndoStep[] = |
| 70 | thread?.pinnedAt != null |
| 71 | ? [pin] |
| 72 | : thread?.activeOrderKey != null |
| 73 | ? [{ action: "reorderActive", orderKey: thread.activeOrderKey }] |
| 74 | : []; |
| 75 | const unsettle: ThreadUndoStep = |
| 76 | thread == null |
| 77 | ? { action: "unsettle" } |
| 78 | : { action: "unsettle", restoreUnsettledAt: thread.unsettledAt ?? null }; |
| 79 | return [unsettle, ...placement, ...snooze]; |
| 80 | } |
| 81 | |
| 82 | export async function executeThreadUndo<Result>( |
| 83 | plan: ThreadUndoPlan, |
| 84 | execute: (step: ThreadUndoStep) => Promise<Result>, |
| 85 | succeeded: (result: Result) => boolean, |
| 86 | ): Promise<Result> { |
| 87 | let result = await execute(plan[0]); |
| 88 | for (const step of plan.slice(1)) { |
| 89 | if (!succeeded(result)) return result; |
| 90 | // A snooze can expire before Undo runs, and the server rejects an expired restore. |
| 91 | // @effect-diagnostics-next-line globalDate:off |
| 92 | if (step.action === "snooze" && Date.parse(step.snoozedUntil) <= Date.now()) continue; |
| 93 | result = await execute(step); |
| 94 | } |
| 95 | return result; |
| 96 | } |