@teambit/component 0.0.565 → 0.0.569
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aspect.section.tsx +16 -0
- package/component-map/component-map.ts +106 -0
- package/component-map/index.ts +1 -0
- package/component.ui.runtime.tsx +216 -0
- package/dependencies/dependencies.ts +74 -0
- package/dependencies/index.ts +1 -0
- package/dist/component.js +7 -13
- package/dist/component.js.map +1 -1
- package/exceptions/could-not-find-latest.ts +8 -0
- package/exceptions/host-not-found.ts +14 -0
- package/exceptions/index.ts +4 -0
- package/exceptions/nothing-to-snap.ts +1 -0
- package/host/component-host-model.ts +9 -0
- package/host/index.ts +2 -0
- package/host/use-component-host.ts +39 -0
- package/package-tar/teambit-component-0.0.569.tgz +0 -0
- package/package.json +34 -49
- package/section/index.ts +1 -0
- package/section/section.tsx +8 -0
- package/show/extensions.fragment.ts +23 -0
- package/show/files.fragment.ts +24 -0
- package/show/id.fragment.ts +20 -0
- package/show/index.ts +8 -0
- package/show/main-file.fragment.ts +13 -0
- package/show/name.fragment.ts +13 -0
- package/show/scope.fragment.ts +15 -0
- package/show/show-fragment.ts +44 -0
- package/show/show.cmd.ts +85 -0
- package/snap/author.ts +19 -0
- package/snap/index.ts +2 -0
- package/snap/snap.ts +63 -0
- package/tag/index.ts +1 -0
- package/tag/tag.ts +37 -0
- package/types/asset.d.ts +29 -0
- package/types/style.d.ts +42 -0
- package/ui/aspect-page/aspect-page.tsx +64 -0
- package/ui/aspect-page/index.ts +1 -0
- package/ui/component-error/component-error.tsx +22 -0
- package/ui/component-error/index.ts +1 -0
- package/ui/component-model/component-model.ts +169 -0
- package/ui/component-model/index.ts +1 -0
- package/ui/component.tsx +48 -0
- package/ui/context/component-context.ts +5 -0
- package/ui/context/component-provider.tsx +20 -0
- package/ui/context/index.ts +2 -0
- package/ui/index.ts +3 -0
- package/ui/menu/index.ts +2 -0
- package/ui/menu/menu-nav.tsx +37 -0
- package/ui/menu/menu.tsx +94 -0
- package/ui/menu/nav-plugin.tsx +9 -0
- package/ui/top-bar-nav/index.ts +1 -0
- package/ui/top-bar-nav/top-bar-nav.tsx +26 -0
- package/ui/use-component-query.ts +195 -0
- package/ui/use-component.tsx +34 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { NotFoundPage } from '@teambit/design.ui.pages.not-found';
|
|
3
|
+
import { ServerErrorPage } from '@teambit/design.ui.pages.server-error';
|
|
4
|
+
|
|
5
|
+
export class ComponentError {
|
|
6
|
+
constructor(
|
|
7
|
+
/**
|
|
8
|
+
* http status code of error
|
|
9
|
+
*/
|
|
10
|
+
public readonly code: number,
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* error message of the error
|
|
14
|
+
*/
|
|
15
|
+
public readonly message?: string
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
renderError() {
|
|
19
|
+
if (this.code === 404) return <NotFoundPage />;
|
|
20
|
+
return <ServerErrorPage />;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ComponentError } from './component-error';
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Composition, CompositionProps } from '@teambit/compositions';
|
|
2
|
+
import { DeprecationInfo } from '@teambit/deprecation';
|
|
3
|
+
import { Descriptor } from '@teambit/envs';
|
|
4
|
+
import { ComponentID, ComponentIdObj } from '@teambit/component-id';
|
|
5
|
+
|
|
6
|
+
import { Tag } from '../../tag';
|
|
7
|
+
import { TagMap } from '../../tag-map';
|
|
8
|
+
import { TagProps } from '../../tag/tag';
|
|
9
|
+
// import { Snap } from '../../snap';
|
|
10
|
+
|
|
11
|
+
// ADDING MORE PROPERTIES HERE IS NOT ALLOWED!!! IF YOU NEED DATA PLEASE ADD A NEW
|
|
12
|
+
// HOOK FROM YOUR ASPECT!!!
|
|
13
|
+
// TODO: remove all properties from here to their rightful place in their aspects.
|
|
14
|
+
export type ComponentModelProps = {
|
|
15
|
+
id: ComponentIdObj;
|
|
16
|
+
description: string;
|
|
17
|
+
buildStatus?: string;
|
|
18
|
+
server?: ComponentServer;
|
|
19
|
+
displayName: string;
|
|
20
|
+
packageName: string; // pkg aspect
|
|
21
|
+
elementsUrl?: string; // pkg aspect
|
|
22
|
+
compositions?: CompositionProps[];
|
|
23
|
+
tags?: TagProps[];
|
|
24
|
+
issuesCount?: number; // component/issues aspect
|
|
25
|
+
status?: any; // workspace aspect.
|
|
26
|
+
deprecation?: DeprecationInfo; // deprecation aspect
|
|
27
|
+
env?: Descriptor; // env aspect.
|
|
28
|
+
labels?: string[];
|
|
29
|
+
host?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type ComponentServer = {
|
|
33
|
+
env: string;
|
|
34
|
+
url: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export class ComponentModel {
|
|
38
|
+
constructor(
|
|
39
|
+
/**
|
|
40
|
+
* id of the component
|
|
41
|
+
*/
|
|
42
|
+
readonly id: ComponentID,
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* display name of the component.
|
|
46
|
+
*/
|
|
47
|
+
readonly displayName: string,
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* package name of the component.
|
|
51
|
+
*/
|
|
52
|
+
readonly packageName: string,
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* the component server.
|
|
56
|
+
*/
|
|
57
|
+
readonly server: ComponentServer | undefined,
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* array of compositions
|
|
61
|
+
*/
|
|
62
|
+
readonly compositions: Composition[],
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* tags of the component.
|
|
66
|
+
*/
|
|
67
|
+
readonly tags: TagMap,
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* component build status
|
|
71
|
+
*/
|
|
72
|
+
readonly buildStatus?: string,
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* issues of component.
|
|
76
|
+
*/
|
|
77
|
+
readonly issuesCount?: number,
|
|
78
|
+
/**
|
|
79
|
+
* elements url
|
|
80
|
+
*/
|
|
81
|
+
readonly elementsUrl?: string,
|
|
82
|
+
/**
|
|
83
|
+
* status of component.
|
|
84
|
+
*/
|
|
85
|
+
readonly status?: any,
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* deprecation info of the component.
|
|
89
|
+
*/
|
|
90
|
+
readonly deprecation?: DeprecationInfo,
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* env descriptor.
|
|
94
|
+
*/
|
|
95
|
+
readonly environment?: Descriptor,
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* description of the component.
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
readonly description = '',
|
|
102
|
+
|
|
103
|
+
readonly labels: string[] = [],
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* host of the component
|
|
107
|
+
*/
|
|
108
|
+
readonly host?: string
|
|
109
|
+
) {}
|
|
110
|
+
|
|
111
|
+
get version() {
|
|
112
|
+
if (!this.id.version) return 'new';
|
|
113
|
+
return this.id.version;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* create an instance of a component from a plain object.
|
|
118
|
+
*/
|
|
119
|
+
static from({
|
|
120
|
+
id,
|
|
121
|
+
server,
|
|
122
|
+
displayName,
|
|
123
|
+
compositions = [],
|
|
124
|
+
packageName,
|
|
125
|
+
elementsUrl,
|
|
126
|
+
tags = [],
|
|
127
|
+
deprecation,
|
|
128
|
+
buildStatus,
|
|
129
|
+
env,
|
|
130
|
+
status,
|
|
131
|
+
issuesCount,
|
|
132
|
+
description,
|
|
133
|
+
labels,
|
|
134
|
+
host,
|
|
135
|
+
}: ComponentModelProps) {
|
|
136
|
+
return new ComponentModel(
|
|
137
|
+
ComponentID.fromObject(id),
|
|
138
|
+
displayName,
|
|
139
|
+
packageName,
|
|
140
|
+
server,
|
|
141
|
+
Composition.fromArray(compositions),
|
|
142
|
+
TagMap.fromArray(tags.map((tag) => Tag.fromObject(tag))),
|
|
143
|
+
buildStatus,
|
|
144
|
+
issuesCount,
|
|
145
|
+
elementsUrl,
|
|
146
|
+
status,
|
|
147
|
+
deprecation,
|
|
148
|
+
env,
|
|
149
|
+
description,
|
|
150
|
+
labels,
|
|
151
|
+
host
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static fromArray(componentsProps: ComponentModelProps[]) {
|
|
156
|
+
return componentsProps.map((rawComponent) => ComponentModel.from(rawComponent));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static empty() {
|
|
160
|
+
return new ComponentModel(
|
|
161
|
+
ComponentID.fromObject({ name: 'root', scope: 'temp' }),
|
|
162
|
+
'',
|
|
163
|
+
'',
|
|
164
|
+
{ env: '', url: '' },
|
|
165
|
+
[],
|
|
166
|
+
TagMap.empty()
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { ComponentModel, ComponentModelProps } from './component-model';
|
package/ui/component.tsx
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import React, { useEffect, ReactNode, useMemo } from 'react';
|
|
2
|
+
import flatten from 'lodash.flatten';
|
|
3
|
+
import { RouteSlot, SlotSubRouter } from '@teambit/ui-foundation.ui.react-router.slot-router';
|
|
4
|
+
import { SlotRegistry } from '@teambit/harmony';
|
|
5
|
+
|
|
6
|
+
import styles from './component.module.scss';
|
|
7
|
+
import { ComponentProvider } from './context';
|
|
8
|
+
import { useComponent } from './use-component';
|
|
9
|
+
import { ComponentModel } from './component-model';
|
|
10
|
+
|
|
11
|
+
export type ComponentPageSlot = SlotRegistry<ComponentPageElement[]>;
|
|
12
|
+
export type ComponentPageElement = {
|
|
13
|
+
type: 'before' | 'after';
|
|
14
|
+
content: ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type ComponentProps = {
|
|
18
|
+
containerSlot?: ComponentPageSlot;
|
|
19
|
+
routeSlot: RouteSlot;
|
|
20
|
+
host: string;
|
|
21
|
+
onComponentChange?: (activeComponent?: ComponentModel) => void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* main UI component of the Component extension.
|
|
26
|
+
*/
|
|
27
|
+
export function Component({ routeSlot, containerSlot, host, onComponentChange }: ComponentProps) {
|
|
28
|
+
const { component, error } = useComponent(host);
|
|
29
|
+
// trigger onComponentChange when component changes
|
|
30
|
+
useEffect(() => onComponentChange?.(component), [component]);
|
|
31
|
+
// cleanup when unmounting component
|
|
32
|
+
useEffect(() => () => onComponentChange?.(undefined), []);
|
|
33
|
+
|
|
34
|
+
const pageItems = useMemo(() => flatten(containerSlot?.values()), [containerSlot]);
|
|
35
|
+
const before = useMemo(() => pageItems.filter((x) => x.type === 'before').map((x) => x.content), [pageItems]);
|
|
36
|
+
const after = useMemo(() => pageItems.filter((x) => x.type === 'after').map((x) => x.content), [pageItems]);
|
|
37
|
+
|
|
38
|
+
if (error) return error.renderError();
|
|
39
|
+
if (!component) return <div></div>;
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<ComponentProvider component={component}>
|
|
43
|
+
{before}
|
|
44
|
+
<div className={styles.container}>{routeSlot && <SlotSubRouter slot={routeSlot} />}</div>
|
|
45
|
+
{after}
|
|
46
|
+
</ComponentProvider>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
import { ComponentModel } from '../component-model';
|
|
4
|
+
import { ComponentContext } from './component-context';
|
|
5
|
+
|
|
6
|
+
export type ComponentProviderProps = {
|
|
7
|
+
/**
|
|
8
|
+
* component model.
|
|
9
|
+
*/
|
|
10
|
+
component: ComponentModel;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* component children.
|
|
14
|
+
*/
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function ComponentProvider({ component, children }: ComponentProviderProps) {
|
|
19
|
+
return <ComponentContext.Provider value={component}>{children}</ComponentContext.Provider>;
|
|
20
|
+
}
|
package/ui/index.ts
ADDED
package/ui/menu/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
import { TopBarNav } from '../top-bar-nav';
|
|
4
|
+
import styles from './menu.module.scss';
|
|
5
|
+
import { NavPlugin, OrderedNavigationSlot } from './nav-plugin';
|
|
6
|
+
|
|
7
|
+
export function MenuNav({ navigationSlot }: { navigationSlot: OrderedNavigationSlot }) {
|
|
8
|
+
const plugins = navigationSlot.toArray().sort(sortFn);
|
|
9
|
+
|
|
10
|
+
return (
|
|
11
|
+
<nav className={styles.navigation}>
|
|
12
|
+
{plugins.map(([id, menuItem]) => (
|
|
13
|
+
<TopBarNav key={id} {...menuItem.props} />
|
|
14
|
+
))}
|
|
15
|
+
</nav>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function sortFn([, { order: first }]: [string, NavPlugin], [, { order: second }]: [string, NavPlugin]) {
|
|
20
|
+
// 0 - equal
|
|
21
|
+
// <0 - first < second
|
|
22
|
+
// >0 - first > second
|
|
23
|
+
|
|
24
|
+
return (first ?? 0) - (second ?? 0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// // this is the aspect-oriented and serialize-able way to sort plugins.
|
|
28
|
+
// const pluginOrder = ['teambit.docs/docs', 'teambit.compositions/compositions', 'teambit.docs/docs'];
|
|
29
|
+
// export function toSortedArray<T>(slot: SlotRegistry<T>, order: string[]) {
|
|
30
|
+
// // sort items according to the order
|
|
31
|
+
// const sorted = order.map((x) => [x, slot.get(x)]).filter(([, val]) => !!val) as [string, T][];
|
|
32
|
+
//
|
|
33
|
+
// // add all other items
|
|
34
|
+
// const unsorted = slot.toArray().filter(([id]) => order.indexOf(id) < 0);
|
|
35
|
+
//
|
|
36
|
+
// return sorted.concat(unsorted);
|
|
37
|
+
// }
|
package/ui/menu/menu.tsx
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { MainDropdown, MenuItemSlot } from '@teambit/ui-foundation.ui.main-dropdown';
|
|
2
|
+
import { VersionDropdown } from '@teambit/component.ui.version-dropdown';
|
|
3
|
+
import { FullLoader } from '@teambit/legacy/dist/to-eject/full-loader';
|
|
4
|
+
import { flatten, groupBy } from 'lodash';
|
|
5
|
+
import classnames from 'classnames';
|
|
6
|
+
import React, { useMemo } from 'react';
|
|
7
|
+
import { UseBoxDropdown } from '@teambit/ui-foundation.ui.use-box.dropdown';
|
|
8
|
+
import { Menu as UseBoxMenu } from '@teambit/ui-foundation.ui.use-box.menu';
|
|
9
|
+
import type { ComponentModel } from '../component-model';
|
|
10
|
+
import { useComponent } from '../use-component';
|
|
11
|
+
import { MenuNav } from './menu-nav';
|
|
12
|
+
import styles from './menu.module.scss';
|
|
13
|
+
import { OrderedNavigationSlot } from './nav-plugin';
|
|
14
|
+
|
|
15
|
+
export type MenuProps = {
|
|
16
|
+
className?: string;
|
|
17
|
+
/**
|
|
18
|
+
* slot for top bar menu nav items
|
|
19
|
+
*/
|
|
20
|
+
navigationSlot: OrderedNavigationSlot;
|
|
21
|
+
/**
|
|
22
|
+
* right side menu item slot
|
|
23
|
+
*/
|
|
24
|
+
widgetSlot: OrderedNavigationSlot;
|
|
25
|
+
host: string;
|
|
26
|
+
/**
|
|
27
|
+
* main dropdown item slot
|
|
28
|
+
*/
|
|
29
|
+
menuItemSlot: MenuItemSlot;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* top bar menu.
|
|
34
|
+
*/
|
|
35
|
+
export function Menu({ navigationSlot, widgetSlot, className, host, menuItemSlot }: MenuProps) {
|
|
36
|
+
const { component } = useComponent(host);
|
|
37
|
+
const mainMenuItems = useMemo(() => groupBy(flatten(menuItemSlot.values()), 'category'), [menuItemSlot]);
|
|
38
|
+
|
|
39
|
+
if (!component) return <FullLoader />;
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<div className={classnames(styles.topBar, className)}>
|
|
43
|
+
<div className={styles.leftSide}>
|
|
44
|
+
<MenuNav navigationSlot={navigationSlot} />
|
|
45
|
+
</div>
|
|
46
|
+
<div className={styles.rightSide}>
|
|
47
|
+
<div className={styles.widgets}>
|
|
48
|
+
<MenuNav navigationSlot={widgetSlot} />
|
|
49
|
+
</div>
|
|
50
|
+
<VersionRelatedDropdowns component={component} />
|
|
51
|
+
<MainDropdown menuItems={mainMenuItems} />
|
|
52
|
+
</div>
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function VersionRelatedDropdowns({ component }: { component: ComponentModel }) {
|
|
58
|
+
const versionList =
|
|
59
|
+
useMemo(
|
|
60
|
+
() =>
|
|
61
|
+
component.tags
|
|
62
|
+
?.toArray()
|
|
63
|
+
.map((tag) => tag?.version?.version)
|
|
64
|
+
.filter((x) => x !== undefined)
|
|
65
|
+
.reverse(),
|
|
66
|
+
[component.tags]
|
|
67
|
+
) || [];
|
|
68
|
+
|
|
69
|
+
const isLatestVersion = useMemo(() => component.version === versionList[0], [component.version]);
|
|
70
|
+
const packageVersion = useMemo(() => (isLatestVersion ? '' : `@${component.version}`), [component.version]);
|
|
71
|
+
const origin = typeof window !== undefined ? window.location.origin : undefined;
|
|
72
|
+
const finalElementsUrl = origin && component.elementsUrl ? `${origin}${component.elementsUrl}` : undefined;
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<>
|
|
76
|
+
{versionList.length > 0 && (
|
|
77
|
+
<UseBoxDropdown
|
|
78
|
+
position="bottom-end"
|
|
79
|
+
className={styles.useBox}
|
|
80
|
+
Menu={() => (
|
|
81
|
+
<UseBoxMenu
|
|
82
|
+
componentName={component.id.name}
|
|
83
|
+
componentId={component.id.toString({ ignoreVersion: isLatestVersion })}
|
|
84
|
+
packageName={`${component.packageName}${packageVersion}`}
|
|
85
|
+
elementsUrl={finalElementsUrl}
|
|
86
|
+
registryName={component.packageName.split('/')[0]}
|
|
87
|
+
/>
|
|
88
|
+
)}
|
|
89
|
+
/>
|
|
90
|
+
)}
|
|
91
|
+
<VersionDropdown versions={versionList} currentVersion={component.version} />
|
|
92
|
+
</>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './top-bar-nav';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { NavLink, NavLinkProps } from '@teambit/base-ui.routing.nav-link';
|
|
2
|
+
import { extendPath } from '@teambit/ui-foundation.ui.react-router.extend-path';
|
|
3
|
+
import classnames from 'classnames';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { useRouteMatch, useLocation } from 'react-router-dom';
|
|
6
|
+
|
|
7
|
+
import styles from './top-bar-nav.module.scss';
|
|
8
|
+
|
|
9
|
+
export function TopBarNav(props: NavLinkProps) {
|
|
10
|
+
const { url } = useRouteMatch();
|
|
11
|
+
const { search } = useLocation(); // sticky query params
|
|
12
|
+
const { href } = props;
|
|
13
|
+
|
|
14
|
+
const target = `${extendPath(url, href)}${search}`;
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<NavLink
|
|
18
|
+
{...props}
|
|
19
|
+
className={classnames(props.className, styles.topBarLink)}
|
|
20
|
+
activeClassName={classnames(props.className, styles.active)}
|
|
21
|
+
href={target}
|
|
22
|
+
>
|
|
23
|
+
<div>{props.children}</div>
|
|
24
|
+
</NavLink>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { useMemo, useEffect, useRef } from 'react';
|
|
2
|
+
import { gql } from '@apollo/client';
|
|
3
|
+
import { useDataQuery } from '@teambit/ui-foundation.ui.hooks.use-data-query';
|
|
4
|
+
import { ComponentID, ComponentIdObj } from '@teambit/component-id';
|
|
5
|
+
|
|
6
|
+
import { ComponentModel } from './component-model';
|
|
7
|
+
import { ComponentError } from './component-error';
|
|
8
|
+
|
|
9
|
+
const componentIdFields = gql`
|
|
10
|
+
fragment componentIdFields on ComponentID {
|
|
11
|
+
name
|
|
12
|
+
version
|
|
13
|
+
scope
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
const componentFields = gql`
|
|
18
|
+
fragment componentFields on Component {
|
|
19
|
+
id {
|
|
20
|
+
...componentIdFields
|
|
21
|
+
}
|
|
22
|
+
packageName
|
|
23
|
+
elementsUrl
|
|
24
|
+
displayName
|
|
25
|
+
server {
|
|
26
|
+
env
|
|
27
|
+
url
|
|
28
|
+
}
|
|
29
|
+
buildStatus
|
|
30
|
+
compositions {
|
|
31
|
+
identifier
|
|
32
|
+
displayName
|
|
33
|
+
}
|
|
34
|
+
tags {
|
|
35
|
+
version
|
|
36
|
+
}
|
|
37
|
+
env {
|
|
38
|
+
id
|
|
39
|
+
icon
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
${componentIdFields}
|
|
43
|
+
`;
|
|
44
|
+
|
|
45
|
+
const GET_COMPONENT = gql`
|
|
46
|
+
query Component($id: String!, $extensionId: String!) {
|
|
47
|
+
getHost(id: $extensionId) {
|
|
48
|
+
id # used for GQL caching
|
|
49
|
+
get(id: $id) {
|
|
50
|
+
...componentFields
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
${componentFields}
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
const SUB_SUBSCRIPTION_ADDED = gql`
|
|
58
|
+
subscription OnComponentAdded {
|
|
59
|
+
componentAdded {
|
|
60
|
+
component {
|
|
61
|
+
...componentFields
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
${componentFields}
|
|
66
|
+
`;
|
|
67
|
+
|
|
68
|
+
const SUB_COMPONENT_CHANGED = gql`
|
|
69
|
+
subscription OnComponentChanged {
|
|
70
|
+
componentChanged {
|
|
71
|
+
component {
|
|
72
|
+
...componentFields
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
${componentFields}
|
|
77
|
+
`;
|
|
78
|
+
|
|
79
|
+
const SUB_COMPONENT_REMOVED = gql`
|
|
80
|
+
subscription OnComponentRemoved {
|
|
81
|
+
componentRemoved {
|
|
82
|
+
componentIds {
|
|
83
|
+
...componentIdFields
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
${componentIdFields}
|
|
88
|
+
`;
|
|
89
|
+
|
|
90
|
+
/** provides data to component ui page, making sure both variables and return value are safely typed and memoized */
|
|
91
|
+
export function useComponentQuery(componentId: string, host: string) {
|
|
92
|
+
const idRef = useRef(componentId);
|
|
93
|
+
idRef.current = componentId;
|
|
94
|
+
const { data, error, loading, subscribeToMore } = useDataQuery(GET_COMPONENT, {
|
|
95
|
+
variables: { id: componentId, extensionId: host },
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
// @TODO @Kutner fix subscription for scope
|
|
100
|
+
if (host !== 'teambit.workspace/workspace') {
|
|
101
|
+
return () => {};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const unsubAddition = subscribeToMore({
|
|
105
|
+
document: SUB_SUBSCRIPTION_ADDED,
|
|
106
|
+
updateQuery: (prev, { subscriptionData }) => {
|
|
107
|
+
const prevComponent = prev?.getHost?.get;
|
|
108
|
+
const addedComponent = subscriptionData?.data?.componentAdded?.component;
|
|
109
|
+
|
|
110
|
+
if (!addedComponent || prevComponent) return prev;
|
|
111
|
+
|
|
112
|
+
if (idRef.current === addedComponent.id.name) {
|
|
113
|
+
return {
|
|
114
|
+
...prev,
|
|
115
|
+
getHost: {
|
|
116
|
+
...prev.getHost,
|
|
117
|
+
get: addedComponent,
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return prev;
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const unsubChanges = subscribeToMore({
|
|
127
|
+
document: SUB_COMPONENT_CHANGED,
|
|
128
|
+
updateQuery: (prev, { subscriptionData }) => {
|
|
129
|
+
if (!subscriptionData.data) return prev;
|
|
130
|
+
|
|
131
|
+
const prevComponent = prev?.getHost?.get;
|
|
132
|
+
const updatedComponent = subscriptionData?.data?.componentChanged?.component;
|
|
133
|
+
|
|
134
|
+
const isUpdated = updatedComponent && ComponentID.isEqualObj(prevComponent?.id, updatedComponent?.id);
|
|
135
|
+
|
|
136
|
+
if (isUpdated) {
|
|
137
|
+
return {
|
|
138
|
+
...prev,
|
|
139
|
+
getHost: {
|
|
140
|
+
...prev.getHost,
|
|
141
|
+
get: updatedComponent,
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return prev;
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const unsubRemoval = subscribeToMore({
|
|
151
|
+
document: SUB_COMPONENT_REMOVED,
|
|
152
|
+
updateQuery: (prev, { subscriptionData }) => {
|
|
153
|
+
if (!subscriptionData.data) return prev;
|
|
154
|
+
|
|
155
|
+
const prevComponent = prev?.getHost?.get;
|
|
156
|
+
const removedIds: ComponentIdObj[] | undefined = subscriptionData?.data?.componentRemoved?.componentIds;
|
|
157
|
+
if (!prevComponent || !removedIds?.length) return prev;
|
|
158
|
+
|
|
159
|
+
const isRemoved = removedIds.some((removedId) => ComponentID.isEqualObj(removedId, prevComponent.id));
|
|
160
|
+
|
|
161
|
+
if (isRemoved) {
|
|
162
|
+
return {
|
|
163
|
+
...prev,
|
|
164
|
+
getHost: {
|
|
165
|
+
...prev.getHost,
|
|
166
|
+
get: null,
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return prev;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
return () => {
|
|
176
|
+
unsubChanges();
|
|
177
|
+
unsubAddition();
|
|
178
|
+
unsubRemoval();
|
|
179
|
+
};
|
|
180
|
+
}, []);
|
|
181
|
+
|
|
182
|
+
const rawComponent = data?.getHost?.get;
|
|
183
|
+
|
|
184
|
+
return useMemo(() => {
|
|
185
|
+
return {
|
|
186
|
+
component: rawComponent ? ComponentModel.from({ ...rawComponent, host }) : undefined,
|
|
187
|
+
// eslint-disable-next-line
|
|
188
|
+
error: error
|
|
189
|
+
? new ComponentError(500, error.message)
|
|
190
|
+
: !rawComponent && !loading
|
|
191
|
+
? new ComponentError(404)
|
|
192
|
+
: undefined,
|
|
193
|
+
};
|
|
194
|
+
}, [rawComponent, host, error]);
|
|
195
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useRouteMatch } from 'react-router-dom';
|
|
2
|
+
import { ComponentID } from '@teambit/component-id';
|
|
3
|
+
import { useQuery } from '@teambit/ui-foundation.ui.react-router.use-query';
|
|
4
|
+
import { ComponentModel } from './component-model';
|
|
5
|
+
import { ComponentError } from './component-error';
|
|
6
|
+
import { useComponentQuery } from './use-component-query';
|
|
7
|
+
|
|
8
|
+
export type Component = {
|
|
9
|
+
component?: ComponentModel;
|
|
10
|
+
error?: ComponentError;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type ComponentRoute = {
|
|
14
|
+
componentId?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function useComponent(host: string, id?: ComponentID): Component {
|
|
18
|
+
const {
|
|
19
|
+
params: { componentId },
|
|
20
|
+
} = useRouteMatch<ComponentRoute>();
|
|
21
|
+
const query = useQuery();
|
|
22
|
+
const version = query.get('version') || undefined;
|
|
23
|
+
|
|
24
|
+
const targetId = id?.toString({ ignoreVersion: true }) || componentId;
|
|
25
|
+
if (!targetId) throw new TypeError('useComponent received no component id');
|
|
26
|
+
|
|
27
|
+
return useComponentQuery(withVersion(targetId, version), host);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function withVersion(id: string, version?: string) {
|
|
31
|
+
if (!version) return id;
|
|
32
|
+
if (id.includes('@')) return id;
|
|
33
|
+
return `${id}@${version}`;
|
|
34
|
+
}
|