From d230d7474e106b4c9e498b5d99bb09df2967ca65 Mon Sep 17 00:00:00 2001 From: Noa Virellia Date: Fri, 13 Feb 2026 11:56:25 +0800 Subject: [PATCH] refactor: improve code quality Signed-off-by: Noa Virellia --- .../event-grid/event-grid.container.tsx | 47 +++++++----- .../events/joined-events.containers.tsx | 14 ++-- client/cms/src/components/login-form.tsx | 73 +++++++++++++------ .../src/components/profile/profile.view.tsx | 12 +-- client/cms/src/lib/utils.ts | 10 --- 5 files changed, 89 insertions(+), 67 deletions(-) diff --git a/client/cms/src/components/events/event-grid/event-grid.container.tsx b/client/cms/src/components/events/event-grid/event-grid.container.tsx index a9722fe..8e4da25 100644 --- a/client/cms/src/components/events/event-grid/event-grid.container.tsx +++ b/client/cms/src/components/events/event-grid/event-grid.container.tsx @@ -1,3 +1,4 @@ +import { useMemo } from 'react'; import { useEvents } from '@/hooks/data/useEvents'; import { Button } from '../../ui/button'; import { DialogTrigger } from '../../ui/dialog'; @@ -10,30 +11,36 @@ import { EventGridView } from './event-grid.view'; export function EventGridContainer() { const { data, isLoading } = useEvents(); + const events = useMemo(() => { + return data?.pages.flatMap(page => page.data ?? []).map(toEventInfo) ?? []; + }, [data]); + return ( isLoading ? : ( - page.data ?? []).map(toEventInfo)} - footer={(eventInfo) => { - const Container = eventInfo.requireKyc ? KycDialogContainer : EventJoinDialogContainer; + { + const Container = eventInfo.requireKyc ? KycDialogContainer : EventJoinDialogContainer; - return ( - - {eventInfo.isJoined ? ( - - ) : ( - - - - )} - - ); - }} - /> - ) + return ( + + {eventInfo.isJoined + ? ( + + ) + : ( + + + + )} + + ); + }} + /> + ) ); } diff --git a/client/cms/src/components/events/joined-events.containers.tsx b/client/cms/src/components/events/joined-events.containers.tsx index 5dbdbbd..76b7283 100644 --- a/client/cms/src/components/events/joined-events.containers.tsx +++ b/client/cms/src/components/events/joined-events.containers.tsx @@ -37,12 +37,12 @@ export function JoinedEventsContainer() { isLoading ? : ( - page.data ?? []).map(toEventInfo)} - footer={event => ( - - )} - /> - ) + page.data ?? []).map(toEventInfo)} + footer={event => ( + + )} + /> + ) ); } diff --git a/client/cms/src/components/login-form.tsx b/client/cms/src/components/login-form.tsx index d680048..d03042f 100644 --- a/client/cms/src/components/login-form.tsx +++ b/client/cms/src/components/login-form.tsx @@ -1,14 +1,17 @@ import type { TurnstileInstance } from '@marsidev/react-turnstile'; import type { AuthorizeSearchParams } from '@/routes/authorize'; import { Turnstile } from '@marsidev/react-turnstile'; +import { useForm } from '@tanstack/react-form'; import { useNavigate } from '@tanstack/react-router'; import { Loader2 } from 'lucide-react'; import { useRef, useState } from 'react'; import { toast } from 'sonner'; +import z from 'zod'; import NixOSLogo from '@/assets/nixos.svg?react'; import { Button } from '@/components/ui/button'; import { Field, + FieldError, FieldGroup, FieldLabel, } from '@/components/ui/field'; @@ -23,29 +26,43 @@ export function LoginForm({ }: React.ComponentProps<'div'> & { oauthParams: AuthorizeSearchParams; }) { - const formRef = useRef(null); const turnstileRef = useRef(null); const [token, setToken] = useState(import.meta.env.DEV ? 'turnstile_token' : null); const { mutateAsync, isPending } = useGetMagicLink(); const navigate = useNavigate(); - const handleSubmit = (event: React.FormEvent) => { - event.preventDefault(); - const formData = new FormData(formRef.current!); - const email = formData.get('email')! as string; - mutateAsync({ body: { email, turnstile_token: token!, ...oauthParams } }).then(() => { - void navigate({ to: '/magicLinkSent', search: { email } }); - }).catch((error) => { - console.error(error); - toast.error('请求登录链接失败'); - turnstileRef.current?.reset(); - }); - }; + const form = useForm({ + defaultValues: { + email: '', + }, + validators: { + onSubmit: z.object({ + email: z.string().email('请输入有效的邮箱地址'), + }), + }, + onSubmit: async ({ value }) => { + try { + await mutateAsync({ body: { email: value.email, turnstile_token: token!, ...oauthParams } }); + await navigate({ to: '/magicLinkSent', search: { email: value.email } }); + } + catch (error) { + console.error(error); + toast.error('请求登录链接失败'); + turnstileRef.current?.reset(); + } + }, + }); const isLoading = isPending || token === null; return (
-
+ { + e.preventDefault(); + e.stopPropagation(); + void form.handleSubmit(); + }} + >
@@ -54,16 +71,24 @@ export function LoginForm({ Nix CN CMS

欢迎来到 Nix CN CMS

- - 参会登记Email - - + + {field => ( + + 参会登记Email + field.handleChange(e.target.value)} + /> + + + )} +