Skip to content

Commit

Permalink
Merge pull request #52980 from Expensify/dangrous-stashedcopilot
Browse files Browse the repository at this point in the history
  • Loading branch information
blimpich authored Dec 10, 2024
2 parents b27d5c7 + 5757334 commit deeab96
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/libs/actions/Delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import * as SequentialQueue from '@libs/Network/SequentialQueue';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Delegate, DelegatedAccess, DelegateRole} from '@src/types/onyx/Account';
import type Credentials from '@src/types/onyx/Credentials';
import type Response from '@src/types/onyx/Response';
import type Session from '@src/types/onyx/Session';
import {confirmReadyToOpenApp, openApp} from './App';
import updateSessionAuthTokens from './Session/updateSessionAuthTokens';
import updateSessionUser from './Session/updateSessionUser';
Expand All @@ -25,13 +27,38 @@ Onyx.connect({
},
});

let credentials: Credentials = {};
Onyx.connect({
key: ONYXKEYS.CREDENTIALS,
callback: (value) => (credentials = value ?? {}),
});

let stashedCredentials: Credentials = {};
Onyx.connect({
key: ONYXKEYS.STASHED_CREDENTIALS,
callback: (value) => (stashedCredentials = value ?? {}),
});

let session: Session = {};
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (value) => (session = value ?? {}),
});

let stashedSession: Session = {};
Onyx.connect({
key: ONYXKEYS.STASHED_SESSION,
callback: (value) => (stashedSession = value ?? {}),
});

const KEYS_TO_PRESERVE_DELEGATE_ACCESS = [
ONYXKEYS.NVP_TRY_FOCUS_MODE,
ONYXKEYS.PREFERRED_THEME,
ONYXKEYS.NVP_PREFERRED_LOCALE,
ONYXKEYS.SESSION,
ONYXKEYS.STASHED_SESSION,
ONYXKEYS.IS_LOADING_APP,
ONYXKEYS.CREDENTIALS,
ONYXKEYS.STASHED_CREDENTIALS,

// We need to preserve the sidebar loaded state since we never unrender the sidebar when connecting as a delegate
// This allows the report screen to load correctly when the delegate token expires and the delegate is returned to their original account.
Expand All @@ -43,6 +70,9 @@ function connect(email: string) {
return;
}

Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, credentials);
Onyx.set(ONYXKEYS.STASHED_SESSION, session);

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -172,6 +202,11 @@ function disconnect() {
updateSessionAuthTokens(response?.authToken, response?.encryptedAuthToken);

NetworkStore.setAuthToken(response?.authToken ?? null);

Onyx.set(ONYXKEYS.CREDENTIALS, stashedCredentials);
Onyx.set(ONYXKEYS.SESSION, stashedSession);
Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, {});
Onyx.set(ONYXKEYS.STASHED_SESSION, {});
confirmReadyToOpenApp();
openApp();

Expand Down

0 comments on commit deeab96

Please sign in to comment.