-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (109 loc) · 4.38 KB
/
Copy pathindex.js
File metadata and controls
120 lines (109 loc) · 4.38 KB
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
import React, { useMemo, useRef } from 'react';
import { useSelector } from 'react-redux';
import get from 'lodash/get';
import {
findCollectionByUid,
findItemInCollection,
findItemInCollectionByPathname,
getGlobalEnvironmentVariables,
getGlobalEnvironmentVariablesMasked
} from 'utils/collections';
import { selectCollections } from 'src/selectors/collections';
import { selectTabs, selectActiveTabUid } from 'src/selectors/tab';
import { ScopedPersistenceProvider } from 'hooks/usePersistedState/PersistedScopeProvider';
import TabPanelErrorBoundary from 'components/RequestTabPanel/TabPanelErrorBoundary';
import AppView from 'components/AppView';
import CollectionApp from 'components/CollectionApp';
import StyledWrapper from './StyledWrapper';
const APP_CAPABLE_TAB_TYPES = new Set([
'app',
'request',
'http-request',
'graphql-request',
'grpc-request',
'ws-request'
]);
const AppPreviewKeepAlive = () => {
const tabs = useSelector(selectTabs);
const activeTabUid = useSelector(selectActiveTabUid);
const collections = useSelector(selectCollections);
const globalEnvironments = useSelector((state) => state.globalEnvironments?.globalEnvironments);
const activeGlobalEnvironmentUid = useSelector(
(state) => state.globalEnvironments?.activeGlobalEnvironmentUid
);
const everActiveRef = useRef(new Set());
const appTabs = useMemo(() => {
const out = [];
let globals = null;
const mergedByUid = new Map();
const withGlobals = (collection) => {
if (!mergedByUid.has(collection.uid)) {
if (!globals) {
globals = {
globalEnvironmentVariables: getGlobalEnvironmentVariables({ globalEnvironments, activeGlobalEnvironmentUid }),
globalEnvSecrets: getGlobalEnvironmentVariablesMasked({ globalEnvironments, activeGlobalEnvironmentUid }),
globalEnvironments,
activeGlobalEnvironmentUid
};
}
mergedByUid.set(collection.uid, { ...collection, ...globals });
}
return mergedByUid.get(collection.uid);
};
for (const tab of tabs) {
if (tab.type && !APP_CAPABLE_TAB_TYPES.has(tab.type)) continue;
const collection = findCollectionByUid(collections, tab.collectionUid);
// File-mode collections render everything through FileEditor.
if (!collection || collection.fileMode) continue;
let item = findItemInCollection(collection, tab.uid);
if (!item && tab.pathname) {
item = findItemInCollectionByPathname(collection, tab.pathname);
}
if (!item || item.partial || item.loading) continue;
if (item.type === 'app') {
out.push({ tabUid: tab.uid, collection: withGlobals(collection), item, kind: 'standalone' });
continue;
}
const itemSource = item.draft ? item.draft : item;
const appEnabled = get(itemSource, 'app.enabled', false) === true
&& tab.appPreview !== false;
if (appEnabled) {
const code = get(itemSource, 'app.code', '');
out.push({ tabUid: tab.uid, collection: withGlobals(collection), item, kind: 'request', code });
}
}
return out;
}, [tabs, collections, globalEnvironments, activeGlobalEnvironmentUid]);
const validUids = new Set(appTabs.map((t) => t.tabUid));
for (const uid of [...everActiveRef.current]) {
if (!validUids.has(uid)) everActiveRef.current.delete(uid);
}
if (validUids.has(activeTabUid)) everActiveRef.current.add(activeTabUid);
const mounted = appTabs.filter((t) => everActiveRef.current.has(t.tabUid));
if (!mounted.length) return null;
return (
<StyledWrapper data-testid="app-preview-keepalive">
{mounted.map(({ tabUid, collection, item, kind, code }) => {
const isActive = tabUid === activeTabUid;
return (
<div
key={tabUid}
className={`app-preview-slot ${isActive ? 'active' : ''}`}
aria-hidden={!isActive}
>
<TabPanelErrorBoundary tabUid={tabUid}>
<ScopedPersistenceProvider scope={tabUid}>
{kind === 'standalone' ? (
<CollectionApp item={item} collection={collection} />
) : (
<AppView item={item} collection={collection} code={code} />
)}
</ScopedPersistenceProvider>
</TabPanelErrorBoundary>
</div>
);
})}
</StyledWrapper>
);
};
export default AppPreviewKeepAlive;