Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add url query params for quick access #248

Merged
merged 5 commits into from
Dec 16, 2024
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
6 changes: 6 additions & 0 deletions frontend/.changeset/stupid-insects-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@liam-hq/erd-core": patch
"@liam-hq/cli": patch
---

Add url query params for quick access
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { type FC, useCallback, useEffect } from 'react'
import styles from './ERDContent.module.css'
import { RelationshipEdge } from './RelationshipEdge'
import { TableNode } from './TableNode'
import { useActiveTableNameFromUrl } from './useActiveTableNameFromUrl'
import { useAutoLayout } from './useAutoLayout'
import { useFitViewWhenActiveTableChange } from './useFitViewWhenActiveTableChange'

Expand Down Expand Up @@ -70,6 +71,7 @@ export const ERDContent: FC<Props> = ({
}, [_nodes, _edges, setNodes, setEdges])

useAutoLayout()
useActiveTableNameFromUrl()
useFitViewWhenActiveTableChange(
enabledFeatures?.fitViewWhenActiveTableChange ?? true,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { QueryParam } from '@/schemas/queryParam'
import { updateActiveTableName } from '@/stores'
import { useEffect } from 'react'

export const useActiveTableNameFromUrl = () => {
useEffect(() => {
const urlParams = new URLSearchParams(window.location.search)
const activeQueryParam: QueryParam = 'active'
const tableFromUrl = urlParams.get(activeQueryParam)

if (!tableFromUrl) return

updateActiveTableName(tableFromUrl)
}, [])
}
2 changes: 2 additions & 0 deletions frontend/packages/erd-core/src/schemas/queryParam/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './schemas'
export * from './types'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { picklist } from 'valibot'

export const queryParamSchema = picklist(['active'])
4 changes: 4 additions & 0 deletions frontend/packages/erd-core/src/schemas/queryParam/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { InferOutput } from 'valibot'
import type { queryParamSchema } from './schemas'

export type QueryParam = InferOutput<typeof queryParamSchema>
17 changes: 16 additions & 1 deletion frontend/packages/erd-core/src/stores/userEditing/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { QueryParam } from '@/schemas/queryParam'
import type { ShowMode } from '@/schemas/showMode'
import { proxy } from 'valtio'
import { proxy, subscribe } from 'valtio'

type UserEditingStore = {
active: {
Expand All @@ -14,3 +15,17 @@ export const userEditingStore = proxy<UserEditingStore>({
},
showMode: 'ALL_FIELDS',
})

subscribe(userEditingStore.active, () => {
const newTableName = userEditingStore.active.tableName
const url = new URL(window.location.href)
const activeQueryParam: QueryParam = 'active'

if (newTableName) {
url.searchParams.set(activeQueryParam, newTableName)
} else {
url.searchParams.delete(activeQueryParam)
}

window.history.pushState({}, '', url)
})
Loading