generated from mantinedev/next-pages-template
-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
DnsHoleControls.tsx
306 lines (283 loc) · 8.66 KB
/
DnsHoleControls.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import {
ActionIcon,
Badge,
Box,
Button,
Card,
Center,
Flex,
Group,
Image,
Stack,
Text,
Title,
Tooltip,
UnstyledButton,
} from '@mantine/core';
import { useDisclosure } from '@mantine/hooks';
import {
IconClockPause,
IconDeviceGamepad,
IconPlayerPlay,
IconPlayerStop,
} from '@tabler/icons-react';
import { useSession } from 'next-auth/react';
import { useTranslation } from 'next-i18next';
import { useState } from 'react';
import { useConfigContext } from '~/config/provider';
import { api } from '~/utils/api';
import { defineWidget } from '../helper';
import { WidgetLoading } from '../loading';
import { IWidget } from '../widgets';
import { useDnsHoleSummeryQuery } from './DnsHoleSummary';
import { TimerModal } from './TimerModal';
const definition = defineWidget({
id: 'dns-hole-controls',
icon: IconDeviceGamepad,
options: {
showToggleAllButtons: {
type: 'switch',
defaultValue: true,
},
},
gridstack: {
minWidth: 2,
minHeight: 1,
maxWidth: 12,
maxHeight: 12,
},
component: DnsHoleControlsWidgetTile,
});
export type IDnsHoleControlsWidget = IWidget<(typeof definition)['id'], typeof definition>;
interface DnsHoleControlsWidgetProps {
widget: IDnsHoleControlsWidget;
}
/**
*
* @param fetching - a expression that return a boolean if the data is been fetched
* @param currentStatus the current status of the dns integration, either enabled or disabled
* @returns
*/
const dnsLightStatus = (
fetching: boolean,
currentStatus: 'enabled' | 'disabled'
): 'blue' | 'green' | 'red' => {
if (fetching) {
return 'blue';
}
if (currentStatus === 'enabled') {
return 'green';
}
return 'red';
};
function DnsHoleControlsWidgetTile({ widget }: DnsHoleControlsWidgetProps) {
const { data: sessionData } = useSession();
const [opened, { close, open }] = useDisclosure(false);
const [appId, setAppId] = useState('');
const { isInitialLoading, data, isFetching: fetchingDnsSummary } = useDnsHoleSummeryQuery();
const { mutateAsync, isLoading: changingStatus } = useDnsHoleControlMutation();
const { t } = useTranslation(['common', 'modules/dns-hole-controls']);
const enableControls = sessionData?.user.isAdmin ?? false;
const { name: configName, config } = useConfigContext();
const trpcUtils = api.useUtils();
if (isInitialLoading || !data || !configName) {
return <WidgetLoading />;
}
if (data.status.length === 0) {
return (
<Center h="100%">
<Stack align="center">
<IconDeviceGamepad size={40} strokeWidth={1} />
<Title align="center" order={6}>
{t('modules/dns-hole-controls:descriptor.errors.general.title')}
</Title>
<Text align="center">
{t('modules/dns-hole-controls:descriptor.errors.general.text')}
</Text>
</Stack>
</Center>
);
}
type getDnsStatusAcc = {
enabled: string[];
disabled: string[];
};
const getDnsStatus = () => {
const dnsList = data?.status.reduce(
(acc: getDnsStatusAcc, dns) => {
if (dns.status === 'enabled') {
acc.enabled.push(dns.appId);
} else if (dns.status === 'disabled') {
acc.disabled.push(dns.appId);
}
return acc;
},
{ enabled: [], disabled: [] }
);
if (dnsList.enabled.length === 0 && dnsList.disabled.length === 0) {
return undefined;
}
return dnsList;
};
const toggleDns = async (
action: 'enable' | 'disable',
appsToChange?: string[],
hours: number = 0,
minutes: number = 0
) => {
const duration = hours * 3600 + minutes * 60;
await mutateAsync(
{
action,
duration,
configName,
appsToChange,
},
{
onSettled: () => {
trpcUtils.dnsHole.summary.invalidate();
},
}
);
setAppId('');
};
return (
<Stack h="100%" spacing="0.25rem">
{enableControls && widget.properties.showToggleAllButtons && (
<Flex gap="xs">
<Tooltip label={t('enableAll')}>
<Button
onClick={() => toggleDns('enable', getDnsStatus()?.disabled)}
disabled={
getDnsStatus()?.disabled.length === 0 || fetchingDnsSummary || changingStatus
}
variant="light"
color="green"
fullWidth
h="2rem"
>
<IconPlayerPlay size={20} />
</Button>
</Tooltip>
<Tooltip label={t('setTimer')}>
<Button
onClick={open}
disabled={
getDnsStatus()?.enabled.length === 0 || fetchingDnsSummary || changingStatus
}
variant="light"
color="yellow"
fullWidth
h="2rem"
>
<IconClockPause size={20} />
</Button>
</Tooltip>
<Tooltip label={t('disableAll')}>
<Button
onClick={() => toggleDns('disable', getDnsStatus()?.enabled)}
disabled={
getDnsStatus()?.enabled.length === 0 || fetchingDnsSummary || changingStatus
}
variant="light"
color="red"
fullWidth
h="2rem"
>
<IconPlayerStop size={20} />
</Button>
</Tooltip>
</Flex>
)}
<TimerModal
toggleDns={toggleDns}
getDnsStatus={getDnsStatus}
opened={opened}
close={close}
appId={appId}
/>
<Stack
spacing="0.25rem"
display="flex"
style={{
flex: '1',
justifyContent:
enableControls && widget.properties.showToggleAllButtons ? 'flex-end' : 'space-evenly',
}}
>
{data.status.map((dnsHole, index) => {
const app = config?.apps.find((x) => x.id === dnsHole.appId);
if (!app) {
return null;
}
return (
<Card withBorder={true} key={dnsHole.appId} p="xs" radius="md">
<Group>
<Box
sx={(theme) => ({
backgroundColor:
theme.colorScheme === 'dark' ? theme.colors.dark[4] : theme.colors.gray[2],
textAlign: 'center',
padding: 5,
borderRadius: theme.radius.md,
})}
>
<Image src={app.appearance.iconUrl} width={40} height={40} fit="contain" />
</Box>
<Stack spacing="0rem">
<Text>{app.name}</Text>
<Flex direction="row" gap="md">
<UnstyledButton
onClick={() =>
toggleDns(dnsHole.status === 'enabled' ? 'disable' : 'enable', [app.id])
}
disabled={fetchingDnsSummary || changingStatus}
style={{ pointerEvents: enableControls ? 'auto' : 'none' }}
>
<Badge
variant="dot"
color={dnsLightStatus(fetchingDnsSummary || changingStatus, dnsHole.status)}
styles={(theme) => ({
root: {
'&:hover': {
background:
theme.colorScheme === 'dark'
? theme.colors.dark[4]
: theme.colors.gray[2],
},
'&:active': {
background:
theme.colorScheme === 'dark'
? theme.colors.dark[5]
: theme.colors.gray[3],
},
},
})}
>
{t(dnsHole.status)}
</Badge>
</UnstyledButton>
<ActionIcon
size={20}
radius="xl"
top="2.67px"
variant="default"
onClick={() => {
setAppId(app.id);
open();
}}
>
<IconClockPause size={20} color="red" />
</ActionIcon>
</Flex>
</Stack>
</Group>
</Card>
);
})}
</Stack>
</Stack>
);
}
const useDnsHoleControlMutation = () => api.dnsHole.control.useMutation();
export default definition;