From 43d7e05a655530677678ef082091ce2eced5c0b2 Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 8 Jul 2023 15:10:12 +0800 Subject: [PATCH] feat: source of gh-commit Signed-off-by: Innei --- .env.template | 3 - src/components/ui/link-card/LinkCard.tsx | 58 +++++++++++++-- .../ui/markdown/renderers/LinkRenderer.tsx | 70 ++++++++++++++++++- src/components/ui/markdown/test-text.md | 6 ++ 4 files changed, 127 insertions(+), 10 deletions(-) diff --git a/.env.template b/.env.template index 39188b1664..79dbd72284 100644 --- a/.env.template +++ b/.env.template @@ -14,6 +14,3 @@ NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/ NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/ - -UPSTASH_URL= -UPSTASH_TOKEN= diff --git a/src/components/ui/link-card/LinkCard.tsx b/src/components/ui/link-card/LinkCard.tsx index 350bb844d1..16de93c90d 100644 --- a/src/components/ui/link-card/LinkCard.tsx +++ b/src/components/ui/link-card/LinkCard.tsx @@ -1,10 +1,10 @@ -import { useCallback, useMemo, useRef, useState } from 'react' +import React, { useCallback, useMemo, useRef, useState } from 'react' import { useInView } from 'react-intersection-observer' import axios from 'axios' import clsx from 'clsx' import Link from 'next/link' import RemoveMarkdown from 'remove-markdown' -import type { FC, SyntheticEvent } from 'react' +import type { FC, ReactNode, SyntheticEvent } from 'react' import { simpleCamelcaseKeys as camelcaseKeys } from '@mx-space/api-client' @@ -16,7 +16,7 @@ import { apiClient } from '~/lib/request' import styles from './LinkCard.module.css' -export type LinkCardSource = 'gh' | 'self' | 'mx-space' +export type LinkCardSource = 'gh' | 'self' | 'mx-space' | 'gh-commit' export interface LinkCardProps { id: string source?: LinkCardSource @@ -43,8 +43,8 @@ const LinkCardImpl: FC = (props) => { const [fullUrl, setFullUrl] = useState('about:blank') const [cardInfo, setCardInfo] = useState<{ - title: string - desc?: string + title: ReactNode + desc?: ReactNode image?: string }>() @@ -139,6 +139,54 @@ const LinkCardImpl: FC = (props) => { return !rest.length } + case 'gh-commit': { + // e.g. mx-space/kami/commit/1234567890 + const [namespace, repo, variant, commitId] = id.split('/') + + if (!namespace || !repo || !commitId) { + return false + } + if (variant !== 'commit') { + return false + } + + fetchFnRef.current = async () => { + const data = await axios + .get( + `https://api.github.com/repos/${namespace}/${repo}/commits/${commitId}`, + ) + .then((data) => camelcaseKeys(data.data)) + + setCardInfo({ + image: data.author.avatarUrl, + title: ( + + + {namespace}/{repo} + + + {data.commit.message.replace(/Signed-off-by:.+/, '')} + + + ), + desc: ( + + + +{data.stats.additions} + + + -{data.stats.deletions} + + + {data.sha.slice(0, 7)} + + ), + }) + setFullUrl(data.htmlUrl) + } + + return true + } } }, [source, id]) const fetchInfo = useCallback(async () => { diff --git a/src/components/ui/markdown/renderers/LinkRenderer.tsx b/src/components/ui/markdown/renderers/LinkRenderer.tsx index 7c3f11819a..bd370dd1cc 100644 --- a/src/components/ui/markdown/renderers/LinkRenderer.tsx +++ b/src/components/ui/markdown/renderers/LinkRenderer.tsx @@ -1,6 +1,8 @@ import React, { useMemo } from 'react' import dynamic from 'next/dynamic' +import { GitHubBrandIcon } from '~/components/icons/platform/GitHubBrandIcon' + import { LinkCard } from '../../link-card' import { MLink } from './link' @@ -32,7 +34,7 @@ export const LinkRenderer = ({ href }: { href: string }) => { case isYoutubeUrl(url): { const id = url.searchParams.get('v')! return ( -
+