-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathFlowChart.tsx
More file actions
90 lines (82 loc) · 2.53 KB
/
FlowChart.tsx
File metadata and controls
90 lines (82 loc) · 2.53 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
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
import { useCallback, useMemo, useReducer, useRef } from 'react';
import { StyleSheet, View } from 'react-native';
import ChartManager from './ChartManager';
import { Grid } from '@mui/material';
import ChartItem, { Coordinate } from './ChartItem';
import Arrow from './Arrow';
type FlowChartProps = {
chartManager: ChartManager;
};
export default function FlowChart({ chartManager }: FlowChartProps) {
const [, forceUpdate] = useReducer((x: number) => x + 1, 0);
const coordinates = useMemo<Map<number, Coordinate>>(() => new Map(), []);
const rootRef = useRef<View>(null);
const updateCoordinates = useCallback(
(id: number, coordinate: Coordinate) => {
const htmlRootElement = rootRef.current as unknown as HTMLElement;
const root = htmlRootElement.getBoundingClientRect();
if (!root) {
return;
}
// Adjust to root relative positioning
coordinates.set(id, {
x: coordinate.x - root.left,
y: coordinate.y - root.top,
});
forceUpdate();
},
[coordinates]
);
return (
<View style={styles.container} ref={rootRef}>
<Grid container rowGap={4}>
{chartManager.layout?.map((row) => (
<Grid container width={'100%'} spacing={4} key={row.toString()}>
{row
.map((itemId) => chartManager.items[itemId])
.map((item) => (
<ChartItem
key={item.id}
updateCoordinates={updateCoordinates}
item={item}
chartManager={chartManager}
/>
))}
</Grid>
))}
</Grid>
{chartManager.connections.map((connection) => {
// we have all the connections layed out,
// but the user may choose not to use some of the available items,
if (
!coordinates.get(connection.from) ||
!coordinates.get(connection.to)
) {
return <View key={connection.id} />;
}
return (
<Arrow
key={connection.id}
startPoint={{
x: coordinates.get(connection.from).x,
y: coordinates.get(connection.from).y,
}}
endPoint={{
x: coordinates.get(connection.to).x,
y: coordinates.get(connection.to).y,
}}
/>
);
})}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
height: '100%',
paddingHorizontal: 40,
},
});