@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 170 lines 6.52 KB
Raw
1 import type { ClientSettingsPatch, DesktopSnapShotState, SnapShotSound } from "@t3tools/contracts";
2 import { msg } from "gt-react";
3
4 import { m, type MessageResolver } from "../../i18n/messages";
5 import {
6 captureSetupBackend,
7 captureSetupDesktopName,
8 captureSetupAccessReady,
9 captureSetupMacPermissionsReady,
10 } from "./SnapShotSetupDialog.logic";
11
12 export function snapShotStatus(
13 state: DesktopSnapShotState | null,
14 enabled: boolean,
15 resolve: MessageResolver = m,
16 ): string {
17 if (!state) return resolve(msg("Checking snapshots…"));
18 if (state.mode === "unavailable")
19 return state.message ?? resolve(msg("Not supported on this platform."));
20 if (!enabled) return resolve(msg("Turn this on to set up snapshots."));
21 return snapShotSetupSummary(state, enabled, resolve);
22 }
23
24 export function snapShotSetupSummary(
25 state: DesktopSnapShotState,
26 enabled: boolean,
27 resolve: MessageResolver = m,
28 ): string {
29 if (state.message) return resolve(msg("Capture needs attention"));
30 if (state.linuxBackend === "hyprland" && state.hyprlandHelper?.status !== "ready")
31 return state.hyprlandHelper?.status === "error"
32 ? resolve(msg("Check capture access in setup"))
33 : resolve(msg("Install the capture helper to continue"));
34 if (captureSetupBackend(state) === "gnome" && state.gnomeExtension?.status !== "enabled")
35 return resolve(msg("Set up active-window snapshots"));
36 if (captureSetupBackend(state) === "kde" && state.kdeHelper?.status !== "ready")
37 return state.kdeHelper?.status === "error"
38 ? resolve(msg("Check capture access in setup"))
39 : resolve(msg("Install the capture helper to continue"));
40 if (captureSetupBackend(state) === "picker")
41 return resolve(msg("Manual capture only. You'll choose a window each time"));
42 if (!enabled) return resolve(msg("Enable capture to continue"));
43 if (state.shortcutPending)
44 return state.linuxBackend === "hyprland"
45 ? resolve(msg("Connecting your shortcut…"))
46 : resolve(msg("Waiting for shortcut permission"));
47 if (state.shortcutVerified) return resolve(msg("Ready to capture"));
48 if (state.linuxBackend === "niri" && state.shortcutBinding)
49 return resolve(msg("Use your shortcut from another app"));
50 if (state.linuxBackend === "hyprland" && state.shortcutActionRegistered)
51 return resolve(msg("Use your shortcut from another app"));
52 if (state.shortcutRegistered)
53 return state.shortcutLabel ? resolve(msg("Ready to capture")) : resolve(msg("Shortcut saved"));
54 return resolve(msg("Finish shortcut setup"));
55 }
56
57 export function snapShotShortcutStatus(
58 state: DesktopSnapShotState | null,
59 resolve: MessageResolver = m,
60 ): string | null {
61 if (!state) return null;
62 if (state.linuxBackend === "hyprland") return state.shortcutMessage;
63 if (state.shortcutPending)
64 return resolve(msg("Approve the shortcut permission prompt to continue."));
65 if (state.shortcutRegistered)
66 return state.mode === "portal" ? null : resolve(msg("Shortcut saved."));
67 return state.shortcutMessage;
68 }
69
70 export function snapShotSetupButtonLabel(
71 state: DesktopSnapShotState | null,
72 resolve: MessageResolver = m,
73 ): string {
74 if (!state) return resolve(msg("Continue setup"));
75 if (captureSetupAccessReady(state)) return resolve(msg("Manage capture"));
76 const desktop = captureSetupDesktopName(state);
77 return desktop
78 ? resolve(msg("Set up {desktop} capture", { desktop }))
79 : resolve(msg("Continue setup"));
80 }
81
82 export function snapShotSetupComplete(
83 state: DesktopSnapShotState | null,
84 includeAccessibility: boolean,
85 ): boolean {
86 if (state?.windows) return true;
87 return (
88 state?.macPermissions !== undefined &&
89 captureSetupAccessReady(state) &&
90 captureSetupMacPermissionsReady(state, includeAccessibility) &&
91 state.shortcutRegistered
92 );
93 }
94
95 export type SnapShotSoundSelection = SnapShotSound | "off";
96
97 export function snapShotFeedbackUnavailableMessage(
98 state: DesktopSnapShotState | null,
99 resolve: MessageResolver = m,
100 ): string | undefined {
101 if (state?.mode !== "portal" || state.linuxFeedbackAvailable) return undefined;
102 if (state.linuxBackend === "hyprland")
103 return state.hyprlandHelper?.status === "ready"
104 ? resolve(msg("Capture effects aren't available on this desktop."))
105 : resolve(msg("Install or update the capture helper to enable effects."));
106 if (state.linuxBackend === "niri")
107 return resolve(msg("Capture effects aren't available on Niri."));
108 if (state.linuxBackend === "kde")
109 return state.kdeHelper?.status === "ready"
110 ? resolve(msg("Capture effects aren't available on this desktop."))
111 : resolve(msg("Install or update the capture helper to enable effects."));
112 return state.linuxBackend === "gnome-extension"
113 ? resolve(msg("Update the GNOME extension, then sign out and back in to enable effects."))
114 : captureSetupBackend(state) === "gnome"
115 ? resolve(msg("Finish extension setup to enable effects."))
116 : resolve(msg("Capture effects aren't available on this desktop."));
117 }
118
119 export function snapShotDescription(
120 state: DesktopSnapShotState | null,
121 resolve: MessageResolver = m,
122 ): string {
123 return state?.mode === "portal" && captureSetupBackend(state) === "picker"
124 ? resolve(msg("Automatic capture isn't available here. Choose a window instead."))
125 : resolve(msg("Capture a window and attach it to your current draft."));
126 }
127
128 export function snapShotAccessibilityUnavailableMessage(
129 state: DesktopSnapShotState | null,
130 resolve: MessageResolver = m,
131 ): string | undefined {
132 if (state?.mode !== "portal") return undefined;
133 if (state.linuxBackend === "picker" || state.linuxBackend === "screenshot-portal")
134 return resolve(msg("This desktop only provides a screenshot."));
135 return undefined;
136 }
137
138 export function snapShotUnavailableMessage(
139 hasBridge: boolean,
140 resolve: MessageResolver = m,
141 ): string | undefined {
142 if (hasBridge) return undefined;
143 return typeof window !== "undefined" && window.desktopBridge
144 ? resolve(msg("Update the desktop app to use snapshots."))
145 : resolve(msg("Only available in the desktop app."));
146 }
147
148 export function snapShotSoundPatch(sound: SnapShotSoundSelection): ClientSettingsPatch {
149 return sound === "off"
150 ? { snapShotPlaySound: false }
151 : { snapShotPlaySound: true, snapShotSound: sound };
152 }
153
154 export function createRecordingRequestTracker() {
155 let currentRequest: symbol | null = null;
156
157 return {
158 tryBegin() {
159 if (currentRequest) return null;
160 currentRequest = Symbol();
161 return currentRequest;
162 },
163 clear() {
164 currentRequest = null;
165 },
166 owns(request: symbol) {
167 return currentRequest === request;
168 },
169 };
170 }