Files
dify/web/app/components/apps/query-params.ts

59 lines
1.9 KiB
TypeScript

import type { GetAppsData } from '@dify/contracts/api/console/apps/types.gen'
import type { inferParserType } from 'nuqs'
import { zGetAppsQuery } from '@dify/contracts/api/console/apps/zod.gen'
import {
createParser,
createSerializer,
debounce,
parseAsArrayOf,
parseAsString,
parseAsStringLiteral,
} from 'nuqs'
import { APP_TYPES } from './app-type'
import { APP_LIST_SEARCH_DEBOUNCE_MS } from './constants'
type AppListMode = NonNullable<NonNullable<GetAppsData['query']>['mode']>
export const studioAppListCategories = [
'all',
...APP_TYPES,
] as const satisfies readonly AppListMode[]
const studioAppListCategorySchema = zGetAppsQuery.shape.mode
.unwrap()
.unwrap()
.extract(studioAppListCategories)
const parseAsAppListCategory = createParser({
parse: (value) => {
const result = studioAppListCategorySchema.safeParse(value)
return result.success ? result.data : null
},
serialize: String,
})
.withDefault('all')
.withOptions({ history: 'push' })
export const appListQueryParsers = {
category: parseAsAppListCategory,
creatorIDs: parseAsArrayOf(parseAsString, ';').withDefault([]),
tagIDs: parseAsArrayOf(parseAsString, ';').withDefault([]),
sort_by: parseAsStringLiteral(zGetAppsQuery.shape.sort_by.unwrap().unwrap().options).withDefault(
zGetAppsQuery.shape.sort_by.parse(undefined),
),
keywords: parseAsString.withDefault('').withOptions({
limitUrlUpdates: debounce(APP_LIST_SEARCH_DEBOUNCE_MS),
}),
}
export const serializeAppListQuery = createSerializer(appListQueryParsers)
// The Studio list opens its creation dialogs from the URL: `create` picks the
// dialog and `mode` the default app type for blank creation.
export const appCreationQueryParsers = {
create: parseAsStringLiteral(['blank', 'template'] as const),
mode: parseAsStringLiteral(APP_TYPES),
}
export type AppListUrlQuery = inferParserType<typeof appListQueryParsers>