Skip to content

Commit 352c802

Browse files
authored
Extract IpPoolCell so we can use it in other places (#2497)
1 parent f0c161d commit 352c802

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

app/pages/project/floating-ips/FloatingIpsPage.tsx

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { Outlet, useNavigate, type LoaderFunctionArgs } from 'react-router-dom'
1313
import {
1414
apiQueryClient,
1515
useApiMutation,
16-
useApiQuery,
1716
useApiQueryClient,
1817
usePrefetchedApiQuery,
1918
type FloatingIp,
@@ -28,8 +27,8 @@ import { getProjectSelector, useProjectSelector } from '~/hooks/use-params'
2827
import { confirmAction } from '~/stores/confirm-action'
2928
import { confirmDelete } from '~/stores/confirm-delete'
3029
import { addToast } from '~/stores/toast'
31-
import { EmptyCell } from '~/table/cells/EmptyCell'
3230
import { InstanceLinkCell } from '~/table/cells/InstanceLinkCell'
31+
import { IpPoolCell } from '~/table/cells/IpPoolCell'
3332
import { useColsWithActions, type MenuAction } from '~/table/columns/action-col'
3433
import { Columns } from '~/table/columns/common'
3534
import { PAGE_SIZE, useQueryTable } from '~/table/QueryTable'
@@ -39,7 +38,6 @@ import { Message } from '~/ui/lib/Message'
3938
import { Modal } from '~/ui/lib/Modal'
4039
import { PageHeader, PageTitle } from '~/ui/lib/PageHeader'
4140
import { TableActions } from '~/ui/lib/Table'
42-
import { Tooltip } from '~/ui/lib/Tooltip'
4341
import { ALL_ISH } from '~/util/consts'
4442
import { docLinks } from '~/util/links'
4543
import { pb } from '~/util/path-builder'
@@ -63,6 +61,9 @@ FloatingIpsPage.loader = async ({ params }: LoaderFunctionArgs) => {
6361
apiQueryClient.prefetchQuery('instanceList', {
6462
query: { project },
6563
}),
64+
// fetch IP Pools and preload into RQ cache so fetches by ID in
65+
// IpPoolCell can be mostly instant yet gracefully fall back to
66+
// fetching individually if we don't fetch them all here
6667
apiQueryClient
6768
.fetchQuery('projectIpPoolList', { query: { limit: ALL_ISH } })
6869
.then((pools) => {
@@ -78,18 +79,6 @@ FloatingIpsPage.loader = async ({ params }: LoaderFunctionArgs) => {
7879
return null
7980
}
8081

81-
const IpPoolCell = ({ ipPoolId }: { ipPoolId: string }) => {
82-
const pool = useApiQuery('projectIpPoolView', { path: { pool: ipPoolId } }).data
83-
if (!pool) return <EmptyCell />
84-
return pool.description ? (
85-
<Tooltip content={pool.description} placement="right">
86-
<span>{pool.name}</span>
87-
</Tooltip>
88-
) : (
89-
<>{pool.name}</>
90-
)
91-
}
92-
9382
const colHelper = createColumnHelper<FloatingIp>()
9483
const staticCols = [
9584
colHelper.accessor('name', {}),
@@ -98,12 +87,12 @@ const staticCols = [
9887
header: 'IP address',
9988
}),
10089
colHelper.accessor('ipPoolId', {
101-
cell: (info) => <IpPoolCell ipPoolId={info.getValue()} />,
10290
header: 'IP pool',
91+
cell: (info) => <IpPoolCell ipPoolId={info.getValue()} />,
10392
}),
10493
colHelper.accessor('instanceId', {
105-
cell: (info) => <InstanceLinkCell instanceId={info.getValue()} />,
10694
header: 'Attached to instance',
95+
cell: (info) => <InstanceLinkCell instanceId={info.getValue()} />,
10796
}),
10897
]
10998

app/table/cells/IpPoolCell.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
import { useApiQuery } from '~/api'
9+
import { Tooltip } from '~/ui/lib/Tooltip'
10+
11+
import { EmptyCell } from './EmptyCell'
12+
13+
export const IpPoolCell = ({ ipPoolId }: { ipPoolId: string }) => {
14+
const pool = useApiQuery('projectIpPoolView', { path: { pool: ipPoolId } }).data
15+
if (!pool) return <EmptyCell />
16+
return (
17+
<Tooltip content={pool.description} placement="right">
18+
<span>{pool.name}</span>
19+
</Tooltip>
20+
)
21+
}

app/ui/lib/Tooltip.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface TooltipProps {
3939
/** The target the tooltip hovers near; can not be a raw string. */
4040
children?: ReactElement
4141
/** The text to appear on hover/focus */
42-
content: string | React.ReactNode
42+
content?: string | React.ReactNode
4343
/**
4444
* `undefined` means automatic, which means the tooltip will be placed in the
4545
* best position based on the available space. When any other placement is
@@ -86,6 +86,8 @@ export const Tooltip = forwardRef(
8686

8787
const zIndex = usePopoverZIndex()
8888

89+
if (!content) return child
90+
8991
return (
9092
<>
9193
{child}

0 commit comments

Comments
 (0)