-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathVpcSubnetsTab.tsx
More file actions
132 lines (118 loc) · 4.2 KB
/
VpcSubnetsTab.tsx
File metadata and controls
132 lines (118 loc) · 4.2 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
/*
* 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 { createColumnHelper } from '@tanstack/react-table'
import { useCallback, useMemo } from 'react'
import { Outlet, type LoaderFunctionArgs } from 'react-router'
import { api, getListQFn, queryClient, useApiMutation, type VpcSubnet } from '@oxide/api'
import { getVpcSelector, useVpcSelector } from '~/hooks/use-params'
import { useQuickActions } from '~/hooks/use-quick-actions'
import { confirmDelete } from '~/stores/confirm-delete'
import { addToast } from '~/stores/toast'
import { makeLinkCell } from '~/table/cells/LinkCell'
import { RouterLinkCell } from '~/table/cells/RouterLinkCell'
import { TwoLineCell } from '~/table/cells/TwoLineCell'
import { getActionsCol, type MenuAction } from '~/table/columns/action-col'
import { Columns } from '~/table/columns/common'
import { useQueryTable } from '~/table/QueryTable'
import { CreateLink } from '~/ui/lib/CreateButton'
import { EmptyMessage } from '~/ui/lib/EmptyMessage'
import { pb } from '~/util/path-builder'
import type * as PP from '~/util/path-params'
const colHelper = createColumnHelper<VpcSubnet>()
const subnetList = (params: PP.Vpc) => getListQFn(api.vpcSubnetList, { query: params })
export async function clientLoader({ params }: LoaderFunctionArgs) {
const { project, vpc } = getVpcSelector(params)
await queryClient.prefetchQuery(subnetList({ project, vpc }).optionsFn())
return null
}
export const handle = { crumb: 'VPC Subnets' }
export default function VpcSubnetsTab() {
const vpcSelector = useVpcSelector()
const { mutateAsync: deleteSubnet } = useApiMutation(api.vpcSubnetDelete, {
onSuccess() {
queryClient.invalidateEndpoint('vpcSubnetList')
// We only have the ID, so will show a generic confirmation message
addToast({ content: 'VPC subnet deleted' })
},
})
const makeActions = useCallback(
(subnet: VpcSubnet): MenuAction[] => [
{
label: 'Edit',
to: pb.vpcSubnetsEdit({ ...vpcSelector, subnet: subnet.name }),
},
// TODO: only show if you have permission to do this
{
label: 'Delete',
onActivate: confirmDelete({
doDelete: () => deleteSubnet({ path: { subnet: subnet.id } }),
label: subnet.name,
}),
},
],
[deleteSubnet, vpcSelector]
)
const columns = useMemo(
() => [
colHelper.accessor('name', {
cell: makeLinkCell((subnet) => pb.vpcSubnetsEdit({ ...vpcSelector, subnet })),
}),
colHelper.accessor('description', Columns.description),
colHelper.accessor((vpc) => [vpc.ipv4Block, vpc.ipv6Block] as const, {
header: 'IP Block',
cell: (info) => <TwoLineCell value={[...info.getValue()]} />,
}),
colHelper.accessor('customRouterId', {
header: 'Custom Router',
// RouterLinkCell needed, as we need to convert the customRouterId to the custom router's name
cell: (info) => <RouterLinkCell routerId={info.getValue()} />,
}),
colHelper.accessor('timeCreated', Columns.timeCreated),
getActionsCol(makeActions),
],
[vpcSelector, makeActions]
)
const emptyState = (
<EmptyMessage
title="No VPC subnets"
body="Create a VPC subnet to see it here"
buttonText="New VPC subnet"
buttonTo={pb.vpcSubnetsNew(vpcSelector)}
/>
)
const { table, query } = useQueryTable({
query: subnetList(vpcSelector),
columns,
emptyState,
rowHeight: 'large',
})
useQuickActions(
() => [
{
value: 'New VPC subnet',
navGroup: 'Actions',
action: pb.vpcSubnetsNew(vpcSelector),
},
...(query.data?.items || []).map((s) => ({
value: s.name,
navGroup: 'Edit VPC subnet',
action: pb.vpcSubnetsEdit({ ...vpcSelector, subnet: s.name }),
})),
],
[vpcSelector, query.data]
)
return (
<>
<div className="mb-3 flex justify-end space-x-2">
<CreateLink to={pb.vpcSubnetsNew(vpcSelector)}>New VPC subnet</CreateLink>
</div>
{table}
<Outlet />
</>
)
}