From 8be65cbb073060c50ecce1cc575347864b8902bd Mon Sep 17 00:00:00 2001 From: yezi Date: Fri, 26 Jan 2024 22:41:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=87=8D=E5=86=99=20URL=20Scheme=20?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/pages/LPRecommendChecker.tsx | 4 +- frontend/src/pages/URLSchemeConvertor.tsx | 175 ++++++++++------------ 2 files changed, 85 insertions(+), 94 deletions(-) diff --git a/frontend/src/pages/LPRecommendChecker.tsx b/frontend/src/pages/LPRecommendChecker.tsx index c63ab23..6a84a6c 100644 --- a/frontend/src/pages/LPRecommendChecker.tsx +++ b/frontend/src/pages/LPRecommendChecker.tsx @@ -35,7 +35,7 @@ function handleCheck() { }); } -function ResultBlock() { +function Result() { const shouldFPRewardHighlight = result.value!.FPReward >= 35; const shouldnextCanRecommendDateHighlight = result.value!.nextCanRecommendDate ? parseTime(result.value!.nextCanRecommendDate) >= dayjs() @@ -109,7 +109,7 @@ export default function LPRecommendChecker() { 检测 - {result.value && } + {result.value && } ); } diff --git a/frontend/src/pages/URLSchemeConvertor.tsx b/frontend/src/pages/URLSchemeConvertor.tsx index bfb5d4a..2caf57a 100644 --- a/frontend/src/pages/URLSchemeConvertor.tsx +++ b/frontend/src/pages/URLSchemeConvertor.tsx @@ -1,10 +1,10 @@ import { useClipboard } from "@mantine/hooks"; -import type { Signal } from "@preact/signals"; import { signal } from "@preact/signals"; import { Center, Column, Row, + SmallText, SolidButton, Text, TextButton, @@ -14,132 +14,123 @@ import { MdContentCopy, MdDone, MdOutlineArrowForward } from "react-icons/md"; import QRCode from "react-qr-code"; import { toastWarning } from "../utils/toastHelper"; -interface JianshuURLType { - URLSchemePrefix: string; - URLPrefix: string; - slugLengthRange: { min: number; max: number }; +interface UrlMappingItem { + name: string; + from: RegExp; + to: string; } -const URLTypes = { - USER: { - URLSchemePrefix: "u", - URLPrefix: "u", - slugLengthRange: { min: 6, max: 12 }, +const UrlMappingArray: Array = [ + { + name: "user", + from: /^https:\/\/www\.jianshu\.com\/u\/([a-z0-9]{6,12})\/?$/, + to: "jianshu://u/$1", }, - ARTICLE: { - URLSchemePrefix: "notes", - URLPrefix: "p", - slugLengthRange: { min: 12, max: 12 }, + { + name: "article", + from: /^https:\/\/www\.jianshu\.com\/p\/([a-z0-9]{12})\/?$/, + to: "jianshu://p/$1", }, - COLLECTION: { - URLSchemePrefix: "c", - URLPrefix: "c", - slugLengthRange: { min: 6, max: 12 }, + { + name: "notebook", + from: /^https:\/\/www\.jianshu\.com\/nb\/(\d{7,8})\/?$/, + to: "jianshu://nb/$1", }, - NOTEBOOK: { - URLSchemePrefix: "nb", - URLPrefix: "nb", - slugLengthRange: { min: 7, max: 8 }, + { + name: "collection", + from: /^https:\/\/www\.jianshu\.com\/c\/([a-z0-9]{6,12})\/?$/, + to: "jianshu://c/$1", }, -}; -const URLTypesArray = [ - URLTypes.USER, - URLTypes.ARTICLE, - URLTypes.COLLECTION, - URLTypes.NOTEBOOK, ]; -const jianshuURL = signal(""); -const hasResult = signal(false); -const result = signal(undefined); +const inputUrl = signal(""); +const urlScheme = signal(null); -function isJianshuURL(url: Signal) { - return url.value.startsWith("https://www.jianshu.com/"); +function isJianshuUrl(url: string) { + return url.startsWith("https://www.jianshu.com/"); } -function getURLType(url: Signal): JianshuURLType | "unknown" { - if (!isJianshuURL(url)) { - return "unknown"; +function handleConvert() { + if (!isJianshuUrl(inputUrl.value)) { + toastWarning({ message: "请输入有效的简书链接" }); + return; } - let splitted; - let perfix; - let slug; - try { - splitted = url.value.split("/"); - [, , , perfix, slug] = splitted; - } catch { - return "unknown"; - } - for (const URLType of URLTypesArray) { - if (perfix === URLType.URLPrefix) { - if ( - URLType.slugLengthRange.min <= slug.length && - slug.length <= URLType.slugLengthRange.max - ) { - return URLType; - } - return "unknown"; + for (const urlMapping of UrlMappingArray) { + const replaceResult = inputUrl.value.replace( + urlMapping.from, + urlMapping.to, + ); + + // 匹配成功 + if (replaceResult.startsWith("jianshu://")) { + urlScheme.value = replaceResult; + return; } } - return "unknown"; + + // 无匹配项 + toastWarning({ message: "请输入有效的简书链接" }); } -function handleConvert() { - const urlType = getURLType(jianshuURL); +function Result() { + const clipboard = useClipboard(); - if (urlType === "unknown") { - toastWarning({ message: "请输入有效的简书链接" }); - return; + if (!urlScheme.value) { + return null; } - hasResult.value = true; - result.value = jianshuURL.value.replace( - `https://www.jianshu.com/${urlType.URLPrefix}/`, - `jianshu://${urlType.URLSchemePrefix}/`, + return ( +
+ + {urlScheme.value} + + } + onClick={() => window.open(urlScheme.value!)} + > + 访问 + + : } + onClick={() => clipboard.copy(urlScheme.value)} + > + {clipboard.copied ? "已复制" : "复制"} + + + +
+ +
+ + 请使用简书 App 扫描二维码 + +
+
); } export default function URLSchemeConvertor() { - const clipboard = useClipboard(); - return ( 转换 - {result.value !== undefined && ( -
- - - {result.value} - - } - onClick={() => window.open(result.value)} - > - 访问 - - : } - onClick={() => clipboard.copy(result.value)} - > - {!clipboard.copied ? "复制" : "复制成功"} - - - - - -
- )} + {urlScheme.value && }
); }