-
Notifications
You must be signed in to change notification settings - Fork 965
Expand file tree
/
Copy pathcomponent-view.tsx
More file actions
279 lines (245 loc) · 11.1 KB
/
Copy pathcomponent-view.tsx
File metadata and controls
279 lines (245 loc) · 11.1 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* eslint-disable complexity */
import type { ComponentTreeSlot } from '@teambit/component-tree';
import { Link, useLocation } from '@teambit/base-react.navigation.link';
import { EnvIcon } from '@teambit/envs.ui.env-icon';
import { DeprecationIcon } from '@teambit/component.ui.deprecation-icon';
import { Icon } from '@teambit/evangelist.elements.icon';
import satisfies from 'semver/functions/satisfies';
import classNames from 'classnames';
import { ComponentID } from '@teambit/component-id';
import type { ComponentModel } from '@teambit/component';
import { ComponentUrl } from '@teambit/component.modules.component-url';
import React, { useCallback, useContext } from 'react';
import { Tooltip } from '@teambit/design.ui.tooltip';
import { TreeContext } from '@teambit/base-ui.graph.tree.tree-context';
import { indentClass } from '@teambit/base-ui.graph.tree.indent';
import type { TreeNodeProps } from '@teambit/base-ui.graph.tree.recursive-tree';
import { LanesContext } from '@teambit/lanes.hooks.use-lanes';
import { LanesModel } from '@teambit/lanes.ui.models.lanes-model';
import { getName } from '../utils/get-name';
import styles from './component-view.module.scss';
export type ComponentViewProps<Payload = any> = {
treeNodeSlot?: ComponentTreeSlot;
scopeName?: string;
} & TreeNodeProps<Payload>;
export function ComponentView(props: ComponentViewProps) {
const { node, scopeName, treeNodeSlot } = props;
let component = node.payload;
const { onSelect } = useContext(TreeContext);
const lanesContextModel = useContext(LanesContext);
const lanesModel = lanesContextModel?.lanesModel;
const hasWindow = typeof window !== 'undefined';
const locationOrigin = hasWindow ? window.location.origin : undefined;
const isLocalhost = hasWindow ? window.location.host.startsWith('localhost') : false;
const handleClick = useCallback(
(event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) => {
onSelect?.(node.id, event);
},
[onSelect, node.id]
);
const location = useLocation();
const scope = { name: scopeName };
const isMissingCompOrEnvId = !component?.id || !component?.environment?.id;
component = component as ComponentModel;
const envId = !isMissingCompOrEnvId ? ComponentID.fromString(component.environment?.id as string) : undefined;
const isEnvOnCurrentLane = envId
? lanesModel?.isComponentOnLaneButNotOnMain(envId, false, lanesModel.viewedLane?.id)
: false;
const envUrl = !envId
? undefined
: (isEnvOnCurrentLane &&
lanesModel?.viewedLane?.id &&
locationOrigin &&
`${locationOrigin}${LanesModel.getLaneComponentUrl(envId, lanesModel.viewedLane?.id, undefined, lanesModel.viewedLane)}`) ||
ComponentUrl.toUrl(envId, {
includeVersion: true,
useLocationOrigin: isLocalhost,
});
const envTooltip = envId ? (
<Link
external
className={styles.envLink}
href={envUrl}
onClick={(event) => {
// do not trigger component selection
event.stopPropagation();
}}
>
<div className={styles.componentEnvTitle}>Environment</div>
<div>{component.environment?.id}</div>
</Link>
) : null;
const href = !isMissingCompOrEnvId
? lanesModel?.getLaneComponentUrlByVersion(
component.id as any,
lanesModel.viewedLane?.id,
!scope.name,
lanesModel.viewedLane
)
: undefined;
const viewingMainCompOnLane = React.useMemo(() => {
if (isMissingCompOrEnvId) return false;
return (
component.status?.isNew === false &&
!lanesModel?.viewedLane?.id.isDefault() &&
lanesModel?.isComponentOnMainButNotOnLane(component.id as any, undefined, lanesModel?.viewedLane?.id)
);
}, [lanesModel?.viewedLane?.id.toString(), component.id.toString()]);
/**
* this covers the following use cases when matching the active component's href with location
*
* 1. viewing main component
*
* - /<component-id>/
* - /<component-id>/~sub-route
* - /<component-id>/~sub-route/another-sub-route
* - /<component-id>/~sub-route?version=<version>
* - /<component-id>/~sub-route?scope=<scope>
* - /<component-id>?scope=<scope>
*
* 2. viewing a lane component
*
* - /~lane/<lane-id>/<component-id>/
* - /~lane/<lane-id>/<component-id>?scope=<scope>
* - /~lane/<lane-id>/<component-id>/~sub-route
* - /~lane/<lane-id>/<component-id>/~sub-route/another-sub-route
* - /~lane/<lane-id>/<component-id>/~sub-route?version=<version>
* - /~lane/<lane-id>/<component-id>/~sub-route?scope=<scope>
*
* 3. viewing a main component when on a lane
*
* - /?lane=<lane-id>/<component-id>/
* - /?lane=<lane-id>/<component-id>?scope=<scope>
* - /?lane=<lane-id>/<component-id>/~sub-route
* - /?lane=<lane-id>/<component-id>/~sub-route/another-sub-route
* - /?lane=<lane-id>/<component-id>/~sub-route?version=<version>
* - /?lane=<lane-id>/<component-id>/~sub-route?scope=<scope>
*
* 4. viewing currently checked out lane component on workspace
*
* - /<component-id-without-scope>/
* - /<component-id-without-scope>?scope=<scope>
*
* 5. viewing lane component from the same scope as the lane on a workspace
*
* - /~lane/<lane-id>/<component-id-without-scope>/
*
* @todo - move this logic to a util function
*/
const isActive = React.useMemo(() => {
if (!href || !location || !component?.id) return false;
const searchParams = new URLSearchParams(location.search);
const scopeFromQueryParams = searchParams.get('scope');
const pathname = location.pathname.substring(1).split('?')[0];
const compIdStr = component.id.toStringWithoutVersion();
const compIdName = component.id.fullName;
const componentScope = component.id.scope;
const locationIncludesLaneId = location.pathname.includes(LanesModel.lanesPrefix);
const sanitizedHref = (href.startsWith('/') ? href.substring(1) : href).split('?')[0];
// if you are in a workspace, the componentId might not have a scopeId, if you are viewing
// a component on the checked out lane
const viewingCheckedOutLaneComp =
lanesModel?.currentLane && lanesModel.currentLane.id.toString() === lanesModel?.viewedLane?.id.toString();
if (
!locationIncludesLaneId &&
(lanesModel?.viewedLane?.id.isDefault() || viewingMainCompOnLane || viewingCheckedOutLaneComp)
) {
// split out any sub routes if exist
const compUrl = pathname.split('/~')[0];
// may or may not contain scope
const scopeUrl = scope.name ? `${scope.name.replace('.', '/')}/` : '';
const compUrlWithoutScope = compUrl.replace(scopeUrl, '');
return !scopeFromQueryParams
? sanitizedHref === compUrlWithoutScope
: sanitizedHref === compUrl && componentScope === scopeFromQueryParams;
}
const laneCompUrlWithSubRoutes = pathname.split(LanesModel.baseLaneComponentRoute)[1] ?? '';
const extractedLaneCompUrl = laneCompUrlWithSubRoutes.split('/~')[0] ?? '';
const laneCompUrl = extractedLaneCompUrl.startsWith('/') ? extractedLaneCompUrl.substring(1) : extractedLaneCompUrl;
/**
* if the laneCompUrl doesn't have the scope as part of it and you are on a bare scope
* attach the bare scope to the laneCompUrl and parse it as a ComponentID
*/
const laneCompIdFromUrl =
ComponentID.tryFromString(laneCompUrl) ??
(scope.name ? ComponentID.tryFromString(`${scope.name}/${laneCompUrl}`) : undefined);
// viewing lane component from the same scope as the lane on a workspace
const laneAndCompFromSameScopeOnWs =
!scope.name && lanesModel?.viewedLane?.id && component.id.scope === lanesModel?.viewedLane?.id.scope;
if (laneAndCompFromSameScopeOnWs) {
return !scopeFromQueryParams
? compIdName === laneCompUrl
: compIdName === laneCompUrl && componentScope === scopeFromQueryParams;
}
if (!laneCompIdFromUrl) return false;
return !scopeFromQueryParams
? laneCompIdFromUrl?.toString() === compIdStr || laneCompIdFromUrl.fullName === compIdName
: laneCompIdFromUrl?.toString() === compIdStr ||
(laneCompIdFromUrl.fullName === compIdName && componentScope === scopeFromQueryParams);
}, [href, viewingMainCompOnLane, location?.pathname, component?.id.toString(), scope.name]);
const viewedVersion = React.useMemo(() => {
if (!location?.search) return undefined;
return new URLSearchParams(location.search).get('version') || undefined;
}, [location?.search]);
// The sidebar payload is always the component's head model, so on its own it can only reflect
// head deprecation. When the active component is being viewed at a historical version, surface
// that version's deprecation too (e.g. a range-deprecated older version of a non-deprecated head).
const isViewedVersionDeprecated = React.useMemo(() => {
if (!isActive || !viewedVersion) return false;
if (component.deprecation?.isDeprecate) return false; // already covered by <DeprecationIcon />
const range = component.deprecation?.range;
if (!range) return false;
try {
return satisfies(viewedVersion, range);
} catch {
return false;
}
}, [isActive, viewedVersion, component.deprecation?.isDeprecate, component.deprecation?.range]);
if (isMissingCompOrEnvId) return null;
const isCompModified =
component.status?.modifyInfo?.hasModifiedFiles || component.status?.modifyInfo?.hasModifiedDependencies;
const addOpacity = viewingMainCompOnLane && !isCompModified;
const Name = viewingMainCompOnLane ? (
<Tooltip className={styles.tooltip} placement="top" content="On Main">
<span className={classNames(addOpacity && styles.opacity)}>{getName(node.id)}</span>
</Tooltip>
) : (
<span>{getName(node.id)}</span>
);
return (
<Link
href={href}
className={classNames(indentClass, styles.component)}
activeClassName={styles.active}
onClick={handleClick}
// exact={true}
active={isActive}
>
<div className={styles.left}>
<Tooltip className={styles.componentEnvTooltip} placement="top" content={envTooltip}>
<EnvIcon component={component} className={styles.envIcon} />
</Tooltip>
{Name}
</div>
<div className={styles.right}>
<DeprecationIcon component={component} />
{isViewedVersionDeprecated && (
<Tooltip placement="bottom" content="This version is deprecated">
<div className={styles.deprecatedIcon}>
<Icon of="note-deprecated" />
</div>
</Tooltip>
)}
{/* {isInternal && <Icon of="Internal" className={styles.componentIcon} />} */}
{treeNodeSlot &&
treeNodeSlot.toArray().map(([id, treeNode]) => {
// treeNode.widget's JSX-usability can't be verified across capsule boundaries in isolated
// builds (its type is resolved via @teambit/component-tree's own react types), so annotate
// it explicitly against this file's react import instead of letting JSX infer it.
const Widget = treeNode.widget as unknown as React.ComponentType<{ component: ComponentModel }>;
return <Widget key={id} component={component} />;
})}
</div>
</Link>
);
}