diff --git a/frontend/src/components/WikiBody.tsx b/frontend/src/components/WikiBody.tsx new file mode 100644 index 0000000..d3d707f --- /dev/null +++ b/frontend/src/components/WikiBody.tsx @@ -0,0 +1,13 @@ +import ReactMarkdown from 'react-markdown' +import { Link } from 'react-router-dom' + +type Props = { body: string } + + +export default ({ body }: Props) => ( + (['/', '.'].some (e => href?.startsWith (e)) + ? {children} + : {children})) }}> + {body} + ) diff --git a/frontend/src/components/common/TabGroup.tsx b/frontend/src/components/common/TabGroup.tsx index 4d789cc..45f669a 100644 --- a/frontend/src/components/common/TabGroup.tsx +++ b/frontend/src/components/common/TabGroup.tsx @@ -5,7 +5,7 @@ type TabProps = { name: string init?: boolean children: React.ReactNode } -type Props = { children: React.ReactElement<{ name: string }>[] } +type Props = { children: React.ReactElement[] } export const Tab = ({ children }: TabProps) => <>{children} diff --git a/frontend/src/pages/posts/PostListPage.tsx b/frontend/src/pages/posts/PostListPage.tsx index 7cbda8f..13a6256 100644 --- a/frontend/src/pages/posts/PostListPage.tsx +++ b/frontend/src/pages/posts/PostListPage.tsx @@ -1,17 +1,21 @@ import axios from 'axios' +import toCamel from 'camelcase-keys' import React, { useEffect, useState } from 'react' import { Helmet } from 'react-helmet' import { Link, useLocation } from 'react-router-dom' import TagSidebar from '@/components/TagSidebar' +import WikiBody from '@/components/WikiBody' +import TabGroup, { Tab } from '@/components/common/TabGroup' import MainArea from '@/components/layout/MainArea' import { API_BASE_URL, SITE_TITLE } from '@/config' -import type { Post, Tag } from '@/types' +import type { Post, Tag, WikiPage } from '@/types' export default () => { const [posts, setPosts] = useState ([]) + const [wikiPage, setWikiPage] = useState (null) const location = useLocation () const query = new URLSearchParams (location.search) @@ -21,21 +25,20 @@ export default () => { const tags = tagsQuery.split (' ').filter (e => e !== '') useEffect(() => { - const fetchPosts = async () => { - try - { - const res = await axios.get (`${ API_BASE_URL }/posts`, { - params: { tags: tags.join (','), - match: (anyFlg ? 'any' : 'all') } }) - setPosts (res.data) - } - catch (error) - { - console.error ('Failed to fetch posts:', error) - setPosts ([]) - } - } - fetchPosts() + void (axios.get (`${ API_BASE_URL }/posts`, { + params: { tags: tags.join (','), + match: (anyFlg ? 'any' : 'all') } }) + .then (res => setPosts (res.data)) + .catch (err => { + console.error ('Failed to fetch posts:', err) + setPosts ([]) + })) + + if (!(tags.length)) + return + void (axios.get (`${ API_BASE_URL }/wiki/title/${ encodeURIComponent (tags.join (' ')) }`) + .then (res => setWikiPage (toCamel (res.data, { deep: true }))) + .catch (() => setWikiPage (null))) }, [location.search]) return ( @@ -49,16 +52,30 @@ export default () => { -
- {posts.map (post => ( - - - - ))} -
+ + + {posts.length + ? ( +
+ {posts.map (post => ( + + + + ))} +
) + : '広場には何もありませんよ.'} +
+ {(wikiPage && wikiPage.body) && ( + + +
+ Wiki を見る +
+
)} +
) } diff --git a/frontend/src/pages/wiki/WikiDetailPage.tsx b/frontend/src/pages/wiki/WikiDetailPage.tsx index a3cff73..c60b8a3 100644 --- a/frontend/src/pages/wiki/WikiDetailPage.tsx +++ b/frontend/src/pages/wiki/WikiDetailPage.tsx @@ -2,9 +2,9 @@ import axios from 'axios' import toCamel from 'camelcase-keys' import { useEffect, useState } from 'react' import { Helmet } from 'react-helmet' -import ReactMarkdown from 'react-markdown' import { Link, useLocation, useParams, useNavigate } from 'react-router-dom' +import WikiBody from '@/components/WikiBody' import PageTitle from '@/components/common/PageTitle' import MainArea from '@/components/layout/MainArea' import { API_BASE_URL, SITE_TITLE } from '@/config' @@ -61,15 +61,9 @@ export default () => { )} {title}
- {wikiPage === undefined ? 'Loading...' : ( - <> - (['/', '.'].some (e => href?.startsWith (e)) - ? {children} - : {children})) }}> - {wikiPage?.body || `このページは存在しません。[新規作成してください](/wiki/new?title=${ title })。`} - - )} + {wikiPage === undefined + ? 'Loading...' + : }
) }