Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SnapShotSetupDialog.tsx

473 lines18.0 KB

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