Skip to content

Commit 9cedf5c

Browse files
authored
Merge pull request #3702 from liam-hq/refactor/table-column-url-functions
♻️ refactor(erd-core): add Hash schema and integrate URL handling functions
2 parents b8f2acb + 8b2c946 commit 9cedf5c

File tree

12 files changed

+67
-10
lines changed

12 files changed

+67
-10
lines changed

frontend/packages/erd-core/src/features/erd/components/ERDContent/components/TableNode/TableDetail/Columns/ColumnsItem/ColumnsItem.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
useUserEditingOrThrow,
88
} from '../../../../../../../../../stores'
99
import { useDiffStyle } from '../../../../../../../../diff/hooks/useDiffStyle'
10+
import { getTableColumnElementId } from '../../../../../../../utils/url/getTableColumnElementId'
1011
import { BlinkCircle } from '../../BlinkCircle/BlinkCircle'
1112
import styles from './ColumnsItem.module.css'
1213
import { Comment } from './Comment'
@@ -16,9 +17,6 @@ import { NotNull } from './NotNull'
1617
import { PrimaryKey } from './PrimaryKey'
1718
import { Type } from './Type'
1819

19-
const columnElementId = (tableName: string, columnName: string) =>
20-
`${tableName}__columns__${columnName}`
21-
2220
type Props = {
2321
tableId: string
2422
column: Column
@@ -32,7 +30,7 @@ export const ColumnsItem: FC<Props> = ({
3230
constraints,
3331
focusedElementId,
3432
}) => {
35-
const elementId = columnElementId(tableId, column.name)
33+
const elementId = getTableColumnElementId(tableId, column.name)
3634

3735
const { operations } = useSchemaOrThrow()
3836
const { showDiff } = useUserEditingOrThrow()

frontend/packages/erd-core/src/features/erd/components/ERDRenderer/CommandPalette/CommandPaletteOptions/TableOptions.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@ import { Command } from 'cmdk'
33
import { type FC, useCallback, useEffect } from 'react'
44
import { useSchemaOrThrow } from '../../../../../../stores'
55
import { useTableSelection } from '../../../../hooks'
6+
import { getTableLinkHref } from '../../../../utils/url/getTableLinkHref'
67
import { useCommandPaletteOrThrow } from '../CommandPaletteProvider'
78
import type { CommandPaletteSuggestion } from '../types'
89
import { getSuggestionText } from '../utils'
910
import styles from './CommandPaletteOptions.module.css'
1011

11-
const getTableLinkHref = (activeTableName: string) => {
12-
const searchParams = new URLSearchParams(window.location.search)
13-
searchParams.set('active', activeTableName)
14-
return `?${searchParams.toString()}`
15-
}
16-
1712
type Props = {
1813
suggestion: CommandPaletteSuggestion | null
1914
}

frontend/packages/erd-core/src/features/erd/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './cookieUtils'
55
export * from './createHash'
66
export * from './highlightNodesAndEdges'
77
export * from './isTableNode'
8+
export * from './url'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { Hash } from '../../../../schemas'
2+
3+
export const getTableColumnElementId = (
4+
tableName: string,
5+
columnName: string,
6+
): Hash => `${tableName}__columns__${columnName}`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getTableLinkHref } from './getTableLinkHref'
3+
4+
it('should return the "active" query parameter with the table name', () => {
5+
window.location.search = ''
6+
7+
expect(getTableLinkHref('users')).toBe('?active=users')
8+
})
9+
10+
describe('when other query parameters are present', () => {
11+
it('should preserve existing query parameters', () => {
12+
window.location.search = '?page=2&sort=asc'
13+
14+
expect(getTableLinkHref('users')).toBe('?page=2&sort=asc&active=users')
15+
})
16+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { QueryParam } from '../../../../schemas'
2+
3+
export const getTableLinkHref = (activeTableName: string) => {
4+
const searchParams = new URLSearchParams(window.location.search)
5+
searchParams.set('active' satisfies QueryParam, activeTableName)
6+
return `?${searchParams.toString()}`
7+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './getTableColumnElementId'
2+
export * from './getTableLinkHref'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './schemas'
2+
export * from './types'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { parse } from 'valibot'
2+
import { expect, it } from 'vitest'
3+
import { hashSchema } from './schemas'
4+
5+
it('should pass valid texts', () => {
6+
expect(parse(hashSchema, 'users__columns__id')).toBe('users__columns__id')
7+
expect(parse(hashSchema, 'user_posts__columns__post_id')).toBe(
8+
'user_posts__columns__post_id',
9+
)
10+
})
11+
12+
it('should throw error with invalid texts', () => {
13+
expect(() => parse(hashSchema, '')).toThrowError()
14+
expect(() => parse(hashSchema, 'users')).toThrowError()
15+
expect(() => parse(hashSchema, 'a__b__c')).toThrowError()
16+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { custom } from 'valibot'
2+
3+
export const hashSchema = custom<`${string}__columns__${string}`>(
4+
(input): input is `${string}__columns__${string}` => {
5+
if (typeof input !== 'string') return false
6+
const parts = input.split('__columns__')
7+
return parts.length === 2
8+
},
9+
)

0 commit comments

Comments
 (0)