diff --git a/gui/src/redux/thunks/selectFirstHubProfile.ts b/gui/src/redux/thunks/selectFirstHubProfile.ts index f8018702c..d1715ed98 100644 --- a/gui/src/redux/thunks/selectFirstHubProfile.ts +++ b/gui/src/redux/thunks/selectFirstHubProfile.ts @@ -11,23 +11,46 @@ export const selectFirstHubProfile = createAsyncThunk< void, undefined, ThunkApiType ->("selectFirstHubProfile", async (messages, { dispatch, extra, getState }) => { - const state = getState(); +>("selectFirstHubProfile", async (_, { dispatch, extra, getState }) => { + let attempts = 0; + const maxAttempts = 3; - const currentOrgProfiles = state.profiles.organizations.find( - (org) => org.id === (state.profiles.selectedOrganizationId ?? "personal"), - ); + const findAndSelectProfile = () => { + const state = getState(); - const firstHubProfile = currentOrgProfiles?.profiles.find( - (profile) => profile.profileType === "platform", - ); + const currentOrgProfiles = state.profiles.organizations.find( + (org) => org.id === (state.profiles.selectedOrganizationId ?? "personal"), + ); - if (!firstHubProfile) { + const firstHubProfile = currentOrgProfiles?.profiles.find( + (profile) => profile.profileType === "platform", + ); + + if (firstHubProfile) { + dispatch(setSelectedProfile(firstHubProfile.id)); + extra.ideMessenger.post("didChangeSelectedProfile", { + id: firstHubProfile.id, + }); + return true; + } + + return false; + }; + + // Try immediately first + if (findAndSelectProfile()) { return; } - dispatch(setSelectedProfile(firstHubProfile.id)); - extra.ideMessenger.post("didChangeSelectedProfile", { - id: firstHubProfile.id, + // If not found, poll every second for up to maxAttempts + return new Promise((resolve) => { + const interval = setInterval(() => { + attempts++; + + if (findAndSelectProfile() || attempts >= maxAttempts) { + clearInterval(interval); + resolve(); + } + }, 1000); }); });