From 830c90871202ae96b94d02129a48761725e193e2 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Sat, 25 Apr 2026 13:31:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(arch):=20=E7=A7=BB=E9=99=A4=20experime?= =?UTF-8?q?ntal=5Fdefaults=EF=BC=8C=E6=8F=90=E7=82=BC=20useInvalidateTodos?= =?UTF-8?q?=EF=BC=8C=E9=97=AD=E7=8E=AF=E8=8B=A5=E5=B9=B2=E6=82=AC=E6=8C=82?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - orpc.ts: 改为纯 createTanstackQueryUtils,不再依赖 experimental_ API - 抽出 src/client/queries/todo.ts 的 useInvalidateTodos,避免 query key 散落页面 - shutdown: setTimeout 内 db.$client.end() 失败也走 process.exit - 删除 db/index.ts 未被使用的 DB 类型导出 - 删除 env.ts 未被消费的 VITE_APP_TITLE,根 title 改为 package.json name - 清理 routes/index.tsx 的 JSX 区段注释、compose.yaml 注释掉的端口块、robots.txt URL 注释 --- compose.yaml | 2 -- public/robots.txt | 1 - src/client/orpc.ts | 28 +--------------------------- src/client/queries/todo.ts | 7 +++++++ src/env.ts | 4 +--- src/routes/__root.tsx | 3 ++- src/routes/index.tsx | 12 ++++++------ src/server/db/index.ts | 2 -- src/server/plugins/shutdown.ts | 7 +++---- 9 files changed, 20 insertions(+), 46 deletions(-) create mode 100644 src/client/queries/todo.ts diff --git a/compose.yaml b/compose.yaml index 057ef8e..8c41ccd 100644 --- a/compose.yaml +++ b/compose.yaml @@ -23,8 +23,6 @@ services: db: image: postgres:18-alpine - # ports: - # - "5432:5432" volumes: - postgres_data:/var/lib/postgresql environment: diff --git a/public/robots.txt b/public/robots.txt index e9e57dc..eb05362 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -1,3 +1,2 @@ -# https://www.robotstxt.org/robotstxt.html User-agent: * Disallow: diff --git a/src/client/orpc.ts b/src/client/orpc.ts index 6b9abab..7bea292 100644 --- a/src/client/orpc.ts +++ b/src/client/orpc.ts @@ -24,30 +24,4 @@ const getORPCClient = createIsomorphicFn() const client: RouterClient = getORPCClient() -export const orpc = createTanstackQueryUtils(client, { - experimental_defaults: { - todo: { - create: { - mutationOptions: { - onSuccess: (_, __, ___, ctx) => { - ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) - }, - }, - }, - update: { - mutationOptions: { - onSuccess: (_, __, ___, ctx) => { - ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) - }, - }, - }, - remove: { - mutationOptions: { - onSuccess: (_, __, ___, ctx) => { - ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) - }, - }, - }, - }, - }, -}) +export const orpc = createTanstackQueryUtils(client) diff --git a/src/client/queries/todo.ts b/src/client/queries/todo.ts new file mode 100644 index 0000000..c02a71b --- /dev/null +++ b/src/client/queries/todo.ts @@ -0,0 +1,7 @@ +import { useQueryClient } from '@tanstack/react-query' +import { orpc } from '@/client/orpc' + +export const useInvalidateTodos = () => { + const queryClient = useQueryClient() + return () => queryClient.invalidateQueries({ queryKey: orpc.todo.list.key() }) +} diff --git a/src/env.ts b/src/env.ts index e8825d9..c2a89c6 100644 --- a/src/env.ts +++ b/src/env.ts @@ -6,9 +6,7 @@ export const env = createEnv({ DATABASE_URL: z.url(), }, clientPrefix: 'VITE_', - client: { - VITE_APP_TITLE: z.string().min(1).optional(), - }, + client: {}, runtimeEnv: process.env, emptyStringAsUndefined: true, }) diff --git a/src/routes/__root.tsx b/src/routes/__root.tsx index 5dea9b1..a26a840 100644 --- a/src/routes/__root.tsx +++ b/src/routes/__root.tsx @@ -4,6 +4,7 @@ import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools' import { createRootRouteWithContext, HeadContent, Scripts } from '@tanstack/react-router' import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools' import type { ReactNode } from 'react' +import { name } from '@/../package.json' import { ErrorComponent } from '@/components/Error' import { NotFoundComponent } from '@/components/NotFound' import appCss from '@/styles.css?url' @@ -23,7 +24,7 @@ export const Route = createRootRouteWithContext()({ content: 'width=device-width, initial-scale=1', }, { - title: 'Furtherverse', + title: name, }, ], links: [ diff --git a/src/routes/index.tsx b/src/routes/index.tsx index d3d052e..bc7fc53 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -1,6 +1,7 @@ import { useMutation, useSuspenseQuery } from '@tanstack/react-query' import { createFileRoute } from '@tanstack/react-router' import { orpc } from '@/client/orpc' +import { useInvalidateTodos } from '@/client/queries/todo' import { TodoForm } from '@/components/TodoForm' import { TodoItem } from '@/components/TodoItem' @@ -13,9 +14,11 @@ export const Route = createFileRoute('/')({ function Todos() { const listQuery = useSuspenseQuery(orpc.todo.list.queryOptions()) - const createMutation = useMutation(orpc.todo.create.mutationOptions()) - const updateMutation = useMutation(orpc.todo.update.mutationOptions()) - const deleteMutation = useMutation(orpc.todo.remove.mutationOptions()) + const invalidateTodos = useInvalidateTodos() + + const createMutation = useMutation(orpc.todo.create.mutationOptions({ onSuccess: invalidateTodos })) + const updateMutation = useMutation(orpc.todo.update.mutationOptions({ onSuccess: invalidateTodos })) + const deleteMutation = useMutation(orpc.todo.remove.mutationOptions({ onSuccess: invalidateTodos })) const todos = listQuery.data const completedCount = todos.filter((todo) => todo.completed).length @@ -25,7 +28,6 @@ function Todos() { return (
- {/* Header */}

我的待办

@@ -42,7 +44,6 @@ function Todos() { createMutation.mutate({ title })} isPending={createMutation.isPending} /> - {/* Progress Bar */} {totalCount > 0 && (
)} - {/* Todo List */}
{todos.length === 0 ? (
diff --git a/src/server/db/index.ts b/src/server/db/index.ts index 14db5da..7635f26 100644 --- a/src/server/db/index.ts +++ b/src/server/db/index.ts @@ -6,5 +6,3 @@ export const db = drizzle({ connection: env.DATABASE_URL, schema, }) - -export type DB = typeof db diff --git a/src/server/plugins/shutdown.ts b/src/server/plugins/shutdown.ts index b402e5a..1f54272 100644 --- a/src/server/plugins/shutdown.ts +++ b/src/server/plugins/shutdown.ts @@ -5,15 +5,14 @@ export default () => { let exiting = false - const shutdown = async () => { + const shutdown = () => { if (exiting) { process.exit(0) } exiting = true - setTimeout(async () => { - await db.$client.end() - process.exit(0) + setTimeout(() => { + db.$client.end().finally(() => process.exit(0)) }, 500) }