@@ -21,6 +21,8 @@ import { trackEvent } from "../Analytics";
21
21
import exclamation from "../images/exclamation.svg" ;
22
22
import ErrorMessage from "../components/ErrorMessage" ;
23
23
24
+ const SELECTED_PROVIDER_HOST_KEY = "gitpod.new-project.selected-provider-host" ;
25
+
24
26
export default function NewProject ( ) {
25
27
const location = useLocation ( ) ;
26
28
const { teams } = useContext ( TeamsContext ) ;
@@ -55,20 +57,34 @@ export default function NewProject() {
55
57
56
58
useEffect ( ( ) => {
57
59
if ( user && authProviders && selectedProviderHost === undefined ) {
60
+ const storedSelectedProviderHost = localStorage . getItem ( SELECTED_PROVIDER_HOST_KEY ) ;
61
+
62
+ const authProvider = authProviders . find ( ( p ) => p . host === storedSelectedProviderHost ) ;
63
+
64
+ // if the stored provider exists and is in one of the user's identities, use it
65
+ if ( authProvider && user . identities . some ( ( idt ) => idt . authProviderId === authProvider . authProviderId ) ) {
66
+ return setSelectedProviderHost ( authProvider . host ) ;
67
+ }
68
+
69
+ // otherwise, use the last provider in the user's identities that also exists in the providers
58
70
for ( let i = user . identities . length - 1 ; i >= 0 ; i -- ) {
59
- const candidate = user . identities [ i ] ;
60
- if ( candidate ) {
61
- const authProvider = authProviders . find ( ( ap ) => ap . authProviderId === candidate . authProviderId ) ;
62
- const host = authProvider ?. host ;
63
- if ( host ) {
64
- setSelectedProviderHost ( host ) ;
65
- break ;
66
- }
71
+ const idt = user . identities [ i ] ! ;
72
+
73
+ const authProvider = authProviders . find ( ( ap ) => ap . authProviderId === idt . authProviderId ) ;
74
+ const host = authProvider ?. host ;
75
+ if ( host ) {
76
+ return setSelectedProviderHost ( host ) ;
67
77
}
68
78
}
69
79
}
70
80
} , [ user , authProviders , selectedProviderHost ] ) ;
71
81
82
+ useEffect ( ( ) => {
83
+ if ( selectedProviderHost ) {
84
+ localStorage . setItem ( SELECTED_PROVIDER_HOST_KEY , selectedProviderHost ) ;
85
+ }
86
+ } , [ selectedProviderHost ] ) ;
87
+
72
88
useEffect ( ( ) => {
73
89
setIsGitHubWebhooksUnauthorized ( false ) ;
74
90
if ( ! authProviders || ! selectedProviderHost || isGitHubAppEnabled ) {
0 commit comments