dh_demo

DreamHanks demo project
git clone git://git.lair.cx/dh_demo
Log | Files | Refs | README

commit a89436ad143e14faee0900736f554df4f085454b
parent a268d02c119d67cd04ae7feea93f37201dd161e7
Author: Yongbin Kim <iam@yongbin.kim>
Date:   Sun, 29 Jan 2023 14:03:51 +0900

feat: wiki page를 찾지 못했을 때 404 대신 수정 페이지 링크 띄우도록 수정

Signed-off-by: Yongbin Kim <iam@yongbin.kim>

Diffstat:
Mpages/wiki/[slug]/[...path].tsx | 82++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 44 insertions(+), 38 deletions(-)

diff --git a/pages/wiki/[slug]/[...path].tsx b/pages/wiki/[slug]/[...path].tsx @@ -1,3 +1,5 @@ +import Container from '@/components/layout/Container' +import Hero from '@/components/layout/Hero' import Section from '@/components/layout/Section' import RevisionAlert from '@/components/wiki/RevisionAlert' import WikiArticle from '@/components/wiki/WikiArticle' @@ -17,9 +19,11 @@ import Link from 'next/link' import { useRouter } from 'next/router' import { Fragment, useMemo } from 'react' -export interface WikiViewPageProps { +export type WikiViewPageProps = { page: WikiPage html: string +} | { + page: null } export const getServerSideProps: GetServerSideProps<WikiViewPageProps> = async (context) => { @@ -36,22 +40,17 @@ export const getServerSideProps: GetServerSideProps<WikiViewPageProps> = async ( const token = await authenticationFromCookies(context.req.cookies) - const result = await withConnection(async (conn): - Promise<[page: WikiPage, html: string] | null> => { + return await withConnection(async (conn) => { const wiki = await getWikiViaSlug(conn, [slug]) - if (wiki == null) { - return null - } - - if (!resolveACL(token, wiki.acl, ACL_ACTION_READ)) { - return null + if (wiki == null || !resolveACL(token, wiki.acl, ACL_ACTION_READ)) { + return { notFound: true } } const page = revId == null ? await getWikiPage(conn, [wiki.id, path]) : await getWikiPageRevision(conn, [wiki.id, path, revId]) if (page == null) { - return null + return { props: { page: null } } } if (await hasHtmlCache(page.textId)) { @@ -59,34 +58,32 @@ export const getServerSideProps: GetServerSideProps<WikiViewPageProps> = async ( if (html != null) { await refreshHtmlCache(page.textId) - return [page, html] + return { + props: { + page: page, + html: html, + }, + } } } // Cache miss; do render const wikiText = await getWikiText(conn, [page.textId]) if (wikiText == null) { - return null + return { props: { page: null } } } const source = await getStringFromWikiText(wikiText) const html = render(source) await putHtmlCache(page.textId, html) - return [page, html] + return { + props: { + page: page, + html: html, + }, + } }) - if (result == null) { - return { notFound: true } - } - - const [page, html] = result - - return { - props: { - page: page, - html: html, - }, - } } export default function WikiViewPage (props: WikiViewPageProps) { @@ -96,19 +93,28 @@ export default function WikiViewPage (props: WikiViewPageProps) { return ( <> - <WikiBase pageKind={'wiki'} title={path ?? ''}> - <Section> - <WikiArticle> - {rev != null && ( - <RevisionAlert - page={props.page} - recentURL={`/wiki/${slug}/${path}`} - /> - )} - <div dangerouslySetInnerHTML={{ __html: props.html }}/> - </WikiArticle> - </Section> - </WikiBase> + {props.page == null ? ( + <> + <Hero> + <p>문서를 찾지 못했습니다.</p> + <p><Link href={`/edit/${slug}/${path}`}>새 문서 만들기</Link></p> + </Hero> + </> + ) : ( + <WikiBase pageKind={'wiki'} title={path ?? ''}> + <Section> + <WikiArticle> + {rev != null && ( + <RevisionAlert + page={props.page} + recentURL={`/wiki/${slug}/${path}`} + /> + )} + <div dangerouslySetInnerHTML={{ __html: props.html }}/> + </WikiArticle> + </Section> + </WikiBase> + )} </> ) }