-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathSettingsTab.tsx
More file actions
147 lines (136 loc) · 4.7 KB
/
SettingsTab.tsx
File metadata and controls
147 lines (136 loc) · 4.7 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
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* Copyright Oxide Computer Company
*/
import { format, formatDistanceToNow } from 'date-fns'
import { useEffect, useId, useState, type ReactNode } from 'react'
import { useForm } from 'react-hook-form'
import { apiQueryClient, useApiMutation, usePrefetchedApiQuery } from '~/api'
import { ListboxField } from '~/components/form/fields/ListboxField'
import { useInstanceSelector } from '~/hooks/use-params'
import { addToast } from '~/stores/toast'
import { Button } from '~/ui/lib/Button'
import { FieldLabel } from '~/ui/lib/FieldLabel'
import { LearnMore, SettingsGroup } from '~/ui/lib/SettingsGroup'
import { TipIcon } from '~/ui/lib/TipIcon'
import { links } from '~/util/links'
Component.displayName = 'SettingsTab'
export function Component() {
const instanceSelector = useInstanceSelector()
const [now, setNow] = useState(new Date())
useEffect(() => {
const timer = setInterval(() => setNow(new Date()), 1000)
return () => clearInterval(timer)
}, [])
const { data: instance } = usePrefetchedApiQuery('instanceView', {
path: { instance: instanceSelector.instance },
query: { project: instanceSelector.project },
})
const instanceUpdate = useApiMutation('instanceUpdate', {
onSuccess() {
apiQueryClient.invalidateQueries('instanceView')
addToast({ content: 'Instance auto-restart policy updated' })
},
})
const form = useForm({
defaultValues: {
autoRestartPolicy: instance.autoRestartPolicy,
},
})
const { isDirty } = form.formState
const onSubmit = form.handleSubmit((values) => {
instanceUpdate.mutate({
path: { instance: instanceSelector.instance },
query: { project: instanceSelector.project },
body: {
ncpus: instance.ncpus,
memory: instance.memory,
bootDisk: instance.bootDiskId,
autoRestartPolicy: values.autoRestartPolicy,
},
})
})
return (
<form className="space-y-6" onSubmit={onSubmit}>
<SettingsGroup.Container>
<SettingsGroup.Header>
<SettingsGroup.Title>Auto-restart</SettingsGroup.Title>
<p>The auto-restart policy configured for this instance</p>
</SettingsGroup.Header>
<SettingsGroup.Body>
<ListboxField
control={form.control}
name="autoRestartPolicy"
label="Policy"
description="If unconfigured this instance uses the default auto-restart policy, which may or may not allow it to be restarted."
placeholder="Default"
items={[
{ value: '', label: 'None (Default)' },
{ value: 'never', label: 'Never' },
{ value: 'best_effort', label: 'Best effort' },
]}
required
className="max-w-none"
/>
<FormMeta label="Enabled" helpText="Help">
{instance.autoRestartEnabled ? 'True' : 'False'}{' '}
{instance.autoRestartEnabled && !instance.autoRestartPolicy && (
<span className="text-tertiary">(Project default)</span>
)}
</FormMeta>
<FormMeta label="Cooldown expiration" helpText="Help">
{instance.autoRestartCooldownExpiration ? (
<>
{format(
new Date(instance.autoRestartCooldownExpiration),
'MMM d, yyyy HH:mm:ss zz'
)}{' '}
{new Date(instance.autoRestartCooldownExpiration) > now && (
<span className="text-tertiary">
({formatDistanceToNow(new Date(instance.autoRestartCooldownExpiration))}
)
</span>
)}
</>
) : (
<span className="text-tertiary">n/a</span>
)}
</FormMeta>
</SettingsGroup.Body>
<SettingsGroup.Footer>
<div>
<LearnMore text="Auto-Restart" href={links.sshDocs} />
</div>
<Button size="sm" type="submit" disabled={!isDirty}>
Save
</Button>
</SettingsGroup.Footer>
</SettingsGroup.Container>
</form>
)
}
const FormMeta = ({
label,
helpText,
children,
}: {
label: string
helpText?: string
children: ReactNode
}) => {
const id = useId()
return (
<div>
<div className="mb-2 flex items-center gap-1 border-b pb-2 border-secondary">
<FieldLabel id={`${id}-label`} htmlFor={id}>
{label}
</FieldLabel>
{helpText && <TipIcon>{helpText}</TipIcon>}
</div>
{children}
</div>
)
}