-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathIFrame.tsx
130 lines (104 loc) · 3.64 KB
/
IFrame.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React, {useEffect, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Session} from '@src/types/onyx';
type OldDotIFrameOnyxProps = {
session: OnyxEntry<Session>;
};
type OldDotIFrameProps = OldDotIFrameOnyxProps;
function getNewDotURL(url: string): string {
const urlObj = new URL(url);
const paramString = urlObj.searchParams.get('param') ?? '';
const pathname = urlObj.pathname.slice(1);
let params: Record<string, string>;
try {
params = JSON.parse(paramString);
} catch {
params = {};
}
if (pathname === 'inbox') {
return 'home';
}
if (pathname === 'expenses') {
return `${params.viewMode === 'charts' ? 'insights' : 'expenses'}${paramString ? `/?param=${paramString}` : ''}`;
}
if (pathname === 'admin_policies') {
const {section} = params;
return section === 'individual' ? 'individual_workspaces' : 'group_workspaces';
}
if (pathname === 'policy') {
const workspaceID = params.policyID || '';
const section = urlObj.hash.slice(1) || 'overview';
return `workspace/${workspaceID}/${section}`;
}
if (pathname === 'settings') {
const {section} = params;
return `settings/${section}`;
}
if (pathname.includes('domain')) {
return pathname;
}
return pathname;
}
function getOldDotURL(url: string): string {
const urlObj = new URL(url);
const pathname = urlObj.pathname;
const paths = pathname.slice(1).split('/');
// TODO: temporary measure until linking config is adjusted
if (pathname.startsWith('/r')) {
return 'inbox';
}
if (pathname === 'home') {
return 'inbox';
}
if (pathname === 'expenses' || pathname === 'insights') {
return `expenses/${urlObj.search}`;
}
if (pathname === 'individual_workspaces' || pathname === 'group_workspaces') {
const param = {section: pathname === 'individual_workspaces' ? 'individual' : 'group'};
return `admin_policies?param=${JSON.stringify(param)}`;
}
if (pathname === 'workspace') {
const [, workspaceID, section] = paths;
const param = {policyID: workspaceID};
return `policy/?param${JSON.stringify(param)}#${section}`;
}
if (pathname === 'settings') {
const [, section] = paths;
const param = {section};
return `settings?param=${JSON.stringify(param)}`;
}
return pathname;
}
function OldDotIFrame({session}: OldDotIFrameProps) {
const [oldDotURL, setOldDotURL] = useState('https://staging.expensify.com');
useEffect(() => {
setOldDotURL(`https://expensify.com.dev/${getOldDotURL(window.location.href)}`);
window.addEventListener('message', (event: MessageEvent<string>) => {
const url = event.data;
// TODO: use this value to navigate to a new path
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const newDotURL = getNewDotURL(url);
});
}, []);
useEffect(() => {
if (!session) {
return;
}
document.cookie = `authToken=${session.authToken}; domain=expensify.com.dev; path=/;`;
document.cookie = `email=${session.email}; domain=expensify.com.dev; path=/;`;
}, [session]);
return (
<iframe
style={{flex: 1}}
src={oldDotURL}
title="OldDot"
/>
);
}
export default withOnyx<OldDotIFrameProps, OldDotIFrameOnyxProps>({
session: {
key: ONYXKEYS.SESSION,
},
})(OldDotIFrame);