From 917fca59472acaa4cb1e71f4a307af62114bf599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=A8=E9=9C=96=E9=93=83?= Date: Fri, 22 May 2026 21:46:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=85=A8=E9=9D=A2=E9=87=8D?= =?UTF-8?q?=E6=9E=84=20Jwt=20=E9=A1=B5=E9=9D=A2=EF=BC=8C=E9=87=87=E7=94=A8?= =?UTF-8?q?=E9=98=B2=E6=8A=96=E8=BE=93=E5=85=A5=E5=B9=B6=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=20shadcn=20=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/Jwt/index.tsx | 157 ++++++++++++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 58 deletions(-) diff --git a/pages/Jwt/index.tsx b/pages/Jwt/index.tsx index 929f37a..b79ca9f 100644 --- a/pages/Jwt/index.tsx +++ b/pages/Jwt/index.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react'; +import { useCallback, useEffect, useMemo, useState } from 'react'; import { Key } from 'lucide-react'; import PageHeader from '@/components/PageHeader'; import { parseJwt, stringifyJson } from '@/utils/jwt'; @@ -6,32 +6,36 @@ import CopyButton from '@/components/CopyButton'; import TextInputArea from '@/components/TextInputArea'; import { useLazyTranslation } from '@/utils/useLazyTranslation'; import { useContextMenuData } from '@/utils/useContextMenuData'; +import { cn } from '@/lib/utils'; interface SectionProps { title: string; content: unknown; - color: string; + colorClass: string; // 💡 1. 废除硬编码十六进制色值,改用语义化的 Tailwind 类名 + bgClass: string; + borderClass: string; } -const Section = ({ title, content, color }: SectionProps) => { +const Section = ({ title, content, colorClass, bgClass, borderClass }: SectionProps) => { const { t } = useLazyTranslation('jwt'); return (
-
- +
+ {title} -
- -
+
-
+      {/* 💡 排版微距精雕:
+        - 彻底移除 border-black/5 这种非暗黑模式友好的硬隔离。
+        - 统一收拢为标准的 bg-muted/40 配合 font-mono text-xs
+      */}
+      
         {content ? stringifyJson(content) : t('jwt:invalidFormat')}
       
@@ -39,9 +43,19 @@ const Section = ({ title, content, color }: SectionProps) => { }; export default function Index() { - const { t } = useLazyTranslation('jwt'); + const { t } = useLazyTranslation(['jwt', 'jsonFormat']); const [jwtInput, setJwtInput] = useState(''); + // 2. 防抖中转管道:切断高频键盘敲击时的红色语法闪烁 + const [debouncedInput, setDebouncedInput] = useState(''); + + useEffect(() => { + const handle = setTimeout(() => { + setDebouncedInput(jwtInput); + }, 200); + return () => clearTimeout(handle); + }, [jwtInput]); + const handleContextMenuData = useCallback((payload: string) => { const cleaned = payload.replace(/^Bearer\s*/i, '').trim(); setJwtInput(cleaned); @@ -49,59 +63,86 @@ export default function Index() { useContextMenuData({ featureKey: 'jwt', onData: handleContextMenuData }); + // 3. 贯彻方案 A:衍生变量流。直接消费防抖后的文本 const result = useMemo(() => { - if (!jwtInput.trim()) { + if (!debouncedInput.trim()) { return null; } - return parseJwt(jwtInput); - }, [jwtInput]); + return parseJwt(debouncedInput); + }, [debouncedInput]); return ( -
-
- } /> +
+ } // 💡 规范锁死 Icon 宽高,抹杀闪烁 + /> -
- {/* Input Area */} - { - const cleaned = val.replace(/^Bearer\s*/i, '').trim(); - setJwtInput(cleaned); - }} - allowCopy={true} - showClear={true} - externalError={result?.error} - /> +
+ {/* 输入终端 */} + { + const cleaned = val.replace(/^Bearer\s*/i, '').trim(); + setJwtInput(cleaned); + }} + allowCopy={true} + showClear={true} + externalError={result?.error || undefined} + onClear={() => setJwtInput('')} + /> - {result && !result.error && ( -
-
-
-
-
- - {t('jwt:signatureTitle')} - - -
- - {result.signature || t('jwt:noSignature')} + {/* 解码看板结果展现 */} + {result && !result.error && ( +
+ {/* Header 分区:完美致敬 JWT.io 的鲜艳色彩,同时实现黑夜暗化自适应 */} +
+ + {/* Payload 分区 */} +
+ + {/* Signature 签名区:完全对齐标准的 shadcn 骨架阶度 */} +
+
+ + {t('jwt:signatureTitle')} +
+ + {result.signature || t('jwt:noSignature')} +
- )} -
+
+ )} + + {/* 当解析错误时的干净中性引导拦截 */} + {result?.error && ( +
+

+ {t('jsonFormat:invalidJson')} +

+
+ )}
);