@@ -33,6 +33,8 @@ export type ProviderSignIn =
3333 readonly method : Extract < ProviderConnectMethod , { type : "oauth" } >
3434 readonly authorization : ProviderAuthorization
3535 readonly browserFailed : boolean
36+ /** Set when the attempt also takes the redirect URL pasted from a browser that cannot reach the server. */
37+ readonly paste ?: ProviderPaste
3638 }
3739 | {
3840 readonly kind : "code"
@@ -55,6 +57,13 @@ export type ProviderPolledSignIn = Extract<ProviderSignIn, { kind: "browser" | "
5557
5658export type ProviderEntryError = { readonly type : "required" } | { readonly type : "rejected" ; readonly message : string }
5759
60+ /** The pasted redirect URL of a browser sign-in. The browser's own callback still completes the sign-in meanwhile. */
61+ export type ProviderPaste =
62+ | { readonly status : "editing" ; readonly value : string ; readonly error ?: ProviderEntryError }
63+ | { readonly status : "submitting" ; readonly value : string }
64+ /** The server took the URL and finishes the sign-in; polling reports how it ends. */
65+ | { readonly status : "accepted" ; readonly value : string }
66+
5867export type ProviderRetry =
5968 | { readonly type : "start" }
6069 | { readonly type : "poll" ; readonly signIn : Extract < ProviderSignIn , { kind : "browser" } > }
@@ -110,6 +119,11 @@ export type ProviderConnectEvent =
110119 readonly opened : boolean
111120 }
112121 | { readonly type : "entry.submitted" ; readonly value : string }
122+ | { readonly type : "paste.changed" ; readonly value : string }
123+ | { readonly type : "paste.submitted" }
124+ /** The server took the pasted URL; the sign-in still settles through polling. */
125+ | { readonly type : "paste.accepted" ; readonly attempt : number }
126+ | { readonly type : "paste.rejected" ; readonly attempt : number ; readonly error : string }
113127 | { readonly type : "oauth.started" ; readonly attempt : number ; readonly authorization : ProviderAuthorization }
114128 | { readonly type : "command.started" ; readonly attempt : number ; readonly command : ProviderCommandAttempt }
115129 | { readonly type : "external.saved" ; readonly attempt : number }
@@ -152,6 +166,12 @@ export type ProviderConnectEffect =
152166 readonly value : string
153167 readonly answer ?: FormAnswer
154168 }
169+ | {
170+ readonly type : "paste"
171+ readonly attempt : number
172+ readonly signIn : Extract < ProviderSignIn , { kind : "browser" } >
173+ readonly value : string
174+ }
155175 | { readonly type : "refresh" ; readonly attempt : number ; readonly method : ProviderConnectMethod }
156176 /** Reloads the catalogs without reporting back, for a credential saved after the dialog moved on. */
157177 | { readonly type : "sync" }
@@ -333,6 +353,7 @@ export function selectProviderConnectView(state: ProviderConnectState) {
333353 commandOutput : signIn ?. kind === "command" ? signIn . output : "" ,
334354 form : state . status === "form" ? selectProviderFormView ( state . form ) : undefined ,
335355 entryError : state . status === "entering" ? state . error : undefined ,
356+ paste : state . status === "waiting" && signIn ?. kind === "browser" && signIn . paste ? pasteView ( signIn . paste ) : undefined ,
336357 // A typed secret is sent and the catalogs are refreshing: the form takes no second submit.
337358 submitting : state . status === "submitting" || state . status === "refreshing" ,
338359 error : state . status === "failed" ? state . error : undefined ,
@@ -452,6 +473,14 @@ function polled(
452473 state : Extract < ProviderConnectState , { status : "waiting" } > ,
453474 event : ProviderConnectEvent ,
454475) : ProviderConnectTransition {
476+ if (
477+ event . type === "paste.changed" ||
478+ event . type === "paste.submitted" ||
479+ event . type === "paste.accepted" ||
480+ event . type === "paste.rejected"
481+ )
482+ return pasted ( state , event )
483+
455484 if ( event . type !== "polled" ) return unchanged ( state )
456485 const outcome = event . outcome
457486
@@ -473,15 +502,69 @@ function polled(
473502 return unchanged ( { ...selection ( state , state . attempt ) , status : "failed" , error : outcome . error , retry } )
474503}
475504
505+ // A paste only hands the server the URL: polling keeps running and reports how the sign-in ends.
506+ function pasted (
507+ state : Extract < ProviderConnectState , { status : "waiting" } > ,
508+ event : Extract <
509+ ProviderConnectEvent ,
510+ { type : "paste.changed" | "paste.submitted" | "paste.accepted" | "paste.rejected" }
511+ > ,
512+ ) : ProviderConnectTransition {
513+ const signIn = state . signIn
514+
515+ if ( signIn . kind !== "browser" || ! signIn . paste ) return unchanged ( state )
516+ const paste = signIn . paste
517+ const set = ( next : ProviderPaste ) => ( { ...state , signIn : { ...signIn , paste : next } } )
518+
519+ if ( event . type === "paste.changed" ) {
520+ if ( paste . status !== "editing" || ( event . value === paste . value && ! paste . error ) ) return unchanged ( state )
521+
522+ return unchanged ( set ( { status : "editing" , value : event . value } ) )
523+ }
524+
525+ if ( event . type === "paste.submitted" ) {
526+ if ( paste . status !== "editing" ) return unchanged ( state )
527+ const value = paste . value . trim ( )
528+
529+ if ( ! value ) return unchanged ( set ( { status : "editing" , value : paste . value , error : { type : "required" } } ) )
530+
531+ return {
532+ state : set ( { status : "submitting" , value : paste . value } ) ,
533+ effects : [ { type : "paste" , attempt : state . attempt , signIn, value } ] ,
534+ }
535+ }
536+
537+ if ( paste . status !== "submitting" ) return unchanged ( state )
538+
539+ if ( event . type === "paste.accepted" ) return unchanged ( set ( { status : "accepted" , value : paste . value } ) )
540+
541+ return unchanged ( set ( { status : "editing" , value : paste . value , error : { type : "rejected" , message : event . error } } ) )
542+ }
543+
544+ function pasteView ( paste : ProviderPaste ) {
545+ return {
546+ value : paste . value ,
547+ error : paste . status === "editing" ? paste . error : undefined ,
548+ locked : paste . status !== "editing" ,
549+ submitting : paste . status === "submitting" ,
550+ }
551+ }
552+
476553function poll (
477554 state : Extract < ProviderConnectState , { status : "starting" | "failed" } > ,
478555 signIn : ProviderPolledSignIn ,
479556) : ProviderConnectTransition {
480557 const attempt = state . status === "starting" ? state . attempt : state . attempt + 1
481558
559+ // A paste in flight when polling failed reports to the old attempt, so a resumed sign-in starts editable too.
560+ const next : ProviderPolledSignIn =
561+ signIn . kind === "browser" && signIn . authorization . paste
562+ ? { ...signIn , paste : { status : "editing" , value : signIn . paste ?. value ?? "" } }
563+ : signIn
564+
482565 return {
483- state : { ...selection ( state , attempt ) , status : "waiting" , signIn } ,
484- effects : [ { type : "poll" , attempt, signIn, delay : false } ] ,
566+ state : { ...selection ( state , attempt ) , status : "waiting" , signIn : next } ,
567+ effects : [ { type : "poll" , attempt, signIn : next , delay : false } ] ,
485568 }
486569}
487570
0 commit comments