fix optimistic org profile updates

This commit is contained in:
Dallin Romney 2025-07-12 01:33:19 -07:00
parent 2da461e110
commit 1b8849db55
3 changed files with 18 additions and 3 deletions

View file

@ -101,6 +101,9 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
this.secretStorage = new SecretStorage(context);
// Immediately refresh any existing sessions
this.initialRefreshAttempt = new Promise((resolve) => {
this.setHasAttemptedRefresh = resolve;
});
void this.refreshSessions();
// Set up a regular interval to refresh tokens
@ -147,6 +150,7 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
public async getSessions(
scopes?: string[],
): Promise<ContinueAuthenticationSession[]> {
await this.initialRefreshAttempt;
const data = await this.secretStorage.get(SESSIONS_SECRET_KEY);
if (!data) {
return [];
@ -182,6 +186,8 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
return this.ideRedirectUri;
}
private initialRefreshAttempt: Promise<void>;
private setHasAttemptedRefresh: () => void = () => undefined;
async refreshSessions() {
// Prevent concurrent refresh operations
if (this._isRefreshing) {
@ -270,6 +276,8 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
.catch(reject);
}, delay);
});
} finally {
this.setHasAttemptedRefresh();
}
}

View file

@ -10,6 +10,7 @@ import { useAppDispatch, useAppSelector } from "../../redux/hooks";
import {
selectCurrentOrg,
setSelectedOrgId,
setSelectedProfile,
} from "../../redux/slices/profilesSlice";
import {
Listbox,
@ -44,8 +45,14 @@ export function ScopeSelect({ onSelect }: ScopeSelectProps) {
const dispatch = useAppDispatch();
const handleChange = (newValue: string) => {
const org = organizations.find((o) => o.id === newValue);
if (!org) {
return;
}
// optimisitic update
dispatch(setSelectedOrgId(newValue));
dispatch(setSelectedOrgId(org.id));
dispatch(setSelectedProfile(org.selectedProfileId));
ideMessenger.post("didChangeSelectedOrg", {
id: newValue,
});

View file

@ -1,6 +1,6 @@
import {
OrganizationDescription,
ProfileDescription,
SerializedOrgWithProfiles,
} from "core/config/ProfileLifecycleManager";
import { ControlPlaneSessionInfo } from "core/control-plane/AuthTypes";
import React, {
@ -28,7 +28,7 @@ interface AuthContextType {
selectedProfile: ProfileDescription | null;
profiles: ProfileDescription[] | null;
refreshProfiles: () => Promise<void>;
organizations: OrganizationDescription[];
organizations: SerializedOrgWithProfiles[];
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);