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