-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainView.tsx
More file actions
96 lines (88 loc) · 3.02 KB
/
MainView.tsx
File metadata and controls
96 lines (88 loc) · 3.02 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
91
92
93
94
95
96
import React, { useState, useMemo } from "react";
import getDataTypes from "../api/getDataTypes";
import DeviceFilterLogic from "../components/DeviceFilterLogic";
import Diagram from "../components/Diagram";
import TimeRangeRow from "../components/TimeRangeRow";
import { FlexColumn } from "../elements/Flex";
import GridContainer from "../elements/GridContainer";
import Label from "../elements/Label";
import Switch from "../elements/Switch";
import useQuery from "../hooks/useQuery";
const REFRESH_FREQUENCY = 10;
const MainView = () => {
const { responseData: dataTypes, error, isLoading } = useQuery({
query: getDataTypes,
});
// 0 is false
const [autoRefreshPeriod, setAutoRefreshPeriod] = useState<number>(0);
// empty list state means are all selected
const [selectedDevices, setSelectedDevices] = useState<string[]>([]);
// set default start time so the app
// wouldn't go refreshing ALL the measurements on each refrash
const initialStartTime = useMemo(
() => new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(),
[]
);
// undefined time means no filter applied
const [startTime, setStartTime] = useState<string | undefined>(
initialStartTime
);
const [endTime, setEndTime] = useState<string | undefined>(undefined);
// color map determines which device is which color on diagram (key: device, value: rgb())
const [deviceColorMap, setDeviceColorMap] = useState(
new Map<string, string>()
);
return (
<FlexColumn style={{ overflowX: "hidden" }}>
<TimeRangeRow
startTime={startTime}
endTime={endTime}
setStartTime={setStartTime}
setEndTime={setEndTime}
/>
<DeviceFilterLogic
selectedDevices={selectedDevices}
setSelectedDevices={setSelectedDevices}
setDeviceColorMap={setDeviceColorMap}
/>
<Switch
checked={!!autoRefreshPeriod}
onChange={() =>
setAutoRefreshPeriod((currentPeriod) =>
!!currentPeriod ? 0 : REFRESH_FREQUENCY
)
}
label={`Automatically refresh measurements every ${REFRESH_FREQUENCY} seconds`}
/>
<GridContainer spacing="d2">
{isLoading ? (
<Label>Loading data types...</Label>
) : error ? (
<Label>Error loading data types.</Label>
) : dataTypes && dataTypes.length ? (
<>
{dataTypes.map((dataType) => {
return (
<Diagram
deviceColorMap={deviceColorMap}
key={dataType.id}
dataType={dataType}
autoRefreshPeriod={autoRefreshPeriod}
queryParams={{
dataTypes: [dataType.id],
devices: selectedDevices,
startTime: startTime,
endTime: endTime,
}}
/>
);
})}
</>
) : (
<Label>No data types found.</Label>
)}
</GridContainer>
</FlexColumn>
);
};
export default MainView;