forked from Chia-Network/chia-blockchain-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLimitCacheSize.tsx
More file actions
82 lines (69 loc) · 1.83 KB
/
Copy pathLimitCacheSize.tsx
File metadata and controls
82 lines (69 loc) · 1.83 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
import { AlertDialog, ButtonLoading, Flex, Form, TextField, useOpenDialog } from '@chia-network/core';
import { Trans } from '@lingui/macro';
import React, { useEffect } from 'react';
import { useForm } from 'react-hook-form';
import useCache from '../../hooks/useCache';
const MB_SIZE = 1024 * 1024;
type FormData = {
maxCacheSize: number;
};
export default function LimitCacheSize() {
const openDialog = useOpenDialog();
const { maxCacheSize, setMaxCacheSize } = useCache();
const methods = useForm<FormData>({
defaultValues: {
maxCacheSize,
},
});
const { reset } = methods;
useEffect(() => {
if (maxCacheSize !== undefined) {
reset({
maxCacheSize: maxCacheSize / MB_SIZE,
});
}
}, [maxCacheSize, reset]);
const { isSubmitting } = methods.formState;
const isLoading = isSubmitting;
const canSubmit = !isLoading;
async function handleSubmit(values: FormData) {
if (isSubmitting) {
return;
}
const newValue = Number(values.maxCacheSize) * MB_SIZE;
await setMaxCacheSize(newValue);
await openDialog(
<AlertDialog>
<Trans>Successfully updated cache size limit.</Trans>
</AlertDialog>,
);
}
return (
<Form methods={methods} onSubmit={handleSubmit} noValidate>
<Flex gap={2} row>
<TextField
label="MiB"
name="maxCacheSize"
type="number"
disabled={!canSubmit}
size="small"
InputProps={{
inputProps: {
min: 0,
},
}}
/>
<ButtonLoading
size="small"
disabled={!canSubmit}
type="submit"
loading={!canSubmit}
variant="outlined"
color="secondary"
>
<Trans>Update</Trans>
</ButtonLoading>
</Flex>
</Form>
);
}