@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
tsx 492 lines 19 KB
Raw
1 import { PermissionChecklist, PermissionContinueButton } from "../permissions/PermissionChecklist";
2 import { usePermissionStatus } from "../permissions/usePermissionStatus";
3 import {
4 isModifierPairShortcut,
5 type DesktopSnapShotSetupAction,
6 type DesktopSnapShotState,
7 } from "@t3tools/contracts";
8 import { useState, type ReactNode } from "react";
9 import { MacAccessibilityIcon, MacScreenRecordingIcon } from "../Icons";
10 import { CaptureShortcutConfig } from "./CaptureShortcutConfig";
11 import { Button } from "../ui/button";
12 import { Dialog, DialogDescription } from "../ui/dialog";
13 import { WizardSteps, WizardPopup, WizardHeader, WizardPanel, WizardFooter } from "../ui/wizard";
14 import {
15 captureSetupAccessReady,
16 captureSetupBackend,
17 captureSetupCheckMessage,
18 captureSetupDesktopName,
19 captureSetupInitialStep,
20 captureSetupShortcutReady,
21 type CaptureSetupStep,
22 } from "./SnapShotSetupDialog.logic";
23 import { msg, T, useGT, useMessages } from "gt-react";
24
25 const SETUP_STEPS = [
26 { id: "access", label: msg("Access", { $context: "setup step: capture permissions" }) },
27 { id: "shortcut", label: msg("Shortcut", { $context: "setup step: keyboard shortcut" }) },
28 ] as const;
29
30 const GNOME_ACCESS_COPY = {
31 "not-installed": {
32 title: msg("Install the extension"),
33 description: msg(
34 "The T3 Code GNOME extension lets you capture other windows and bring them into your draft. Sign out once after installing.",
35 ),
36 },
37 "restart-required": {
38 title: msg("Extension installed"),
39 description: msg("Save your work, then sign out and back in. Your setup will be waiting here."),
40 },
41 "update-required": {
42 title: msg("Update the extension"),
43 description: msg("Install the update, then sign out and back in."),
44 },
45 "extensions-disabled": {
46 title: msg("Allow GNOME extensions"),
47 description: msg("Open GNOME Extensions and turn on extensions, then check again."),
48 },
49 disabled: {
50 title: msg("Enable the extension"),
51 description: msg("Enable T3 Code SnapShots to start capturing windows."),
52 },
53 enabled: {
54 title: msg("Capture is ready"),
55 description: msg("Next, choose your shortcut."),
56 },
57 unsupported: {
58 title: msg("Automatic capture isn't available"),
59 description: msg("Use Take snapshot from the command palette to choose a window."),
60 },
61 error: {
62 title: msg("Couldn't set up the extension"),
63 description: msg("Check T3 Code SnapShots in GNOME Extensions, then try again."),
64 },
65 };
66
67 export function SnapShotSetupDialog({
68 state,
69 initialStep,
70 wasEnabled,
71 includeAccessibility,
72 busy: actionBusy,
73 error,
74 shortcutInput,
75 shortcutStatus,
76 shortcutChanged,
77 canSaveShortcut,
78 onSaveShortcut,
79 onEnable,
80 onAction,
81 onRefresh,
82 onClose,
83 onLeaveStep,
84 }: {
85 state: DesktopSnapShotState;
86 initialStep: CaptureSetupStep;
87 wasEnabled: boolean;
88 includeAccessibility: boolean;
89 busy: boolean;
90 error: string | null;
91 shortcutInput: ReactNode;
92 shortcutStatus: string | null | undefined;
93 shortcutChanged: boolean;
94 canSaveShortcut: boolean;
95 onSaveShortcut: () => Promise<boolean>;
96 onEnable: () => Promise<boolean>;
97 onAction: (action: DesktopSnapShotSetupAction) => Promise<void>;
98 onRefresh: () => Promise<DesktopSnapShotState | undefined>;
99 onClose: (completed: boolean) => Promise<void>;
100 onLeaveStep: () => void;
101 }) {
102 const messages = useMessages();
103 const gt = useGT();
104 const [step, setStep] = useState(() => captureSetupInitialStep(state, initialStep));
105 const [checking, setChecking] = useState(false);
106 const [checked, setChecked] = useState(false);
107 const [configBusy, setConfigBusy] = useState(false);
108 const busy = actionBusy || checking || configBusy;
109 const backend = captureSetupBackend(state);
110 const configShortcut = backend === "niri" || backend === "hyprland";
111 const desktop = captureSetupDesktopName(state);
112 const extension = state.gnomeExtension;
113 const helper = backend === "hyprland" ? state.hyprlandHelper : state.kdeHelper;
114 const helperBackend = backend === "kde" || backend === "hyprland";
115 const installHelper = backend === "hyprland" ? "install-hyprland-helper" : "install-kde-helper";
116 const removeHelper = backend === "hyprland" ? "remove-hyprland-helper" : "remove-kde-helper";
117 const accessReady = captureSetupAccessReady(state);
118 const permissionStatus = usePermissionStatus(
119 async () => {
120 const refreshed = await onRefresh();
121 if (!refreshed?.macPermissions) throw new Error("Permission status unavailable");
122 return refreshed.macPermissions;
123 },
124 state.macPermissions ?? { screenRecording: false, accessibility: false },
125 Boolean(state.macPermissions) && step === "access" && !busy,
126 );
127 const macPermissions = state.macPermissions ? permissionStatus.status : undefined;
128 const macPermissionsReady =
129 !macPermissions ||
130 permissionStatus.isReady(
131 includeAccessibility ? ["screenRecording", "accessibility"] : ["screenRecording"],
132 );
133 const shortcutReady = captureSetupShortcutReady(state, shortcutChanged);
134 const install = extension?.status === "not-installed" || extension?.status === "update-required";
135 const enable = extension?.status === "disabled";
136 const changeStep = (next: CaptureSetupStep) => {
137 onLeaveStep();
138 setChecked(false);
139 setStep(next);
140 };
141 const checkAgain = async () => {
142 if (busy) return;
143 setChecking(true);
144 setChecked(false);
145 const refreshed = await onRefresh().finally(() => setChecking(false));
146 setChecked(refreshed !== undefined);
147 };
148 const accessCopy =
149 state.message && !macPermissions
150 ? {
151 title: gt("Let's try that again"),
152 description: gt("Couldn't check snapshots. Try again to continue."),
153 }
154 : backend === "gnome" && extension
155 ? extension.status === "enabled" && !accessReady
156 ? {
157 title: gt("Check capture access"),
158 description: gt("The extension isn't ready yet. Try again in a moment."),
159 }
160 : {
161 title: messages(GNOME_ACCESS_COPY[extension.status].title),
162 description: messages(GNOME_ACCESS_COPY[extension.status].description),
163 }
164 : helperBackend
165 ? helper?.status === "ready"
166 ? {
167 title: gt("Capture is ready"),
168 description: gt("Next, choose your shortcut."),
169 }
170 : helper?.status === "error"
171 ? {
172 title: gt("Let's fix capture access"),
173 description: gt("Try reinstalling the capture helper, then check again."),
174 }
175 : {
176 title:
177 helper?.status === "update-required"
178 ? gt("Update the capture helper")
179 : gt("Allow snapshots"),
180 description: gt(
181 "T3 Code's capture helper lets you capture other apps and return to your draft. It's included with T3 Code.",
182 ),
183 }
184 : backend === "niri"
185 ? {
186 title: gt("Capture is ready"),
187 description: gt("Next, choose your shortcut."),
188 }
189 : backend === "picker"
190 ? {
191 title: gt("Choose a window each time"),
192 description: gt(
193 "Your desktop doesn't support automatic capture. You'll choose the window to capture instead.",
194 ),
195 }
196 : {
197 title: gt("Allow snapshots"),
198 description:
199 backend === "portal"
200 ? gt("Your desktop may ask for permission when you first capture.")
201 : macPermissions
202 ? macPermissionsReady
203 ? gt(
204 "Test a snapshot of the current window. If macOS asks to bypass its window picker, choose Allow. The test image is discarded.",
205 )
206 : gt("Allow each permission, then continue.")
207 : gt("Allow access when prompted to start capturing windows."),
208 };
209 const title = step === "access" ? accessCopy.title : gt("Choose your shortcut");
210 const description =
211 step === "access"
212 ? accessCopy.description
213 : configShortcut
214 ? gt("Click the shortcut, then press the keys you want.")
215 : state.mode === "portal"
216 ? gt("Choose your keys, then approve the permission prompt if asked.")
217 : gt("Use both Shift keys, or record a different shortcut.");
218 const stepIndex = SETUP_STEPS.findIndex(({ id }) => id === step);
219 const details = [
220 ...new Set(
221 [
222 error,
223 ...(step === "access"
224 ? [
225 state.message,
226 backend === "gnome" &&
227 (extension?.status === "error" || extension?.status === "unsupported")
228 ? extension.message
229 : null,
230 helperBackend && helper?.status === "error" ? helper.message : null,
231 ]
232 : []),
233 ].filter((detail) => detail !== null),
234 ),
235 ];
236
237 return (
238 <Dialog
239 open
240 onOpenChange={(open) => {
241 if (!open && !busy) void onClose(false);
242 }}
243 >
244 <WizardPopup showCloseButton={!busy}>
245 <WizardHeader
246 title={
247 desktop ? gt("Set up snapshots for {desktop}", { desktop }) : gt("Set up snapshots")
248 }
249 >
250 <WizardSteps
251 steps={SETUP_STEPS.map((item) => messages(item.label))}
252 currentStep={stepIndex}
253 isStepDisabled={(index) => busy || index > stepIndex}
254 onStepChange={(index) => {
255 const next = SETUP_STEPS[index];
256 if (next && next.id !== step) changeStep(next.id);
257 }}
258 />
259 </WizardHeader>
260 <WizardPanel>
261 <div className="space-y-4 text-sm">
262 <div className="space-y-2" aria-live="polite">
263 <h3 className="flex items-center gap-2 font-medium">{title}</h3>
264 <DialogDescription>{description}</DialogDescription>
265 </div>
266 {step === "access" ? (
267 <>
268 <p
269 role="status"
270 aria-atomic="true"
271 className={
272 checked && !busy && !error ? "text-xs text-muted-foreground" : "sr-only"
273 }
274 >
275 {checked && !busy && !error ? captureSetupCheckMessage(state, messages) : null}
276 </p>
277 {macPermissions ? (
278 <PermissionChecklist
279 busy={busy}
280 permissions={[
281 {
282 id: "screenRecording",
283 icon: <MacScreenRecordingIcon className="size-8 shrink-0 drop-shadow-sm" />,
284 title: gt("Screen Recording"),
285 description: gt("Capture the window you're using."),
286 granted: macPermissions.screenRecording,
287 onAllow: () => void onAction("allow-screen-recording"),
288 },
289 {
290 id: "accessibility",
291 icon: <MacAccessibilityIcon className="size-8 shrink-0 drop-shadow-sm" />,
292 title: gt("Accessibility"),
293 description: includeAccessibility
294 ? gt("Include text and controls from the captured app.")
295 : gt("Optional. Include text and controls from the captured app."),
296 granted: macPermissions.accessibility,
297 onAllow: () => void onAction("allow-accessibility"),
298 },
299 ]}
300 />
301 ) : null}
302 {permissionStatus.error && macPermissions ? (
303 <p role="status" className="text-xs text-muted-foreground">
304 {permissionStatus.error}
305 </p>
306 ) : null}
307 {helperBackend && helper?.status === "error" ? (
308 <Button
309 size="xs"
310 variant="outline"
311 disabled={busy}
312 onClick={() => void onAction(installHelper)}
313 >
314 <T>Reinstall helper</T>
315 </Button>
316 ) : null}
317 </>
318 ) : configShortcut ? (
319 <CaptureShortcutConfig
320 state={state}
321 disabled={actionBusy || checking || !accessReady}
322 onBusyChange={setConfigBusy}
323 onSaved={onRefresh}
324 onComplete={() => onClose(true)}
325 />
326 ) : (
327 <div className="space-y-3">
328 {shortcutInput}
329 {shortcutStatus ? (
330 <p className="text-xs text-muted-foreground" role="status">
331 {shortcutStatus}
332 </p>
333 ) : null}
334 {!shortcutChanged &&
335 !state.shortcutRegistered &&
336 !state.shortcutPending &&
337 state.shortcutCanRetry !== false &&
338 !isModifierPairShortcut(state.shortcut) ? (
339 <Button
340 variant="ghost"
341 size="sm"
342 disabled={busy}
343 onClick={() => void onAction("retry-shortcut")}
344 >
345 {state.mode === "portal" ? gt("Shortcut permissions") : gt("Try again")}
346 </Button>
347 ) : null}
348 </div>
349 )}
350 {step === "shortcut" && !accessReady ? (
351 <p role="alert" className="text-destructive">
352 <T>Capture needs attention. Go back to check access.</T>
353 </p>
354 ) : null}
355 {error ? (
356 <p role="alert" className="text-destructive">
357 <T>Couldn't finish this step. Try again or check Advanced for help.</T>
358 </p>
359 ) : null}
360 {details.length > 0 || (step === "access" && (backend === "gnome" || helperBackend)) ? (
361 <details className="text-xs text-muted-foreground">
362 <summary className="cursor-pointer">
363 <T>Advanced</T>
364 </summary>
365 <div className="mt-3 space-y-3">
366 {details.map((detail) => (
367 <p key={detail} className="break-words">
368 {detail}
369 </p>
370 ))}
371 {step === "access" && (backend === "gnome" || helperBackend) ? (
372 <p>
373 <T>Included with T3 Code. No download needed.</T>
374 </p>
375 ) : null}
376 {step === "access" && backend === "gnome" && extension?.status === "enabled" ? (
377 <Button
378 size="xs"
379 variant="ghost"
380 disabled={busy}
381 onClick={() => void onAction("disable-extension")}
382 >
383 <T>Disable extension</T>
384 </Button>
385 ) : null}
386 {step === "access" && helperBackend && helper?.status !== "not-installed" ? (
387 <Button
388 size="xs"
389 variant="ghost"
390 disabled={busy}
391 onClick={() => void onAction(removeHelper)}
392 >
393 <T>Remove capture helper</T>
394 </Button>
395 ) : null}
396 </div>
397 </details>
398 ) : null}
399 </div>
400 </WizardPanel>
401 <WizardFooter>
402 {step !== "access" ? (
403 <Button variant="ghost" disabled={busy} onClick={() => changeStep("access")}>
404 <T>Back</T>
405 </Button>
406 ) : null}
407 <Button variant="ghost" disabled={busy} onClick={() => void onClose(false)}>
408 {wasEnabled ? gt("Close") : gt("Finish later")}
409 </Button>
410 {step === "access" ? (
411 helperBackend && !accessReady && helper?.status !== "ready" ? (
412 <Button
413 disabled={busy}
414 aria-busy={busy}
415 onClick={() =>
416 void (helper?.status === "error" ? checkAgain() : onAction(installHelper))
417 }
418 >
419 {checking
420 ? gt("Checking…")
421 : busy
422 ? gt("Installing…")
423 : helper?.status === "error"
424 ? gt("Check again")
425 : helper?.status === "update-required"
426 ? gt("Update helper")
427 : gt("Install helper")}
428 </Button>
429 ) : backend === "gnome" && !accessReady && extension?.status !== "enabled" ? (
430 <Button
431 disabled={busy}
432 aria-busy={checking}
433 onClick={() =>
434 void (install
435 ? onAction("install-extension")
436 : enable
437 ? onAction("enable-extension")
438 : checkAgain())
439 }
440 >
441 {checking
442 ? gt("Checking…")
443 : busy
444 ? install
445 ? gt("Installing…")
446 : enable
447 ? gt("Enabling…")
448 : gt("Working…")
449 : install
450 ? extension?.status === "update-required"
451 ? gt("Update extension")
452 : gt("Install extension")
453 : enable
454 ? gt("Enable extension")
455 : gt("Check again")}
456 </Button>
457 ) : (
458 <PermissionContinueButton
459 ready={macPermissionsReady}
460 busy={busy}
461 onClick={async () => {
462 if (await onEnable()) changeStep("shortcut");
463 }}
464 >
465 {busy
466 ? gt("Working…")
467 : macPermissions
468 ? gt("Test capture and continue")
469 : backend === "direct"
470 ? gt("Allow capture")
471 : !accessReady && !macPermissions
472 ? gt("Try again")
473 : gt("Continue")}
474 </PermissionContinueButton>
475 )
476 ) : !configShortcut ? (
477 <Button
478 disabled={
479 busy || !accessReady || (shortcutChanged ? !canSaveShortcut : !shortcutReady)
480 }
481 onClick={async () => {
482 if (!shortcutChanged || (await onSaveShortcut())) await onClose(true);
483 }}
484 >
485 {busy ? gt("Saving…") : shortcutChanged ? gt("Save and finish") : gt("Done")}
486 </Button>
487 ) : null}
488 </WizardFooter>
489 </WizardPopup>
490 </Dialog>
491 );
492 }