1- import { useCallback } from "react" ;
1+ import { useCallback , useRef } from "react" ;
22import type { TFn } from "../i18n/shared" ;
33import { readJsonIfOk } from "../fetch-json" ;
44import type { OAuthAccount , OAuthStatus } from "./providers-shared" ;
@@ -8,7 +8,6 @@ export function useProvidersOAuth({
88 apiBase,
99 t,
1010 aliveRef,
11- oauthLoginGenerationRef,
1211 accountSets,
1312 setBusy,
1413 setStatus,
@@ -24,7 +23,6 @@ export function useProvidersOAuth({
2423 apiBase : string ;
2524 t : TFn ;
2625 aliveRef : React . MutableRefObject < boolean > ;
27- oauthLoginGenerationRef : React . MutableRefObject < Map < string , number > > ;
2826 accountSets : Record < string , { accounts : OAuthAccount [ ] } > ;
2927 setBusy : React . Dispatch < React . SetStateAction < string | null > > ;
3028 setStatus : React . Dispatch < React . SetStateAction < string > > ;
@@ -37,9 +35,12 @@ export function useProvidersOAuth({
3735 fetchProviderQuotas : ( refresh ?: boolean ) => Promise < void > ;
3836 bumpModelsRefresh : ( ) => void ;
3937} ) {
38+ const oauthLoginGenerationRef = useRef < Map < string , number > | null > ( null ) ;
39+ if ( oauthLoginGenerationRef . current === null ) oauthLoginGenerationRef . current = new Map ( ) ;
40+
4041 const cancelLoginOAuth = useCallback ( async ( provider : string ) => {
41- const gen = ( oauthLoginGenerationRef . current . get ( provider ) ?? 0 ) + 1 ;
42- oauthLoginGenerationRef . current . set ( provider , gen ) ;
42+ const gen = ( oauthLoginGenerationRef . current ! . get ( provider ) ?? 0 ) + 1 ;
43+ oauthLoginGenerationRef . current ! . set ( provider , gen ) ;
4344 try {
4445 await fetch ( `${ apiBase } /api/oauth/login/cancel` , {
4546 method : "POST" ,
@@ -48,16 +49,16 @@ export function useProvidersOAuth({
4849 } ) ;
4950 } catch { /* ignore */ }
5051 if ( ! aliveRef . current ) return ;
51- if ( oauthLoginGenerationRef . current . get ( provider ) === gen ) {
52+ if ( oauthLoginGenerationRef . current ! . get ( provider ) === gen ) {
5253 setBusy ( current => current === provider ? null : current ) ;
5354 setLoginInfo ( current => current ?. provider === provider ? null : current ) ;
5455 }
5556 notify ( t ( "prov.loginCancelled" , { provider : oauthLabel ( provider ) } ) , false ) ;
56- } , [ aliveRef , apiBase , notify , oauthLoginGenerationRef , setBusy , setLoginInfo , t ] ) ;
57+ } , [ aliveRef , apiBase , notify , setBusy , setLoginInfo , t ] ) ;
5758
5859 const loginOAuth = async ( provider : string , addAccount = false , accountId ?: string ) => {
59- const nextGen = ( oauthLoginGenerationRef . current . get ( provider ) ?? 0 ) + 1 ;
60- oauthLoginGenerationRef . current . set ( provider , nextGen ) ;
60+ const nextGen = ( oauthLoginGenerationRef . current ! . get ( provider ) ?? 0 ) + 1 ;
61+ oauthLoginGenerationRef . current ! . set ( provider , nextGen ) ;
6162 const generation = nextGen ;
6263 const reauthTargetId = accountId ?. trim ( ) || undefined ;
6364 setBusy ( provider ) ;
@@ -73,7 +74,7 @@ export function useProvidersOAuth({
7374 ...( reauthTargetId ? { accountId : reauthTargetId , reauth : true } : { } ) ,
7475 } ) ,
7576 } ) ;
76- if ( oauthLoginGenerationRef . current . get ( provider ) !== generation || ! aliveRef . current ) return ;
77+ if ( oauthLoginGenerationRef . current ! . get ( provider ) !== generation || ! aliveRef . current ) return ;
7778 if ( ! res . ok ) {
7879 const data = await res . json ( ) . catch ( ( ) => ( { } ) ) as { error ?: string } ;
7980 notify ( data . error || t ( "prov.loginFailStart" , { provider : oauthLabel ( provider ) } ) , false ) ;
@@ -85,9 +86,9 @@ export function useProvidersOAuth({
8586 }
8687 const baselineCount = accountSets [ provider ] ?. accounts . length ?? 0 ;
8788 let finished = false ;
88- for ( let i = 0 ; i < 150 && aliveRef . current && oauthLoginGenerationRef . current . get ( provider ) === generation ; i ++ ) {
89+ for ( let i = 0 ; i < 150 && aliveRef . current && oauthLoginGenerationRef . current ! . get ( provider ) === generation ; i ++ ) {
8990 await new Promise ( r => setTimeout ( r , 2000 ) ) ;
90- if ( oauthLoginGenerationRef . current . get ( provider ) !== generation || ! aliveRef . current ) return ;
91+ if ( oauthLoginGenerationRef . current ! . get ( provider ) !== generation || ! aliveRef . current ) return ;
9192 const sRes = await fetch ( `${ apiBase } /api/oauth/status?provider=${ provider } ` ) . catch ( ( ) => null ) ;
9293 const s : ( OAuthStatus & { accounts ?: OAuthAccount [ ] } ) | null = sRes
9394 ? ( ( await readJsonIfOk < OAuthStatus & { accounts ?: OAuthAccount [ ] } > ( sRes ) ) ?? null )
@@ -138,7 +139,7 @@ export function useProvidersOAuth({
138139 break ;
139140 }
140141 }
141- if ( ! finished && oauthLoginGenerationRef . current . get ( provider ) === generation && aliveRef . current ) {
142+ if ( ! finished && oauthLoginGenerationRef . current ! . get ( provider ) === generation && aliveRef . current ) {
142143 await fetch ( `${ apiBase } /api/oauth/login/cancel` , {
143144 method : "POST" ,
144145 headers : { "Content-Type" : "application/json" } ,
@@ -148,11 +149,11 @@ export function useProvidersOAuth({
148149 setLoginInfo ( null ) ;
149150 }
150151 } catch {
151- if ( oauthLoginGenerationRef . current . get ( provider ) === generation ) {
152+ if ( oauthLoginGenerationRef . current ! . get ( provider ) === generation ) {
152153 notify ( t ( "prov.loginRequestFail" , { provider : oauthLabel ( provider ) } ) , false ) ;
153154 }
154155 } finally {
155- if ( aliveRef . current && oauthLoginGenerationRef . current . get ( provider ) === generation ) setBusy ( null ) ;
156+ if ( aliveRef . current && oauthLoginGenerationRef . current ! . get ( provider ) === generation ) setBusy ( null ) ;
156157 }
157158 } ;
158159
0 commit comments