dh_demo

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

index.tsx (2001B)


      1 import Title from '@/components/elements/Title'
      2 import Container from '@/components/layout/Container'
      3 import Hero from '@/components/layout/Hero'
      4 import Section from '@/components/layout/Section'
      5 import { withConnection } from '@/lib/model_helpers'
      6 import { getUserProfile, UserProfile } from '@/lib/models/user_profile'
      7 import { listWikiLinksByOwnerId } from '@/lib/models/wiki_info'
      8 import { parseIntOrDefault } from '@/lib/utils/number'
      9 import { GetServerSideProps } from 'next'
     10 
     11 interface UserProfilePageProps {
     12   profile: UserProfile
     13   wikiList: Array<{ slug: string, title: string }>
     14 }
     15 
     16 export const getServerSideProps: GetServerSideProps<UserProfilePageProps> = async (context) => {
     17   const id = parseIntOrDefault(context.query.id, -1)
     18   if (id < 0) {
     19     return { notFound: true }
     20   }
     21 
     22   return await withConnection(async (conn) => {
     23     const user = await getUserProfile(conn, [id])
     24     if (user == null) {
     25       return { notFound: true }
     26     }
     27 
     28     const wikiList = await listWikiLinksByOwnerId(conn, [id])
     29 
     30     return {
     31       props: {
     32         profile: user,
     33         wikiList: wikiList,
     34       }
     35     }
     36   })
     37 }
     38 
     39 export default function UserProfilePage (props: UserProfilePageProps) {
     40   return (
     41     <>
     42       <Hero>
     43         <Title kind="headline">{props.profile.nickname}님의 프로필</Title>
     44       </Hero>
     45 
     46       <Container>
     47         <Section>
     48           <Title kind="headline" size="medium">프로필</Title>
     49           <article>
     50             {props.profile.bio ?? '소개글이 없습니다.'}
     51           </article>
     52         </Section>
     53 
     54         <Section>
     55           <Title kind="headline" size="medium">위키 목록</Title>
     56           <ul>
     57             {props.wikiList.map((wiki) => (
     58               <li key={wiki.slug}>
     59                 <a href={`/wiki/${wiki.slug}`}>{wiki.title}</a>
     60               </li>
     61             ))}
     62 
     63             {props.wikiList.length === 0 && (
     64               <li>위키가 없습니다.</li>
     65             )}
     66           </ul>
     67         </Section>
     68       </Container>
     69     </>
     70   )
     71 }