Skip to content

Commit

Permalink
Refactor TableColumnList to simplify column filtering and remove rela…
Browse files Browse the repository at this point in the history
…tionships prop
  • Loading branch information
FunamaYukina committed Dec 19, 2024
1 parent c0934d3 commit ab66460
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Relationships } from '@liam-hq/db-structure'
import type { Column } from '@liam-hq/db-structure'
import type { FC } from 'react'
import { columnHandleId } from '../../../columnHandleId'
import type { Data } from '../type'
Expand All @@ -7,34 +7,30 @@ import { TableColumn } from './TableColumn'
type TableColumnListProps = {
data: Data
filter?: 'KEY_ONLY'
relationships: Relationships
}

export const TableColumnList: FC<TableColumnListProps> = ({
data,
filter,
relationships,
}) => {
const foreignKeyColumns = Object.values(data.table.columns).filter((column) =>
Object.values(relationships).some(
(rel) =>
rel.foreignTableName === data.table.name &&
rel.foreignColumnName === column.name,
),
)

const primaryColumns = Object.values(data.table.columns).filter((column) => {
return column.primary
})

const columns =
filter === 'KEY_ONLY'
? primaryColumns.concat(foreignKeyColumns)
: Object.values(data.table.columns)
const shouldDisplayColumn = (
column: Column,
filter: 'KEY_ONLY' | undefined,
targetColumnCardinalities: Data['targetColumnCardinalities'],
): boolean => {
if (filter === 'KEY_ONLY') {
return (
column.primary || targetColumnCardinalities?.[column.name] !== undefined
)
}
return true
}

export const TableColumnList: FC<TableColumnListProps> = ({ data, filter }) => {
return (
<ul>
{columns.map((column) => {
{Object.values(data.table.columns).map((column) => {
if (
!shouldDisplayColumn(column, filter, data.targetColumnCardinalities)
) {
return null
}
const handleId = columnHandleId(data.table.name, column.name)
const isHighlighted =
data.isHighlighted || data.highlightedHandles?.includes(handleId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,9 @@ export const TableNode: FC<Props> = ({ data }) => {
data-erd="table-node"
>
<TableHeader data={data} />
{showMode === 'ALL_FIELDS' && (
<TableColumnList data={data} relationships={relationships} />
)}
{showMode === 'ALL_FIELDS' && <TableColumnList data={data} />}
{showMode === 'KEY_ONLY' && (
<TableColumnList
data={data}
filter="KEY_ONLY"
relationships={relationships}
/>
<TableColumnList data={data} filter="KEY_ONLY" />
)}
</div>
)
Expand Down

0 comments on commit ab66460

Please sign in to comment.