From 4c5668653f192345daa94b879236efe4586b6a82 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sat, 7 Feb 2026 01:01:10 +0900 Subject: [PATCH] #140 --- frontend/src/components/TopNav.tsx | 2 +- frontend/src/components/WikiBody.tsx | 28 +++++++++------------------ frontend/src/lib/prefetchers.ts | 29 +++++++++------------------- frontend/src/lib/queryKeys.ts | 2 +- frontend/src/lib/tags.ts | 12 ++++++++++-- frontend/src/lib/wiki.ts | 17 ++++++++++++---- 6 files changed, 43 insertions(+), 47 deletions(-) diff --git a/frontend/src/components/TopNav.tsx b/frontend/src/components/TopNav.tsx index 6d77c95..f8bf4d4 100644 --- a/frontend/src/components/TopNav.tsx +++ b/frontend/src/components/TopNav.tsx @@ -123,7 +123,7 @@ export default (({ user }: Props) => { const wikiPage = await fetchWikiPage (String (wikiId ?? ''), { }) const tag = await fetchTagByName (wikiPage.title) - setPostCount (tag.postCount) + setPostCount (tag?.postCount ?? 0) } catch { diff --git a/frontend/src/components/WikiBody.tsx b/frontend/src/components/WikiBody.tsx index 7851e5a..50316b2 100644 --- a/frontend/src/components/WikiBody.tsx +++ b/frontend/src/components/WikiBody.tsx @@ -1,18 +1,18 @@ -import { useEffect, useMemo, useState } from 'react' +import { useQuery } from '@tanstack/react-query' +import { useMemo } from 'react' import ReactMarkdown from 'react-markdown' import remarkGFM from 'remark-gfm' import PrefetchLink from '@/components/PrefetchLink' import SectionTitle from '@/components/common/SectionTitle' import SubsectionTitle from '@/components/common/SubsectionTitle' -import { apiGet } from '@/lib/api' +import { wikiKeys } from '@/lib/queryKeys' import remarkWikiAutoLink from '@/lib/remark-wiki-autolink' +import { fetchWikiPages } from '@/lib/wiki' import type { FC } from 'react' import type { Components } from 'react-markdown' -import type { WikiPage } from '@/types' - type Props = { title: string body?: string } @@ -32,25 +32,15 @@ const mdComponents = { h1: ({ children }) => {children} { - const [pageNames, setPageNames] = useState ([]) + const { data } = useQuery ({ + enabled: Boolean (body), + queryKey: wikiKeys.index ({ }), + queryFn: () => fetchWikiPages ({ }) }) + const pageNames = (data ?? []).map (page => page.title).sort ((a, b) => b.length - a.length) const remarkPlugins = useMemo ( () => [() => remarkWikiAutoLink (pageNames), remarkGFM], [pageNames]) - useEffect (() => { - void (async () => { - try - { - const data = await apiGet ('/wiki') - setPageNames (data.map (page => page.title).sort ((a, b) => b.length - a.length)) - } - catch - { - setPageNames ([]) - } - }) () - }, []) - return ( {body || `このページは存在しません。[新規作成してください](/wiki/new?title=${ encodeURIComponent (title) })。`} diff --git a/frontend/src/lib/prefetchers.ts b/frontend/src/lib/prefetchers.ts index 335f511..f034459 100644 --- a/frontend/src/lib/prefetchers.ts +++ b/frontend/src/lib/prefetchers.ts @@ -31,31 +31,20 @@ const prefetchWikiPageShow: Prefetcher = async (qc, url) => { const wikiPage = await qc.fetchQuery ({ queryKey: wikiKeys.show (title, { version }), - queryFn: async () => { - try - { - return await fetchWikiPageByTitle (title, { version }) - } - catch - { - return null - } - } }) + queryFn: () => fetchWikiPageByTitle (title, { version }) }) + + if (wikiPage?.body) + { + await qc.prefetchQuery ({ + queryKey: wikiKeys.index ({ }), + queryFn: () => fetchWikiPages ({ }) }) + } const effectiveTitle = wikiPage?.title ?? title await qc.prefetchQuery ({ queryKey: tagsKeys.show (effectiveTitle), - queryFn: async () => { - try - { - return await fetchTagByName (effectiveTitle) - } - catch - { - return null - } - } }) + queryFn: () => fetchTagByName (effectiveTitle) }) if (version) return diff --git a/frontend/src/lib/queryKeys.ts b/frontend/src/lib/queryKeys.ts index a6816be..909d54e 100644 --- a/frontend/src/lib/queryKeys.ts +++ b/frontend/src/lib/queryKeys.ts @@ -13,5 +13,5 @@ export const tagsKeys = { export const wikiKeys = { root: ['wiki'] as const, - index: (p: { title: string }) => ['wiki', 'index', p] as const, + index: (p: { title?: string }) => ['wiki', 'index', p] as const, show: (title: string, p: { version?: string }) => ['wiki', title, p] as const } diff --git a/frontend/src/lib/tags.ts b/frontend/src/lib/tags.ts index ce7650b..3d25684 100644 --- a/frontend/src/lib/tags.ts +++ b/frontend/src/lib/tags.ts @@ -3,5 +3,13 @@ import { apiGet } from '@/lib/api' import type { Tag } from '@/types' -export const fetchTagByName = async (name: string): Promise => - await apiGet (`/tags/name/${ name }`) +export const fetchTagByName = async (name: string): Promise => { + try + { + return await apiGet (`/tags/name/${ name }`) + } + catch + { + return null + } +} diff --git a/frontend/src/lib/wiki.ts b/frontend/src/lib/wiki.ts index ce07111..2899dad 100644 --- a/frontend/src/lib/wiki.ts +++ b/frontend/src/lib/wiki.ts @@ -3,7 +3,9 @@ import { apiGet } from '@/lib/api' import type { WikiPage } from '@/types' -export const fetchWikiPages = async ({ title }: { title: string }) => +export const fetchWikiPages = async ( + { title }: { title?: string }, +): Promise => await apiGet ('/wiki', { params: { title } }) @@ -17,6 +19,13 @@ export const fetchWikiPage = async ( export const fetchWikiPageByTitle = async ( title: string, { version }: { version?: string }, -): Promise => - await apiGet (`/wiki/title/${ title }`, - { params: version ? { version } : { } }) +): Promise => { + try + { + return await apiGet (`/wiki/title/${ title }`, { params: { version } }) + } + catch + { + return null + } +}