Compare commits

..

2 Commits

Author SHA1 Message Date
rsgltzyd 14d85e5265 refactor: 更新样式和组件结构以提升一致性和可读性
- 在 utils.ts 中引入 twMerge 以合并 Tailwind 类。
- 在 ConverterForm.tsx 中简化类名,移除冗余样式。
- 在 LiveClock.tsx 中调整样式,优化文本显示。
- 在 ResultView.tsx 中更新布局和样式,提升视觉效果。
2026-07-08 23:24:54 +08:00
rsgltzyd c8c3c88d99 refactor: 移除 PageSkeleton 组件及相关测试,更新 RouterContainer 以简化加载逻辑 2026-07-06 20:05:52 +08:00
13 changed files with 41 additions and 125 deletions
+2 -3
View File
@@ -669,7 +669,7 @@ src/pages/StorageCleaner/useStorageCleaner.ts — 页面级 Hook
### 9.1 功能元数据 ### 9.1 功能元数据
功能名称与描述在 `config/featureMeta.ts``FEATURES` 数组中定义: 功能名称与描述在 `config/features.tsx``FEATURES` 数组中定义:
```typescript ```typescript
{ {
@@ -677,11 +677,10 @@ src/pages/StorageCleaner/useStorageCleaner.ts — 页面级 Hook
label: '时间戳转换', label: '时间戳转换',
description: '日期与时间戳互转', description: '日期与时间戳互转',
defaultVisible: true, defaultVisible: true,
component: TimestampPage,
} }
``` ```
页面组件通过 `config/pageLoaders/` 按需加载,不在 `FEATURES` 中引用。
Dashboard 卡片、TopBar 搜索等功能从此处读取 `label` / `description` Dashboard 卡片、TopBar 搜索等功能从此处读取 `label` / `description`
### 9.2 页面与组件文案 ### 9.2 页面与组件文案
+7 -7
View File
@@ -382,14 +382,14 @@ function loadRule(ruleId: string): void {
使用 `localStorage`,键名 `testDataGenerator_rules`。API 为**命名导出函数**(见 `src/utils/ruleStorage.ts`): 使用 `localStorage`,键名 `testDataGenerator_rules`。API 为**命名导出函数**(见 `src/utils/ruleStorage.ts`):
| 函数 | 说明 | | 函数 | 说明 |
| ------------------------------------------------------ | ---------------------------------- | | ---- | ---- |
| `getAll()` / `getById()` / `getByName()` | 读取 | | `getAll()` / `getById()` / `getByName()` | 读取 |
| `save()` / `update()` / `deleteRule()` / `duplicate()` | 写入;失败时返回 `null``false` | | `save()` / `update()` / `deleteRule()` / `duplicate()` | 写入;失败时返回 `null``false` |
| `recordUse()` | 递增 `useCount`、更新 `lastUsedAt` | | `recordUse()` | 递增 `useCount`、更新 `lastUsedAt` |
| `search()` / `getRecent()` | 搜索与最近使用 | | `search()` / `getRecent()` | 搜索与最近使用 |
| `exportRules()` / `importRules()` | 导入导出 JSON 数组 | | `exportRules()` / `importRules()` | 导入导出 JSON 数组 |
| `clear()` | 清空全部规则 | | `clear()` | 清空全部规则 |
写入失败(如 `QuotaExceededError`)时,内部 `setAll()` 返回 `false``save`/`update` 返回 `null``deleteRule` 返回 `false`,并在控制台输出 `[ruleStorage] 保存规则失败`。调用方须检查返回值,避免误报成功。 写入失败(如 `QuotaExceededError`)时,内部 `setAll()` 返回 `false``save`/`update` 返回 `null``deleteRule` 返回 `false`,并在控制台输出 `[ruleStorage] 保存规则失败`。调用方须检查返回值,避免误报成功。
@@ -1505,7 +1505,9 @@ export class DataExporter {
```typescript ```typescript
// src/types/testDataGenerator.ts // src/types/testDataGenerator.ts
type WorkerRequestMessage = { type: 'start'; payload: WorkerStartPayload } | { type: 'cancel' }; type WorkerRequestMessage =
| { type: 'start'; payload: WorkerStartPayload }
| { type: 'cancel' };
type WorkerResponseMessage = type WorkerResponseMessage =
| { type: 'progress'; generationId: number; payload: GenerateProgress } | { type: 'progress'; generationId: number; payload: GenerateProgress }
-41
View File
@@ -1,41 +0,0 @@
/**
* PageSkeleton 组件 - 页面加载骨架屏
*
* 用于 Suspense fallback 和初始加载状态,提供平滑的视觉过渡
* 避免白屏闪烁,减少布局偏移
*/
/**
* 工具页面骨架屏
*/
function ToolPageSkeleton() {
return (
<div className="p-5">
{/* 标题区域 */}
<div className="w-44 h-7 bg-muted rounded animate-pulse mb-4" />
{/* 输入区域 */}
<div className="w-full h-[120px] bg-muted rounded-xl animate-pulse mb-4" />
{/* 控制栏 */}
<div className="flex gap-2 mb-4">
<div className="w-24 h-9 bg-muted rounded-lg animate-pulse" />
<div className="w-20 h-9 bg-muted rounded-lg animate-pulse" />
<div className="flex-1" />
<div className="w-24 h-9 bg-muted rounded-lg animate-pulse" />
</div>
{/* 结果区域 */}
<div className="w-full h-[160px] bg-muted rounded-xl animate-pulse" />
</div>
);
}
/**
* 页面加载骨架屏
*/
export default function PageSkeleton() {
return <ToolPageSkeleton />;
}
PageSkeleton.displayName = 'PageSkeleton';
+1 -2
View File
@@ -6,7 +6,7 @@
| 组件 | 用途 | | 组件 | 用途 |
| ----------------------- | ------------------------------------------------------------------------------------------------------ | | ----------------------- | ------------------------------------------------------------------------------------------------------ |
| `RouterContainer.tsx` | 路由容器,根据当前路由动态渲染对应页面组件,集成错误边界和骨架屏 | | `RouterContainer.tsx` | 路由容器,根据当前路由动态渲染对应页面组件,集成错误边界 |
| `SwitchButtonGroup.tsx` | 通用切换按钮组,支持 `small/medium/large` 三种尺寸,用于页面子模式切换 | | `SwitchButtonGroup.tsx` | 通用切换按钮组,支持 `small/medium/large` 三种尺寸,用于页面子模式切换 |
| `EmptyPlaceholder.tsx` | 虚线边框空状态占位,统一工具页「暂无结果」提示样式 | | `EmptyPlaceholder.tsx` | 虚线边框空状态占位,统一工具页「暂无结果」提示样式 |
| `TextInputArea.tsx` | 增强文本输入区域,支持校验规则、工具栏操作、字符计数、清空 | | `TextInputArea.tsx` | 增强文本输入区域,支持校验规则、工具栏操作、字符计数、清空 |
@@ -17,7 +17,6 @@
| `GlobalSnackbar.tsx` | 全局消息提示组件 + Context Provider,支持受控/Hook/全局单例三种使用方式 | | `GlobalSnackbar.tsx` | 全局消息提示组件 + Context Provider,支持受控/Hook/全局单例三种使用方式 |
| `ErrorBoundary.tsx` | 全局错误边界(类组件),捕获子组件树 JS 错误并展示友好错误页面 | | `ErrorBoundary.tsx` | 全局错误边界(类组件),捕获子组件树 JS 错误并展示友好错误页面 |
| `PageErrorBoundary.tsx` | 页面级错误边界,适配 shadcn 暗黑模式,支持 `resetKey` 自动恢复 | | `PageErrorBoundary.tsx` | 页面级错误边界,适配 shadcn 暗黑模式,支持 `resetKey` 自动恢复 |
| `PageSkeleton.tsx` | 页面骨架屏,提供 `dashboard``tool` 两种变体,用于 Suspense fallback |
## 使用约定 ## 使用约定
+1 -2
View File
@@ -4,7 +4,6 @@ import { useRouter } from '@/providers/RouterProvider';
import type { PageType } from '@/types/storage'; import type { PageType } from '@/types/storage';
import { type ComponentType, useEffect, useState } from 'react'; import { type ComponentType, useEffect, useState } from 'react';
import PageErrorBoundary from '@/components/PageErrorBoundary'; import PageErrorBoundary from '@/components/PageErrorBoundary';
import PageSkeleton from '@/components/PageSkeleton';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { AlertTriangle } from 'lucide-react'; import { AlertTriangle } from 'lucide-react';
@@ -30,7 +29,7 @@ function LoadedPage({ pageKey }: { pageKey: PageType }) {
}, [pageKey]); }, [pageKey]);
if (!Page) { if (!Page) {
return <PageSkeleton />; return <div className="flex-1" aria-hidden="true" />;
} }
return <Page />; return <Page />;
@@ -1,32 +0,0 @@
import { describe, expect, it } from 'vitest';
import { render } from '@testing-library/react';
import PageSkeleton from '@/components/PageSkeleton';
describe('PageSkeleton 组件', () => {
describe('渲染测试', () => {
it('应渲染工具页面骨架', () => {
const { container } = render(<PageSkeleton />);
const skeletons = container.querySelectorAll('.animate-pulse');
expect(skeletons.length).toBeGreaterThan(0);
});
});
describe('布局结构测试', () => {
it('工具骨架屏应有内边距', () => {
const { container } = render(<PageSkeleton />);
const toolContainer = container.firstChild as HTMLElement;
expect(toolContainer).toHaveClass('p-5');
});
});
describe('骨架屏元素测试', () => {
it('工具骨架屏应包含动画脉冲效果', () => {
const { container } = render(<PageSkeleton />);
const skeletons = container.querySelectorAll('.animate-pulse');
expect(skeletons.length).toBeGreaterThan(0);
});
});
});
+2 -1
View File
@@ -1,5 +1,6 @@
import { type ClassValue, clsx } from 'clsx'; import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) { export function cn(...inputs: ClassValue[]) {
return clsx(inputs); return twMerge(clsx(inputs));
} }
@@ -41,10 +41,7 @@ export default function ConverterForm({
placeholder={INPUT_PLACEHOLDERS[mode]} placeholder={INPUT_PLACEHOLDERS[mode]}
value={input} value={input}
onChange={(e) => setInput(e.target.value)} onChange={(e) => setInput(e.target.value)}
className={cn( className={cn('h-10', error && 'border-destructive')}
'font-mono font-semibold h-10 shadow-sm placeholder:text-muted-foreground/60 focus:bg-background',
error && 'border-destructive focus-visible:ring-destructive',
)}
/> />
{error && <p className="text-destructive text-xs font-medium px-0.5">{error}</p>} {error && <p className="text-destructive text-xs font-medium px-0.5">{error}</p>}
@@ -60,16 +57,13 @@ export default function ConverterForm({
/> />
<Select value={zone} onValueChange={(v: string) => setZone(v as ZoneType)}> <Select value={zone} onValueChange={(v: string) => setZone(v as ZoneType)}>
<SelectTrigger className="flex-1 font-mono font-semibold h-9 shadow-sm bg-background"> <SelectTrigger>
<SelectValue placeholder="选择时区" /> <SelectValue placeholder="选择时区" />
</SelectTrigger> </SelectTrigger>
<SelectContent className="max-h-64 font-mono">
<SelectContent className="max-h-64">
{ZONES.map((z) => ( {ZONES.map((z) => (
<SelectItem <SelectItem key={z} value={z} className="cursor-pointer">
key={z}
value={z}
className="text-xs font-semibold focus:bg-accent cursor-pointer"
>
{z} {z}
</SelectItem> </SelectItem>
))} ))}
+8 -11
View File
@@ -1,4 +1,5 @@
import React, { useEffect, useState } from 'react'; import type { HTMLAttributes } from 'react';
import { useEffect, useState } from 'react';
import { Clock } from 'lucide-react'; import { Clock } from 'lucide-react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { CopyButton } from '@/components/CopyButton'; import { CopyButton } from '@/components/CopyButton';
@@ -7,7 +8,7 @@ import { msToUnit } from '../constants';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
interface LiveClockProps extends React.HTMLAttributes<HTMLDivElement> { interface LiveClockProps extends HTMLAttributes<HTMLDivElement> {
unit: UnitType; unit: UnitType;
onUseNow: (val: number) => void; onUseNow: (val: number) => void;
} }
@@ -28,18 +29,14 @@ export default function LiveClock({ unit, onUseNow, className, ...props }: LiveC
return ( return (
<div <div
className={cn( className={cn(
'flex items-center gap-3 px-3 h-10 rounded-lg border border-border/80 bg-secondary/50', 'flex items-center gap-3 px-3 h-10 rounded-lg font-bold text-[12px] border px-[20px]',
className, className,
)} )}
{...props} {...props}
> >
<span className="text-muted-foreground font-bold text-[10px] uppercase tracking-wider whitespace-nowrap shrink-0 selection:bg-transparent select-none"> <span></span>
</span>
<span className="flex-1 font-mono font-bold text-foreground text-sm tracking-tight leading-none truncate tabular-nums"> <span className="flex-1 text-sm ">{text}</span>
{text}
</span>
<Button <Button
type="button" type="button"
@@ -50,12 +47,12 @@ export default function LiveClock({ unit, onUseNow, className, ...props }: LiveC
title="填充到下方" title="填充到下方"
variant="ghost" variant="ghost"
size="icon" size="icon"
className="h-7 w-7 text-muted-foreground hover:text-foreground" className="h-7 w-7"
> >
<Clock className="w-3.5 h-3.5" /> <Clock className="w-3.5 h-3.5" />
</Button> </Button>
<CopyButton text={text} tooltip="复制时间戳" className="h-7 w-7 rounded-md border" /> <CopyButton text={text} tooltip="复制时间戳" className="h-7 w-7" />
</div> </div>
); );
} }
@@ -34,16 +34,15 @@ export default function ResultView({
</span> </span>
<div className="bg-card text-card-foreground border border-border p-4 sm:p-5 rounded-xl relative shadow-sm flex justify-between items-center gap-4 focus-within:ring-1 focus-within:ring-ring"> <div className="bg-card border p-4 rounded-lg flex items-center gap-3 justify-between">
<span className="font-mono font-extrabold text-foreground break-all text-xl sm:text-2xl tracking-tight leading-tight select-all tabular-nums"> <span className="break-all text-lg select-all flex-1">{result}</span>
{result}
</span>
<CopyButton <CopyButton
text={result} text={result}
tooltip="复制结果" tooltip="复制结果"
variant="ghost" variant="ghost"
size="icon" size="icon"
className="h-7 w-7 text-muted-foreground hover:text-foreground" className="h-7 w-7"
/> />
</div> </div>
</div> </div>
+8 -8
View File
@@ -7,8 +7,8 @@
| 文件 | 用途 | | 文件 | 用途 |
| ------------------------ | --------------------------------------------------------------------------------------------------- | | ------------------------ | --------------------------------------------------------------------------------------------------- |
| `chromeStorage.ts` | Chrome Storage API 封装:类型安全的 `StorageUtils` 类,提供 `get/set/remove` 方法 | | `chromeStorage.ts` | Chrome Storage API 封装:类型安全的 `StorageUtils` 类,提供 `get/set/remove` 方法 |
| `syncSnapshot.ts` | 通用 `localStorage` 快照读取(`snapshot/{key}`),用于 Router 与 `useStorageState` 首屏防闪烁 | | `syncSnapshot.ts` | 通用 `localStorage` 快照读取(`snapshot/{key}`),用于 Router 与 `useStorageState` 首屏防闪烁 |
| `themeSnapshot.ts` | 主题专用快照读写与 `document.documentElement` class 切换 | | `themeSnapshot.ts` | 主题专用快照读写与 `document.documentElement` class 切换 |
| `restrictedUrls.ts` | 受限 URL 检测(`chrome://``about:` 等),供 Storage Cleaner 等模块复用 | | `restrictedUrls.ts` | 受限 URL 检测(`chrome://``about:` 等),供 Storage Cleaner 等模块复用 |
| `chromeTabs.ts` | Chrome Tabs API 封装:获取活动标签页、获取域名、在新标签页打开扩展页面 | | `chromeTabs.ts` | Chrome Tabs API 封装:获取活动标签页、获取域名、在新标签页打开扩展页面 |
| `clipboard.ts` | 剪贴板操作:`copyTextToClipboard`(文本)、`copyImageToClipboard`(图片) | | `clipboard.ts` | 剪贴板操作:`copyTextToClipboard`(文本)、`copyImageToClipboard`(图片) |
@@ -42,12 +42,12 @@
写入经内部 `setAll()` 完成;`localStorage.setItem` 抛错(如配额超限)时返回 `false`,并 `console.error`**不会部分提交** 写入经内部 `setAll()` 完成;`localStorage.setItem` 抛错(如配额超限)时返回 `false`,并 `console.error`**不会部分提交**
| 方法 | 写入失败返回值 | 常见失败原因 | | 方法 | 写入失败返回值 | 常见失败原因 |
| -------------- | ---------------------------- | ------------------------------------------------------ | | ---- | -------------- | ------------ |
| `save()` | `null` | 达上限、规则不存在(更新时)、`setItem` 异常 | | `save()` | `null` | 达上限、规则不存在(更新时)、`setItem` 异常 |
| `update()` | `null` | 规则不存在、`setItem` 异常 | | `update()` | `null` | 规则不存在、`setItem` 异常 |
| `deleteRule()` | `false` | 规则不存在、`setItem` 异常 | | `deleteRule()` | `false` | 规则不存在、`setItem` 异常 |
| `duplicate()` | 仍可能返回对象但数据未持久化 | 见源码:`duplicate` 未检查 `setAll` 返回值(已知缺口) | | `duplicate()` | 仍可能返回对象但数据未持久化 | 见源码:`duplicate` 未检查 `setAll` 返回值(已知缺口) |
调用方应检查返回值后再展示成功 Toast。页面层参考 `FieldList.tsx``save`/`update` 返回非空才提示「规则已保存/已更新」。 调用方应检查返回值后再展示成功 Toast。页面层参考 `FieldList.tsx``save`/`update` 返回非空才提示「规则已保存/已更新」。
-1
View File
@@ -11,7 +11,6 @@ export default {
'./src/components/ErrorBoundary.tsx', './src/components/ErrorBoundary.tsx',
'./src/components/ErrorFallback.tsx', './src/components/ErrorFallback.tsx',
'./src/components/PageErrorBoundary.tsx', './src/components/PageErrorBoundary.tsx',
'./src/components/PageSkeleton.tsx',
'./src/components/ui/button.tsx', './src/components/ui/button.tsx',
'./src/components/ui/input.tsx', './src/components/ui/input.tsx',
'./src/components/ui/sonner.tsx', './src/components/ui/sonner.tsx',