|
| 1 | +import { GetServerSideProps, NextPage } from 'next'; |
| 2 | +import { useTranslation } from 'next-i18next'; |
| 3 | +import { serverSideTranslations } from 'next-i18next/serverSideTranslations'; |
| 4 | + |
| 5 | +import RepoDetailNavbar from '@/components/navbar/RepoNavbar'; |
| 6 | +import CommentContainer from '@/components/respository/CommentContainer'; |
| 7 | +import Info from '@/components/respository/Info'; |
| 8 | +import Tabs from '@/components/respository/Tabs'; |
| 9 | +import Seo from '@/components/Seo'; |
| 10 | + |
| 11 | +import { getDetail } from '@/services/repository'; |
| 12 | +import { getClientIP } from '@/utils/util'; |
| 13 | + |
| 14 | +import { Repository } from '@/types/repository'; |
| 15 | + |
| 16 | +interface Props { |
| 17 | + repo: Repository; |
| 18 | +} |
| 19 | + |
| 20 | +const RepositoryPage: NextPage<Props> = ({ repo }) => { |
| 21 | + const { t, i18n } = useTranslation('repository'); |
| 22 | + |
| 23 | + const getLocalizedField = (field: any, fallback: any) => |
| 24 | + i18n.language === 'en' ? field ?? fallback : fallback; |
| 25 | + |
| 26 | + return ( |
| 27 | + <> |
| 28 | + <Seo |
| 29 | + title={`${repo.full_name}: ${getLocalizedField( |
| 30 | + repo.title_en, |
| 31 | + repo.title |
| 32 | + )}`} |
| 33 | + description={getLocalizedField(repo.summary_en, repo.summary)} |
| 34 | + image={repo.image_url ? repo.image_url : repo.author_avatar} |
| 35 | + /> |
| 36 | + <RepoDetailNavbar |
| 37 | + avatar={repo.share_user.avatar} |
| 38 | + uid={repo.share_user.uid} |
| 39 | + t={t} |
| 40 | + /> |
| 41 | + <div className='mt-2 bg-white px-2 pb-3 pt-2 dark:bg-gray-800 md:rounded-lg'> |
| 42 | + <Info repo={repo} t={t} i18n_lang={i18n.language} /> |
| 43 | + <Tabs repo={repo} t={t} i18n_lang={i18n.language} /> |
| 44 | + </div> |
| 45 | + <CommentContainer |
| 46 | + className='mt-2 bg-white dark:bg-gray-800 md:rounded-lg' |
| 47 | + belong='repository' |
| 48 | + belongId={repo.rid} |
| 49 | + t={t} |
| 50 | + i18n_lang={i18n.language} |
| 51 | + /> |
| 52 | + <div className='h-8 lg:h-36' /> |
| 53 | + </> |
| 54 | + ); |
| 55 | +}; |
| 56 | + |
| 57 | +export const getServerSideProps: GetServerSideProps = async ({ |
| 58 | + req, |
| 59 | + query, |
| 60 | + locale, |
| 61 | +}) => { |
| 62 | + const ip = getClientIP(req); |
| 63 | + console.log(query); |
| 64 | + const rid = query?.rid as string; |
| 65 | + const data = await getDetail(ip, rid); |
| 66 | + if (data.success) { |
| 67 | + return { |
| 68 | + props: { |
| 69 | + ...(await serverSideTranslations(locale as string, [ |
| 70 | + 'common', |
| 71 | + 'repository', |
| 72 | + ])), |
| 73 | + repo: data.data, |
| 74 | + }, |
| 75 | + }; |
| 76 | + } else { |
| 77 | + return { |
| 78 | + notFound: true, |
| 79 | + }; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +export default RepositoryPage; |
0 commit comments