|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | + |
| 9 | +import { useQuery } from '@tanstack/react-query' |
| 10 | +import { createColumnHelper } from '@tanstack/react-table' |
| 11 | +import { useMemo } from 'react' |
| 12 | +import { Outlet, type LoaderFunctionArgs } from 'react-router-dom' |
| 13 | + |
| 14 | +import { apiq, getListQFn, queryClient, type InternetGateway } from '~/api' |
| 15 | +import { getVpcSelector, useVpcSelector } from '~/hooks/use-params' |
| 16 | +import { EmptyCell } from '~/table/cells/EmptyCell' |
| 17 | +import { IpPoolCell } from '~/table/cells/IpPoolCell' |
| 18 | +import { LinkCell, makeLinkCell } from '~/table/cells/LinkCell' |
| 19 | +import { Columns } from '~/table/columns/common' |
| 20 | +import { useQueryTable } from '~/table/QueryTable' |
| 21 | +import { CopyableIp } from '~/ui/lib/CopyableIp' |
| 22 | +import { EmptyMessage } from '~/ui/lib/EmptyMessage' |
| 23 | +import { ALL_ISH } from '~/util/consts' |
| 24 | +import { pb } from '~/util/path-builder' |
| 25 | +import type * as PP from '~/util/path-params' |
| 26 | + |
| 27 | +import { |
| 28 | + gatewayIpAddressList, |
| 29 | + gatewayIpPoolList, |
| 30 | + routeList, |
| 31 | + routerList, |
| 32 | + useGatewayRoutes, |
| 33 | +} from '../../gateway-data' |
| 34 | + |
| 35 | +const gatewayList = ({ project, vpc }: PP.Vpc) => |
| 36 | + getListQFn('internetGatewayList', { query: { project, vpc, limit: ALL_ISH } }) |
| 37 | +const projectIpPoolList = getListQFn('projectIpPoolList', { query: { limit: ALL_ISH } }) |
| 38 | + |
| 39 | +const IpAddressCell = (gatewaySelector: PP.VpcInternetGateway) => { |
| 40 | + const { data: addresses } = useQuery(gatewayIpAddressList(gatewaySelector).optionsFn()) |
| 41 | + if (!addresses || addresses.items.length < 1) return <EmptyCell /> |
| 42 | + return <CopyableIp ip={addresses.items[0].address} isLinked={false} /> |
| 43 | +} |
| 44 | + |
| 45 | +const GatewayIpPoolCell = (gatewaySelector: PP.VpcInternetGateway) => { |
| 46 | + const { data: gateways } = useQuery(gatewayIpPoolList(gatewaySelector).optionsFn()) |
| 47 | + if (!gateways || gateways.items.length < 1) return <EmptyCell /> |
| 48 | + return <IpPoolCell ipPoolId={gateways.items[0].ipPoolId} /> |
| 49 | +} |
| 50 | + |
| 51 | +const GatewayRoutes = ({ project, vpc, gateway }: PP.VpcInternetGateway) => { |
| 52 | + const matchingRoutes = useGatewayRoutes({ project, vpc, gateway }) |
| 53 | + const to = pb.vpcInternetGateway({ project, vpc, gateway }) |
| 54 | + if (!matchingRoutes?.length) return <EmptyCell /> |
| 55 | + return <LinkCell to={to}>{matchingRoutes.length}</LinkCell> |
| 56 | +} |
| 57 | + |
| 58 | +const colHelper = createColumnHelper<InternetGateway>() |
| 59 | + |
| 60 | +VpcInternetGatewaysTab.loader = async ({ params }: LoaderFunctionArgs) => { |
| 61 | + const { project, vpc } = getVpcSelector(params) |
| 62 | + const [gateways, routers] = await Promise.all([ |
| 63 | + queryClient.fetchQuery(gatewayList({ project, vpc }).optionsFn()), |
| 64 | + queryClient.fetchQuery(routerList({ project, vpc }).optionsFn()), |
| 65 | + ]) |
| 66 | + |
| 67 | + await Promise.all([ |
| 68 | + ...gateways.items.flatMap((gateway: InternetGateway) => [ |
| 69 | + queryClient.prefetchQuery( |
| 70 | + gatewayIpAddressList({ project, vpc, gateway: gateway.name }).optionsFn() |
| 71 | + ), |
| 72 | + queryClient.prefetchQuery( |
| 73 | + gatewayIpPoolList({ project, vpc, gateway: gateway.name }).optionsFn() |
| 74 | + ), |
| 75 | + ]), |
| 76 | + ...routers.items.map((router) => |
| 77 | + queryClient.prefetchQuery( |
| 78 | + routeList({ project, vpc, router: router.name }).optionsFn() |
| 79 | + ) |
| 80 | + ), |
| 81 | + queryClient.fetchQuery(projectIpPoolList.optionsFn()).then((pools) => { |
| 82 | + for (const pool of pools.items) { |
| 83 | + const { queryKey } = apiq('projectIpPoolView', { path: { pool: pool.id } }) |
| 84 | + queryClient.setQueryData(queryKey, pool) |
| 85 | + } |
| 86 | + }), |
| 87 | + ] satisfies Promise<unknown>[]) |
| 88 | + |
| 89 | + return null |
| 90 | +} |
| 91 | + |
| 92 | +export function VpcInternetGatewaysTab() { |
| 93 | + const { project, vpc } = useVpcSelector() |
| 94 | + |
| 95 | + const emptyState = ( |
| 96 | + <EmptyMessage |
| 97 | + title="No internet gateways" |
| 98 | + body="Create an internet gateway to see it here" |
| 99 | + // buttonText="New internet gateway" |
| 100 | + // buttonTo={pb.vpcInternetGatewaysNew(vpcSelector)} |
| 101 | + /> |
| 102 | + ) |
| 103 | + |
| 104 | + const columns = useMemo( |
| 105 | + () => [ |
| 106 | + colHelper.accessor('name', { |
| 107 | + cell: makeLinkCell((gateway) => pb.vpcInternetGateway({ project, vpc, gateway })), |
| 108 | + }), |
| 109 | + colHelper.accessor('description', Columns.description), |
| 110 | + colHelper.accessor('name', { |
| 111 | + // ID needed to avoid key collision with other name column |
| 112 | + id: 'ip-address', |
| 113 | + header: 'Attached IP Address', |
| 114 | + cell: (info) => ( |
| 115 | + <IpAddressCell project={project} vpc={vpc} gateway={info.getValue()} /> |
| 116 | + ), |
| 117 | + }), |
| 118 | + colHelper.accessor('name', { |
| 119 | + // ID needed to avoid key collision with other name column |
| 120 | + id: 'ip-pool', |
| 121 | + header: 'Attached IP Pool', |
| 122 | + cell: (info) => ( |
| 123 | + <GatewayIpPoolCell project={project} vpc={vpc} gateway={info.getValue()} /> |
| 124 | + ), |
| 125 | + }), |
| 126 | + colHelper.accessor('name', { |
| 127 | + // ID needed to avoid key collision with other name column |
| 128 | + id: 'routes', |
| 129 | + header: 'Routes', |
| 130 | + cell: (info) => ( |
| 131 | + <GatewayRoutes project={project} vpc={vpc} gateway={info.getValue()} /> |
| 132 | + ), |
| 133 | + }), |
| 134 | + colHelper.accessor('timeCreated', Columns.timeCreated), |
| 135 | + ], |
| 136 | + [project, vpc] |
| 137 | + ) |
| 138 | + |
| 139 | + const { table } = useQueryTable({ |
| 140 | + query: gatewayList({ project, vpc }), |
| 141 | + columns, |
| 142 | + emptyState, |
| 143 | + }) |
| 144 | + |
| 145 | + return ( |
| 146 | + <> |
| 147 | + {table} |
| 148 | + <Outlet /> |
| 149 | + </> |
| 150 | + ) |
| 151 | +} |
0 commit comments