@@ -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 {
@@ -105,7 +108,8 @@ export function SnapShotSettings() {
105108 const soundLabel =
106109 soundSelection === "off" ? "Off" : soundSelection === "soft-pop" ? "Whoosh (Default)" : "Click" ;
107110
108- const refreshState = useCallback ( async ( ) => {
111+ // Registered as a window focus listener, so it keeps one identity.
112+ const refreshState = useStableCallback ( async ( ) => {
109113 const requestId = ++ stateRequestIdRef . current ;
110114 try {
111115 if ( bridge ) {
@@ -117,48 +121,48 @@ export function SnapShotSettings() {
117121 if ( requestId === stateRequestIdRef . current )
118122 setSetupError ( captureSettingsError ( "Couldn't check capture setup" , error ) ) ;
119123 }
120- } , [ bridge ] ) ;
124+ } ) ;
121125
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 ) ;
126+ const setup = async ( action : DesktopSnapShotSetupAction ) => {
127+ if ( ! bridge ?. setupSnapShot || setupBusy ) return ;
128+ setSetupBusy ( true ) ;
129+ setSetupError ( null ) ;
130+ const result = await settlePromise ( async ( ) => {
131+ if (
132+ state ?. macPermissions &&
133+ wizard &&
134+ ( action === "allow-screen-recording" || action === "allow-accessibility" )
135+ ) {
136+ // macOS can quit the app from its permission prompt.
137+ saveSnapShotSetupResume ( wizard . wasEnabled ) ;
149138 }
150- } ,
151- [ bridge , refreshState , setupBusy , state , wizard ] ,
152- ) ;
139+ await bridge . setupSnapShot ! ( action ) ;
140+ await refreshState ( ) ;
141+ } ) ;
142+ if ( result . _tag === "Failure" ) {
143+ setSetupError (
144+ captureSettingsError (
145+ action === "retry-shortcut"
146+ ? "Couldn't open shortcut permissions"
147+ : "Couldn't complete capture setup" ,
148+ Cause . squash ( result . cause ) ,
149+ ) ,
150+ ) ;
151+ }
152+ setSetupBusy ( false ) ;
153+ } ;
153154
155+ // Outside the wizard there is nowhere to show an error inline, so it goes to a toast,
156+ // once. The error stays in state until the next action clears it.
157+ const toastedErrorRef = useRef < typeof setupError > ( null ) ;
154158 useEffect ( ( ) => {
155- if ( ! setupError || wizard ) return ;
159+ if ( ! setupError || wizard || toastedErrorRef . current === setupError ) return ;
160+ toastedErrorRef . current = setupError ;
156161 toastManager . add ( {
157162 type : "error" ,
158163 title : setupError . title ,
159164 description : setupError . message ,
160165 } ) ;
161- setSetupError ( null ) ;
162166 } , [ setupError , wizard ] ) ;
163167
164168 useEffect ( ( ) => {
@@ -184,63 +188,62 @@ export function SnapShotSettings() {
184188 [ bridge , refreshState ] ,
185189 ) ;
186190
187- useEffect ( ( ) => {
188- shortcutCheckIdRef . current ++ ;
191+ const [ renderedSavedShortcut , setRenderedSavedShortcut ] = useState ( savedShortcut ) ;
192+ if ( renderedSavedShortcut !== savedShortcut ) {
193+ setRenderedSavedShortcut ( savedShortcut ) ;
189194 setCandidate ( savedShortcut ) ;
190195 setShortcutCheck ( { status : "idle" , availability : null } ) ;
196+ }
197+ // A check still in flight was for the shortcut being replaced.
198+ const checkedForSavedShortcutRef = useRef ( savedShortcut ) ;
199+ useEffect ( ( ) => {
200+ if ( checkedForSavedShortcutRef . current === savedShortcut ) return ;
201+ checkedForSavedShortcutRef . current = savedShortcut ;
202+ shortcutCheckIdRef . current ++ ;
191203 } , [ savedShortcut ] ) ;
192204
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- ) ;
205+ const save = async ( patch : ClientSettingsPatch ) => {
206+ setSetupError ( null ) ;
207+ try {
208+ await updateSettings ( patch ) ;
209+ return await refreshState ( ) ;
210+ } catch ( error ) {
211+ setSetupError ( captureSettingsError ( "Couldn't save capture settings" , error ) ) ;
212+ }
213+ } ;
205214
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- ) ;
215+ const saveIncludeAccessibility = async ( includeAccessibility : boolean ) => {
216+ try {
217+ if ( includeAccessibility && settings . snapShotEnabled )
218+ await bridge ?. requestSnapShotPermissions ( true ) ;
219+ await save ( { snapShotIncludeAccessibility : includeAccessibility } ) ;
220+ } catch ( error ) {
221+ setSetupError ( captureSettingsError ( "Couldn't allow app text capture" , error ) ) ;
222+ }
223+ } ;
218224
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- } ) ;
225+ const checkShortcut = async ( shortcut : SnapShotShortcut ) => {
226+ const checkId = ++ shortcutCheckIdRef . current ;
227+ setCandidate ( shortcut ) ;
228+ const conflict = snapShotKeybindingConflict ( shortcut , keybindings ) ;
229+ if ( conflict || ! bridge ) return ;
230+ setShortcutCheck ( { status : "checking" , availability : null } ) ;
231+ try {
232+ const availability = await bridge . checkSnapShotShortcut ( shortcut ) ;
233+ if ( checkId === shortcutCheckIdRef . current ) {
234+ setShortcutCheck ( { status : "checked" , availability } ) ;
240235 }
241- } ,
242- [ bridge , keybindings ] ,
243- ) ;
236+ } catch ( error ) {
237+ if ( checkId !== shortcutCheckIdRef . current ) return ;
238+ setShortcutCheck ( {
239+ status : "checked" ,
240+ availability : {
241+ available : false ,
242+ message : error instanceof Error ? error . message : "Could not check this shortcut." ,
243+ } ,
244+ } ) ;
245+ }
246+ } ;
244247
245248 const {
246249 recording,
@@ -284,33 +287,33 @@ export function SnapShotSettings() {
284287 setShortcutCheck ( { status : "idle" , availability : null } ) ;
285288 setSetupError ( null ) ;
286289 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- }
290+ await prepareSetup ( requested ) . finally ( ( ) => setSetupBusy ( false ) ) ;
291+ } ;
292+
293+ const prepareSetup = async ( requested : CaptureSetupStep | "resume" ) => {
294+ let current = await refreshState ( ) ;
295+ if ( ! current ) return ;
296+ if ( current . macPermissions ) requested = "access" ;
297+ if ( ! settings . snapShotEnabled && captureSetupInitialStep ( current , requested ) !== "access" ) {
298+ current = await save ( { snapShotEnabled : true } ) ;
299+ if ( ! current ) {
300+ // A settings write can succeed even if the following status check
301+ // fails. Keep Finish later available to turn capture back off.
302+ setWizard ( { initialStep : "access" , wasEnabled : false } ) ;
303+ return ;
299304 }
300- setWizard ( {
301- initialStep : captureSetupInitialStep ( current , requested ) ,
302- wasEnabled : settings . snapShotEnabled ,
303- } ) ;
304- } finally {
305- setSetupBusy ( false ) ;
306305 }
306+ setWizard ( {
307+ initialStep : captureSetupInitialStep ( current , requested ) ,
308+ wasEnabled : settings . snapShotEnabled ,
309+ } ) ;
307310 } ;
308311
309312 const enableForSetup = async ( ) => {
310313 if ( setupBusy ) return false ;
311314 setSetupBusy ( true ) ;
312315 setSetupError ( null ) ;
313- try {
316+ const verified = await settlePromise ( async ( ) => {
314317 if ( state ?. macPermissions ) {
315318 saveSnapShotSetupResume ( wizard ?. wasEnabled ?? settings . snapShotEnabled ) ;
316319 if ( ! bridge ?. setupSnapShot ) throw new Error ( "Restart T3 Code to finish capture setup." ) ;
@@ -323,18 +326,19 @@ export function SnapShotSettings() {
323326 ? await refreshState ( )
324327 : await save ( { snapShotEnabled : true } ) ;
325328 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- }
329+ } ) ;
330+ setSetupBusy ( false ) ;
331+ if ( verified . _tag === "Success" ) return verified . value ;
332+ setSetupError (
333+ captureSettingsError ( "Couldn't verify capture access" , Cause . squash ( verified . cause ) ) ,
334+ ) ;
335+ return false ;
332336 } ;
333337
334338 const closeSetup = async ( completed : boolean ) => {
335339 if ( ! wizard || setupBusy ) return ;
336340 setSetupBusy ( true ) ;
337- try {
341+ const closed = await settlePromise ( async ( ) => {
338342 if (
339343 settings . snapShotEnabled &&
340344 captureSetupShouldDisableOnClose ( wizard . wasEnabled , completed )
@@ -345,22 +349,20 @@ export function SnapShotSettings() {
345349 setSetupError ( null ) ;
346350 clearSnapShotSetupResume ( ) ;
347351 setWizard ( null ) ;
348- } catch ( error ) {
349- setSetupError ( captureSettingsError ( "Couldn't close capture setup" , error ) ) ;
350- } finally {
351- setSetupBusy ( false ) ;
352+ } ) ;
353+ if ( closed . _tag === "Failure" ) {
354+ setSetupError (
355+ captureSettingsError ( "Couldn't close capture setup" , Cause . squash ( closed . cause ) ) ,
356+ ) ;
352357 }
358+ setSetupBusy ( false ) ;
353359 } ;
354360
355361 const saveShortcut = async ( ) => {
356362 if ( ! canSaveShortcut || setupBusy ) return false ;
357363 setSetupBusy ( true ) ;
358- try {
359- const saved = await save ( { snapShotShortcut : candidate } ) ;
360- return Boolean ( saved ?. shortcutRegistered || saved ?. shortcutPending ) ;
361- } finally {
362- setSetupBusy ( false ) ;
363- }
364+ const saved = await save ( { snapShotShortcut : candidate } ) . finally ( ( ) => setSetupBusy ( false ) ) ;
365+ return Boolean ( saved ?. shortcutRegistered || saved ?. shortcutPending ) ;
364366 } ;
365367
366368 return (
0 commit comments