-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDashboard.jsx
177 lines (164 loc) · 4.51 KB
/
Dashboard.jsx
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as React from 'react';
import {
Box,
Button,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
Fab,
makeStyles,
Paper,
TextField,
} from '@material-ui/core';
import EditIcon from '@material-ui/icons/Edit';
import { useEffect, useRef, useState } from 'react';
import { safeLoad } from 'js-yaml';
import { Fs } from '../util/fs';
import { usePromiseGenerator } from '../util/usePromiseGenerator';
import { NctrlValueWidgets } from '../components/*.jsx';
import Typography from '@material-ui/core/Typography';
import { NCTRL_BASE_PATH } from '../util/nctrl';
export const title = 'Dashboard';
export const route = '/dashboard';
export const explanation = `
The dashboard allows you to control parameters of the camera in a convenient and
**set-compatible** way. Here you can set the ISO, exposure time and related parameters.`;
const YAML_PATH = 'dashboard.yml';
const useStyles = makeStyles(theme => ({
add: {
position: 'fixed',
bottom: 0,
right: 0,
margin: '50px',
},
ul: {
listStyle: 'none',
padding: 0,
paddingBottom: 100,
},
notWide: {
maxWidth: 750,
padding: 3,
margin: '10px auto',
marginBottom: '20px',
},
paper: {
padding: '10px 5px',
},
}));
export function Component(props) {
const classes = useStyles();
const [yaml, setYaml] = useState(null);
const file_yml = usePromiseGenerator(() => Fs.of(YAML_PATH).load(), YAML_PATH);
useEffect(() => {
if (yaml === null && file_yml !== undefined) {
setYaml(file_yml);
}
}, [file_yml]);
const parsed = useYaml(yaml) || [];
const [rerenderDep, setRerenderDep] = useState(0);
const rerender = () => setRerenderDep(rerenderDep + 1);
return (
<Box>
{
<EditDashboard
current_yml={yaml}
setYaml={yaml => {
setYaml(yaml);
saveYaml(yaml);
}}
/>
}
<div className={classes.ul}>
{Object.keys(parsed).map((heading, i) => {
return (
<div className={classes.notWide} key={i}>
<Typography variant="h6">{heading}:</Typography>
<Paper className={classes.paper}>
{parsed[heading].map((x, i) => {
const InputWidget =
NctrlValueWidgets[
`NctrlValue${x.widget.replace(/^(.)/, v => v.toUpperCase())}`
];
return (
<InputWidget
key={i}
rerender={rerender}
rerenderDep={rerenderDep}
{...x}
path={`${NCTRL_BASE_PATH}${x.path}`}
/>
);
})}
</Paper>
</div>
);
})}
</div>
</Box>
);
}
function EditDashboard({ current_yml: currentYaml, setYaml }) {
const classes = useStyles();
const [dialogOpen, setDialogOpen] = useState(false);
const inputEl = useRef(null);
return (
<>
<Dialog
open={dialogOpen}
onClose={() => setDialogOpen(false)}
aria-labelledby="form-dialog-title"
>
<DialogTitle id="form-dialog-title">Edit Dashboard</DialogTitle>
<DialogContent>
<DialogContentText>
To edit the dashboard, edit the yml description below. It can also be coppied and shared
(e.g. in the apertus wiki)
</DialogContentText>
<TextField
multiline
fullWidth
variant={'filled'}
label="YAML dashboard description"
defaultValue={currentYaml}
inputRef={inputEl}
/>
</DialogContent>
<DialogActions>
<Button onClick={() => setDialogOpen(false)} color="primary">
Cancel
</Button>
<Button
onClick={() => {
setDialogOpen(false);
setYaml(inputEl.current.value);
}}
color="primary"
>
Apply
</Button>
</DialogActions>
</Dialog>
<Fab
color="primary"
className={classes.add}
aria-label="add"
onClick={() => setDialogOpen(true)}
>
<EditIcon />
</Fab>
</>
);
}
function useYaml(yamlString) {
const [deserialized, setDeserialized] = useState(null);
useEffect(() => {
setDeserialized(safeLoad(yamlString));
}, [yamlString]);
return deserialized;
}
function saveYaml(yamlString) {
Fs.of(YAML_PATH).upload(yamlString);
}