@teambit/graph 0.0.568 → 0.0.572

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.
@@ -0,0 +1,105 @@
1
+ import { Component } from '@teambit/component';
2
+ import { Graph } from 'cleargraph';
3
+
4
+ import { Dependency } from '../model/dependency';
5
+ import { DuplicateDependency, VersionSubgraph } from '../duplicate-dependency';
6
+
7
+ export const DEPENDENCIES_TYPES = ['dependencies', 'devDependencies'];
8
+
9
+ type Node = { id: string; node: Component };
10
+ type Edge = { sourceId: string; targetId: string; edge: Dependency };
11
+
12
+ export class ComponentGraph extends Graph<Component, Dependency> {
13
+ versionMap: Map<string, { allVersionNodes: string[]; latestVersionNode: string }>;
14
+ constructor(nodes: Node[] = [], edges: Edge[] = []) {
15
+ super(nodes, edges);
16
+ this.versionMap = new Map();
17
+ }
18
+
19
+ protected create(nodes: Node[] = [], edges: Edge[] = []): this {
20
+ return new ComponentGraph(nodes, edges) as this;
21
+ }
22
+
23
+ findDuplicateDependencies(): Map<string, DuplicateDependency> {
24
+ const duplicateDependencies: Map<string, DuplicateDependency> = new Map();
25
+ for (const [compFullName, versions] of this.versionMap) {
26
+ if (versions.allVersionNodes.length > 1) {
27
+ const versionSubgraphs: VersionSubgraph[] = [];
28
+ const notLatestVersions = versions.allVersionNodes.filter((version) => version !== versions.latestVersionNode);
29
+ notLatestVersions.forEach((version) => {
30
+ const predecessors = this.predecessorsSubgraph(version);
31
+ const immediatePredecessors = this.predecessors(version).map((predecessor) => predecessor.id);
32
+ const subGraph = this.buildFromCleargraph(predecessors);
33
+ const versionSubgraph: VersionSubgraph = {
34
+ versionId: version,
35
+ subGraph,
36
+ // TODO: validate that this is working correctly
37
+ immediateDependents: immediatePredecessors,
38
+ };
39
+ versionSubgraphs.push(versionSubgraph);
40
+ });
41
+ if (versionSubgraphs.length > 0) {
42
+ const duplicateDep = new DuplicateDependency(versions.latestVersionNode, versionSubgraphs);
43
+ duplicateDependencies.set(compFullName, duplicateDep);
44
+ }
45
+ }
46
+ }
47
+ return duplicateDependencies;
48
+ }
49
+
50
+ buildFromCleargraph(graph: Graph<Component, Dependency>): ComponentGraph {
51
+ // TODO: once cleargraph constructor and graph.nodes are consistent we should just use this line
52
+ // this.create(graph.nodes, graph.edges)
53
+
54
+ const newGraph = new ComponentGraph();
55
+ const newGraphNodes: Node[] = graph.nodes.map((node) => {
56
+ return {
57
+ id: node.id,
58
+ node: node.attr,
59
+ };
60
+ });
61
+ const newGraphEdges: Edge[] = graph.edges.map((edge) => {
62
+ return {
63
+ sourceId: edge.sourceId,
64
+ targetId: edge.targetId,
65
+ edge: edge.attr,
66
+ };
67
+ });
68
+ newGraph.setNodes(newGraphNodes);
69
+ newGraph.setEdges(newGraphEdges);
70
+
71
+ return newGraph;
72
+ }
73
+
74
+ runtimeOnly(componentIds: string[]) {
75
+ return this.successorsSubgraph(componentIds, (edge) => edge.attr.type === 'runtime');
76
+ }
77
+
78
+ _calculateVersionMap() {
79
+ const versionMap: Map<string, { allVersionNodes: string[]; latestVersionNode: string }> = new Map();
80
+ for (const node of this.nodes) {
81
+ const comp = node.attr;
82
+ const compKey = node.id;
83
+ const compFullName = comp.id._legacy.toStringWithoutVersion();
84
+ if (!versionMap.has(compFullName)) {
85
+ versionMap.set(compFullName, {
86
+ allVersionNodes: [compKey],
87
+ latestVersionNode: compKey,
88
+ });
89
+ } else {
90
+ const value = versionMap.get(compFullName);
91
+ if (value) {
92
+ if (Object.prototype.hasOwnProperty.call(value, 'allVersionNodes')) {
93
+ value.allVersionNodes.push(compKey);
94
+ }
95
+ const currentCompVersion = this.node(compKey)?.attr.id._legacy.getVersion();
96
+ const latestCompVersion = this.node(value.latestVersionNode)?.attr.id._legacy.getVersion();
97
+ if (!!currentCompVersion && !!latestCompVersion && currentCompVersion.isLaterThan(latestCompVersion)) {
98
+ value.latestVersionNode = compKey;
99
+ }
100
+ }
101
+ }
102
+ }
103
+ return versionMap;
104
+ }
105
+ }
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line import/no-cycle
2
+ export { ComponentGraph } from './component-graph';
@@ -0,0 +1,41 @@
1
+ import { ComponentType } from 'react';
2
+ import { UIRuntime } from '@teambit/ui';
3
+ import { Slot, SlotRegistry } from '@teambit/harmony';
4
+
5
+ import { ComponentAspect, ComponentUI, ComponentModel } from '@teambit/component';
6
+ import { GraphAspect } from './graph.aspect';
7
+ import { GraphSection } from './ui/graph.section';
8
+
9
+ export interface ComponentWidgetProps extends React.HTMLAttributes<HTMLDivElement> {
10
+ component: ComponentModel;
11
+ }
12
+ export type ComponentWidget = ComponentType<ComponentWidgetProps>;
13
+ export type ComponentWidgetSlot = SlotRegistry<ComponentWidget>;
14
+
15
+ /**
16
+ * Presents dependencies graph in the component page
17
+ */
18
+ export class GraphUI {
19
+ /**
20
+ * adds plugins to component nodes
21
+ * @param value
22
+ */
23
+ registerComponentWidget(value: ComponentWidget) {
24
+ this.componentWidgetSlot.register(value);
25
+ }
26
+
27
+ constructor(private componentWidgetSlot: ComponentWidgetSlot) {}
28
+ static dependencies = [ComponentAspect];
29
+ static runtime = UIRuntime;
30
+ static slots = [Slot.withType<ComponentWidget>()];
31
+ static async provider([componentUI]: [ComponentUI], config, [componentWidgetSlot]: [ComponentWidgetSlot]) {
32
+ const graphUI = new GraphUI(componentWidgetSlot);
33
+ const section = new GraphSection(componentWidgetSlot);
34
+ componentUI.registerNavigation(section.navigationLink, section.order);
35
+ componentUI.registerRoute(section.route);
36
+
37
+ return graphUI;
38
+ }
39
+ }
40
+
41
+ GraphAspect.addRuntime(GraphUI);
@@ -0,0 +1,11 @@
1
+ export type DependencyType = 'dev' | 'peer' | 'runtime';
2
+
3
+ export class Dependency {
4
+ readonly type: DependencyType;
5
+ constructor(type: DependencyType) {
6
+ this.type = type;
7
+ }
8
+ stringify(): string {
9
+ return this.type;
10
+ }
11
+ }
@@ -0,0 +1 @@
1
+ export { Dependency, DependencyType } from './dependency';
@@ -0,0 +1 @@
1
+ export type GraphFilter = 'runtimeOnly' | undefined;
@@ -0,0 +1 @@
1
+ export type { GraphFilter } from './graph-filters';
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/graph",
3
- "version": "0.0.568",
3
+ "version": "0.0.572",
4
4
  "homepage": "https://bit.dev/teambit/component/graph",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.component",
8
8
  "name": "graph",
9
- "version": "0.0.568"
9
+ "version": "0.0.572"
10
10
  },
11
11
  "dependencies": {
12
12
  "@teambit/harmony": "0.2.11",
@@ -23,17 +23,17 @@
23
23
  "@teambit/base-ui.text.muted-text": "1.0.1",
24
24
  "@teambit/evangelist.input.checkbox.label": "1.0.3",
25
25
  "@teambit/documenter.ui.heading": "4.1.1",
26
- "@teambit/component": "0.0.568",
27
- "@teambit/graphql": "0.0.568",
28
- "@teambit/cli": "0.0.394",
29
- "@teambit/ui": "0.0.568",
30
- "@teambit/legacy-bit-id": "0.0.383",
31
- "@teambit/component.ui.deprecation-icon": "0.0.477",
26
+ "@teambit/component": "0.0.572",
27
+ "@teambit/graphql": "0.0.572",
28
+ "@teambit/cli": "0.0.395",
29
+ "@teambit/ui": "0.0.572",
30
+ "@teambit/legacy-bit-id": "0.0.384",
31
+ "@teambit/component.ui.deprecation-icon": "0.0.478",
32
32
  "@teambit/design.ui.styles.ellipsis": "0.0.346",
33
- "@teambit/envs.ui.env-icon": "0.0.471",
33
+ "@teambit/envs.ui.env-icon": "0.0.472",
34
+ "@teambit/ui-foundation.ui.hooks.use-data-query": "0.0.473",
34
35
  "@teambit/design.ui.pages.not-found": "0.0.352",
35
- "@teambit/design.ui.pages.server-error": "0.0.352",
36
- "@teambit/ui-foundation.ui.hooks.use-data-query": "0.0.472"
36
+ "@teambit/design.ui.pages.server-error": "0.0.352"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/react": "^17.0.8",
@@ -49,7 +49,7 @@
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@apollo/client": "^3.0.0",
52
- "@teambit/legacy": "1.0.180",
52
+ "@teambit/legacy": "1.0.181",
53
53
  "react-dom": "^16.8.0 || ^17.0.0",
54
54
  "react": "^16.8.0 || ^17.0.0"
55
55
  },
@@ -77,27 +77,12 @@
77
77
  "react": "-"
78
78
  },
79
79
  "peerDependencies": {
80
- "@teambit/legacy": "1.0.180",
80
+ "@teambit/legacy": "1.0.181",
81
81
  "react-dom": "^16.8.0 || ^17.0.0",
82
82
  "react": "^16.8.0 || ^17.0.0"
83
83
  }
84
84
  }
85
85
  },
86
- "files": [
87
- "dist",
88
- "!dist/tsconfig.tsbuildinfo",
89
- "**/*.md",
90
- "**/*.mdx",
91
- "**/*.js",
92
- "**/*.json",
93
- "**/*.sass",
94
- "**/*.scss",
95
- "**/*.less",
96
- "**/*.css",
97
- "**/*.css",
98
- "**/*.jpeg",
99
- "**/*.gif"
100
- ],
101
86
  "private": false,
102
87
  "engines": {
103
88
  "node": ">=12.22.0"
@@ -0,0 +1,29 @@
1
+ declare module '*.png' {
2
+ const value: any;
3
+ export = value;
4
+ }
5
+ declare module '*.svg' {
6
+ import type { FunctionComponent, SVGProps } from 'react';
7
+
8
+ export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
9
+ const src: string;
10
+ export default src;
11
+ }
12
+
13
+ // @TODO Gilad
14
+ declare module '*.jpg' {
15
+ const value: any;
16
+ export = value;
17
+ }
18
+ declare module '*.jpeg' {
19
+ const value: any;
20
+ export = value;
21
+ }
22
+ declare module '*.gif' {
23
+ const value: any;
24
+ export = value;
25
+ }
26
+ declare module '*.bmp' {
27
+ const value: any;
28
+ export = value;
29
+ }
@@ -0,0 +1,42 @@
1
+ declare module '*.module.css' {
2
+ const classes: { readonly [key: string]: string };
3
+ export default classes;
4
+ }
5
+ declare module '*.module.scss' {
6
+ const classes: { readonly [key: string]: string };
7
+ export default classes;
8
+ }
9
+ declare module '*.module.sass' {
10
+ const classes: { readonly [key: string]: string };
11
+ export default classes;
12
+ }
13
+
14
+ declare module '*.module.less' {
15
+ const classes: { readonly [key: string]: string };
16
+ export default classes;
17
+ }
18
+
19
+ declare module '*.less' {
20
+ const classes: { readonly [key: string]: string };
21
+ export default classes;
22
+ }
23
+
24
+ declare module '*.css' {
25
+ const classes: { readonly [key: string]: string };
26
+ export default classes;
27
+ }
28
+
29
+ declare module '*.sass' {
30
+ const classes: { readonly [key: string]: string };
31
+ export default classes;
32
+ }
33
+
34
+ declare module '*.scss' {
35
+ const classes: { readonly [key: string]: string };
36
+ export default classes;
37
+ }
38
+
39
+ declare module '*.mdx' {
40
+ const component: any;
41
+ export default component;
42
+ }
@@ -0,0 +1,61 @@
1
+ import React, { useContext } from 'react';
2
+ import classnames from 'classnames';
3
+ import { mutedText } from '@teambit/base-ui.text.muted-text';
4
+ import { ComponentID } from '@teambit/component';
5
+ import { DeprecationIcon } from '@teambit/component.ui.deprecation-icon';
6
+ import { EnvIcon } from '@teambit/envs.ui.env-icon';
7
+ import { ellipsis } from '@teambit/design.ui.styles.ellipsis';
8
+ import { Card, CardProps } from '@teambit/base-ui.surfaces.card';
9
+ import { NodeModel } from '../query/node-model';
10
+ import { ComponentGraphContext } from '../dependencies-graph/';
11
+
12
+ // keep order: styles, then variants
13
+ import styles from './component-node.module.scss';
14
+ import variants from './variants.module.scss';
15
+
16
+ export interface ComponentNode extends CardProps {
17
+ node: NodeModel;
18
+ type: string;
19
+ }
20
+
21
+ export function ComponentNode({ node, type = 'defaultNode', ...rest }: ComponentNode) {
22
+ const graphContext = useContext(ComponentGraphContext);
23
+ const { component } = node;
24
+ const { id } = component;
25
+
26
+ return (
27
+ <Card className={classnames(styles.compNode, variants[type])} elevation="none" {...rest}>
28
+ <div className={styles.firstRow}>
29
+ <EnvIcon component={component} className={styles.envIcon} />
30
+ <Breadcrumbs componentId={id} className={mutedText} />
31
+ </div>
32
+ <div className={styles.nameLine}>
33
+ <span className={classnames(styles.name, ellipsis)}>{id.name}</span>
34
+ {id.version && <span className={classnames(styles.version, ellipsis)}>{id.version}</span>}
35
+
36
+ <div className={styles.buffs}>
37
+ <DeprecationIcon component={component} />
38
+ {graphContext &&
39
+ graphContext.componentWidgets
40
+ .toArray()
41
+ .map(([widgetId, Widget]) => <Widget key={widgetId} component={component} />)}
42
+ </div>
43
+ </div>
44
+ </Card>
45
+ );
46
+ }
47
+
48
+ type BreadcrumbsProps = { componentId: ComponentID } & React.HTMLAttributes<HTMLDivElement>;
49
+
50
+ function Breadcrumbs({ componentId, className, ...rest }: BreadcrumbsProps) {
51
+ const { scope, namespace } = componentId;
52
+ const showSep = !!scope && !!namespace;
53
+
54
+ return (
55
+ <div {...rest} className={classnames(styles.breadcrumbs, ellipsis, className)}>
56
+ {scope}
57
+ {showSep && '/'}
58
+ {namespace}
59
+ </div>
60
+ );
61
+ }
@@ -0,0 +1,3 @@
1
+ export { ComponentNode } from './component-node';
2
+
3
+ export { defaultNodeColor, rootNodeColor, externalNodeColor } from './variants';
@@ -0,0 +1,5 @@
1
+ import variants from './variants.module.scss';
2
+
3
+ const { root, defaultNode, rootNodeColor, defaultNodeColor, externalNodeColor } = variants;
4
+
5
+ export { root, defaultNode, rootNodeColor, defaultNodeColor, externalNodeColor };
@@ -0,0 +1,47 @@
1
+ import { useMemo } from 'react';
2
+ import { Node, Edge, ArrowHeadType } from 'react-flow-renderer';
3
+ import { ComponentID } from '@teambit/component';
4
+ import { calcLayout } from './calc-layout';
5
+ import { GraphModel } from '../query';
6
+
7
+ import { depTypeToClass, depTypeToLabel } from './dep-edge';
8
+
9
+ type ElementsOptions = {
10
+ rootNode?: ComponentID;
11
+ };
12
+
13
+ /**
14
+ * generate Nodes and Edges for the ReactFlowRenderer graph renderer
15
+ */
16
+ export function calcElements(graph: GraphModel | undefined, { rootNode }: ElementsOptions) {
17
+ return useMemo(() => {
18
+ if (!graph) return [];
19
+
20
+ const positions = calcLayout(graph);
21
+
22
+ const nodes: Node[] = Array.from(graph.nodes.values()).map((x) => {
23
+ return {
24
+ id: x.id,
25
+ type: 'ComponentNode',
26
+ data: {
27
+ node: x,
28
+ type: rootNode && x.component.id.isEqual(rootNode, { ignoreVersion: true }) ? 'root' : undefined,
29
+ },
30
+ position: positions.get(x.id) || { x: 0, y: 0 },
31
+ };
32
+ });
33
+
34
+ const edges: Edge[] = graph.edges.map((e) => ({
35
+ id: `_${e.sourceId}__${e.targetId}`,
36
+ source: e.sourceId,
37
+ target: e.targetId,
38
+ label: depTypeToLabel(e.dependencyLifecycleType),
39
+ labelBgPadding: [4, 4],
40
+ type: 'smoothstep',
41
+ className: depTypeToClass(e.dependencyLifecycleType),
42
+ arrowHeadType: ArrowHeadType.Arrow,
43
+ }));
44
+
45
+ return [...nodes, ...edges];
46
+ }, [graph]);
47
+ }
@@ -0,0 +1,36 @@
1
+ import dagre, { graphlib } from 'dagre';
2
+ import { GraphModel } from '../query';
3
+
4
+ const NODE_WIDTH = 260;
5
+ const NODE_HEIGHT = 90;
6
+
7
+ const BOTTOM_TO_TOP = 'BT';
8
+
9
+ /**
10
+ * calculate the specific location of each node in the graph
11
+ */
12
+ export function calcLayout(graph: GraphModel) {
13
+ const g = new graphlib.Graph();
14
+ g.setGraph({ rankdir: BOTTOM_TO_TOP });
15
+ g.setDefaultEdgeLabel(() => ({}));
16
+
17
+ // make a new instance of { width, height } per node, or dagre will get confused and place all nodes in the same spot
18
+ graph.nodes.forEach((n) => g.setNode(n.id, { width: NODE_WIDTH, height: NODE_HEIGHT }));
19
+ graph.edges.forEach((e) => g.setEdge({ v: e.sourceId, w: e.targetId }));
20
+
21
+ // position items in graph
22
+ dagre.layout(g);
23
+
24
+ const positionsArr: [string, { x: number; y: number }][] = g.nodes().map((nodeId) => {
25
+ const node = g.node(nodeId);
26
+
27
+ const pos = {
28
+ x: node.x - node.width / 2,
29
+ y: node.y - node.height / 2,
30
+ };
31
+
32
+ return [nodeId, pos];
33
+ });
34
+
35
+ return new Map(positionsArr);
36
+ }
@@ -0,0 +1,28 @@
1
+ import { EdgeType } from '@teambit/graph';
2
+ import edgeStyles from './edge.module.scss';
3
+
4
+ export function depTypeToClass(depType: string) {
5
+ switch (depType) {
6
+ case 'DEV':
7
+ return edgeStyles.dev;
8
+ case 'PEER':
9
+ return edgeStyles.peer;
10
+ case 'RUNTIME':
11
+ return edgeStyles.runtime;
12
+ default:
13
+ return undefined;
14
+ }
15
+ }
16
+
17
+ export function depTypeToLabel(type: EdgeType) {
18
+ switch (type) {
19
+ case EdgeType.peer:
20
+ return 'Peer Dependency';
21
+ case EdgeType.dev:
22
+ return 'Development Dependency';
23
+ case EdgeType.runtime:
24
+ return 'Dependency';
25
+ default:
26
+ return (type as string).toLowerCase();
27
+ }
28
+ }
@@ -0,0 +1 @@
1
+ export { depTypeToClass, depTypeToLabel } from './dep-edge';
@@ -0,0 +1,98 @@
1
+ import React, { useMemo, useCallback, useRef, useEffect } from 'react';
2
+ import classnames from 'classnames';
3
+ import ReactFlow, {
4
+ Controls,
5
+ Background,
6
+ MiniMap,
7
+ OnLoadParams,
8
+ NodeTypesType,
9
+ Handle,
10
+ Position,
11
+ NodeProps,
12
+ ReactFlowProps,
13
+ } from 'react-flow-renderer';
14
+ import { ComponentID } from '@teambit/component';
15
+
16
+ import { ComponentWidgetSlot } from '../../graph.ui.runtime';
17
+ import { ComponentNode } from '../component-node';
18
+ import { GraphModel } from '../query';
19
+ import { calcElements } from './calc-elements';
20
+ import { calcMinimapColors } from './minimap';
21
+ import { ComponentGraphContext } from './graph-context';
22
+
23
+ import styles from './dependencies-graph.module.scss';
24
+
25
+ function ComponentNodeContainer(props: NodeProps) {
26
+ const { sourcePosition = Position.Top, targetPosition = Position.Bottom, data, id } = props;
27
+
28
+ return (
29
+ <div key={id}>
30
+ <Handle type="target" position={targetPosition} isConnectable={false} />
31
+ <Handle type="source" position={sourcePosition} isConnectable={false} />
32
+ <ComponentNode node={data.node} type={data.type} />
33
+ </div>
34
+ );
35
+ }
36
+
37
+ const NodeTypes: NodeTypesType = { ComponentNode: ComponentNodeContainer };
38
+
39
+ export type DependenciesGraphProps = {
40
+ rootNode: ComponentID;
41
+ graph: GraphModel;
42
+ componentWidgets: ComponentWidgetSlot;
43
+ onLoad?: (instance: OnLoadParams) => void;
44
+ } & Omit<ReactFlowProps, 'elements'>;
45
+
46
+ export function DependenciesGraph({
47
+ graph,
48
+ rootNode,
49
+ componentWidgets,
50
+ className,
51
+ onLoad,
52
+ children,
53
+ ...rest
54
+ }: DependenciesGraphProps) {
55
+ const graphRef = useRef<OnLoadParams>();
56
+ const elements = calcElements(graph, { rootNode });
57
+ const context = useMemo(() => ({ componentWidgets }), [componentWidgets]);
58
+
59
+ const handleLoad = useCallback(
60
+ (instance: OnLoadParams) => {
61
+ graphRef.current = instance;
62
+ instance.fitView();
63
+ onLoad?.(instance);
64
+ },
65
+ [onLoad]
66
+ );
67
+
68
+ // clear ref on unmount
69
+ useEffect(() => () => (graphRef.current = undefined), []);
70
+
71
+ useEffect(() => {
72
+ graphRef.current?.fitView();
73
+ }, [graph]);
74
+
75
+ return (
76
+ <ComponentGraphContext.Provider value={context}>
77
+ <ReactFlow
78
+ draggable={false}
79
+ nodesDraggable={true}
80
+ selectNodesOnDrag={false}
81
+ nodesConnectable={false}
82
+ zoomOnDoubleClick={false}
83
+ elementsSelectable={false}
84
+ maxZoom={1}
85
+ {...rest}
86
+ className={classnames(styles.graph, className)}
87
+ elements={elements}
88
+ nodeTypes={NodeTypes}
89
+ onLoad={handleLoad}
90
+ >
91
+ <Background />
92
+ <Controls className={styles.controls} />
93
+ <MiniMap nodeColor={calcMinimapColors} className={styles.minimap} />
94
+ {children}
95
+ </ReactFlow>
96
+ </ComponentGraphContext.Provider>
97
+ );
98
+ }
@@ -0,0 +1,9 @@
1
+ import { createContext } from 'react';
2
+ import { ComponentWidgetSlot } from '../../graph.ui.runtime';
3
+
4
+ /** internal context, to pass shared information to all nodes */
5
+ export type ComponentGraph = {
6
+ componentWidgets: ComponentWidgetSlot;
7
+ };
8
+
9
+ export const ComponentGraphContext = createContext<ComponentGraph | undefined>(undefined);
@@ -0,0 +1,3 @@
1
+ export { DependenciesGraph } from './dependencies-graph';
2
+ export { ComponentGraphContext, ComponentGraph } from './graph-context';
3
+ export type { DependenciesGraphProps } from './dependencies-graph';
@@ -0,0 +1,15 @@
1
+ import { Node } from 'react-flow-renderer';
2
+ import { rootNodeColor, defaultNodeColor, externalNodeColor } from '../component-node';
3
+
4
+ export function calcMinimapColors(node: Node) {
5
+ const type = node.data?.type;
6
+
7
+ switch (type) {
8
+ case 'root':
9
+ return rootNodeColor;
10
+ case 'external':
11
+ return externalNodeColor;
12
+ default:
13
+ return defaultNodeColor;
14
+ }
15
+ }
@@ -0,0 +1,27 @@
1
+ import React from 'react';
2
+ import { Card, CardProps } from '@teambit/base-ui.surfaces.card';
3
+ import { CheckboxLabel } from '@teambit/evangelist.input.checkbox.label';
4
+
5
+ type GraphFilters = {
6
+ isFiltered: boolean;
7
+ onChangeFilter: (isFiltered: boolean) => void;
8
+ disable?: boolean;
9
+ } & CardProps;
10
+
11
+ export function GraphFilters({ onChangeFilter, isFiltered, disable, ...rest }: GraphFilters) {
12
+ return (
13
+ <Card {...rest}>
14
+ <div>
15
+ {/* show non-runtime === !isFiltered */}
16
+ <CheckboxLabel
17
+ checked={!isFiltered}
18
+ disabled={disable}
19
+ onInputChanged={(e) => onChangeFilter(!e.target.checked)}
20
+ >
21
+ {' '}
22
+ show non-runtime
23
+ </CheckboxLabel>
24
+ </div>
25
+ </Card>
26
+ );
27
+ }
@@ -0,0 +1,54 @@
1
+ import React, { useContext, useState } from 'react';
2
+
3
+ import { H2 } from '@teambit/documenter.ui.heading';
4
+ import { NotFoundPage } from '@teambit/design.ui.pages.not-found';
5
+ import { ServerErrorPage } from '@teambit/design.ui.pages.server-error';
6
+ import { ComponentContext } from '@teambit/component';
7
+ import { FullLoader } from '@teambit/legacy/dist/to-eject/full-loader';
8
+
9
+ import { useGraphQuery } from '../query';
10
+ import { DependenciesGraph } from '../dependencies-graph';
11
+ import { ComponentWidgetSlot } from '../../graph.ui.runtime';
12
+ import type { GraphFilter } from '../../model/graph-filters';
13
+
14
+ import { GraphFilters } from './graph-filters';
15
+
16
+ import styles from './graph-page.module.scss';
17
+
18
+ type GraphPageProps = {
19
+ componentWidgets: ComponentWidgetSlot;
20
+ };
21
+
22
+ export function GraphPage({ componentWidgets }: GraphPageProps) {
23
+ const component = useContext(ComponentContext);
24
+
25
+ const [filter, setFilter] = useState<GraphFilter>('runtimeOnly');
26
+ const onCheckFilter = (isFiltered: boolean) => {
27
+ setFilter(isFiltered ? 'runtimeOnly' : undefined);
28
+ };
29
+
30
+ const { graph, error, loading } = useGraphQuery([component.id.toString()], filter);
31
+ if (error) return error.code === 404 ? <NotFoundPage /> : <ServerErrorPage />;
32
+ if (!graph) return <FullLoader />;
33
+
34
+ const isFiltered = filter === 'runtimeOnly';
35
+
36
+ return (
37
+ <div className={styles.page}>
38
+ <H2 size="xs">Component Dependencies</H2>
39
+ <DependenciesGraph
40
+ componentWidgets={componentWidgets}
41
+ graph={graph}
42
+ rootNode={component.id}
43
+ className={styles.graph}
44
+ >
45
+ <GraphFilters
46
+ className={styles.filters}
47
+ disable={loading}
48
+ isFiltered={isFiltered}
49
+ onChangeFilter={onCheckFilter}
50
+ />
51
+ </DependenciesGraph>
52
+ </div>
53
+ );
54
+ }
@@ -0,0 +1 @@
1
+ export { GraphPage } from './graph-page';
@@ -0,0 +1,19 @@
1
+ import { Section } from '@teambit/component';
2
+ import React from 'react';
3
+
4
+ import { GraphPage } from './graph-page';
5
+ import { ComponentWidgetSlot } from '../graph.ui.runtime';
6
+
7
+ export class GraphSection implements Section {
8
+ constructor(private componentWidgetSlot: ComponentWidgetSlot) {}
9
+
10
+ route = {
11
+ path: '~dependencies',
12
+ children: <GraphPage componentWidgets={this.componentWidgetSlot} />,
13
+ };
14
+ navigationLink = {
15
+ href: '~dependencies',
16
+ children: 'Dependencies',
17
+ };
18
+ order = 40;
19
+ }
@@ -0,0 +1,16 @@
1
+ import { EdgeType } from '@teambit/graph';
2
+ import { RawEdge } from './get-graph.query';
3
+
4
+ export class EdgeModel {
5
+ sourceId: string;
6
+ targetId: string;
7
+ dependencyLifecycleType: EdgeType;
8
+
9
+ static from(rawEdge: RawEdge) {
10
+ const edge = new EdgeModel();
11
+ edge.sourceId = rawEdge.sourceId;
12
+ edge.targetId = rawEdge.targetId;
13
+ edge.dependencyLifecycleType = rawEdge.dependencyLifecycleType;
14
+ return edge;
15
+ }
16
+ }
@@ -0,0 +1,73 @@
1
+ import { gql } from '@apollo/client';
2
+ import { EdgeType } from '@teambit/graph';
3
+
4
+ // please update types when updating query, for added safety
5
+
6
+ export const GET_GRAPH = gql`
7
+ query graph($ids: [String], $filter: String) {
8
+ graph(ids: $ids, filter: $filter) {
9
+ nodes {
10
+ id
11
+ component {
12
+ id {
13
+ name
14
+ version
15
+ scope
16
+ }
17
+ displayName
18
+
19
+ deprecation {
20
+ isDeprecate
21
+ }
22
+
23
+ env {
24
+ id
25
+ icon
26
+ }
27
+ }
28
+ }
29
+ edges {
30
+ sourceId
31
+ targetId
32
+ dependencyLifecycleType
33
+ }
34
+ }
35
+ }
36
+ `;
37
+
38
+ export type RawGraphQuery = {
39
+ graph: RawGraph;
40
+ };
41
+
42
+ export type RawGraph = {
43
+ nodes: RawNode[];
44
+ edges: [];
45
+ };
46
+
47
+ export type RawNode = {
48
+ id: string;
49
+ component: {
50
+ id: {
51
+ name: string;
52
+ scope: string;
53
+ version: string;
54
+ };
55
+
56
+ displayName: string;
57
+
58
+ deprecation: {
59
+ isDeprecate: boolean;
60
+ };
61
+
62
+ env: {
63
+ id: string;
64
+ icon: string;
65
+ };
66
+ };
67
+ };
68
+
69
+ export type RawEdge = {
70
+ sourceId: string;
71
+ targetId: string;
72
+ dependencyLifecycleType: EdgeType;
73
+ };
@@ -0,0 +1,12 @@
1
+ import { RawGraph } from './get-graph.query';
2
+ import { NodeModel } from './node-model';
3
+ import { EdgeModel } from './edge-model';
4
+
5
+ export class GraphModel {
6
+ constructor(public nodes: NodeModel[], public edges: EdgeModel[]) {}
7
+ static from(rawGraph: RawGraph) {
8
+ const nodes = rawGraph.nodes.map(NodeModel.from);
9
+ const edges = rawGraph.edges.map(EdgeModel.from);
10
+ return new GraphModel(nodes, edges);
11
+ }
12
+ }
@@ -0,0 +1,6 @@
1
+ export { useGraphQuery } from './use-graph-query';
2
+ export { useGraph } from './use-graph';
3
+
4
+ export { GraphModel } from './graph-model';
5
+ export { EdgeModel } from './edge-model';
6
+ export { NodeModel } from './node-model';
@@ -0,0 +1,16 @@
1
+ import { ComponentModel } from '@teambit/component';
2
+ import { RawNode } from './get-graph.query';
3
+
4
+ export class NodeModel {
5
+ id: string;
6
+ component: ComponentModel;
7
+
8
+ static from(rawNode: RawNode) {
9
+ const node = new NodeModel();
10
+ node.id = rawNode.id;
11
+ // @TODO - component model should not expect all fields to have values
12
+ // @ts-ignore
13
+ node.component = ComponentModel.from(rawNode.component);
14
+ return node;
15
+ }
16
+ }
@@ -0,0 +1,29 @@
1
+ import { useMemo } from 'react';
2
+ import { useDataQuery } from '@teambit/ui-foundation.ui.hooks.use-data-query';
3
+ import { GraphQlError } from '@teambit/graphql';
4
+ import { GET_GRAPH, RawGraphQuery } from './get-graph.query';
5
+ import { GraphModel } from './graph-model';
6
+
7
+ type QueryVariables = {
8
+ ids: string[];
9
+ filter?: string;
10
+ };
11
+
12
+ /** provides dependencies graph data from graphQL */
13
+ export function useGraphQuery(componentId: string[], filter?: string) {
14
+ const { data, error, loading } = useDataQuery<RawGraphQuery, QueryVariables>(GET_GRAPH, {
15
+ variables: { ids: componentId, filter },
16
+ });
17
+
18
+ const rawGraph = data?.graph;
19
+ const clientError = !rawGraph && !loading ? new GraphQlError(404) : undefined;
20
+ const serverError = error ? new GraphQlError(500, error.message) : undefined;
21
+
22
+ return useMemo(() => {
23
+ return {
24
+ graph: rawGraph ? GraphModel.from(rawGraph) : undefined,
25
+ error: serverError || clientError,
26
+ loading,
27
+ };
28
+ }, [rawGraph, error]);
29
+ }
@@ -0,0 +1,12 @@
1
+ import { useRouteMatch } from 'react-router-dom';
2
+
3
+ import { useGraphQuery } from './use-graph-query';
4
+
5
+ export function useGraph() {
6
+ const {
7
+ // @ts-ignore
8
+ params: { componentId },
9
+ } = useRouteMatch();
10
+
11
+ return useGraphQuery([componentId]);
12
+ }