Skip to content

refactor(dashboard/new-project): persist selected provider host #10543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions components/dashboard/src/projects/NewProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { trackEvent } from "../Analytics";
import exclamation from "../images/exclamation.svg";
import ErrorMessage from "../components/ErrorMessage";

const SELECTED_PROVIDER_HOST_KEY = "gitpod.new-project.selected-provider-host";

export default function NewProject() {
const location = useLocation();
const { teams } = useContext(TeamsContext);
Expand Down Expand Up @@ -55,20 +57,34 @@ export default function NewProject() {

useEffect(() => {
if (user && authProviders && selectedProviderHost === undefined) {
const storedSelectedProviderHost = localStorage.getItem(SELECTED_PROVIDER_HOST_KEY);

const authProvider = authProviders.find((p) => p.host === storedSelectedProviderHost);

// if the stored provider exists and is in one of the user's identities, use it
if (storedSelectedProviderHost && authProvider && user.identities.some((idt) => idt.authProviderId === authProvider.authProviderId)) {
return setSelectedProviderHost(authProvider.host);
}

// otherwise, use the last provider in the user's identities that also exists in the providers
for (let i = user.identities.length - 1; i >= 0; i--) {
const candidate = user.identities[i];
if (candidate) {
const authProvider = authProviders.find((ap) => ap.authProviderId === candidate.authProviderId);
const host = authProvider?.host;
if (host) {
setSelectedProviderHost(host);
break;
}
const idt = user.identities[i]!;

const authProvider = authProviders.find((ap) => ap.authProviderId === idt.authProviderId);
const host = authProvider?.host;
if (host) {
return setSelectedProviderHost(host);
}
}
}
}, [user, authProviders, selectedProviderHost]);

useEffect(() => {
if (selectedProviderHost) {
localStorage.setItem(SELECTED_PROVIDER_HOST_KEY, selectedProviderHost);
}
}, [selectedProviderHost]);

useEffect(() => {
setIsGitHubWebhooksUnauthorized(false);
if (!authProviders || !selectedProviderHost || isGitHubAppEnabled) {
Expand Down