From cedc9210351d8b6bbd660c389f5d6d94cd5c6e73 Mon Sep 17 00:00:00 2001 From: Defectink Date: Mon, 25 Apr 2022 10:45:53 +0800 Subject: [PATCH] Add gist page --- components/gists/GistsCode.tsx | 12 +---- pages/g/[id].tsx | 87 +++++++++++++++++++++++++++++++--- pages/g/styles.module.css | 13 +++++ pages/gists.tsx | 33 +++++++++---- types/index.ts | 61 +----------------------- 5 files changed, 122 insertions(+), 84 deletions(-) create mode 100644 pages/g/styles.module.css diff --git a/components/gists/GistsCode.tsx b/components/gists/GistsCode.tsx index 78081c9..701bee2 100644 --- a/components/gists/GistsCode.tsx +++ b/components/gists/GistsCode.tsx @@ -10,7 +10,6 @@ import classNames from 'classnames'; import useInView from 'lib/hooks/useInView'; import loadingImage from 'public/images/img/mona-loading-default.gif'; import Image from 'next/image'; -import Link from 'next/link'; interface Props { gist: Gist; @@ -48,20 +47,13 @@ const GistsCode = ({ gist, f }: Props) => { return ( <> -
-

- {gist.owner.login} / - {file[f].filename} -

-

Update at: {gist.updated_at}

-

{gist.description}

- +
{rawCode ? (
{code}
) : (
diff --git a/pages/g/[id].tsx b/pages/g/[id].tsx index ba84472..a110540 100644 --- a/pages/g/[id].tsx +++ b/pages/g/[id].tsx @@ -1,13 +1,82 @@ import { GetStaticProps, InferGetStaticPropsType, GetStaticPaths } from 'next'; import dynamic from 'next/dynamic'; import { ReactElement } from 'react'; +import { SignalGist } from 'types'; +import avatar from 'public/images/img/avatar.svg'; +import Image from 'next/image'; +import styles from './styles.module.css'; +import classNames from 'classnames'; const MainLayout = dynamic(() => import('layouts/MainLayout')); +const GistsCode = dynamic(() => import('components/gists/GistsCode')); -const Gist = ({ id }: InferGetStaticPropsType) => { +const Gist = ({ gist }: InferGetStaticPropsType) => { return ( <> -
{id}
+
+
+
+ Avatar +

+ {gist.owner.login} /{Object.keys(gist.files)[0]} +

+
+

Last active: {gist.updated_at}

+ +
+

{gist.description}

+ + {Object.keys(gist.files).map((f) => ( +
+
+
+
+
+
+
+ +
+ {gist.files[f].filename} +
+
+ + +
+ ))} +
+
+
); }; @@ -15,16 +84,22 @@ const Gist = ({ id }: InferGetStaticPropsType) => { export const getStaticPaths: GetStaticPaths = async () => { return { paths: [], - fallback: true, + fallback: 'blocking', }; }; -export const getStaticProps: GetStaticProps = async ({ params }) => { +export const getStaticProps: GetStaticProps<{ + id: string | undefined; + gist: SignalGist; +}> = async ({ params }) => { return { props: { - id: params?.id, + id: params?.id?.toString(), + gist: await fetch(`https://api.github.com/gists/${params?.id}`).then( + (res) => res.json() + ), }, - revalidate: 60, + revalidate: 3600, }; }; diff --git a/pages/g/styles.module.css b/pages/g/styles.module.css new file mode 100644 index 0000000..240fa40 --- /dev/null +++ b/pages/g/styles.module.css @@ -0,0 +1,13 @@ +.wrapper { + @apply overflow-hidden rounded-lg; + @apply mb-8 shadow-lg; + /* box-shadow: 0 13px 27px -5px rgb(50 50 93 / 25%), + 0 8px 16px -8px rgb(0 0 0 / 30%), 0 -6px 16px -6px rgb(0 0 0 / 3%); */ +} +.wrapper pre { + margin: unset; + border-radius: unset; +} +.wrapper .loading span { + margin: unset; +} diff --git a/pages/gists.tsx b/pages/gists.tsx index 5832d00..f3b2d0f 100644 --- a/pages/gists.tsx +++ b/pages/gists.tsx @@ -1,18 +1,21 @@ -import { InferGetStaticPropsType } from 'next'; +import { GetStaticProps, InferGetStaticPropsType } from 'next'; import { ReactElement } from 'react'; import { Gist, GithubUser } from 'types'; import Image from 'next/image'; import classNames from 'classnames'; import dynamic from 'next/dynamic'; import avatar from 'public/images/img/avatar.svg'; +import Link from 'next/link'; const MainLayout = dynamic(() => import('layouts/MainLayout')); const GistsCode = dynamic(() => import('components/gists/GistsCode')); +const Anchor = dynamic(() => import('components/mdx/Anchor')); const Gists = ({ gists, user, }: InferGetStaticPropsType) => { + console.log(gists); return ( <>
@@ -53,7 +56,18 @@ const Gists = ({ {gists.map((g) => (
{Object.keys(g.files).map((f) => ( - +
+

+ {g.owner.login} / + + {g.files[f].filename} + +

+

Update at: {g.updated_at}

+

{g.description}

+ + +
))}
))} @@ -64,17 +78,20 @@ const Gists = ({ ); }; -export const getStaticProps = async () => { +export const getStaticProps: GetStaticProps<{ + gists: Gist[]; + user: GithubUser; +}> = async () => { return { props: { - gists: (await fetch( + gists: await fetch( 'https://api.github.com/users/DefectingCat/gists' - ).then((res) => res.json())) as Gist[], - user: (await fetch('https://api.github.com/users/DefectingCat').then( + ).then((res) => res.json()), + user: await fetch('https://api.github.com/users/DefectingCat').then( (res) => res.json() - )) as GithubUser, + ), }, - revalidate: 60, + revalidate: 600, }; }; diff --git a/types/index.ts b/types/index.ts index 36e9dd2..fbb4159 100644 --- a/types/index.ts +++ b/types/index.ts @@ -138,66 +138,7 @@ export interface GithubUser { } // Generated by https://quicktype.io -export interface SignalGist { - url: string; - forks_url: string; - commits_url: string; - id: string; - node_id: string; - git_pull_url: string; - git_push_url: string; - html_url: string; - files: { [key: string]: File }; - public: boolean; - created_at: string; - updated_at: string; - description: string; - comments: number; - user: null; - comments_url: string; - owner: Owner; +export interface SignalGist extends Gist { forks: any[]; history: History[]; - truncated: boolean; -} -export interface File { - filename: string; - type: string; - language: string; - raw_url: string; - size: number; - truncated: boolean; - content: string; -} -export interface History { - user: Owner; - version: string; - committed_at: string; - change_status: ChangeStatus; - url: string; -} -export interface ChangeStatus { - total: number; - additions: number; - deletions: number; -} -export interface Owner { - login: string; - id: number; - node_id: string; - avatar_url: string; - gravatar_id: string; - url: string; - html_url: string; - followers_url: string; - following_url: string; - gists_url: string; - starred_url: string; - subscriptions_url: string; - organizations_url: string; - repos_url: string; - events_url: string; - received_events_url: string; - type: string; - site_admin: boolean; }