Skip to content

Commit 554bdd2

Browse files
authoredJan 17, 2025
Merge pull request #55392 from software-mansion-labs/revert-55363-revert-54296-war-in/enable-hybrid-app-copilot
Bring "[HybridApp] Necessary copilot changes" back
2 parents 72723ab + 30a357b commit 554bdd2

File tree

4 files changed

+72
-20
lines changed

4 files changed

+72
-20
lines changed
 

‎src/libs/actions/Delegate.ts

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {NativeModules} from 'react-native';
22
import Onyx from 'react-native-onyx';
3-
import type {OnyxUpdate} from 'react-native-onyx';
3+
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
44
import * as API from '@libs/API';
55
import type {AddDelegateParams, RemoveDelegateParams, UpdateDelegateRoleParams} from '@libs/API/parameters';
66
import {SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
77
import * as ErrorUtils from '@libs/ErrorUtils';
88
import Log from '@libs/Log';
99
import * as NetworkStore from '@libs/Network/NetworkStore';
10-
import {getCurrentUserEmail} from '@libs/Network/NetworkStore';
1110
import * as SequentialQueue from '@libs/Network/SequentialQueue';
1211
import CONST from '@src/CONST';
1312
import ONYXKEYS from '@src/ONYXKEYS';
@@ -16,6 +15,7 @@ import type Credentials from '@src/types/onyx/Credentials';
1615
import type Response from '@src/types/onyx/Response';
1716
import type Session from '@src/types/onyx/Session';
1817
import {confirmReadyToOpenApp, openApp} from './App';
18+
import {getCurrentUserAccountID} from './Report';
1919
import updateSessionAuthTokens from './Session/updateSessionAuthTokens';
2020
import updateSessionUser from './Session/updateSessionUser';
2121

@@ -51,6 +51,14 @@ Onyx.connect({
5151
callback: (value) => (stashedSession = value ?? {}),
5252
});
5353

54+
let activePolicyID: OnyxEntry<string>;
55+
Onyx.connect({
56+
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
57+
callback: (newActivePolicyID) => {
58+
activePolicyID = newActivePolicyID;
59+
},
60+
});
61+
5462
const KEYS_TO_PRESERVE_DELEGATE_ACCESS = [
5563
ONYXKEYS.NVP_TRY_FOCUS_MODE,
5664
ONYXKEYS.PREFERRED_THEME,
@@ -73,6 +81,8 @@ function connect(email: string) {
7381
Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, credentials);
7482
Onyx.set(ONYXKEYS.STASHED_SESSION, session);
7583

84+
const previousAccountID = getCurrentUserAccountID();
85+
7686
const optimisticData: OnyxUpdate[] = [
7787
{
7888
onyxMethod: Onyx.METHOD.MERGE,
@@ -130,6 +140,14 @@ function connect(email: string) {
130140
Onyx.update(failureData);
131141
return;
132142
}
143+
if (!activePolicyID) {
144+
Log.alert('[Delegate] Unable to access activePolicyID');
145+
Onyx.update(failureData);
146+
return;
147+
}
148+
const restrictedToken = response.restrictedToken;
149+
const policyID = activePolicyID;
150+
133151
return SequentialQueue.waitForIdle()
134152
.then(() => Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS))
135153
.then(() => {
@@ -138,9 +156,7 @@ function connect(email: string) {
138156

139157
NetworkStore.setAuthToken(response?.restrictedToken ?? null);
140158
confirmReadyToOpenApp();
141-
openApp();
142-
143-
NativeModules.HybridAppModule.switchAccount(email);
159+
openApp().then(() => NativeModules.HybridAppModule.switchAccount(email, restrictedToken, policyID, String(previousAccountID)));
144160
});
145161
})
146162
.catch((error) => {
@@ -195,22 +211,34 @@ function disconnect() {
195211
return;
196212
}
197213

214+
if (!response?.requesterID || !response?.requesterEmail) {
215+
Log.alert('[Delegate] No requester data returned while disconnecting as a delegate');
216+
return;
217+
}
218+
219+
const requesterEmail = response.requesterEmail;
220+
const authToken = response.authToken;
198221
return SequentialQueue.waitForIdle()
199222
.then(() => Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS))
200223
.then(() => {
201-
// Update authToken in Onyx and in our local variables so that API requests will use the new authToken
202-
updateSessionAuthTokens(response?.authToken, response?.encryptedAuthToken);
224+
Onyx.set(ONYXKEYS.CREDENTIALS, {
225+
...stashedCredentials,
226+
accountID: response.requesterID,
227+
});
228+
Onyx.set(ONYXKEYS.SESSION, {
229+
...stashedSession,
230+
accountID: response.requesterID,
231+
email: requesterEmail,
232+
authToken,
233+
encryptedAuthToken: response.encryptedAuthToken,
234+
});
235+
Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, {});
236+
Onyx.set(ONYXKEYS.STASHED_SESSION, {});
203237

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

206-
Onyx.set(ONYXKEYS.CREDENTIALS, stashedCredentials);
207-
Onyx.set(ONYXKEYS.SESSION, stashedSession);
208-
Onyx.set(ONYXKEYS.STASHED_CREDENTIALS, {});
209-
Onyx.set(ONYXKEYS.STASHED_SESSION, {});
210240
confirmReadyToOpenApp();
211-
openApp();
212-
213-
NativeModules.HybridAppModule.switchAccount(getCurrentUserEmail() ?? '');
241+
openApp().then(() => NativeModules.HybridAppModule.switchAccount(requesterEmail, authToken, '', ''));
214242
});
215243
})
216244
.catch((error) => {

‎src/libs/actions/Session/index.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Navigation from '@libs/Navigation/Navigation';
3434
import navigationRef from '@libs/Navigation/navigationRef';
3535
import * as MainQueue from '@libs/Network/MainQueue';
3636
import * as NetworkStore from '@libs/Network/NetworkStore';
37+
import {getCurrentUserEmail} from '@libs/Network/NetworkStore';
3738
import NetworkConnection from '@libs/NetworkConnection';
3839
import * as Pusher from '@libs/Pusher/pusher';
3940
import {getReportIDFromLink, parseReportRouteParams as parseReportRouteParamsReportUtils} from '@libs/ReportUtils';
@@ -525,7 +526,7 @@ function signInAfterTransitionFromOldDot(transitionURL: string) {
525526
nudgeMigrationTimestamp,
526527
isSingleNewDotEntry,
527528
primaryLogin,
528-
shouldRemoveDelegatedAccess,
529+
oldDotOriginalAccountEmail,
529530
} = Object.fromEntries(
530531
queryParams.split('&').map((param) => {
531532
const [key, value] = param.split('=');
@@ -544,22 +545,39 @@ function signInAfterTransitionFromOldDot(transitionURL: string) {
544545
const setSessionDataAndOpenApp = new Promise<Route>((resolve) => {
545546
clearOnyxForNewAccount()
546547
.then(() => {
547-
if (!shouldRemoveDelegatedAccess) {
548+
// This section controls copilot changes
549+
const currentUserEmail = getCurrentUserEmail();
550+
551+
// If ND and OD account are the same - do nothing
552+
if (email === currentUserEmail) {
553+
return;
554+
}
555+
556+
// If account was changed to original one on OD side - clear onyx
557+
if (!oldDotOriginalAccountEmail) {
558+
return Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS);
559+
}
560+
561+
// If we're already logged in - do nothing, data will be set in next step
562+
if (currentUserEmail) {
548563
return;
549564
}
550-
return Onyx.clear(KEYS_TO_PRESERVE_DELEGATE_ACCESS);
565+
566+
// If we're not logged in - set stashed data
567+
return Onyx.multiSet({
568+
[ONYXKEYS.STASHED_CREDENTIALS]: {autoGeneratedLogin, autoGeneratedPassword},
569+
});
551570
})
552571
.then(() =>
553572
Onyx.multiSet({
554573
[ONYXKEYS.SESSION]: {email, authToken, encryptedAuthToken: decodeURIComponent(encryptedAuthToken), accountID: Number(accountID)},
555-
[ONYXKEYS.ACCOUNT]: {primaryLogin},
556574
[ONYXKEYS.CREDENTIALS]: {autoGeneratedLogin, autoGeneratedPassword},
557575
[ONYXKEYS.IS_SINGLE_NEW_DOT_ENTRY]: isSingleNewDotEntry === 'true',
558576
[ONYXKEYS.NVP_TRYNEWDOT]: {
559577
classicRedirect: {completedHybridAppOnboarding: completedHybridAppOnboarding === 'true'},
560578
nudgeMigration: nudgeMigrationTimestamp ? {timestamp: new Date(nudgeMigrationTimestamp)} : undefined,
561579
},
562-
}),
580+
}).then(() => Onyx.merge(ONYXKEYS.ACCOUNT, {primaryLogin})),
563581
)
564582
.then(() => {
565583
if (clearOnyxOnStart === 'true') {

‎src/types/modules/react-native.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type StartupTimer from '@libs/StartupTimer/types';
88
type HybridAppModule = {
99
closeReactNativeApp: (shouldSignOut: boolean, shouldSetNVP: boolean) => void;
1010
completeOnboarding: (status: boolean) => void;
11-
switchAccount: (newDotCurrentAccount: string) => void;
11+
switchAccount: (newDotCurrentAccountEmail: string, authToken: string, policyID: string, accountID: string) => void;
1212
exitApp: () => void;
1313
};
1414

‎src/types/onyx/Response.ts

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ type Response = {
8383

8484
/** If there is newer data to load for pagination commands */
8585
hasNewerActions?: boolean;
86+
87+
/** The email of the original user (returned when in delegate mode) */
88+
requesterEmail?: string;
89+
90+
/** The ID of the original user (returned when in delegate mode) */
91+
requesterID?: number;
8692
};
8793

8894
export default Response;

0 commit comments

Comments
 (0)
Please sign in to comment.