2121 * door's status answers carries a token, and the URL goes only to the window.
2222 */
2323
24- import { useEffect , useRef , useState , type ReactNode } from 'react' ;
24+ import { useEffect , useState , type ReactNode } from 'react' ;
2525import { AlertCircle , Info , Plug } from 'lucide-react' ;
2626import {
2727 Badge ,
@@ -70,9 +70,17 @@ interface Waiting {
7070 window : Window ;
7171 /** The connect being waited for has landed once `connectedAt` moves off this. */
7272 before ?: string ;
73- until : number ;
73+ /** When the begun connect stops being completable, as the server answered it. */
74+ until ?: number ;
7475}
7576
77+ /** What the notice line says: a message this page words, or the server's own sentence. */
78+ type Notice =
79+ | { tone : 'ok' ; kind : 'connected' | 'disconnected' ; name : string }
80+ | { tone : 'error' ; kind : 'expired' ; name : string }
81+ | { tone : 'error' ; kind : 'popupBlocked' }
82+ | { tone : 'error' ; kind : 'refusal' ; text : string } ;
83+
7684interface RevokeDraft {
7785 name : string ;
7886 busy : boolean ;
@@ -89,11 +97,9 @@ export function ConnectorConnectionsPage({ pollMs = 2000 }: ConnectorConnections
8997 const displayLocale = useDisplayLocale ( ) ;
9098 const [ nonce , setNonce ] = useState ( 0 ) ;
9199 const [ listing , setListing ] = useState < ConnectionListing | null > ( null ) ;
92- const [ notice , setNotice ] = useState < { tone : 'ok' | 'error' ; text : string } | null > ( null ) ;
100+ const [ notice , setNotice ] = useState < Notice | null > ( null ) ;
93101 const [ waiting , setWaiting ] = useState < Waiting | null > ( null ) ;
94102 const [ revokeDraft , setRevokeDraft ] = useState < RevokeDraft | null > ( null ) ;
95- const waitingRef = useRef < Waiting | null > ( null ) ;
96- waitingRef . current = waiting ;
97103
98104 const reload = ( ) => {
99105 setListing ( null ) ;
@@ -118,61 +124,52 @@ export function ConnectorConnectionsPage({ pollMs = 2000 }: ConnectorConnections
118124 // A begun connect: re-read until it lands, its window closes, or it expires.
119125 useEffect ( ( ) => {
120126 if ( ! waiting ) return ;
127+ const current = waiting ;
128+ const deadline = current . until ?? Date . now ( ) + 10 * 60_000 ;
121129 let busy = false ;
130+ let cancelled = false ;
122131 const timer = setInterval ( ( ) => {
123- const current = waitingRef . current ;
124- if ( busy || ! current ) return ;
132+ if ( busy ) return ;
125133 busy = true ;
126134 const closed = current . window . closed ;
127135 void fetchConnection ( current . name ) . then ( ( read ) => {
128136 busy = false ;
129- if ( waitingRef . current !== current ) return ;
137+ if ( cancelled ) return ;
130138 const landed = read . ok && read . state . status === 'connected' && read . state . connectedAt !== current . before ;
131- const expired = Date . now ( ) > current . until ;
139+ const expired = Date . now ( ) > deadline ;
132140 if ( ! landed && ! closed && ! expired ) return ;
133141 applyConnection ( current . name , read ) ;
134142 if ( ! current . window . closed ) current . window . close ( ) ;
135143 setWaiting ( null ) ;
136- if ( landed ) {
137- setNotice ( { tone : 'ok' , text : t ( 'connectorConnections.connected' , { defaultValue : 'Connected {{name}}.' , name : current . name } ) } ) ;
138- } else if ( expired ) {
139- setNotice ( {
140- tone : 'error' ,
141- text : t ( 'connectorConnections.expired' , {
142- defaultValue : 'The sign-in for {{name}} was not completed in time. Connect again.' ,
143- name : current . name ,
144- } ) ,
145- } ) ;
146- }
144+ if ( landed ) setNotice ( { tone : 'ok' , kind : 'connected' , name : current . name } ) ;
145+ else if ( expired && ! closed ) setNotice ( { tone : 'error' , kind : 'expired' , name : current . name } ) ;
147146 } , ( ) => { busy = false ; } ) ;
148147 } , pollMs ) ;
149- return ( ) => clearInterval ( timer ) ;
148+ return ( ) => {
149+ cancelled = true ;
150+ clearInterval ( timer ) ;
151+ } ;
150152 } , [ waiting , pollMs ] ) ;
151153
152154 const connect = async ( name : string , before ?: string ) => {
153155 if ( waiting ) return ;
154156 setNotice ( null ) ;
155157 const signIn = window . open ( 'about:blank' , SIGN_IN_WINDOW , SIGN_IN_FEATURES ) ;
156158 if ( ! signIn ) {
157- setNotice ( {
158- tone : 'error' ,
159- text : t ( 'connectorConnections.popupBlocked' , {
160- defaultValue : 'The browser blocked the sign-in window. Allow pop-ups for this site and try again.' ,
161- } ) ,
162- } ) ;
159+ setNotice ( { tone : 'error' , kind : 'popupBlocked' } ) ;
163160 return ;
164161 }
165162 // Detach the window before it reaches the vendor: the vendor's page gets no handle on this one.
166163 signIn . opener = null ;
167164 const begun = await beginConnection ( name ) ;
168165 if ( ! begun . ok ) {
169166 signIn . close ( ) ;
170- setNotice ( { tone : 'error' , text : begun . message } ) ;
167+ setNotice ( { tone : 'error' , kind : 'refusal' , text : begun . message } ) ;
171168 return ;
172169 }
173170 signIn . location . href = begun . authorizationUrl ;
174- const expiresAt = Date . parse ( begun . expiresAt ) ;
175- setWaiting ( { name, window : signIn , before, until : Number . isNaN ( expiresAt ) ? Date . now ( ) + 10 * 60_000 : expiresAt } ) ;
171+ const until = Date . parse ( begun . expiresAt ) ;
172+ setWaiting ( { name, window : signIn , before, ... ( Number . isNaN ( until ) ? { } : { until } ) } ) ;
176173 } ;
177174
178175 const confirmRevoke = async ( ) => {
@@ -186,7 +183,7 @@ export function ConnectorConnectionsPage({ pollMs = 2000 }: ConnectorConnections
186183 }
187184 applyConnection ( name , out ) ;
188185 setRevokeDraft ( null ) ;
189- setNotice ( { tone : 'ok' , text : t ( 'connectorConnections. disconnected', { defaultValue : 'Disconnected {{ name}}.' , name } ) } ) ;
186+ setNotice ( { tone : 'ok' , kind : ' disconnected', name } ) ;
190187 } ;
191188
192189 const formatAt = ( at : string | undefined ) : string | null => {
@@ -195,6 +192,26 @@ export function ConnectorConnectionsPage({ pollMs = 2000 }: ConnectorConnections
195192 return Number . isNaN ( d . getTime ( ) ) ? at : d . toLocaleString ( displayLocale ) ;
196193 } ;
197194
195+ const noticeText = ( n : Notice ) : string => {
196+ switch ( n . kind ) {
197+ case 'connected' :
198+ return t ( 'connectorConnections.connected' , { defaultValue : 'Connected {{name}}.' , name : n . name } ) ;
199+ case 'disconnected' :
200+ return t ( 'connectorConnections.disconnected' , { defaultValue : 'Disconnected {{name}}.' , name : n . name } ) ;
201+ case 'expired' :
202+ return t ( 'connectorConnections.expired' , {
203+ defaultValue : 'The sign-in for {{name}} was not completed in time. Connect again.' ,
204+ name : n . name ,
205+ } ) ;
206+ case 'popupBlocked' :
207+ return t ( 'connectorConnections.popupBlocked' , {
208+ defaultValue : 'The browser blocked the sign-in window. Allow pop-ups for this site and try again.' ,
209+ } ) ;
210+ default :
211+ return n . text ;
212+ }
213+ } ;
214+
198215 const statusLabel = ( status : ConnectorConnectionState [ 'status' ] ) => ( status === 'connected'
199216 ? t ( 'connectorConnections.statusConnected' , { defaultValue : 'Connected' } )
200217 : status === 'reconnect_required'
@@ -362,7 +379,7 @@ export function ConnectorConnectionsPage({ pollMs = 2000 }: ConnectorConnections
362379 : 'flex items-start gap-2 rounded-md border border-green-500/30 bg-green-500/5 p-3 text-sm text-green-700 dark:text-green-400' }
363380 >
364381 { notice . tone === 'error' ? < AlertCircle className = "mt-0.5 h-4 w-4 shrink-0" aria-hidden = "true" /> : < Info className = "mt-0.5 h-4 w-4 shrink-0" aria-hidden = "true" /> }
365- < div className = "flex-1" > { notice . text } </ div >
382+ < div className = "flex-1" > { noticeText ( notice ) } </ div >
366383 < button type = "button" className = "text-xs underline opacity-60 hover:opacity-100" onClick = { ( ) => setNotice ( null ) } >
367384 { t ( 'connectorConnections.dismiss' , { defaultValue : 'Dismiss' } ) }
368385 </ button >
0 commit comments