@@ -23,28 +23,38 @@ type AudioChoice = typeof AudioChoice.Type;
2323const savedKey = ( id : string ) => `${ AUDIO_KEY } :saved:${ id } ` ;
2424const pendingGrant = ( choice : AudioChoice ) =>
2525 choice && "pending" in choice ? choice . pending : null ;
26- const activeGrant = ( choice : AudioChoice , saved : boolean ) =>
27- choice && "pending" in choice ? ( saved ? choice . pending : choice . previous ) : choice ;
2826const CleanupGrant = Schema . NullOr ( Schema . String ) ;
2927const decodeChoice = Schema . decodeSync ( Schema . fromJsonString ( AudioChoice ) ) ;
28+ const readChoice = ( ) => readLocalStorageValue ( AUDIO_KEY , null , AudioChoice ) ;
3029
31- function isSaved ( id : string | undefined ) {
32- return ! ! id && window . localStorage . getItem ( savedKey ( id ) ) === "true" ;
30+ function readSaveMarker ( id : string | undefined ) {
31+ try {
32+ return id ? window . localStorage . getItem ( savedKey ( id ) ) : null ;
33+ } catch {
34+ return undefined ;
35+ }
36+ }
37+
38+ function activeGrant ( choice : AudioChoice , marker : string | null | undefined ) {
39+ if ( ! choice || ! ( "pending" in choice ) ) return choice ;
40+ // Only a confirmed missing marker means the previous grant still applies.
41+ if ( marker === null ) return choice . previous ;
42+ return marker === "true" ? choice . pending : null ;
3343}
3444
3545export function useAudioGrant ( ) {
3646 const [ choice ] = useLocalStorage ( AUDIO_KEY , null , AudioChoice ) ;
3747 const id = pendingGrant ( choice ) ?. id ;
38- const saved = useSyncExternalStore (
48+ const marker = useSyncExternalStore (
3949 ( notify ) => ( id ? subscribeLocalStorageKey ( savedKey ( id ) , notify ) : ( ) => { } ) ,
40- ( ) => isSaved ( id ) ,
41- ( ) => false ,
50+ ( ) => readSaveMarker ( id ) ,
51+ ( ) => undefined ,
4252 ) ;
43- return activeGrant ( choice , saved ) ;
53+ return activeGrant ( choice , marker ) ;
4454}
4555
46- function replaceChoice ( choice : AudioChoice ) {
47- const old = pendingGrant ( readLocalStorageValue ( AUDIO_KEY , null , AudioChoice ) ) ;
56+ function replaceChoice ( choice : AudioChoice , previous = readChoice ( ) ) {
57+ const old = pendingGrant ( previous ) ;
4858 writeLocalStorageValue ( AUDIO_KEY , choice , null , AudioChoice ) ;
4959 if ( old && pendingGrant ( getLocalStorageItem ( AUDIO_KEY , AudioChoice ) ) ?. id !== old . id )
5060 window . localStorage . removeItem ( savedKey ( old . id ) ) ;
@@ -57,12 +67,15 @@ export function chooseAudioDestination(destination: VoiceTranscriptionDestinatio
5767export function pendingCloudAudioChoice (
5868 destination : Extract < VoiceTranscriptionDestination , { kind : "cloud" } > ,
5969) {
60- const choice = getLocalStorageItem ( AUDIO_KEY , AudioChoice ) ;
70+ const choice = readChoice ( ) ;
6171 const pending = { id : randomUUID ( ) , destination } ;
62- replaceChoice ( { pending, previous : activeGrant ( choice , isSaved ( pendingGrant ( choice ) ?. id ) ) } ) ;
72+ replaceChoice (
73+ { pending, previous : activeGrant ( choice , readSaveMarker ( pendingGrant ( choice ) ?. id ) ) } ,
74+ choice ,
75+ ) ;
6376 // A late response can acknowledge only its own choice, never write the destination another tab picked.
6477 return ( ) => {
65- if ( pendingGrant ( getLocalStorageItem ( AUDIO_KEY , AudioChoice ) ) ?. id === pending . id )
78+ if ( pendingGrant ( readChoice ( ) ) ?. id === pending . id )
6679 writeLocalStorageValue ( savedKey ( pending . id ) , true , false , Schema . Boolean ) ;
6780 } ;
6881}
@@ -78,7 +91,7 @@ export function bindAudioGrant(id: string) {
7891 choice = raw ? decodeChoice ( raw ) : null ;
7992 cachedRaw = raw ;
8093 }
81- return activeGrant ( choice , isSaved ( pendingGrant ( choice ) ?. id ) ) ;
94+ return activeGrant ( choice , readSaveMarker ( pendingGrant ( choice ) ?. id ) ) ;
8295 } ;
8396 const bound = current ( ) ;
8497 if ( bound ?. id !== id ) return ( ) => false ;
0 commit comments