Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useUserEditingOrThrow,
} from '../../../../../../../../../stores'
import { useDiffStyle } from '../../../../../../../../diff/hooks/useDiffStyle'
import { getTableColumnElementId } from '../../../../../../../utils/url/getTableColumnElementId'
import { BlinkCircle } from '../../BlinkCircle/BlinkCircle'
import styles from './ColumnsItem.module.css'
import { Comment } from './Comment'
Expand All @@ -16,9 +17,6 @@ import { NotNull } from './NotNull'
import { PrimaryKey } from './PrimaryKey'
import { Type } from './Type'

const columnElementId = (tableName: string, columnName: string) =>
`${tableName}__columns__${columnName}`

type Props = {
tableId: string
column: Column
Expand All @@ -32,7 +30,7 @@ export const ColumnsItem: FC<Props> = ({
constraints,
focusedElementId,
}) => {
const elementId = columnElementId(tableId, column.name)
const elementId = getTableColumnElementId(tableId, column.name)

const { operations } = useSchemaOrThrow()
const { showDiff } = useUserEditingOrThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ import { Command } from 'cmdk'
import { type FC, useCallback, useEffect } from 'react'
import { useSchemaOrThrow } from '../../../../../../stores'
import { useTableSelection } from '../../../../hooks'
import { getTableLinkHref } from '../../../../utils/url/getTableLinkHref'
import { useCommandPaletteOrThrow } from '../CommandPaletteProvider'
import type { CommandPaletteSuggestion } from '../types'
import { getSuggestionText } from '../utils'
import styles from './CommandPaletteOptions.module.css'

const getTableLinkHref = (activeTableName: string) => {
const searchParams = new URLSearchParams(window.location.search)
searchParams.set('active', activeTableName)
return `?${searchParams.toString()}`
}

type Props = {
suggestion: CommandPaletteSuggestion | null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './cookieUtils'
export * from './createHash'
export * from './highlightNodesAndEdges'
export * from './isTableNode'
export * from './url'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Hash } from '../../../../schemas'

export const getTableColumnElementId = (
tableName: string,
columnName: string,
): Hash => `${tableName}__columns__${columnName}`
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from 'vitest'
import { getTableLinkHref } from './getTableLinkHref'

it('should return the "active" query parameter with the table name', () => {
window.location.search = ''

expect(getTableLinkHref('users')).toBe('?active=users')
})

describe('when other query parameters are present', () => {
it('should preserve existing query parameters', () => {
window.location.search = '?page=2&sort=asc'

expect(getTableLinkHref('users')).toBe('?page=2&sort=asc&active=users')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { QueryParam } from '../../../../schemas'

export const getTableLinkHref = (activeTableName: string) => {
const searchParams = new URLSearchParams(window.location.search)
searchParams.set('active' satisfies QueryParam, activeTableName)
return `?${searchParams.toString()}`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './getTableColumnElementId'
export * from './getTableLinkHref'
2 changes: 2 additions & 0 deletions frontend/packages/erd-core/src/schemas/hash/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './schemas'
export * from './types'
16 changes: 16 additions & 0 deletions frontend/packages/erd-core/src/schemas/hash/schemas.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { parse } from 'valibot'
import { expect, it } from 'vitest'
import { hashSchema } from './schemas'

it('should pass valid texts', () => {
expect(parse(hashSchema, 'users__columns__id')).toBe('users__columns__id')
expect(parse(hashSchema, 'user_posts__columns__post_id')).toBe(
'user_posts__columns__post_id',
)
})

it('should throw error with invalid texts', () => {
expect(() => parse(hashSchema, '')).toThrowError()
expect(() => parse(hashSchema, 'users')).toThrowError()
expect(() => parse(hashSchema, 'a__b__c')).toThrowError()
})
9 changes: 9 additions & 0 deletions frontend/packages/erd-core/src/schemas/hash/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { custom } from 'valibot'

export const hashSchema = custom<`${string}__columns__${string}`>(
(input): input is `${string}__columns__${string}` => {
if (typeof input !== 'string') return false
const parts = input.split('__columns__')
return parts.length === 2
},
)
4 changes: 4 additions & 0 deletions frontend/packages/erd-core/src/schemas/hash/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { InferOutput } from 'valibot'
import type { hashSchema } from './schemas'

export type Hash = InferOutput<typeof hashSchema>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Hash type is same with ${string}__columns__${string}. It will extend its definition when we allow other hash values by updating hashSchema.

1 change: 1 addition & 0 deletions frontend/packages/erd-core/src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './hash'
export * from './queryParam'
export * from './showMode'
export * from './version'