バグ修正(#253) #254
@@ -2,7 +2,8 @@ import { QueryClient } from '@tanstack/react-query'
|
|||||||
import { match } from 'path-to-regexp'
|
import { match } from 'path-to-regexp'
|
||||||
|
|
||||||
import { fetchPost, fetchPosts, fetchPostChanges } from '@/lib/posts'
|
import { fetchPost, fetchPosts, fetchPostChanges } from '@/lib/posts'
|
||||||
import { postsKeys, wikiKeys } from '@/lib/queryKeys'
|
import { postsKeys, tagsKeys, wikiKeys } from '@/lib/queryKeys'
|
||||||
|
import { fetchTagByName } from '@/lib/tags'
|
||||||
import { fetchWikiPageByTitle, fetchWikiPages } from '@/lib/wiki'
|
import { fetchWikiPageByTitle, fetchWikiPages } from '@/lib/wiki'
|
||||||
|
|
||||||
type Prefetcher = (qc: QueryClient, url: URL) => Promise<void>
|
type Prefetcher = (qc: QueryClient, url: URL) => Promise<void>
|
||||||
@@ -13,6 +14,7 @@ const mWiki = match<{ title: string }> ('/wiki/:title')
|
|||||||
|
|
||||||
const prefetchWikiPagesIndex: Prefetcher = async (qc, url) => {
|
const prefetchWikiPagesIndex: Prefetcher = async (qc, url) => {
|
||||||
const title = url.searchParams.get ('title') ?? ''
|
const title = url.searchParams.get ('title') ?? ''
|
||||||
|
|
||||||
await qc.prefetchQuery ({
|
await qc.prefetchQuery ({
|
||||||
queryKey: wikiKeys.index ({ title }),
|
queryKey: wikiKeys.index ({ title }),
|
||||||
queryFn: () => fetchWikiPages ({ title }) })
|
queryFn: () => fetchWikiPages ({ title }) })
|
||||||
@@ -24,11 +26,26 @@ const prefetchWikiPageShow: Prefetcher = async (qc, url) => {
|
|||||||
if (!(m))
|
if (!(m))
|
||||||
return
|
return
|
||||||
|
|
||||||
const version = url.searchParams.get ('version')
|
const title = decodeURIComponent (m.params.title)
|
||||||
const { title } = m.params
|
const version = url.searchParams.get ('version') || undefined
|
||||||
|
|
||||||
|
const wikiPage = await qc.fetchQuery ({
|
||||||
|
queryKey: wikiKeys.show (title, { version }),
|
||||||
|
queryFn: () => fetchWikiPageByTitle (title, { version }) })
|
||||||
|
|
||||||
|
const effectiveTitle = wikiPage.title
|
||||||
|
|
||||||
await qc.prefetchQuery ({
|
await qc.prefetchQuery ({
|
||||||
queryKey: wikiKeys.show (title, version ? { version } : { }),
|
queryKey: tagsKeys.show (effectiveTitle),
|
||||||
queryFn: () => fetchWikiPageByTitle (title, version ? { version } : { }) })
|
queryFn: () => fetchTagByName (effectiveTitle) })
|
||||||
|
|
||||||
|
if (version)
|
||||||
|
return
|
||||||
|
|
||||||
|
const p = { tags: effectiveTitle, match: 'all', page: 1, limit: 8 } as const
|
||||||
|
await qc.prefetchQuery ({
|
||||||
|
queryKey: postsKeys.index (p),
|
||||||
|
queryFn: () => fetchPosts (p) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -37,6 +54,7 @@ const prefetchPostsIndex: Prefetcher = async (qc, url) => {
|
|||||||
const m = url.searchParams.get ('match') === 'any' ? 'any' : 'all'
|
const m = url.searchParams.get ('match') === 'any' ? 'any' : 'all'
|
||||||
const page = Number (url.searchParams.get ('page') || 1)
|
const page = Number (url.searchParams.get ('page') || 1)
|
||||||
const limit = Number (url.searchParams.get ('limit') || 20)
|
const limit = Number (url.searchParams.get ('limit') || 20)
|
||||||
|
|
||||||
await qc.prefetchQuery ({
|
await qc.prefetchQuery ({
|
||||||
queryKey: postsKeys.index ({ tags, match: m, page, limit }),
|
queryKey: postsKeys.index ({ tags, match: m, page, limit }),
|
||||||
queryFn: () => fetchPosts ({ tags, match: m, page, limit }) })
|
queryFn: () => fetchPosts ({ tags, match: m, page, limit }) })
|
||||||
@@ -49,6 +67,7 @@ const prefetchPostShow: Prefetcher = async (qc, url) => {
|
|||||||
return
|
return
|
||||||
|
|
||||||
const { id } = m.params
|
const { id } = m.params
|
||||||
|
|
||||||
await qc.prefetchQuery ({
|
await qc.prefetchQuery ({
|
||||||
queryKey: postsKeys.show (id),
|
queryKey: postsKeys.show (id),
|
||||||
queryFn: () => fetchPost (id) })
|
queryFn: () => fetchPost (id) })
|
||||||
@@ -59,6 +78,7 @@ const prefetchPostChanges: Prefetcher = async (qc, url) => {
|
|||||||
const id = url.searchParams.get ('id')
|
const id = url.searchParams.get ('id')
|
||||||
const page = Number (url.searchParams.get ('page') || 1)
|
const page = Number (url.searchParams.get ('page') || 1)
|
||||||
const limit = Number (url.searchParams.get ('limit') || 20)
|
const limit = Number (url.searchParams.get ('limit') || 20)
|
||||||
|
|
||||||
await qc.prefetchQuery ({
|
await qc.prefetchQuery ({
|
||||||
queryKey: postsKeys.changes ({ ...(id && { id }), page, limit }),
|
queryKey: postsKeys.changes ({ ...(id && { id }), page, limit }),
|
||||||
queryFn: () => fetchPostChanges ({ ...(id && { id }), page, limit }) })
|
queryFn: () => fetchPostChanges ({ ...(id && { id }), page, limit }) })
|
||||||
|
|||||||
@@ -30,12 +30,12 @@ export default () => {
|
|||||||
const defaultTag = useMemo (() => ({ name: title, category: 'general' } as Tag), [title])
|
const defaultTag = useMemo (() => ({ name: title, category: 'general' } as Tag), [title])
|
||||||
|
|
||||||
const query = new URLSearchParams (location.search)
|
const query = new URLSearchParams (location.search)
|
||||||
const version = query.get ('version')
|
const version = query.get ('version') || undefined
|
||||||
|
|
||||||
const { data: wikiPage } = useQuery ({
|
const { data: wikiPage, isLoading: loading } = useQuery ({
|
||||||
enabled: Boolean (title) && !(/^\d+$/.test (title)),
|
enabled: Boolean (title) && !(/^\d+$/.test (title)),
|
||||||
queryKey: wikiKeys.show (title ?? '', version ? { version } : { }),
|
queryKey: wikiKeys.show (title, { version }),
|
||||||
queryFn: () => fetchWikiPageByTitle (title ?? '', version ? { version } : { }) })
|
queryFn: () => fetchWikiPageByTitle (title, { version }) })
|
||||||
|
|
||||||
const effectiveTitle = wikiPage?.title ?? title
|
const effectiveTitle = wikiPage?.title ?? title
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ export default () => {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
}) ()
|
}) ()
|
||||||
}, [title, version])
|
}, [title, navigate])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MainArea>
|
<MainArea>
|
||||||
@@ -109,9 +109,7 @@ export default () => {
|
|||||||
{...(version && { to: `/wiki/${ encodeURIComponent (title) }` })}/>
|
{...(version && { to: `/wiki/${ encodeURIComponent (title) }` })}/>
|
||||||
</PageTitle>
|
</PageTitle>
|
||||||
<div className="prose mx-auto p-4">
|
<div className="prose mx-auto p-4">
|
||||||
{wikiPage === undefined
|
{loading ? 'Loading...' : <WikiBody title={title} body={wikiPage?.body}/>}
|
||||||
? 'Loading...'
|
|
||||||
: <WikiBody title={title} body={wikiPage?.body}/>}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{(!(version) && posts.length > 0) && (
|
{(!(version) && posts.length > 0) && (
|
||||||
|
|||||||
Reference in New Issue
Block a user