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 { useState, type ReactNode } from "react";
9import { MacAccessibilityIcon, MacScreenRecordingIcon } from "../Icons";
10import { CaptureShortcutConfig } from "./CaptureShortcutConfig";
11import { Button } from "../ui/button";
12import { Dialog, DialogDescription } from "../ui/dialog";
13import { WizardSteps, WizardPopup, WizardHeader, WizardPanel, WizardFooter } from "../ui/wizard";
14import {
15 captureSetupAccessReady,
16 captureSetupBackend,
17 captureSetupCheckMessage,
18 captureSetupDesktopName,
19 captureSetupInitialStep,
20 captureSetupShortcutReady,
21 type CaptureSetupStep,
22} from "./SnapShotSetupDialog.logic";
23import { msg, T, useGT, useMessages } from "gt-react";
24
25const SETUP_STEPS = [
26 { id: "access", label: msg({ context: "setup step: capture permissions" })`Access` },
27 { id: "shortcut", label: msg({ context: "setup step: keyboard shortcut" })`Shortcut` },
28] as const;
29
30const 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
67export 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`T3 Code's capture helper lets you capture other apps and return to your draft. It's included with T3 Code.`,
181 }
182 : backend === "niri"
183 ? {
184 title: gt`Capture is ready`,
185 description: gt`Next, choose your shortcut.`,
186 }
187 : backend === "picker"
188 ? {
189 title: gt`Choose a window each time`,
190 description: gt`Your desktop doesn't support automatic capture. You'll choose the window to capture instead.`,
191 }
192 : {
193 title: gt`Allow snapshots`,
194 description:
195 backend === "portal"
196 ? gt`Your desktop may ask for permission when you first capture.`
197 : macPermissions
198 ? macPermissionsReady
199 ? gt`Test a snapshot of the current window. If macOS asks to bypass its window picker, choose Allow. The test image is discarded.`
200 : gt`Allow each permission, then continue.`
201 : gt`Allow access when prompted to start capturing windows.`,
202 };
203 const title = step === "access" ? accessCopy.title : gt`Choose your shortcut`;
204 const description =
205 step === "access"
206 ? accessCopy.description
207 : configShortcut
208 ? gt`Click the shortcut, then press the keys you want.`
209 : state.mode === "portal"
210 ? gt`Choose your keys, then approve the permission prompt if asked.`
211 : gt`Use both Shift keys, or record a different shortcut.`;
212 const stepIndex = SETUP_STEPS.findIndex(({ id }) => id === step);
213 const details = [
214 ...new Set(
215 [
216 error,
217 ...(step === "access"
218 ? [
219 state.message,
220 backend === "gnome" &&
221 (extension?.status === "error" || extension?.status === "unsupported")
222 ? extension.message
223 : null,
224 helperBackend && helper?.status === "error" ? helper.message : null,
225 ]
226 : []),
227 ].filter((detail) => detail !== null),
228 ),
229 ];
230
231 return (
232 <Dialog
233 open
234 onOpenChange={(open) => {
235 if (!open && !busy) void onClose(false);
236 }}
237 >
238 <WizardPopup showCloseButton={!busy}>
239 <WizardHeader
240 title={desktop ? gt`Set up snapshots for ${{ desktop }}` : gt`Set up snapshots`}
241 >
242 <WizardSteps
243 steps={SETUP_STEPS.map((item) => messages(item.label))}
244 currentStep={stepIndex}
245 isStepDisabled={(index) => busy || index > stepIndex}
246 onStepChange={(index) => {
247 const next = SETUP_STEPS[index];
248 if (next && next.id !== step) changeStep(next.id);
249 }}
250 />
251 </WizardHeader>
252 <WizardPanel>
253 <div className="space-y-4 text-sm">
254 <div className="space-y-2" aria-live="polite">
255 <h3 className="flex items-center gap-2 font-medium">{title}</h3>
256 <DialogDescription>{description}</DialogDescription>
257 </div>
258 {step === "access" ? (
259 <>
260 <p
261 role="status"
262 aria-atomic="true"
263 className={
264 checked && !busy && !error ? "text-xs text-muted-foreground" : "sr-only"
265 }
266 >
267 {checked && !busy && !error ? captureSetupCheckMessage(state, messages) : null}
268 </p>
269 {macPermissions ? (
270 <PermissionChecklist
271 busy={busy}
272 permissions={[
273 {
274 id: "screenRecording",
275 icon: <MacScreenRecordingIcon className="size-8 shrink-0 drop-shadow-sm" />,
276 title: gt`Screen Recording`,
277 description: gt`Capture the window you're using.`,
278 granted: macPermissions.screenRecording,
279 onAllow: () => void onAction("allow-screen-recording"),
280 },
281 {
282 id: "accessibility",
283 icon: <MacAccessibilityIcon className="size-8 shrink-0 drop-shadow-sm" />,
284 title: gt`Accessibility`,
285 description: includeAccessibility
286 ? gt`Include text and controls from the captured app.`
287 : gt`Optional. Include text and controls from the captured app.`,
288 granted: macPermissions.accessibility,
289 onAllow: () => void onAction("allow-accessibility"),
290 },
291 ]}
292 />
293 ) : null}
294 {permissionStatus.error && macPermissions ? (
295 <p role="status" className="text-xs text-muted-foreground">
296 {permissionStatus.error}
297 </p>
298 ) : null}
299 {helperBackend && helper?.status === "error" ? (
300 <Button
301 size="xs"
302 variant="outline"
303 disabled={busy}
304 onClick={() => void onAction(installHelper)}
305 >
306 <T>Reinstall helper</T>
307 </Button>
308 ) : null}
309 </>
310 ) : configShortcut ? (
311 <CaptureShortcutConfig
312 state={state}
313 disabled={actionBusy || checking || !accessReady}
314 onBusyChange={setConfigBusy}
315 onSaved={onRefresh}
316 onComplete={() => onClose(true)}
317 />
318 ) : (
319 <div className="space-y-3">
320 {shortcutInput}
321 {shortcutStatus ? (
322 <p className="text-xs text-muted-foreground" role="status">
323 {shortcutStatus}
324 </p>
325 ) : null}
326 {!shortcutChanged &&
327 !state.shortcutRegistered &&
328 !state.shortcutPending &&
329 state.shortcutCanRetry !== false &&
330 !isModifierPairShortcut(state.shortcut) ? (
331 <Button
332 variant="ghost"
333 size="sm"
334 disabled={busy}
335 onClick={() => void onAction("retry-shortcut")}
336 >
337 {state.mode === "portal" ? gt`Shortcut permissions` : gt`Try again`}
338 </Button>
339 ) : null}
340 </div>
341 )}
342 {step === "shortcut" && !accessReady ? (
343 <p role="alert" className="text-destructive">
344 <T>Capture needs attention. Go back to check access.</T>
345 </p>
346 ) : null}
347 {error ? (
348 <p role="alert" className="text-destructive">
349 <T>Couldn't finish this step. Try again or check Advanced for help.</T>
350 </p>
351 ) : null}
352 {details.length > 0 || (step === "access" && (backend === "gnome" || helperBackend)) ? (
353 <details className="text-xs text-muted-foreground">
354 <summary className="cursor-pointer">
355 <T>Advanced</T>
356 </summary>
357 <div className="mt-3 space-y-3">
358 {details.map((detail) => (
359 <p key={detail} className="break-words">
360 {detail}
361 </p>
362 ))}
363 {step === "access" && (backend === "gnome" || helperBackend) ? (
364 <p>
365 <T>Included with T3 Code. No download needed.</T>
366 </p>
367 ) : null}
368 {step === "access" && backend === "gnome" && extension?.status === "enabled" ? (
369 <Button
370 size="xs"
371 variant="ghost"
372 disabled={busy}
373 onClick={() => void onAction("disable-extension")}
374 >
375 <T>Disable extension</T>
376 </Button>
377 ) : null}
378 {step === "access" && helperBackend && helper?.status !== "not-installed" ? (
379 <Button
380 size="xs"
381 variant="ghost"
382 disabled={busy}
383 onClick={() => void onAction(removeHelper)}
384 >
385 <T>Remove capture helper</T>
386 </Button>
387 ) : null}
388 </div>
389 </details>
390 ) : null}
391 </div>
392 </WizardPanel>
393 <WizardFooter>
394 {step !== "access" ? (
395 <Button variant="ghost" disabled={busy} onClick={() => changeStep("access")}>
396 <T>Back</T>
397 </Button>
398 ) : null}
399 <Button variant="ghost" disabled={busy} onClick={() => void onClose(false)}>
400 {wasEnabled ? gt`Close` : gt`Finish later`}
401 </Button>
402 {step === "access" ? (
403 helperBackend && !accessReady && helper?.status !== "ready" ? (
404 <Button
405 disabled={busy}
406 aria-busy={busy}
407 onClick={() =>
408 void (helper?.status === "error" ? checkAgain() : onAction(installHelper))
409 }
410 >
411 {checking
412 ? gt`Checking…`
413 : busy
414 ? gt`Installing…`
415 : helper?.status === "error"
416 ? gt`Check again`
417 : helper?.status === "update-required"
418 ? gt`Update helper`
419 : gt`Install helper`}
420 </Button>
421 ) : backend === "gnome" && !accessReady && extension?.status !== "enabled" ? (
422 <Button
423 disabled={busy}
424 aria-busy={checking}
425 onClick={() =>
426 void (install
427 ? onAction("install-extension")
428 : enable
429 ? onAction("enable-extension")
430 : checkAgain())
431 }
432 >
433 {checking
434 ? gt`Checking…`
435 : busy
436 ? install
437 ? gt`Installing…`
438 : enable
439 ? gt`Enabling…`
440 : gt`Working…`
441 : install
442 ? extension?.status === "update-required"
443 ? gt`Update extension`
444 : gt`Install extension`
445 : enable
446 ? gt`Enable extension`
447 : gt`Check again`}
448 </Button>
449 ) : (
450 <PermissionContinueButton
451 ready={macPermissionsReady}
452 busy={busy}
453 onClick={async () => {
454 if (await onEnable()) changeStep("shortcut");
455 }}
456 >
457 {busy
458 ? gt`Working…`
459 : macPermissions
460 ? gt`Test capture and continue`
461 : backend === "direct"
462 ? gt`Allow capture`
463 : !accessReady && !macPermissions
464 ? gt`Try again`
465 : gt`Continue`}
466 </PermissionContinueButton>
467 )
468 ) : !configShortcut ? (
469 <Button
470 disabled={
471 busy || !accessReady || (shortcutChanged ? !canSaveShortcut : !shortcutReady)
472 }
473 onClick={async () => {
474 if (!shortcutChanged || (await onSaveShortcut())) await onClose(true);
475 }}
476 >
477 {busy ? gt`Saving…` : shortcutChanged ? gt`Save and finish` : gt`Done`}
478 </Button>
479 ) : null}
480 </WizardFooter>
481 </WizardPopup>
482 </Dialog>
483 );
484}