-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
settings-global.tsx
200 lines (190 loc) · 7.42 KB
/
settings-global.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { Button, Card, CardActions, CardContent, CardHeader, Collapse, Grid, InputAdornment, MenuItem, Stack } from '@mui/material';
import React from 'react';
import { useForm } from 'react-hook-form';
import { useDispatch, useSelector } from 'react-redux';
import { FormCheckbox, FormInput, FormSwitch } from '@src/components';
import type { Global } from '@src/models';
import { ActionScope, defaultGlobal, InterfaceHeader, NavbarButtonType, ThemeMode } from '@src/models';
import type { StoreState } from '@src/store';
import { syncInterface } from '@src/store/actions';
import { getGlobal } from '@src/store/selectors';
import { useI18n } from '@src/utils';
export const SettingsGlobal = () => {
const i18n = useI18n('panel', 'settings', 'global');
const dispatch = useDispatch();
const state = useSelector<StoreState, Global>(getGlobal);
const {
handleSubmit,
reset,
control,
getValues,
formState: { isValid, isDirty, isSubmitSuccessful },
} = useForm<Global>({
mode: 'onChange',
defaultValues: {
...state,
theme: state.theme ?? defaultGlobal.theme,
actions: state.actions ?? defaultGlobal.actions,
loading: state.loading ?? defaultGlobal.loading,
task: state.task ?? {
progressBar: true,
background: true,
},
navbar: state.navbar ?? defaultGlobal.navbar,
},
});
const onSubmit = (data: Global) => {
dispatch(syncInterface(data));
reset(data);
};
const onSubmitColor = () => {
if (isDirty) return 'primary';
return isSubmitSuccessful ? 'success' : 'info';
};
return (
<Card raised={true}>
<CardHeader
id={InterfaceHeader.global}
title={i18n('title')}
titleTypographyProps={{ variant: 'h6', color: 'text.primary', sx: { textTransform: 'capitalize' } }}
subheader={i18n('subheader')}
subheaderTypographyProps={{ color: 'text.secondary', gutterBottom: true }}
sx={{ p: '1rem 1rem 0' }}
/>
<CardContent>
<CardHeader
title={i18n('theme_title')}
subheader={i18n('theme_subheader')}
titleTypographyProps={{ variant: 'subtitle2' }}
subheaderTypographyProps={{ variant: 'subtitle2' }}
action={
<FormInput
controllerProps={{ name: 'theme', control }}
textFieldProps={{
select: true,
label: i18n('theme_label'),
sx: { flex: '0 0 10rem', textTransform: 'capitalize' },
}}
>
{Object.values(ThemeMode).map(theme => (
<MenuItem key={theme} value={theme} sx={{ textTransform: 'capitalize' }}>
{i18n(theme, 'common', 'model', 'theme_mode')}
</MenuItem>
))}
</FormInput>
}
sx={{ p: '0.5rem 0' }}
/>
<CardHeader
title={i18n('actions_title')}
subheader={i18n('actions_subheader')}
titleTypographyProps={{ variant: 'subtitle2' }}
subheaderTypographyProps={{ variant: 'subtitle2' }}
action={
<FormInput
controllerProps={{ name: 'actions', control }}
textFieldProps={{
select: true,
label: i18n('actions_label'),
sx: { flex: '0 0 10rem', textTransform: 'capitalize' },
}}
>
{Object.values(ActionScope).map(scope => (
<MenuItem key={scope} value={scope} sx={{ textTransform: 'capitalize' }}>
{i18n(scope, 'common', 'model', 'action_scope')}
</MenuItem>
))}
</FormInput>
}
sx={{ p: '0.5rem 0' }}
/>
<CardHeader
title={i18n('loading_title')}
subheader={i18n('loading_subheader')}
titleTypographyProps={{ variant: 'subtitle2' }}
subheaderTypographyProps={{ variant: 'subtitle2' }}
action={<FormSwitch controllerProps={{ name: 'loading.enabled', control }} formControlLabelProps={{ label: '' }} />}
sx={{ p: '0.5rem 0' }}
/>
<Collapse in={getValues()?.loading?.enabled} unmountOnExit={true}>
<CardHeader
title={i18n('loading_threshold_title')}
subheader={i18n('loading_threshold_subheader')}
titleTypographyProps={{ variant: 'subtitle2' }}
subheaderTypographyProps={{ variant: 'subtitle2' }}
action={
<FormInput
controllerProps={{ name: 'loading.threshold', control, rules: { min: 0, max: 60000 } }}
textFieldProps={{
type: 'number',
label: i18n('loading_threshold_label'),
InputProps: {
endAdornment: <InputAdornment position="end">ms</InputAdornment>,
},
disabled: !getValues()?.loading?.enabled,
sx: { flex: '0 0 10rem', ml: '0.5rem' },
}}
/>
}
sx={{ p: '0.5rem 0', mt: '0.5rem' }}
/>
</Collapse>
<CardHeader
title={i18n('task__progress_bar_title')}
subheader={i18n('task__progress_bar_subheader')}
titleTypographyProps={{ variant: 'subtitle2' }}
subheaderTypographyProps={{ variant: 'subtitle2' }}
action={<FormSwitch controllerProps={{ name: 'task.progressBar', control }} formControlLabelProps={{ label: '' }} />}
sx={{ p: '0.5rem 0' }}
/>
<CardHeader
title={i18n('task__background_title')}
subheader={i18n('task__background_subheader')}
titleTypographyProps={{ variant: 'subtitle2' }}
subheaderTypographyProps={{ variant: 'subtitle2' }}
action={<FormSwitch controllerProps={{ name: 'task.background', control }} formControlLabelProps={{ label: '' }} />}
sx={{ p: '0.5rem 0' }}
/>
<CardHeader
title={i18n('navbar__buttons_title')}
titleTypographyProps={{ variant: 'subtitle2' }}
subheader={i18n('navbar__buttons_subheader')}
subheaderTypographyProps={{ variant: 'subtitle2' }}
sx={{ p: '0.5rem 0' }}
/>
<Card sx={{ p: '0.5rem 1rem', m: '0.5rem 0' }}>
<Grid container spacing={1} columnSpacing={1}>
{Object.values(NavbarButtonType).map(t => (
<Grid item xs={4} lg={2} key={t}>
<Button disableTouchRipple={true} sx={{ p: '0 0 0 0.5rem' }}>
<FormCheckbox
controllerProps={{ name: 'navbar.buttons', control }}
formControlLabelProps={{ label: i18n(t, 'common', 'model', 'navbar_button_type'), sx: { textTransform: 'capitalize' } }}
checkboxProps={{
multiple: true,
value: t,
}}
/>
</Button>
</Grid>
))}
</Grid>
</Card>
</CardContent>
<CardActions sx={{ justifyContent: 'flex-end', padding: '0 1.5rem 1.5rem' }}>
<Stack direction="row" spacing={2}>
<Button
variant="outlined"
color={onSubmitColor()}
sx={{ width: '5rem' }}
type="submit"
disabled={!isValid}
onClick={handleSubmit(onSubmit)}
>
{i18n('save', 'common', 'buttons')}
</Button>
</Stack>
</CardActions>
</Card>
);
};