/
WorkerLayoutControl.tsx
134 lines (119 loc) · 3.12 KB
/
WorkerLayoutControl.tsx
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
import React, { useEffect, CSSProperties } from "react";
import { useSigma } from "@react-sigma/core";
import { ReactComponent as StartLayoutIcon } from "./assets/icons/play-solid.svg";
import { ReactComponent as StopLayoutIcon } from "./assets/icons/stop-solid.svg";
import { LayoutWorkerHook } from "./useWorkerLayoutFactory";
type WorkerLayoutLabelKeys = "start" | "stop";
/**
* Properties for `WorkerLayoutControl` component
*/
export interface WorkerLayoutControlProps<T> {
/**
* HTML id
*/
id?: string;
/**
* HTML class
*/
className?: string;
/**
* HTML CSS style
*/
style?: CSSProperties;
/**
* The layout hook to use
*/
layout: LayoutWorkerHook<T>;
/**
* Settings of the layout.
*/
settings: T;
/**
* Option to tell what we do when the component is mounted
* - <code>-1</code> means that we do nothing (it's the same as no value)
* - <code>0</code> means that we start the algo (and don't auto stop it)
* - <code>X</code> mans that we start the algo, and stop it after X milliseconds
*/
autoRunFor?: number;
/**
* It's possible to customize the button, by passing to JSX Element.
* First one is for the "start layout", and the second to "stop layout".
* Example :
* ```jsx
* <WorkerLayoutControl>
* <>
* <span>Start</span>
* <span>Stop</span>
* </>
* </WorkerLayoutControl>
* ```
*/
children?: [JSX.Element, JSX.Element];
/**
* Map of the labels we use in the component.
* This is usefull for I18N
*/
labels?: { [Key in WorkerLayoutLabelKeys]?: string };
}
export function WorkerLayoutControl<T>({
id,
className,
style,
layout,
settings,
autoRunFor,
children,
labels = {},
}: WorkerLayoutControlProps<T>) {
// Get Sigma
const sigma = useSigma();
// Get layout
const { stop, start, isRunning } = layout(settings);
// Common html props for the div
const props = {
className: `react-sigma-control ${className || ""}`,
id,
style,
};
/**
* Init component when Sigma or component settings change.
*/
useEffect(() => {
if (!sigma) {
return;
}
// we run the algo
let timeout: number | null = null;
if (autoRunFor !== undefined && autoRunFor > -1 && sigma.getGraph().order > 0) {
start();
// set a timeout to stop it
timeout =
autoRunFor > 0
? window.setTimeout(() => {
stop();
}, autoRunFor)
: null;
}
//cleaning
return () => {
if (timeout) {
clearTimeout(timeout);
}
};
}, [autoRunFor, start, stop, sigma]);
return (
<div {...props}>
<button
onClick={() => (isRunning ? stop() : start())}
title={
isRunning ? labels["stop"] || "Stop the layout animation" : labels["start"] || "Start the layout animation"
}
>
{children && !isRunning && children[0]}
{children && isRunning && children[1]}
{!children && !isRunning && <StartLayoutIcon style={{ width: "1em" }} />}
{!children && isRunning && <StopLayoutIcon style={{ width: "1em" }} />}
</button>
</div>
);
}