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['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) export type AppListUrlQuery = inferParserType