11import type { ProviderAuthAuthorization } from "@opencode-ai/sdk/v2/client" ;
22import { CheckCircle2 , Loader2 , X } from "lucide-solid" ;
33import type { ProviderListItem } from "../types" ;
4- import { createEffect , createMemo , createSignal , For , Show } from "solid-js" ;
4+ import { createEffect , createMemo , createSignal , For , onCleanup , Show } from "solid-js" ;
55import { isTauriRuntime } from "../utils" ;
66
77import Button from "./button" ;
@@ -43,7 +43,12 @@ export type ProviderAuthModalProps = {
4343 authMethods : Record < string , ProviderAuthMethod [ ] > ;
4444 onSelect : ( providerId : string ) => Promise < ProviderOAuthStartResult > ;
4545 onSubmitApiKey : ( providerId : string , apiKey : string ) => Promise < string | void > ;
46- onSubmitOAuth : ( providerId : string , methodIndex : number , code ?: string ) => Promise < string | void > ;
46+ onSubmitOAuth : (
47+ providerId : string ,
48+ methodIndex : number ,
49+ code ?: string
50+ ) => Promise < { connected : boolean ; pending ?: boolean ; message ?: string } > ;
51+ onRefreshProviders ?: ( ) => Promise < unknown > ;
4752 onClose : ( ) => void ;
4853} ;
4954
@@ -109,7 +114,11 @@ export default function ProviderAuthModal(props: ProviderAuthModalProps) {
109114 const [ searchQuery , setSearchQuery ] = createSignal ( "" ) ;
110115 const [ activeEntryIndex , setActiveEntryIndex ] = createSignal ( 0 ) ;
111116 const [ localError , setLocalError ] = createSignal < string | null > ( null ) ;
117+ const [ pollingBusy , setPollingBusy ] = createSignal ( false ) ;
118+ const [ oauthAutoBusy , setOauthAutoBusy ] = createSignal ( false ) ;
112119 let searchInputEl : HTMLInputElement | undefined ;
120+ let providerPoll : number | null = null ;
121+ let oauthAutoPoll : number | null = null ;
113122
114123 const selectedEntry = createMemo ( ( ) =>
115124 entries ( ) . find ( ( entry ) => entry . id === selectedProviderId ( ) ) ?? null ,
@@ -178,10 +187,90 @@ export default function ProviderAuthModal(props: ProviderAuthModalProps) {
178187 ! ! entry ?. methods ?. some ( ( method ) => method . type === type ) ;
179188
180189 const handleClose = ( ) => {
190+ void props . onRefreshProviders ?.( ) ;
191+ if ( oauthAutoPoll !== null ) {
192+ window . clearInterval ( oauthAutoPoll ) ;
193+ oauthAutoPoll = null ;
194+ }
195+ if ( providerPoll !== null ) {
196+ window . clearInterval ( providerPoll ) ;
197+ providerPoll = null ;
198+ }
181199 resetState ( ) ;
182200 props . onClose ( ) ;
183201 } ;
184202
203+ onCleanup ( ( ) => {
204+ if ( oauthAutoPoll !== null ) {
205+ window . clearInterval ( oauthAutoPoll ) ;
206+ oauthAutoPoll = null ;
207+ }
208+ if ( providerPoll !== null ) {
209+ window . clearInterval ( providerPoll ) ;
210+ providerPoll = null ;
211+ }
212+ } ) ;
213+
214+ const isOauthView = ( ) => resolvedView ( ) === "oauth-code" || resolvedView ( ) === "oauth-auto" ;
215+ const activeProviderId = ( ) => oauthSession ( ) ?. providerId ?? selectedProviderId ( ) ;
216+
217+ const isActiveProviderConnected = ( ) => {
218+ const id = activeProviderId ( ) ;
219+ if ( ! id ) return false ;
220+ return ( props . connectedProviderIds ?? [ ] ) . includes ( id ) ;
221+ } ;
222+
223+ const pollProviders = async ( ) => {
224+ const id = activeProviderId ( ) ;
225+ if ( ! id ) return ;
226+ if ( pollingBusy ( ) ) return ;
227+ setPollingBusy ( true ) ;
228+ try {
229+ await props . onRefreshProviders ?.( ) ;
230+ } finally {
231+ setPollingBusy ( false ) ;
232+ }
233+ if ( isActiveProviderConnected ( ) ) {
234+ handleClose ( ) ;
235+ }
236+ } ;
237+
238+ const startProviderPolling = ( ) => {
239+ if ( typeof window === "undefined" ) return ;
240+ if ( providerPoll !== null ) return ;
241+ void pollProviders ( ) ;
242+ providerPoll = window . setInterval ( ( ) => {
243+ void pollProviders ( ) ;
244+ } , 2000 ) ;
245+ } ;
246+
247+ const stopProviderPolling = ( ) => {
248+ if ( providerPoll !== null ) {
249+ window . clearInterval ( providerPoll ) ;
250+ providerPoll = null ;
251+ }
252+ } ;
253+
254+ createEffect ( ( ) => {
255+ if ( ! props . open || ! isOauthView ( ) ) {
256+ stopProviderPolling ( ) ;
257+ return ;
258+ }
259+ if ( isActiveProviderConnected ( ) ) {
260+ handleClose ( ) ;
261+ return ;
262+ }
263+ startProviderPolling ( ) ;
264+ } ) ;
265+
266+ createEffect ( ( ) => {
267+ if ( ! props . open || resolvedView ( ) !== "oauth-auto" || ! oauthSession ( ) ) {
268+ stopOauthAutoPolling ( ) ;
269+ return ;
270+ }
271+ startOauthAutoPolling ( ) ;
272+ } ) ;
273+
185274 const openOauthUrl = async ( url : string ) => {
186275 if ( ! url ) return ;
187276 if ( isTauriRuntime ( ) ) {
@@ -196,14 +285,44 @@ export default function ProviderAuthModal(props: ProviderAuthModalProps) {
196285 const trimmedCode = code ?. trim ( ) ;
197286 setLocalError ( null ) ;
198287 try {
199- await props . onSubmitOAuth ( providerId , methodIndex , trimmedCode || undefined ) ;
288+ return await props . onSubmitOAuth ( providerId , methodIndex , trimmedCode || undefined ) ;
200289 } catch ( error ) {
201290 const message = error instanceof Error ? error . message : "Failed to complete OAuth" ;
202291 setLocalError ( message ) ;
203292 throw error instanceof Error ? error : new Error ( message ) ;
204293 }
205294 } ;
206295
296+ const stopOauthAutoPolling = ( ) => {
297+ if ( oauthAutoPoll !== null ) {
298+ window . clearInterval ( oauthAutoPoll ) ;
299+ oauthAutoPoll = null ;
300+ }
301+ } ;
302+
303+ const attemptOauthAutoCompletion = async ( ) => {
304+ const session = oauthSession ( ) ;
305+ if ( ! session || oauthAutoBusy ( ) ) return ;
306+ setOauthAutoBusy ( true ) ;
307+ try {
308+ const result = await submitOauth ( session . providerId , session . methodIndex ) ;
309+ if ( result ?. connected ) {
310+ stopOauthAutoPolling ( ) ;
311+ }
312+ } finally {
313+ setOauthAutoBusy ( false ) ;
314+ }
315+ } ;
316+
317+ const startOauthAutoPolling = ( ) => {
318+ if ( typeof window === "undefined" ) return ;
319+ if ( oauthAutoPoll !== null ) return ;
320+ void attemptOauthAutoCompletion ( ) ;
321+ oauthAutoPoll = window . setInterval ( ( ) => {
322+ void attemptOauthAutoCompletion ( ) ;
323+ } , 2000 ) ;
324+ } ;
325+
207326 const startOauth = async ( entry : ProviderAuthEntry ) => {
208327 if ( actionDisabled ( ) ) return ;
209328 setLocalError ( null ) ;
@@ -225,7 +344,6 @@ export default function ProviderAuthModal(props: ProviderAuthModalProps) {
225344 }
226345
227346 setView ( "oauth-auto" ) ;
228- await submitOauth ( entry . id , started . methodIndex ) ;
229347 } catch ( error ) {
230348 const message = error instanceof Error ? error . message : "Failed to start OAuth" ;
231349 setLocalError ( message ) ;
@@ -304,13 +422,6 @@ export default function ProviderAuthModal(props: ProviderAuthModalProps) {
304422 await submitOauth ( entry . id , session . methodIndex , trimmed ) ;
305423 } ;
306424
307- const handleOauthAutoRetry = async ( ) => {
308- const entry = selectedEntry ( ) ;
309- const session = oauthSession ( ) ;
310- if ( ! entry || ! session || actionDisabled ( ) ) return ;
311- await submitOauth ( entry . id , session . methodIndex ) ;
312- } ;
313-
314425 const handleBack = ( ) => {
315426 if ( resolvedView ( ) === "oauth-code" || resolvedView ( ) === "oauth-auto" ) {
316427 if ( hasMethod ( selectedEntry ( ) , "api" ) ) {
@@ -656,8 +767,8 @@ export default function ProviderAuthModal(props: ProviderAuthModalProps) {
656767 < TextInput label = "Confirmation code" value = { oauthDisplayCode ( ) } readOnly class = "font-mono" />
657768 </ Show >
658769 < div class = "flex items-center gap-2 text-xs text-gray-9" >
659- < Loader2 size = { 14 } class = { props . submitting ? "animate-spin" : "" } />
660- < span > { props . submitting ? "Waiting for confirmation ..." : "Ready to retry completion." } </ span >
770+ < Loader2 size = { 14 } class = { props . submitting || pollingBusy ( ) || oauthAutoBusy ( ) ? "animate-spin" : "" } />
771+ < span > Checking connection status automatically ...</ span >
661772 </ div >
662773 < div class = "flex items-center justify-between gap-3" >
663774 < Button
@@ -670,9 +781,7 @@ export default function ProviderAuthModal(props: ProviderAuthModalProps) {
670781 >
671782 Open browser again
672783 </ Button >
673- < Button variant = "secondary" onClick = { ( ) => void handleOauthAutoRetry ( ) } disabled = { actionDisabled ( ) } >
674- { props . submitting ? "Waiting..." : "Retry completion" }
675- </ Button >
784+ < div class = "text-[11px] text-gray-9 text-right" > This window will close once the provider is connected.</ div >
676785 </ div >
677786 </ div >
678787 </ Show >
0 commit comments