Files
Joshen Lim 4ed3627d72 Joshenlim/fe 3891 improve handling of partition tables in (#53268)
## Context

The Table Editor doesn't really handle partitioned tables well given the
following issues:
- Partitioned tables are rendered as a flat list in the Table Editor
side menu, so the menu could get very long and noisy - this was the
primary motivation for this PR
- Child partitioned tables are not indicated with a proper icon, they're
treated as regular tables in the Table Editor which is incorrect

## Changes involved
- Table Editor side menu now only lists parent partitioned tables, omits
all child partitioned tables
- Child partitioned tables can be viewed by clicking on the button
tooltip here or drilling down via "View partitions"
<img width="300" alt="image"
src="https://github.com/user-attachments/assets/e3cfbe4b-21ff-47df-8fcd-81d0c9f68d94"
/><br />
<img width="400" alt="image"
src="https://github.com/user-attachments/assets/5d0747cb-fb52-4301-ad9e-c7be504f43c9"
/>

  - Selecting that will then render a nested nav as such
<img width="291" height="323" alt="image"
src="https://github.com/user-attachments/assets/bff0a42b-4265-4317-8299-537d82544388"
/>

- Sub partitioned tables can be drilled into the same way, and the back
CTA steps up one level
<img width="485" height="306" alt="image"
src="https://github.com/user-attachments/assets/66f6f6cb-8b7d-46c5-8ea3-4ee4fd57aa84"
/>

- Parent partition tables have the following actions (Previously was
only copy name and schema)
<img width="465" height="257" alt="image"
src="https://github.com/user-attachments/assets/2aa90995-7c30-4bcf-a446-d01ab52c1ede"
/>

- Deleting a parent partition table will also delete the child partition
table, and that's highlighted in the confirmation modal
<img width="418" height="316" alt="image"
src="https://github.com/user-attachments/assets/0d61984b-f85e-4c93-84d0-549d300838c9"
/>

- Child partition tables have the following actions (Edit + duplicate
removed since they aren't supported via the UI)
<img width="482" height="229" alt="image"
src="https://github.com/user-attachments/assets/e028b13d-2e92-4491-b05c-7a6cb0a129d6"
/>


## To test
In general, just verify the viewing UX of partitioned tables to see if
it all feels alright, can use the following SQL to quickly set up
partitioned tables
```sql
   create schema if not exists tint;
   create table public.nest_demo (id int, hue int not null, primary key (id, hue))
     partition by range (hue);
   create table public.nest_demo_blue partition of public.nest_demo
     for values from (200) to (300) partition by range (hue);
   create table public.nest_demo_blue_light partition of public.nest_demo_blue
     for values from (200) to (250);
   create table tint.nest_demo_blue_deep partition of public.nest_demo_blue
     for values from (250) to (300);
   create table public.nest_demo_other partition of public.nest_demo
     for values from (0) to (200);
   ```
2026-11-16 17:23:59 +07:00

182 lines
5.7 KiB
TypeScript

import { keepPreviousData, useQuery } from '@tanstack/react-query'
import { ArrowLeftIcon } from 'lucide-react'
import { useCallback, useMemo, useState } from 'react'
import { Button } from 'ui'
import {
InnerSideBarEmptyPanel,
InnerSideBarFilters,
InnerSideBarFilterSearchInput,
} from 'ui-patterns/InnerSideMenu'
import { EntityListItem, type EntityListItemProps } from './EntityListItem'
import { ErrorMatcher } from '@/components/interfaces/ErrorHandling/ErrorMatcher'
import { EditorMenuListSkeleton } from '@/components/layouts/TableEditorLayout/EditorMenuListSkeleton'
import { InfiniteListDefault, LoaderForIconMenuItems } from '@/components/ui/InfiniteList'
import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'
import { useEntityTypesQuery } from '@/data/entity-types/entity-types-infinite-query'
import { partitionInfoQueryOptions } from '@/data/entity-types/partition-info-query'
import { useTableApiAccessQuery } from '@/data/privileges/table-api-access-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
// Foreign tables can be partitions too (e.g. archival partitions served via a foreign server)
const PARTITION_ENTITY_TYPES = [
ENTITY_TYPE.TABLE,
ENTITY_TYPE.PARTITIONED_TABLE,
ENTITY_TYPE.FOREIGN_TABLE,
]
type PartitionListMenuProps = {
parentId: number
schema: string
onViewPartitions: (parentId: number) => void
onBack: (parentId: number | undefined) => void
itemProps: Pick<EntityListItemProps, 'projectRef' | 'id' | 'isLocked' | 'onExportCLI'>
}
export const PartitionListMenu = ({
parentId,
schema,
onViewPartitions,
onBack,
itemProps,
}: PartitionListMenuProps) => {
const { data: project } = useSelectedProjectQuery()
const [searchText, setSearchText] = useState('')
const { data: parentInfo, isPending: isParentInfoPending } = useQuery(
partitionInfoQueryOptions({
projectRef: project?.ref,
connectionString: project?.connectionString,
id: parentId,
})
)
const {
data,
isLoading,
isSuccess,
isError,
error,
hasNextPage,
isFetchingNextPage,
fetchNextPage,
} = useEntityTypesQuery(
{
projectRef: project?.ref,
connectionString: project?.connectionString,
search: searchText.trim() || undefined,
sort: 'alphabetical',
filterTypes: PARTITION_ENTITY_TYPES,
parentId,
},
{ placeholderData: searchText ? keepPreviousData : undefined }
)
const partitions = useMemo(() => data?.pages.flatMap((page) => page.data.entities), [data?.pages])
const partitionNames = useMemo(
() => partitions?.filter((x) => x.schema === schema).map((x) => x.name) ?? [],
[partitions, schema]
)
const { data: apiAccessByTableName } = useTableApiAccessQuery(
{
projectRef: project?.ref,
connectionString: project?.connectionString ?? undefined,
schemaName: schema,
tableNames: partitionNames,
},
{ enabled: partitionNames.length > 0 }
)
const listItemProps = useMemo(
() => ({
...itemProps,
apiAccessMap: apiAccessByTableName,
onViewPartitions,
}),
[itemProps, apiAccessByTableName, onViewPartitions]
)
const getItemKey = useCallback(
(index: number) => {
const item = partitions?.[index]
return item?.id ? String(item.id) : `table-editor-partition-${index}`
},
[partitions]
)
const hasPartitions = (partitions?.length ?? 0) > 0
return (
<div className="grow min-h-0 flex flex-col gap-2 pb-4">
<InnerSideBarFilters className="mx-2">
<InnerSideBarFilterSearchInput
name="search-partitions"
value={searchText}
placeholder="Search partitions..."
aria-labelledby="Search partitions"
onChange={(e) => setSearchText(e.target.value)}
/>
</InnerSideBarFilters>
<div className="flex items-center gap-x-1 mx-2">
<Button
variant="text"
className="px-1"
icon={<ArrowLeftIcon />}
disabled={isParentInfoPending}
aria-label={
parentInfo?.parent_id != null ? 'Back to parent partitions' : 'Back to all tables'
}
onClick={() => onBack(parentInfo?.parent_id ?? undefined)}
/>
<span className="text-sm text-foreground-light truncate" title={parentInfo?.name}>
{parentInfo?.name}
</span>
</div>
{isLoading && <EditorMenuListSkeleton />}
{isError && (
<ErrorMatcher
title="Failed to load partitions"
error={error ?? 'Failed to load partitions'}
supportFormParams={{ projectRef: project?.ref }}
className="mx-4 mt-3"
/>
)}
{isSuccess && !hasPartitions && searchText.length === 0 && (
<InnerSideBarEmptyPanel
className="mx-2"
title="No partitions"
description="This table doesn't have any partitions yet"
/>
)}
{isSuccess && !hasPartitions && searchText.length > 0 && (
<InnerSideBarEmptyPanel
className="mx-2"
title="No results found"
description={`Your search for "${searchText}" did not return any results`}
/>
)}
{isSuccess && hasPartitions && (
<div className="flex flex-1 min-h-0 w-full" data-testid="partitions-list">
<InfiniteListDefault
className="h-full w-full"
items={partitions!}
ItemComponent={EntityListItem}
LoaderComponent={LoaderForIconMenuItems}
itemProps={listItemProps}
getItemKey={getItemKey}
getItemSize={() => 28}
hasNextPage={hasNextPage}
isLoadingNextPage={isFetchingNextPage}
onLoadNextPage={fetchNextPage}
/>
</div>
)}
</div>
)
}