mirror of
https://github.com/documenso/documenso.git
synced 2026-10-03 10:13:52 +10:00
refactor: simplify template type filter to single-value enum
The type filter schema used comma-separated array parsing which was unnecessary for a single-select faceted filter. Moved URL param parsing to the client with parseToStringArray and simplified the tRPC schema to a plain optional TemplateType enum.
This commit is contained in:
@@ -1,16 +1,18 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
|
||||
import { Trans } from '@lingui/react/macro';
|
||||
import type { TemplateType } from '@prisma/client';
|
||||
import { EnvelopeType } from '@prisma/client';
|
||||
import { Bird } from 'lucide-react';
|
||||
import { useParams, useSearchParams } from 'react-router';
|
||||
|
||||
import { useSessionStorage } from '@documenso/lib/client-only/hooks/use-session-storage';
|
||||
import { FolderType } from '@documenso/lib/types/folder-type';
|
||||
import { ZFindSearchParamsSchema } from '@documenso/lib/types/search-params';
|
||||
import { formatAvatarUrl } from '@documenso/lib/utils/avatars';
|
||||
import { parseToStringArray } from '@documenso/lib/utils/params';
|
||||
import { formatDocumentsPath, formatTemplatesPath } from '@documenso/lib/utils/teams';
|
||||
import { trpc } from '@documenso/trpc/react';
|
||||
import { ZFindTemplatesRequestSchema } from '@documenso/trpc/server/template-router/schema';
|
||||
import { Avatar, AvatarFallback, AvatarImage } from '@documenso/ui/primitives/avatar';
|
||||
import type { RowSelectionState } from '@documenso/ui/primitives/data-table';
|
||||
|
||||
@@ -28,9 +30,8 @@ export function meta() {
|
||||
return appMetaTags('Templates');
|
||||
}
|
||||
|
||||
const ZTemplatesSearchParamsSchema = ZFindTemplatesRequestSchema.pick({
|
||||
const ZTemplatesSearchParamsSchema = ZFindSearchParamsSchema.pick({
|
||||
query: true,
|
||||
type: true,
|
||||
page: true,
|
||||
perPage: true,
|
||||
});
|
||||
@@ -47,6 +48,11 @@ export default function TemplatesPage() {
|
||||
[searchParams],
|
||||
);
|
||||
|
||||
const typeFilter = useMemo(() => {
|
||||
const selected = parseToStringArray(searchParams.get('type'));
|
||||
return selected.length === 1 ? (selected[0] as TemplateType) : undefined;
|
||||
}, [searchParams]);
|
||||
|
||||
const [rowSelection, setRowSelection] = useSessionStorage<RowSelectionState>(
|
||||
'templates-bulk-selection',
|
||||
{},
|
||||
@@ -63,6 +69,7 @@ export default function TemplatesPage() {
|
||||
|
||||
const { data, isLoading, isLoadingError } = trpc.template.findTemplates.useQuery({
|
||||
...findTemplatesSearchParams,
|
||||
type: typeFilter,
|
||||
folderId,
|
||||
});
|
||||
|
||||
|
||||
@@ -30,7 +30,6 @@ import {
|
||||
ZTemplateManySchema,
|
||||
ZTemplateSchema,
|
||||
} from '@documenso/lib/types/template';
|
||||
import { parseCommaSeparatedValues } from '@documenso/lib/utils/params';
|
||||
import { LegacyTemplateDirectLinkSchema } from '@documenso/prisma/types/template-legacy-schema';
|
||||
import { ZDocumentExternalIdSchema } from '@documenso/trpc/server/document-router/schema';
|
||||
|
||||
@@ -290,9 +289,7 @@ export const ZUpdateTemplateRequestSchema = z.object({
|
||||
export const ZUpdateTemplateResponseSchema = ZTemplateLiteSchema;
|
||||
|
||||
export const ZFindTemplatesRequestSchema = ZFindSearchParamsSchema.extend({
|
||||
type: z
|
||||
.preprocess(parseCommaSeparatedValues, z.array(z.nativeEnum(TemplateType)).optional())
|
||||
.describe('Filter templates by type.'),
|
||||
type: z.nativeEnum(TemplateType).describe('Filter templates by type.').optional(),
|
||||
folderId: z.string().describe('The ID of the folder to filter templates by.').optional(),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user