This repository was archived by the owner on Mar 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraph.tsx
More file actions
47 lines (44 loc) · 1.52 KB
/
Graph.tsx
File metadata and controls
47 lines (44 loc) · 1.52 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
import React, { FC, useEffect } from 'react';
import PropTypes from 'prop-types';
import { ContainerCluster } from '../contexts/ContainerCluster';
import { CurrentCluster } from '../contexts/CurrentCluster';
import { useGraph } from '../hooks/use-graph';
import { useRenderedID } from '../hooks/use-rendered-id';
import { useContainerCluster } from '../hooks/use-container-cluster';
import { DuplicatedRootClusterErrorMessage } from '../errors';
import { useClusterMap } from '../hooks/use-cluster-map';
import { RootClusterProps } from '../types';
/**
* `Graph` component.
*/
export const Graph: FC<RootClusterProps> = ({ children, label, ...options }) => {
const container = useContainerCluster();
if (container !== null) {
throw Error(DuplicatedRootClusterErrorMessage);
}
const renderedLabel = useRenderedID(label);
if (renderedLabel !== undefined) Object.assign(options, { label: renderedLabel });
const graph = useGraph(options);
const clusters = useClusterMap();
useEffect(() => {
if (graph.id !== undefined) {
clusters.set(graph.id, graph);
}
}, [clusters, graph]);
return (
<ContainerCluster.Provider value={graph}>
<CurrentCluster.Provider value={graph}>{children}</CurrentCluster.Provider>
</ContainerCluster.Provider>
);
};
Graph.displayName = 'Graph';
Graph.defaultProps = {
id: undefined,
comment: undefined,
label: undefined,
};
Graph.propTypes = {
id: PropTypes.string,
comment: PropTypes.string,
label: PropTypes.oneOfType([PropTypes.element, PropTypes.string]),
};