|
1 | 1 | import { useMemo, useEffect } from 'react'; |
2 | | -import { Graph, RootClusterAttributes } from 'ts-graphviz'; |
| 2 | +import { Graph, RootClusterAttributes, NodeAttributes, EdgeAttributes, ClusterSubgraphAttributes } from 'ts-graphviz'; |
3 | 3 | import { useGraphvizContext } from './use-graphviz-context'; |
| 4 | +import { ClusterAttributesProps, useClusterAttributes } from './use-cluster-attributes'; |
| 5 | +import { useHasComment } from './use-comment'; |
4 | 6 |
|
5 | 7 | export type GraphProps = { |
6 | 8 | id?: string; |
7 | 9 | comment?: string; |
8 | | -} & RootClusterAttributes; |
| 10 | + edge?: EdgeAttributes; |
| 11 | + node?: NodeAttributes; |
| 12 | + graph?: ClusterSubgraphAttributes; |
| 13 | +} & RootClusterAttributes & |
| 14 | + ClusterAttributesProps; |
9 | 15 |
|
10 | | -export const useGraph = ({ id, comment, ...attributes }: GraphProps = {}): Graph => { |
| 16 | +export const useGraph = ({ id, comment, edge, node, graph, ...attributes }: GraphProps = {}): Graph => { |
11 | 17 | const context = useGraphvizContext(); |
12 | | - const graph = useMemo(() => { |
| 18 | + const memoGraph = useMemo(() => { |
13 | 19 | const g = new Graph(context, id); |
14 | 20 | g.comment = comment; |
15 | 21 | g.apply(attributes); |
| 22 | + g.attributes.node.apply(node ?? {}); |
| 23 | + g.attributes.edge.apply(edge ?? {}); |
| 24 | + g.attributes.graph.apply(graph ?? {}); |
16 | 25 | return g; |
17 | | - }, [context, id, comment, attributes]); |
| 26 | + }, [context, id, comment, edge, node, graph, attributes]); |
| 27 | + useHasComment(memoGraph, comment); |
| 28 | + useClusterAttributes(memoGraph, attributes, { edge, node, graph }); |
18 | 29 | useEffect(() => { |
19 | 30 | return (): void => { |
20 | 31 | context.root = undefined; |
21 | 32 | }; |
22 | 33 | }, [context]); |
23 | | - |
24 | | - useEffect(() => { |
25 | | - graph.clear(); |
26 | | - graph.apply(attributes); |
27 | | - }, [graph, attributes]); |
28 | | - |
29 | | - useEffect(() => { |
30 | | - graph.comment = comment; |
31 | | - }, [graph, comment]); |
32 | | - return graph; |
| 34 | + return memoGraph; |
33 | 35 | }; |
0 commit comments