poll for new hub assistant
This commit is contained in:
parent
11b9388690
commit
9d477c346c
1 changed files with 35 additions and 12 deletions
|
|
@ -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<void>((resolve) => {
|
||||
const interval = setInterval(() => {
|
||||
attempts++;
|
||||
|
||||
if (findAndSelectProfile() || attempts >= maxAttempts) {
|
||||
clearInterval(interval);
|
||||
resolve();
|
||||
}
|
||||
}, 1000);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue