fix: use literal parser for view param

This commit is contained in:
yyh 2026-03-26 16:34:16 +08:00
parent 9c8a5ff527
commit e03c2be0ad
No known key found for this signature in database
2 changed files with 8 additions and 16 deletions

View File

@ -2,16 +2,16 @@ import { ViewType } from '@/app/components/workflow/types'
import { parseAsViewType } from '../search-params'
describe('workflow-app search params', () => {
it('should parse the new file view value and keep serializing it as file', () => {
it('should parse valid view values and serialize them unchanged', () => {
expect(parseAsViewType.parse('graph')).toBe(ViewType.graph)
expect(parseAsViewType.serialize(ViewType.graph)).toBe('graph')
expect(parseAsViewType.parse('file')).toBe(ViewType.file)
expect(parseAsViewType.serialize(ViewType.file)).toBe('file')
})
it('should keep supporting legacy skill view links by mapping them to file', () => {
expect(parseAsViewType.parse('skill')).toBe(ViewType.file)
})
it('should reject unsupported view values', () => {
expect(parseAsViewType.parse('skill')).toBeNull()
expect(parseAsViewType.parse('invalid-view')).toBeNull()
})
})

View File

@ -1,17 +1,9 @@
import { createParser } from 'nuqs'
import { parseAsStringLiteral } from 'nuqs'
import { ViewType } from '@/app/components/workflow/types'
const VIEW_TYPES = Object.values(ViewType) as ViewType[]
const VIEW_TYPES = Object.values(ViewType)
export const parseAsViewType = createParser<ViewType>({
parse: (value) => {
if (value === 'skill')
return ViewType.file
return VIEW_TYPES.includes(value as ViewType) ? value as ViewType : null
},
serialize: value => value,
})
export const parseAsViewType = parseAsStringLiteral(VIEW_TYPES)
.withDefault(ViewType.graph)
.withOptions({
history: 'push',