This commit is contained in:
@@ -123,7 +123,7 @@ export default (({ user }: Props) => {
|
|||||||
const wikiPage = await fetchWikiPage (String (wikiId ?? ''), { })
|
const wikiPage = await fetchWikiPage (String (wikiId ?? ''), { })
|
||||||
const tag = await fetchTagByName (wikiPage.title)
|
const tag = await fetchTagByName (wikiPage.title)
|
||||||
|
|
||||||
setPostCount (tag.postCount)
|
setPostCount (tag?.postCount ?? 0)
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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 ReactMarkdown from 'react-markdown'
|
||||||
import remarkGFM from 'remark-gfm'
|
import remarkGFM from 'remark-gfm'
|
||||||
|
|
||||||
import PrefetchLink from '@/components/PrefetchLink'
|
import PrefetchLink from '@/components/PrefetchLink'
|
||||||
import SectionTitle from '@/components/common/SectionTitle'
|
import SectionTitle from '@/components/common/SectionTitle'
|
||||||
import SubsectionTitle from '@/components/common/SubsectionTitle'
|
import SubsectionTitle from '@/components/common/SubsectionTitle'
|
||||||
import { apiGet } from '@/lib/api'
|
import { wikiKeys } from '@/lib/queryKeys'
|
||||||
import remarkWikiAutoLink from '@/lib/remark-wiki-autolink'
|
import remarkWikiAutoLink from '@/lib/remark-wiki-autolink'
|
||||||
|
import { fetchWikiPages } from '@/lib/wiki'
|
||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import type { Components } from 'react-markdown'
|
import type { Components } from 'react-markdown'
|
||||||
|
|
||||||
import type { WikiPage } from '@/types'
|
|
||||||
|
|
||||||
type Props = { title: string
|
type Props = { title: string
|
||||||
body?: string }
|
body?: string }
|
||||||
|
|
||||||
@@ -32,25 +32,15 @@ const mdComponents = { h1: ({ children }) => <SectionTitle>{children}</SectionT
|
|||||||
|
|
||||||
|
|
||||||
export default (({ title, body }: Props) => {
|
export default (({ title, body }: Props) => {
|
||||||
const [pageNames, setPageNames] = useState<string[]> ([])
|
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 (
|
const remarkPlugins = useMemo (
|
||||||
() => [() => remarkWikiAutoLink (pageNames), remarkGFM], [pageNames])
|
() => [() => remarkWikiAutoLink (pageNames), remarkGFM], [pageNames])
|
||||||
|
|
||||||
useEffect (() => {
|
|
||||||
void (async () => {
|
|
||||||
try
|
|
||||||
{
|
|
||||||
const data = await apiGet<WikiPage[]> ('/wiki')
|
|
||||||
setPageNames (data.map (page => page.title).sort ((a, b) => b.length - a.length))
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
setPageNames ([])
|
|
||||||
}
|
|
||||||
}) ()
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ReactMarkdown components={mdComponents} remarkPlugins={remarkPlugins}>
|
<ReactMarkdown components={mdComponents} remarkPlugins={remarkPlugins}>
|
||||||
{body || `このページは存在しません。[新規作成してください](/wiki/new?title=${ encodeURIComponent (title) })。`}
|
{body || `このページは存在しません。[新規作成してください](/wiki/new?title=${ encodeURIComponent (title) })。`}
|
||||||
|
|||||||
@@ -31,31 +31,20 @@ const prefetchWikiPageShow: Prefetcher = async (qc, url) => {
|
|||||||
|
|
||||||
const wikiPage = await qc.fetchQuery ({
|
const wikiPage = await qc.fetchQuery ({
|
||||||
queryKey: wikiKeys.show (title, { version }),
|
queryKey: wikiKeys.show (title, { version }),
|
||||||
queryFn: async () => {
|
queryFn: () => fetchWikiPageByTitle (title, { version }) })
|
||||||
try
|
|
||||||
|
if (wikiPage?.body)
|
||||||
{
|
{
|
||||||
return await fetchWikiPageByTitle (title, { version })
|
await qc.prefetchQuery ({
|
||||||
|
queryKey: wikiKeys.index ({ }),
|
||||||
|
queryFn: () => fetchWikiPages ({ }) })
|
||||||
}
|
}
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
} })
|
|
||||||
|
|
||||||
const effectiveTitle = wikiPage?.title ?? title
|
const effectiveTitle = wikiPage?.title ?? title
|
||||||
|
|
||||||
await qc.prefetchQuery ({
|
await qc.prefetchQuery ({
|
||||||
queryKey: tagsKeys.show (effectiveTitle),
|
queryKey: tagsKeys.show (effectiveTitle),
|
||||||
queryFn: async () => {
|
queryFn: () => fetchTagByName (effectiveTitle) })
|
||||||
try
|
|
||||||
{
|
|
||||||
return await fetchTagByName (effectiveTitle)
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
} })
|
|
||||||
|
|
||||||
if (version)
|
if (version)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -13,5 +13,5 @@ export const tagsKeys = {
|
|||||||
|
|
||||||
export const wikiKeys = {
|
export const wikiKeys = {
|
||||||
root: ['wiki'] as const,
|
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 }
|
show: (title: string, p: { version?: string }) => ['wiki', title, p] as const }
|
||||||
|
|||||||
@@ -3,5 +3,13 @@ import { apiGet } from '@/lib/api'
|
|||||||
import type { Tag } from '@/types'
|
import type { Tag } from '@/types'
|
||||||
|
|
||||||
|
|
||||||
export const fetchTagByName = async (name: string): Promise<Tag> =>
|
export const fetchTagByName = async (name: string): Promise<Tag | null> => {
|
||||||
await apiGet (`/tags/name/${ name }`)
|
try
|
||||||
|
{
|
||||||
|
return await apiGet (`/tags/name/${ name }`)
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ import { apiGet } from '@/lib/api'
|
|||||||
import type { WikiPage } from '@/types'
|
import type { WikiPage } from '@/types'
|
||||||
|
|
||||||
|
|
||||||
export const fetchWikiPages = async ({ title }: { title: string }) =>
|
export const fetchWikiPages = async (
|
||||||
|
{ title }: { title?: string },
|
||||||
|
): Promise<WikiPage[]> =>
|
||||||
await apiGet ('/wiki', { params: { title } })
|
await apiGet ('/wiki', { params: { title } })
|
||||||
|
|
||||||
|
|
||||||
@@ -17,6 +19,13 @@ export const fetchWikiPage = async (
|
|||||||
export const fetchWikiPageByTitle = async (
|
export const fetchWikiPageByTitle = async (
|
||||||
title: string,
|
title: string,
|
||||||
{ version }: { version?: string },
|
{ version }: { version?: string },
|
||||||
): Promise<WikiPage> =>
|
): Promise<WikiPage | null> => {
|
||||||
await apiGet (`/wiki/title/${ title }`,
|
try
|
||||||
{ params: version ? { version } : { } })
|
{
|
||||||
|
return await apiGet (`/wiki/title/${ title }`, { params: { version } })
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user