The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { DesktopSnapShotState } from "@t3tools/contracts"; |
| 2 | import { msg } from "gt-react"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 5 | |
| 6 | export type CaptureSetupStep = "access" | "shortcut"; |
| 7 | |
| 8 | export function captureSetupDesktopName(state: DesktopSnapShotState): string | undefined { |
| 9 | if (state.mode !== "portal") return undefined; |
| 10 | const desktop = state.linuxDesktop ?? captureSetupBackend(state); |
| 11 | switch (desktop) { |
| 12 | case "gnome": |
| 13 | return "GNOME"; |
| 14 | case "kde": |
| 15 | return "KDE Plasma"; |
| 16 | case "niri": |
| 17 | return "Niri"; |
| 18 | case "hyprland": |
| 19 | return "Hyprland"; |
| 20 | default: |
| 21 | return undefined; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | export function captureSetupBackend(state: DesktopSnapShotState) { |
| 26 | if (state.mode !== "portal") return "direct"; |
| 27 | if (state.linuxBackend === "niri") return "niri"; |
| 28 | if (state.linuxBackend === "kde") return "kde"; |
| 29 | if (state.linuxBackend === "hyprland") return "hyprland"; |
| 30 | if (state.linuxBackend === "screenshot-portal") return "portal"; |
| 31 | if (state.linuxBackend === "picker" && state.gnomeExtension?.status === "unsupported") |
| 32 | return "picker"; |
| 33 | if (state.linuxBackend === "gnome-extension" || state.gnomeExtension) return "gnome"; |
| 34 | return "picker"; |
| 35 | } |
| 36 | |
| 37 | export function captureSetupAccessReady(state: DesktopSnapShotState): boolean { |
| 38 | if (state.mode === "unavailable" || state.message) return false; |
| 39 | if (captureSetupBackend(state) === "gnome") |
| 40 | return state.gnomeExtension?.status === "enabled" && state.linuxBackend === "gnome-extension"; |
| 41 | if (captureSetupBackend(state) === "kde") return state.kdeHelper?.status === "ready"; |
| 42 | if (captureSetupBackend(state) === "hyprland") return state.hyprlandHelper?.status === "ready"; |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | export function captureSetupMacPermissionsReady( |
| 47 | state: DesktopSnapShotState, |
| 48 | includeAccessibility: boolean, |
| 49 | ): boolean { |
| 50 | const permissions = state.macPermissions; |
| 51 | if (!permissions) return true; |
| 52 | return permissions.screenRecording && (!includeAccessibility || permissions.accessibility); |
| 53 | } |
| 54 | |
| 55 | export function captureSetupCheckMessage( |
| 56 | state: DesktopSnapShotState, |
| 57 | resolve: MessageResolver = m, |
| 58 | ): string { |
| 59 | const backend = captureSetupBackend(state); |
| 60 | const gnome = backend === "gnome"; |
| 61 | if ( |
| 62 | state.message || |
| 63 | (gnome && state.gnomeExtension?.status === "error") || |
| 64 | (backend === "kde" && state.kdeHelper?.status === "error") || |
| 65 | (backend === "hyprland" && state.hyprlandHelper?.status === "error") |
| 66 | ) |
| 67 | return resolve(msg("Still unable to check access. See Advanced for help.")); |
| 68 | if (captureSetupBackend(state) === "picker") { |
| 69 | return resolve(msg("Ready. You'll choose a window each time.")); |
| 70 | } |
| 71 | if (gnome && state.gnomeExtension?.status === "restart-required") |
| 72 | return resolve(msg("Still waiting for you to sign out and back in.")); |
| 73 | return captureSetupAccessReady(state) |
| 74 | ? resolve(msg("Ready. Continue to choose your shortcut.")) |
| 75 | : resolve(msg("Not ready yet. Finish the step above.")); |
| 76 | } |
| 77 | |
| 78 | export function captureSetupShortcutReady(state: DesktopSnapShotState, unsaved: boolean): boolean { |
| 79 | if (unsaved || !captureSetupAccessReady(state)) return false; |
| 80 | if (state.linuxBackend === "hyprland") return Boolean(state.shortcutActionRegistered); |
| 81 | return state.linuxBackend === "niri" |
| 82 | ? Boolean(state.shortcutBinding) |
| 83 | : state.shortcutRegistered || Boolean(state.shortcutPending); |
| 84 | } |
| 85 | |
| 86 | export function captureSetupInitialStep( |
| 87 | state: DesktopSnapShotState, |
| 88 | requested: CaptureSetupStep | "resume" = "resume", |
| 89 | ): CaptureSetupStep { |
| 90 | if (!captureSetupAccessReady(state)) return "access"; |
| 91 | if (requested === "resume") { |
| 92 | // Disabled backends have not checked permissions; pickers need an explanation, not an active-window |
| 93 | // access claim. |
| 94 | if ( |
| 95 | (state.mode === "direct" && !state.shortcutRegistered) || |
| 96 | captureSetupBackend(state) === "picker" |
| 97 | ) |
| 98 | return "access"; |
| 99 | return "shortcut"; |
| 100 | } |
| 101 | return requested; |
| 102 | } |
| 103 | |
| 104 | export function captureSetupShouldDisableOnClose(wasEnabled: boolean, completed: boolean): boolean { |
| 105 | return !wasEnabled && !completed; |
| 106 | } |