-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCpuPlatformCard.tsx
More file actions
98 lines (87 loc) · 3.03 KB
/
CpuPlatformCard.tsx
File metadata and controls
98 lines (87 loc) · 3.03 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
/*
* 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 { useForm } from 'react-hook-form'
import { match } from 'ts-pattern'
import { api, q, queryClient, useApiMutation, usePrefetchedQuery } 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 { CardBlock, LearnMore } from '~/ui/lib/CardBlock'
import { cpuPlatformItems, type FormCpuPlatform } from '~/util/cpu-platform'
import { docLinks } from '~/util/links'
type FormValues = {
cpuPlatform: FormCpuPlatform
}
export function CpuPlatformCard() {
const instanceSelector = useInstanceSelector()
const { data: instance } = usePrefetchedQuery(
q(api.instanceView, {
path: { instance: instanceSelector.instance },
query: { project: instanceSelector.project },
})
)
const instanceUpdate = useApiMutation(api.instanceUpdate, {
onSuccess() {
queryClient.invalidateEndpoint('instanceView')
addToast({ content: 'CPU platform preference updated' })
},
onError(err) {
addToast({
title: 'Could not update CPU platform preference',
content: err.message,
variant: 'error',
})
},
})
const cpuPlatform: FormCpuPlatform = instance.cpuPlatform || 'none'
const defaultValues: FormValues = { cpuPlatform }
const form = useForm({ defaultValues })
const disableSubmit = form.watch('cpuPlatform') === cpuPlatform
const onSubmit = form.handleSubmit((values) => {
instanceUpdate.mutate({
path: { instance: instanceSelector.instance },
query: { project: instanceSelector.project },
body: {
cpuPlatform: match(values.cpuPlatform)
.with('none', () => null)
.with('amd_milan', () => 'amd_milan' as const)
.with('amd_turin', () => 'amd_turin' as const)
.exhaustive(),
ncpus: instance.ncpus,
memory: instance.memory,
autoRestartPolicy: instance.autoRestartPolicy || null,
bootDisk: instance.bootDiskId || null,
},
})
})
return (
<form onSubmit={onSubmit}>
<CardBlock>
<CardBlock.Header title="CPU platform" />
<CardBlock.Body>
<ListboxField
control={form.control}
name="cpuPlatform"
label="Required CPU"
description="If a CPU platform is specified, the instance will only be placed on compatible hosts."
items={cpuPlatformItems}
className="max-w-lg"
required
/>
</CardBlock.Body>
<CardBlock.Footer>
<LearnMore doc={docLinks.cpuPlatform} />
<Button size="sm" type="submit" disabled={disableSubmit}>
Save
</Button>
</CardBlock.Footer>
</CardBlock>
</form>
)
}