The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import type { DesktopSnapShotState } from "@t3tools/contracts"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import { Atom, type AtomRegistry } from "effect/reactivity"; |
| 5 | import { msg } from "gt-react"; |
| 6 | |
| 7 | import { m } from "../i18n/messages"; |
| 8 | import { getDesktopSnapShotBridge } from "../lib/desktopSnapShot"; |
| 9 | |
| 10 | export interface SnapShotSetupError { |
| 11 | readonly title: string; |
| 12 | readonly message: string; |
| 13 | } |
| 14 | |
| 15 | export function snapShotSetupError(title: string, error: unknown): SnapShotSetupError { |
| 16 | return { title, message: thrownErrorMessage(error, m) ?? m(msg("Try again.")) }; |
| 17 | } |
| 18 | |
| 19 | export interface DesktopSnapShotLoad { |
| 20 | readonly state: DesktopSnapShotState | null; |
| 21 | /** Why the last background read (load, focus, shortcut change) failed; a later success clears it. */ |
| 22 | readonly refreshError: SnapShotSetupError | null; |
| 23 | } |
| 24 | |
| 25 | const EMPTY_LOAD: DesktopSnapShotLoad = { state: null, refreshError: null }; |
| 26 | |
| 27 | // Only the newest read lands, so a slow answer cannot regress the state. Dropping the atom bumps |
| 28 | // it too, so a read that was in flight when the page closed does not seed the next visit. |
| 29 | let stateRequestId = 0; |
| 30 | |
| 31 | /** |
| 32 | * The desktop capture state. Reading it starts a load, then re-reads on window focus (the user |
| 33 | * may have changed OS permissions) and when the desktop reports a shortcut change. |
| 34 | */ |
| 35 | export const desktopSnapShotAtom = Atom.writable( |
| 36 | (get): DesktopSnapShotLoad => { |
| 37 | get.addFinalizer(() => { |
| 38 | stateRequestId++; |
| 39 | }); |
| 40 | const bridge = getDesktopSnapShotBridge(); |
| 41 | if (!bridge) return EMPTY_LOAD; |
| 42 | const refresh = () => |
| 43 | void refreshDesktopSnapShotState(get.registry, (error) => { |
| 44 | get.setSelf({ |
| 45 | ...Option.getOrElse(get.self<DesktopSnapShotLoad>(), () => EMPTY_LOAD), |
| 46 | refreshError: snapShotSetupError(m(msg("Couldn't check capture setup")), error), |
| 47 | }); |
| 48 | }); |
| 49 | window.addEventListener("focus", refresh); |
| 50 | get.addFinalizer(() => window.removeEventListener("focus", refresh)); |
| 51 | get.addFinalizer( |
| 52 | bridge.onSnapShotEvent((event) => { |
| 53 | if (event.type === "shortcut-changed") refresh(); |
| 54 | }), |
| 55 | ); |
| 56 | refresh(); |
| 57 | return EMPTY_LOAD; |
| 58 | }, |
| 59 | (ctx, value: DesktopSnapShotLoad) => ctx.setSelf(value), |
| 60 | ).pipe(Atom.withLabel("desktop:snap-shot:state")); |
| 61 | |
| 62 | /** |
| 63 | * Reads the capture state now and stores it. `onError` hears only a failure of the newest read; |
| 64 | * the result is `undefined` whenever this read did not produce the current state. |
| 65 | */ |
| 66 | export async function refreshDesktopSnapShotState( |
| 67 | registry: AtomRegistry.AtomRegistry, |
| 68 | onError: (error: unknown) => void, |
| 69 | ): Promise<DesktopSnapShotState | undefined> { |
| 70 | const bridge = getDesktopSnapShotBridge(); |
| 71 | if (!bridge) return undefined; |
| 72 | const requestId = ++stateRequestId; |
| 73 | try { |
| 74 | const next = await bridge.getSnapShotState(); |
| 75 | if (requestId === stateRequestId) { |
| 76 | registry.set(desktopSnapShotAtom, { state: next, refreshError: null }); |
| 77 | } |
| 78 | return next; |
| 79 | } catch (error) { |
| 80 | if (requestId === stateRequestId) onError(error); |
| 81 | return undefined; |
| 82 | } |
| 83 | } |