Skip to content

Commit

Permalink
Merge pull request #350 from liam-hq/refactor-table-node-4
Browse files Browse the repository at this point in the history
refactor: Remove unused isRelatedToTable function and simplify TableNode component logic
  • Loading branch information
hoshinotsuyoshi authored Dec 20, 2024
2 parents 68ebac1 + 34dd3eb commit 70a0915
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 42 deletions.
6 changes: 6 additions & 0 deletions frontend/.changeset/gold-singers-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@liam-hq/erd-core": patch
"@liam-hq/cli": patch
---

refactor: Remove unused isRelatedToTable function and simplify TableNode component logic
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { selectTableLogEvent } from '@/features/gtm/utils'
import { repositionTableLogEvent } from '@/features/gtm/utils/repositionTableLogEvent'
import { useCliVersion } from '@/providers'
import { updateActiveTableName, useUserEditingActiveStore } from '@/stores'
import type { Relationships } from '@liam-hq/db-structure'
import {
Background,
BackgroundVariant,
Expand Down Expand Up @@ -47,23 +46,6 @@ type Props = {
| undefined
}

export const isRelatedToTable = (
relationships: Relationships,
tableName: string,
targetTableName: string | undefined,
) => {
if (!targetTableName) {
return false
}
return Object.values(relationships).some(
(relationship) =>
(relationship.primaryTableName === tableName ||
relationship.foreignTableName === tableName) &&
(relationship.primaryTableName === targetTableName ||
relationship.foreignTableName === targetTableName),
)
}

export const ERDContentInner: FC<Props> = ({
nodes: _nodes,
edges: _edges,
Expand Down Expand Up @@ -151,13 +133,7 @@ export const ERDContentInner: FC<Props> = ({
<div className={styles.wrapper} data-loading={loading}>
{loading && <Spinner className={styles.loading} />}
<ReactFlow
nodes={nodes.map((node) => ({
...node,
data: {
...node.data,
onClick: handleNodeClick,
},
}))}
nodes={nodes}
edges={edges}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useDBStructureStore, useUserEditingStore } from '@/stores'
import { useUserEditingStore } from '@/stores'
import type { Node, NodeProps } from '@xyflow/react'
import clsx from 'clsx'
import type { FC } from 'react'
import { isRelatedToTable } from '../ERDContent'
import { TableColumnList } from './TableColumnList'
import { TableHeader } from './TableHeader'
import styles from './TableNode.module.css'
Expand All @@ -14,27 +13,16 @@ export const isTableNode = (node: Node): node is TableNodeType =>
type Props = NodeProps<TableNodeType>

export const TableNode: FC<Props> = ({ data }) => {
const { relationships } = useDBStructureStore()
const {
active: { tableName },
showMode,
} = useUserEditingStore()

// TODO: remove tableName and isRelatedToTable() from here
const isActive = data.isActiveHighlighted || tableName === data.table.name

const isTableHighlighted =
data.isHighlighted ||
isRelatedToTable(relationships, data.table.name, tableName)
const { showMode } = useUserEditingStore()

return (
<div
className={clsx(
styles.wrapper,
isTableHighlighted && styles.wrapperHighlighted,
isActive && styles.wrapperActive,
data.isHighlighted && styles.wrapperHighlighted,
data.isActiveHighlighted && styles.wrapperActive,
)}
data-erd={isTableHighlighted && 'table-node-highlighted'}
data-erd={data.isHighlighted && 'table-node-highlighted'}
>
<TableHeader data={data} />
{showMode === 'ALL_FIELDS' && <TableColumnList data={data} />}
Expand Down

0 comments on commit 70a0915

Please sign in to comment.