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 (
-
+
+
+ )
+ }
+ case isGistUrl(url): {
+ const [_, owner, id] = url.pathname.split('/')
+ return (
+ <>
+
+
+
+
+
+
+ {href}
+
+ >
+ )
+ }
+
+ case isGithubCommitUrl(url): {
+ const [_, owner, repo, type, id] = url.pathname.split('/')
+ return (
+ <>
+
+ {href}
+
+
+ >
)
}
}
@@ -54,6 +91,26 @@ export const LinkRenderer = ({ href }: { href: string }) => {
)
}
+
+const FixedRatioContainer = ({
+ children,
+ ratio = 58,
+}: {
+ ratio?: number
+ children: React.ReactNode
+}) => {
+ return (
+
+ {children}
+
+ )
+}
+
const isTweetUrl = (url: URL) => {
return url.hostname === 'twitter.com' && url.pathname.startsWith('/')
}
@@ -72,3 +129,12 @@ const isGithubRepoUrl = (url: URL) => {
const isYoutubeUrl = (url: URL) => {
return url.hostname === 'www.youtube.com' && url.pathname.startsWith('/watch')
}
+
+const isGistUrl = (url: URL) => {
+ return url.hostname === 'gist.github.com'
+}
+
+const isGithubCommitUrl = (url: URL) => {
+ const [_, owner, repo, type, ...rest] = url.pathname.split('/')
+ return url.hostname === 'github.com' && type === 'commit'
+}
diff --git a/src/components/ui/markdown/test-text.md b/src/components/ui/markdown/test-text.md
index 82a64f93ea..54b8eaeb4b 100644
--- a/src/components/ui/markdown/test-text.md
+++ b/src/components/ui/markdown/test-text.md
@@ -7,6 +7,12 @@ https://twitter.com/strrlthedev/status/1671044378289393664
https://github.com/ast-grep/ast-grep
+https://www.youtube.com/watch?v=ZRv0Z-M7NqM
+
+https://gist.github.com/Innei/cb67fc579a460d6b863b3ca3c1bd6e1b
+
+https://github.com/Innei/Shiro/commit/e1b0b57aaea0eec1b695c4f1961297b42b935044
+
# Test 文本