-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(structure): remove useEffect and set table columns as initial state
- Loading branch information
1 parent
e823009
commit 34f988e
Showing
4 changed files
with
118 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import {Checkbox, Flex, Text} from '@sanity/ui' | ||
import {type AccessorKeyColumnDef, createColumnHelper} from '@tanstack/react-table' | ||
import { | ||
type AccessorKeyColumnDef, | ||
createColumnHelper, | ||
type GroupColumnDef, | ||
type VisibilityState, | ||
} from '@tanstack/react-table' | ||
import {useMemo} from 'react' | ||
import {useMemoObservable} from 'react-rx' | ||
import { | ||
|
@@ -14,6 +19,8 @@ import { | |
import {type PaneItemPreviewState} from '../../components/paneItem/types' | ||
import {SheetListCell} from './SheetListCell' | ||
|
||
export const VISIBLE_COLUMN_LIMIT = 5 | ||
|
||
const PreviewCell = (props: { | ||
documentPreviewStore: DocumentPreviewStore | ||
schemaType: SchemaType | ||
|
@@ -47,42 +54,68 @@ const PreviewCell = (props: { | |
const columnHelper = createColumnHelper<SanityDocument>() | ||
const SUPPORTED_FIELDS = ['string', 'number', 'boolean'] | ||
|
||
const getColsFromSchemaType = (schemaType: SchemaType, parentalField?: string) => { | ||
type Columns = ( | ||
| AccessorKeyColumnDef<SanityDocument, unknown> | ||
| GroupColumnDef<SanityDocument, unknown> | ||
)[] | ||
|
||
const getColsFromSchemaType = (schemaType: SchemaType, parentalField?: string): Columns => { | ||
This comment has been minimized.
Sorry, something went wrong.
pedrobonamin
Author
Contributor
|
||
//@ts-expect-error - wip. | ||
return schemaType.fields.reduce( | ||
(cols: AccessorKeyColumnDef<SanityDocument, unknown>[], field: any) => { | ||
const {type, name} = field | ||
if (SUPPORTED_FIELDS.includes(type.name)) { | ||
const nextCol = columnHelper.accessor( | ||
parentalField ? `${parentalField}.${field.name}` : field.name, | ||
{ | ||
header: field.type.title, | ||
enableHiding: true, | ||
cell: (info) => <SheetListCell {...info} type={type} />, | ||
}, | ||
) | ||
|
||
return [...cols, nextCol] | ||
} | ||
|
||
// if first layer nested object | ||
if (type.name === 'object' && !parentalField) { | ||
return [ | ||
...cols, | ||
columnHelper.group({header: name, columns: getColsFromSchemaType(type, field.name)}), | ||
] | ||
} | ||
|
||
return cols | ||
}, | ||
[], | ||
) | ||
return schemaType.fields.reduce((cols: Columns[], field: any) => { | ||
const {type, name} = field | ||
if (SUPPORTED_FIELDS.includes(type.name)) { | ||
const nextCol = columnHelper.accessor( | ||
parentalField ? `${parentalField}.${field.name}` : field.name, | ||
{ | ||
header: field.type.title, | ||
enableHiding: true, | ||
cell: (info) => <SheetListCell {...info} type={type} />, | ||
}, | ||
) | ||
|
||
return [...cols, nextCol] | ||
} | ||
|
||
// if first layer nested object | ||
if (type.name === 'object' && !parentalField) { | ||
return [ | ||
...cols, | ||
columnHelper.group({header: name, columns: getColsFromSchemaType(type, field.name)}), | ||
] | ||
} | ||
|
||
return cols | ||
}, []) | ||
} | ||
|
||
// Type guard function to check if a column is of type GroupColumnDef | ||
function isAccessorKeyColumnDef( | ||
This comment has been minimized.
Sorry, something went wrong. |
||
col: Columns[number], | ||
): col is AccessorKeyColumnDef<SanityDocument, unknown> { | ||
return (col as AccessorKeyColumnDef<SanityDocument, unknown>).accessorKey !== undefined | ||
} | ||
function isGroupColumnDef( | ||
column: AccessorKeyColumnDef<SanityDocument, unknown> | GroupColumnDef<SanityDocument, unknown>, | ||
): column is GroupColumnDef<SanityDocument, unknown> { | ||
return (column as GroupColumnDef<SanityDocument, unknown>).columns !== undefined | ||
} | ||
|
||
const flatColumns = (cols: Columns): AccessorKeyColumnDef<SanityDocument, unknown>[] => { | ||
This comment has been minimized.
Sorry, something went wrong.
pedrobonamin
Author
Contributor
|
||
return cols.flatMap((col) => { | ||
if (isAccessorKeyColumnDef(col)) { | ||
return col | ||
} | ||
if (isGroupColumnDef(col)) { | ||
return col.columns ? flatColumns(col.columns) : [] | ||
} | ||
return [] | ||
}) | ||
} | ||
|
||
export function useDocumentSheetColumns(schemaType?: SchemaType) { | ||
const documentPreviewStore = useDocumentPreviewStore() | ||
|
||
const columns: AccessorKeyColumnDef<SanityDocument, unknown>[] = useMemo(() => { | ||
const columns: Columns = useMemo(() => { | ||
if (!schemaType) { | ||
return [] | ||
} | ||
|
@@ -119,5 +152,26 @@ export function useDocumentSheetColumns(schemaType?: SchemaType) { | |
] | ||
}, [documentPreviewStore, schemaType]) | ||
|
||
return columns | ||
const [newColumns]: [VisibilityState, number] = useMemo( | ||
() => | ||
flatColumns(columns).reduce( | ||
([accCols, countAllowedVisible], column) => { | ||
// this column is always visible | ||
if (!column.enableHiding) { | ||
return [{...accCols, [column.accessorKey]: true}, countAllowedVisible] | ||
} | ||
|
||
// have already reached column visibility limit, hide column by default | ||
if (countAllowedVisible === VISIBLE_COLUMN_LIMIT) { | ||
return [{...accCols, [column.accessorKey]: false}, countAllowedVisible] | ||
} | ||
|
||
return [{...accCols, [column.accessorKey]: true}, countAllowedVisible + 1] | ||
}, | ||
[{}, 0], | ||
), | ||
[columns], | ||
) | ||
|
||
return {columns, initialColumnsVisibility: newColumns} | ||
} |
Can get ride of this and just use
table.resetColumnVisibility()
removing need to passinitialState
into here