Skip to content

Commit 28d3488

Browse files
committed
refactor(dashboard/new-project): persist selected provider host
1 parent bc74f87 commit 28d3488

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

components/dashboard/src/projects/NewProject.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { trackEvent } from "../Analytics";
2121
import exclamation from "../images/exclamation.svg";
2222
import ErrorMessage from "../components/ErrorMessage";
2323

24+
const SELECTED_PROVIDER_HOST_KEY = "gitpod.new-project.selected-provider-host";
25+
2426
export default function NewProject() {
2527
const location = useLocation();
2628
const { teams } = useContext(TeamsContext);
@@ -55,20 +57,34 @@ export default function NewProject() {
5557

5658
useEffect(() => {
5759
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
5870
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);
6777
}
6878
}
6979
}
7080
}, [user, authProviders, selectedProviderHost]);
7181

82+
useEffect(() => {
83+
if (selectedProviderHost) {
84+
localStorage.setItem(SELECTED_PROVIDER_HOST_KEY, selectedProviderHost);
85+
}
86+
}, [selectedProviderHost]);
87+
7288
useEffect(() => {
7389
setIsGitHubWebhooksUnauthorized(false);
7490
if (!authProviders || !selectedProviderHost || isGitHubAppEnabled) {

0 commit comments

Comments
 (0)