@@ -8,8 +8,11 @@ import {
88 type SnapShotShortcut ,
99} from "@t3tools/contracts" ;
1010import { PlayIcon } from "lucide-react" ;
11- import { useCallback , useEffect , useRef , useState } from "react" ;
11+ import { settlePromise } from "@t3tools/client-runtime/state/runtime" ;
12+ import * as Cause from "effect/Cause" ;
13+ import { useEffect , useRef , useState } from "react" ;
1214
15+ import { useStableCallback } from "../../hooks/useStableCallback" ;
1316import { useClientSettings , useUpdateClientSettings } from "../../hooks/useSettings" ;
1417import { getDesktopSnapShotBridge } from "../../lib/desktopSnapShot" ;
1518import {
@@ -62,9 +65,14 @@ function captureSettingsError(title: string, error: unknown) {
6265
6366type ShortcutCheck =
6467 | { readonly status : "idle" ; readonly availability : null }
65- | { readonly status : "checking" ; readonly availability : null }
68+ | {
69+ readonly status : "checking" ;
70+ readonly availability : null ;
71+ readonly shortcut : SnapShotShortcut ;
72+ }
6673 | {
6774 readonly status : "checked" ;
75+ readonly shortcut : SnapShotShortcut ;
6876 readonly availability : DesktopSnapShotShortcutAvailability ;
6977 } ;
7078
@@ -83,10 +91,17 @@ export function SnapShotSettings() {
8391 wasEnabled : boolean ;
8492 } | null > ( null ) ;
8593 const [ candidate , setCandidate ] = useState < SnapShotShortcut > ( settings . snapShotShortcut ) ;
86- const [ shortcutCheck , setShortcutCheck ] = useState < ShortcutCheck > ( {
94+ const [ latestShortcutCheck , setShortcutCheck ] = useState < ShortcutCheck > ( {
8795 status : "idle" ,
8896 availability : null ,
8997 } ) ;
98+ // A check answers for one shortcut; once the candidate moves on (an edit, or hydration
99+ // resetting it) a late answer for the old one no longer shows.
100+ const shortcutCheck : ShortcutCheck =
101+ latestShortcutCheck . status === "idle" ||
102+ sameSnapShotShortcut ( latestShortcutCheck . shortcut , candidate )
103+ ? latestShortcutCheck
104+ : { status : "idle" , availability : null } ;
90105 const shortcutCheckIdRef = useRef ( 0 ) ;
91106 const stateRequestIdRef = useRef ( 0 ) ;
92107 const unavailableMessage = snapShotUnavailableMessage ( Boolean ( bridge ) ) ;
@@ -105,7 +120,8 @@ export function SnapShotSettings() {
105120 const soundLabel =
106121 soundSelection === "off" ? "Off" : soundSelection === "soft-pop" ? "Whoosh (Default)" : "Click" ;
107122
108- const refreshState = useCallback ( async ( ) => {
123+ // Registered as a window focus listener, so it keeps one identity.
124+ const refreshState = useStableCallback ( async ( ) => {
109125 const requestId = ++ stateRequestIdRef . current ;
110126 try {
111127 if ( bridge ) {
@@ -117,48 +133,48 @@ export function SnapShotSettings() {
117133 if ( requestId === stateRequestIdRef . current )
118134 setSetupError ( captureSettingsError ( "Couldn't check capture setup" , error ) ) ;
119135 }
120- } , [ bridge ] ) ;
136+ } ) ;
121137
122- const setup = useCallback (
123- async ( action : DesktopSnapShotSetupAction ) => {
124- if ( ! bridge ?. setupSnapShot || setupBusy ) return ;
125- setSetupBusy ( true ) ;
126- setSetupError ( null ) ;
127- try {
128- if (
129- state ?. macPermissions &&
130- wizard &&
131- ( action === "allow-screen-recording" || action === "allow-accessibility" )
132- ) {
133- // macOS can quit the app from its permission prompt.
134- saveSnapShotSetupResume ( wizard . wasEnabled ) ;
135- }
136- await bridge . setupSnapShot ( action ) ;
137- await refreshState ( ) ;
138- } catch ( error ) {
139- setSetupError (
140- captureSettingsError (
141- action === "retry-shortcut"
142- ? "Couldn't open shortcut permissions"
143- : "Couldn't complete capture setup" ,
144- error ,
145- ) ,
146- ) ;
147- } finally {
148- setSetupBusy ( false ) ;
138+ const setup = async ( action : DesktopSnapShotSetupAction ) => {
139+ if ( ! bridge ?. setupSnapShot || setupBusy ) return ;
140+ setSetupBusy ( true ) ;
141+ setSetupError ( null ) ;
142+ const result = await settlePromise ( async ( ) => {
143+ if (
144+ state ?. macPermissions &&
145+ wizard &&
146+ ( action === "allow-screen-recording" || action === "allow-accessibility" )
147+ ) {
148+ // macOS can quit the app from its permission prompt.
149+ saveSnapShotSetupResume ( wizard . wasEnabled ) ;
149150 }
150- } ,
151- [ bridge , refreshState , setupBusy , state , wizard ] ,
152- ) ;
151+ await bridge . setupSnapShot ! ( action ) ;
152+ await refreshState ( ) ;
153+ } ) ;
154+ if ( result . _tag === "Failure" ) {
155+ setSetupError (
156+ captureSettingsError (
157+ action === "retry-shortcut"
158+ ? "Couldn't open shortcut permissions"
159+ : "Couldn't complete capture setup" ,
160+ Cause . squash ( result . cause ) ,
161+ ) ,
162+ ) ;
163+ }
164+ setSetupBusy ( false ) ;
165+ } ;
153166
167+ // Outside the wizard there is nowhere to show an error inline, so it goes to a toast,
168+ // once. The error stays in state until the next action clears it.
169+ const toastedErrorRef = useRef < typeof setupError > ( null ) ;
154170 useEffect ( ( ) => {
155- if ( ! setupError || wizard ) return ;
171+ if ( ! setupError || wizard || toastedErrorRef . current === setupError ) return ;
172+ toastedErrorRef . current = setupError ;
156173 toastManager . add ( {
157174 type : "error" ,
158175 title : setupError . title ,
159176 description : setupError . message ,
160177 } ) ;
161- setSetupError ( null ) ;
162178 } , [ setupError , wizard ] ) ;
163179
164180 useEffect ( ( ) => {
@@ -184,63 +200,64 @@ export function SnapShotSettings() {
184200 [ bridge , refreshState ] ,
185201 ) ;
186202
187- useEffect ( ( ) => {
188- shortcutCheckIdRef . current ++ ;
203+ // Hydration can hand back an equal shortcut object; only a different shortcut resets the edit.
204+ const [ renderedSavedShortcut , setRenderedSavedShortcut ] = useState ( savedShortcut ) ;
205+ if ( ! sameSnapShotShortcut ( renderedSavedShortcut , savedShortcut ) ) {
206+ setRenderedSavedShortcut ( savedShortcut ) ;
189207 setCandidate ( savedShortcut ) ;
190208 setShortcutCheck ( { status : "idle" , availability : null } ) ;
209+ }
210+ // A check still in flight was for the shortcut being replaced.
211+ const checkedForSavedShortcutRef = useRef ( savedShortcut ) ;
212+ useEffect ( ( ) => {
213+ if ( sameSnapShotShortcut ( checkedForSavedShortcutRef . current , savedShortcut ) ) return ;
214+ checkedForSavedShortcutRef . current = savedShortcut ;
215+ shortcutCheckIdRef . current ++ ;
191216 } , [ savedShortcut ] ) ;
192217
193- const save = useCallback (
194- async ( patch : ClientSettingsPatch ) => {
195- setSetupError ( null ) ;
196- try {
197- await updateSettings ( patch ) ;
198- return await refreshState ( ) ;
199- } catch ( error ) {
200- setSetupError ( captureSettingsError ( "Couldn't save capture settings" , error ) ) ;
201- }
202- } ,
203- [ refreshState , updateSettings ] ,
204- ) ;
218+ const save = async ( patch : ClientSettingsPatch ) => {
219+ setSetupError ( null ) ;
220+ try {
221+ await updateSettings ( patch ) ;
222+ return await refreshState ( ) ;
223+ } catch ( error ) {
224+ setSetupError ( captureSettingsError ( "Couldn't save capture settings" , error ) ) ;
225+ }
226+ } ;
205227
206- const saveIncludeAccessibility = useCallback (
207- async ( includeAccessibility : boolean ) => {
208- try {
209- if ( includeAccessibility && settings . snapShotEnabled )
210- await bridge ?. requestSnapShotPermissions ( true ) ;
211- await save ( { snapShotIncludeAccessibility : includeAccessibility } ) ;
212- } catch ( error ) {
213- setSetupError ( captureSettingsError ( "Couldn't allow app text capture" , error ) ) ;
214- }
215- } ,
216- [ bridge , save , settings . snapShotEnabled ] ,
217- ) ;
228+ const saveIncludeAccessibility = async ( includeAccessibility : boolean ) => {
229+ try {
230+ if ( includeAccessibility && settings . snapShotEnabled )
231+ await bridge ?. requestSnapShotPermissions ( true ) ;
232+ await save ( { snapShotIncludeAccessibility : includeAccessibility } ) ;
233+ } catch ( error ) {
234+ setSetupError ( captureSettingsError ( "Couldn't allow app text capture" , error ) ) ;
235+ }
236+ } ;
218237
219- const checkShortcut = useCallback (
220- async ( shortcut : SnapShotShortcut ) => {
221- const checkId = ++ shortcutCheckIdRef . current ;
222- setCandidate ( shortcut ) ;
223- const conflict = snapShotKeybindingConflict ( shortcut , keybindings ) ;
224- if ( conflict || ! bridge ) return ;
225- setShortcutCheck ( { status : "checking" , availability : null } ) ;
226- try {
227- const availability = await bridge . checkSnapShotShortcut ( shortcut ) ;
228- if ( checkId === shortcutCheckIdRef . current ) {
229- setShortcutCheck ( { status : "checked" , availability } ) ;
230- }
231- } catch ( error ) {
232- if ( checkId !== shortcutCheckIdRef . current ) return ;
233- setShortcutCheck ( {
234- status : "checked" ,
235- availability : {
236- available : false ,
237- message : error instanceof Error ? error . message : "Could not check this shortcut." ,
238- } ,
239- } ) ;
238+ const checkShortcut = async ( shortcut : SnapShotShortcut ) => {
239+ const checkId = ++ shortcutCheckIdRef . current ;
240+ setCandidate ( shortcut ) ;
241+ const conflict = snapShotKeybindingConflict ( shortcut , keybindings ) ;
242+ if ( conflict || ! bridge ) return ;
243+ setShortcutCheck ( { status : "checking" , availability : null , shortcut } ) ;
244+ try {
245+ const availability = await bridge . checkSnapShotShortcut ( shortcut ) ;
246+ if ( checkId === shortcutCheckIdRef . current ) {
247+ setShortcutCheck ( { status : "checked" , availability, shortcut } ) ;
240248 }
241- } ,
242- [ bridge , keybindings ] ,
243- ) ;
249+ } catch ( error ) {
250+ if ( checkId !== shortcutCheckIdRef . current ) return ;
251+ setShortcutCheck ( {
252+ status : "checked" ,
253+ shortcut,
254+ availability : {
255+ available : false ,
256+ message : error instanceof Error ? error . message : "Could not check this shortcut." ,
257+ } ,
258+ } ) ;
259+ }
260+ } ;
244261
245262 const {
246263 recording,
@@ -257,7 +274,11 @@ export function SnapShotSettings() {
257274 setShortcutCheck ( { status : "idle" , availability : null } ) ;
258275 } ,
259276 onError : ( message ) =>
260- setShortcutCheck ( { status : "checked" , availability : { available : false , message } } ) ,
277+ setShortcutCheck ( {
278+ status : "checked" ,
279+ shortcut : candidate ,
280+ availability : { available : false , message } ,
281+ } ) ,
261282 } ) ;
262283
263284 const shortcutStatus = recording
@@ -284,33 +305,33 @@ export function SnapShotSettings() {
284305 setShortcutCheck ( { status : "idle" , availability : null } ) ;
285306 setSetupError ( null ) ;
286307 setSetupBusy ( true ) ;
287- try {
288- let current = await refreshState ( ) ;
289- if ( ! current ) return ;
290- if ( current . macPermissions ) requested = "access" ;
291- if ( ! settings . snapShotEnabled && captureSetupInitialStep ( current , requested ) !== "access" ) {
292- current = await save ( { snapShotEnabled : true } ) ;
293- if ( ! current ) {
294- // A settings write can succeed even if the following status check
295- // fails. Keep Finish later available to turn capture back off.
296- setWizard ( { initialStep : "access" , wasEnabled : false } ) ;
297- return ;
298- }
308+ await prepareSetup ( requested ) . finally ( ( ) => setSetupBusy ( false ) ) ;
309+ } ;
310+
311+ const prepareSetup = async ( requested : CaptureSetupStep | "resume" ) => {
312+ let current = await refreshState ( ) ;
313+ if ( ! current ) return ;
314+ if ( current . macPermissions ) requested = "access" ;
315+ if ( ! settings . snapShotEnabled && captureSetupInitialStep ( current , requested ) !== "access" ) {
316+ current = await save ( { snapShotEnabled : true } ) ;
317+ if ( ! current ) {
318+ // A settings write can succeed even if the following status check
319+ // fails. Keep Finish later available to turn capture back off.
320+ setWizard ( { initialStep : "access" , wasEnabled : false } ) ;
321+ return ;
299322 }
300- setWizard ( {
301- initialStep : captureSetupInitialStep ( current , requested ) ,
302- wasEnabled : settings . snapShotEnabled ,
303- } ) ;
304- } finally {
305- setSetupBusy ( false ) ;
306323 }
324+ setWizard ( {
325+ initialStep : captureSetupInitialStep ( current , requested ) ,
326+ wasEnabled : settings . snapShotEnabled ,
327+ } ) ;
307328 } ;
308329
309330 const enableForSetup = async ( ) => {
310331 if ( setupBusy ) return false ;
311332 setSetupBusy ( true ) ;
312333 setSetupError ( null ) ;
313- try {
334+ const verified = await settlePromise ( async ( ) => {
314335 if ( state ?. macPermissions ) {
315336 saveSnapShotSetupResume ( wizard ?. wasEnabled ?? settings . snapShotEnabled ) ;
316337 if ( ! bridge ?. setupSnapShot ) throw new Error ( "Restart T3 Code to finish capture setup." ) ;
@@ -323,18 +344,19 @@ export function SnapShotSettings() {
323344 ? await refreshState ( )
324345 : await save ( { snapShotEnabled : true } ) ;
325346 return nextState !== undefined && captureSetupAccessReady ( nextState ) ;
326- } catch ( error ) {
327- setSetupError ( captureSettingsError ( "Couldn't verify capture access" , error ) ) ;
328- return false ;
329- } finally {
330- setSetupBusy ( false ) ;
331- }
347+ } ) ;
348+ setSetupBusy ( false ) ;
349+ if ( verified . _tag === "Success" ) return verified . value ;
350+ setSetupError (
351+ captureSettingsError ( "Couldn't verify capture access" , Cause . squash ( verified . cause ) ) ,
352+ ) ;
353+ return false ;
332354 } ;
333355
334356 const closeSetup = async ( completed : boolean ) => {
335357 if ( ! wizard || setupBusy ) return ;
336358 setSetupBusy ( true ) ;
337- try {
359+ const closed = await settlePromise ( async ( ) => {
338360 if (
339361 settings . snapShotEnabled &&
340362 captureSetupShouldDisableOnClose ( wizard . wasEnabled , completed )
@@ -345,22 +367,20 @@ export function SnapShotSettings() {
345367 setSetupError ( null ) ;
346368 clearSnapShotSetupResume ( ) ;
347369 setWizard ( null ) ;
348- } catch ( error ) {
349- setSetupError ( captureSettingsError ( "Couldn't close capture setup" , error ) ) ;
350- } finally {
351- setSetupBusy ( false ) ;
370+ } ) ;
371+ if ( closed . _tag === "Failure" ) {
372+ setSetupError (
373+ captureSettingsError ( "Couldn't close capture setup" , Cause . squash ( closed . cause ) ) ,
374+ ) ;
352375 }
376+ setSetupBusy ( false ) ;
353377 } ;
354378
355379 const saveShortcut = async ( ) => {
356380 if ( ! canSaveShortcut || setupBusy ) return false ;
357381 setSetupBusy ( true ) ;
358- try {
359- const saved = await save ( { snapShotShortcut : candidate } ) ;
360- return Boolean ( saved ?. shortcutRegistered || saved ?. shortcutPending ) ;
361- } finally {
362- setSetupBusy ( false ) ;
363- }
382+ const saved = await save ( { snapShotShortcut : candidate } ) . finally ( ( ) => setSetupBusy ( false ) ) ;
383+ return Boolean ( saved ?. shortcutRegistered || saved ?. shortcutPending ) ;
364384 } ;
365385
366386 return (
0 commit comments