Compare commits
14 Commits
0af99b6b70
...
b8cb884e9e
| Author | SHA1 | Date | |
|---|---|---|---|
| b8cb884e9e | |||
| 73d0b0d5fe | |||
| fe5e8533b5 | |||
| 12eb5c412a | |||
| 3635fbd256 | |||
| 058ecf7e39 | |||
| 6c7095ffb3 | |||
| 8ed9b011f4 | |||
| 2537b9fe75 | |||
| 5e24fa76a3 | |||
| 9d42a45bb1 | |||
| d5dde77ee9 | |||
| c4a9247cad | |||
| 56552863bf |
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -6,5 +6,8 @@
|
||||
},
|
||||
"[typescriptreact]": {
|
||||
"editor.defaultFormatter": "vscode.typescript-language-features"
|
||||
},
|
||||
"[css]": {
|
||||
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint"
|
||||
}
|
||||
}
|
||||
69
CLAUDE.md
69
CLAUDE.md
@@ -22,9 +22,7 @@ pnpm run start
|
||||
pnpm run lint
|
||||
|
||||
# 数据库操作
|
||||
pnpm prisma generate # 生成 Prisma client 到 src/generated/prisma
|
||||
pnpm prisma db push # 推送 schema 变更到数据库
|
||||
pnpm prisma studio # 打开 Prisma Studio 查看数据库
|
||||
# 不要进行数据库操作,让用户操作数据库
|
||||
```
|
||||
|
||||
## 技术栈
|
||||
@@ -36,7 +34,7 @@ pnpm prisma studio # 打开 Prisma Studio 查看数据库
|
||||
- **PostgreSQL** + **Prisma ORM**(自定义输出目录:`src/generated/prisma`)
|
||||
- **better-auth** 身份验证(邮箱/密码 + OAuth)
|
||||
- **next-intl** 国际化(支持:en-US, zh-CN, ja-JP, ko-KR, de-DE, fr-FR, it-IT, ug-CN)
|
||||
- **edge-tts-universal** 文本转语音
|
||||
- **阿里云千问 TTS** (qwen3-tts-flash) 文本转语音
|
||||
- **pnpm** 包管理器
|
||||
|
||||
## 架构设计
|
||||
@@ -51,10 +49,36 @@ src/app/
|
||||
│ └── [locale]/ # 国际化路由
|
||||
├── auth/ # 认证页面(sign-in, sign-up)
|
||||
├── folders/ # 用户学习文件夹管理
|
||||
├── api/ # API 路由
|
||||
└── profile/ # 用户资料页面
|
||||
├── users/[username]/# 用户资料页面(Server Component)
|
||||
├── profile/ # 重定向到当前用户的资料页面
|
||||
└── api/ # API 路由
|
||||
```
|
||||
|
||||
### 后端架构模式
|
||||
|
||||
项目使用 **Action-Service-Repository 三层架构**:
|
||||
|
||||
```
|
||||
src/modules/{module}/
|
||||
├── {module}-action.ts # Server Actions 层(表单处理、重定向)
|
||||
├── {module}-action-dto.ts # Action 层 DTO(Zod 验证)
|
||||
├── {module}-service.ts # Service 层(业务逻辑)
|
||||
├── {module}-service-dto.ts # Service 层 DTO
|
||||
├── {module}-repository.ts # Repository 层(数据库操作)
|
||||
└── {module}-repository-dto.ts # Repository 层 DTO
|
||||
```
|
||||
|
||||
各层职责:
|
||||
- **Action 层**:处理表单数据、验证输入、调用 service 层、处理重定向和错误响应
|
||||
- **Service 层**:实现业务逻辑、调用 better-auth API、协调多个 repository 操作
|
||||
- **Repository 层**:直接使用 Prisma 进行数据库查询和操作
|
||||
|
||||
现有模块:
|
||||
- `auth` - 认证和用户管理(支持用户名/邮箱登录)
|
||||
- `folder` - 学习文件夹管理
|
||||
- `dictionary` - 词典查询
|
||||
- `translator` - 翻译服务
|
||||
|
||||
### 数据库 Schema
|
||||
|
||||
核心模型(见 [prisma/schema.prisma](prisma/schema.prisma)):
|
||||
@@ -76,31 +100,6 @@ src/app/
|
||||
|
||||
**LLM 集成**: 使用智谱 AI API 进行翻译和 IPA 生成。通过环境变量 `ZHIPU_API_KEY` 和 `ZHIPU_MODEL_NAME` 配置。
|
||||
|
||||
### 环境变量
|
||||
|
||||
需要在 `.env.local` 中配置:
|
||||
|
||||
```env
|
||||
# LLM 集成
|
||||
ZHIPU_API_KEY=your-api-key
|
||||
ZHIPU_MODEL_NAME=your-model-name
|
||||
|
||||
# 认证
|
||||
BETTER_AUTH_SECRET=your-secret
|
||||
BETTER_AUTH_URL=http://localhost:3000
|
||||
GITHUB_CLIENT_ID=your-client-id
|
||||
GITHUB_CLIENT_SECRET=your-client-secret
|
||||
|
||||
# 数据库
|
||||
DATABASE_URL=postgresql://username:password@localhost:5432/database_name
|
||||
|
||||
// DashScore
|
||||
DASHSCORE_API_KEY=
|
||||
```
|
||||
|
||||
## 重要配置细节
|
||||
|
||||
- **Prisma client 输出**: 自定义目录 `src/generated/prisma`(不是默认的 `node_modules/.prisma`)
|
||||
- **Standalone 输出**: 为 Docker 部署配置
|
||||
- **React Compiler**: 在 `next.config.ts` 中启用以自动优化
|
||||
- **HTTPS 开发**: 开发服务器使用 `--experimental-https` 标志
|
||||
@@ -108,18 +107,22 @@ DASHSCORE_API_KEY=
|
||||
|
||||
## 代码组织
|
||||
|
||||
- `src/lib/actions/`: 数据库变更的 Server Actions
|
||||
- `src/modules/`: 业务模块(auth, folder, dictionary, translator)
|
||||
- `src/lib/actions/`: 数据库变更的 Server Actions(旧架构,正在迁移到 modules)
|
||||
- `src/lib/server/`: 服务端工具(AI 集成、认证、翻译器)
|
||||
- `src/lib/browser/`: 客户端工具
|
||||
- `src/hooks/`: 自定义 React hooks(认证 hooks、会话管理)
|
||||
- `src/i18n/`: 国际化配置
|
||||
- `messages/`: 各支持语言的翻译文件
|
||||
- `src/components/`: 可复用的 UI 组件(buttons, cards 等)
|
||||
- `src/shared/`: 共享常量和类型定义
|
||||
|
||||
## 开发注意事项
|
||||
|
||||
- 使用 pnpm,而不是 npm 或 yarn
|
||||
- schema 变更后,先运行 `pnpm prisma generate` 再运行 `pnpm prisma db push`
|
||||
- 应用使用 TypeScript 严格模式 - 确保类型安全
|
||||
- 所有面向用户的文本都需要国际化
|
||||
- **优先使用 Server Components**,只在需要交互时使用 Client Components
|
||||
- **新功能应遵循 action-service-repository 架构**
|
||||
- Better-auth 处理会话管理 - 使用 authClient 适配器进行认证操作
|
||||
- 使用 better-auth username 插件支持用户名登录
|
||||
|
||||
45
README.md
45
README.md
@@ -9,7 +9,9 @@
|
||||
- **SRT字幕播放器** - 结合视频字幕学习,支持多种字幕格式
|
||||
- **字母学习模块** - 针对初学者的字母和发音基础学习
|
||||
- **记忆强化工具** - 通过科学记忆法巩固学习内容
|
||||
- **词典查询** - 查询单词和短语,提供详细释义和例句
|
||||
- **个人学习空间** - 用户可以创建、管理和组织自己的学习资料
|
||||
- **用户资料系统** - 支持用户名登录、个人资料页面展示
|
||||
|
||||
## 🛠 技术栈
|
||||
|
||||
@@ -26,7 +28,7 @@
|
||||
|
||||
### 国际化与辅助功能
|
||||
- **next-intl** - 国际化解决方案
|
||||
- **qwen3-tts-flash** - 通义千问语音合成
|
||||
- **阿里云千问 TTS** - qwen3-tts-flash 语音合成
|
||||
|
||||
### 开发工具
|
||||
- **ESLint** - 代码质量检查
|
||||
@@ -38,8 +40,16 @@
|
||||
src/
|
||||
├── app/ # Next.js App Router 路由
|
||||
│ ├── (features)/ # 功能模块路由
|
||||
│ ├── api/ # API 路由
|
||||
│ └── auth/ # 认证相关页面
|
||||
│ ├── auth/ # 认证相关页面
|
||||
│ ├── profile/ # 用户资料重定向
|
||||
│ ├── users/[username]/ # 用户资料页面
|
||||
│ ├── folders/ # 文件夹管理
|
||||
│ └── api/ # API 路由
|
||||
├── modules/ # 业务模块(action-service-repository 架构)
|
||||
│ ├── auth/ # 认证模块
|
||||
│ ├── folder/ # 文件夹模块
|
||||
│ ├── dictionary/ # 词典模块
|
||||
│ └── translator/ # 翻译模块
|
||||
├── components/ # React 组件
|
||||
│ ├── buttons/ # 按钮组件
|
||||
│ ├── cards/ # 卡片组件
|
||||
@@ -50,6 +60,7 @@ src/
|
||||
│ └── server/ # 服务器端工具
|
||||
├── hooks/ # 自定义 React Hooks
|
||||
├── i18n/ # 国际化配置
|
||||
├── shared/ # 共享常量和类型
|
||||
└── config/ # 应用配置
|
||||
```
|
||||
|
||||
@@ -57,7 +68,7 @@ src/
|
||||
|
||||
### 环境要求
|
||||
|
||||
- Node.js 24
|
||||
- Node.js 23
|
||||
- PostgreSQL 数据库
|
||||
- pnpm (推荐) 或 npm
|
||||
|
||||
@@ -85,17 +96,20 @@ cp .env.example .env.local
|
||||
然后编辑 `.env.local` 文件,配置所有必要的环境变量:
|
||||
|
||||
```env
|
||||
// LLM
|
||||
# LLM 集成(智谱 AI 用于翻译和 IPA 生成)
|
||||
ZHIPU_API_KEY=your-zhipu-api-key
|
||||
ZHIPU_MODEL_NAME=your-zhipu-model-name
|
||||
|
||||
// Auth
|
||||
# 阿里云千问 TTS(文本转语音)
|
||||
DASHSCORE_API_KEY=your-dashscore-api-key
|
||||
|
||||
# 认证
|
||||
BETTER_AUTH_SECRET=your-better-auth-secret
|
||||
BETTER_AUTH_URL=http://localhost:3000
|
||||
GITHUB_CLIENT_ID=your-github-client-id
|
||||
GITHUB_CLIENT_SECRET=your-github-client-secret
|
||||
|
||||
// Database
|
||||
# 数据库
|
||||
DATABASE_URL=postgresql://username:password@localhost:5432/database_name
|
||||
```
|
||||
|
||||
@@ -118,14 +132,27 @@ pnpm run dev
|
||||
|
||||
### 认证系统
|
||||
|
||||
应用使用 better-auth 提供安全的用户认证系统,支持邮箱/密码登录和第三方登录。
|
||||
应用使用 better-auth 提供安全的用户认证系统,支持:
|
||||
- 邮箱/密码登录和注册
|
||||
- **用户名登录**(可通过用户名或邮箱登录)
|
||||
- GitHub OAuth 第三方登录
|
||||
- 邮箱验证功能
|
||||
|
||||
### 后端架构
|
||||
|
||||
项目采用 **Action-Service-Repository 三层架构**:
|
||||
- **Action 层**:处理 Server Actions、表单验证、重定向
|
||||
- **Service 层**:业务逻辑、better-auth 集成
|
||||
- **Repository 层**:Prisma 数据库操作
|
||||
|
||||
### 数据模型
|
||||
|
||||
核心数据模型包括:
|
||||
- **User** - 用户信息
|
||||
- **User** - 用户信息(支持用户名、邮箱、头像)
|
||||
- **Folder** - 学习资料文件夹
|
||||
- **Pair** - 语言对(翻译对、词汇对等)
|
||||
- **Session/Account** - 认证会话追踪
|
||||
- **Verification** - 邮箱验证系统
|
||||
|
||||
详细模型定义请参考 [prisma/schema.prisma](./prisma/schema.prisma)
|
||||
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "Sprache 2",
|
||||
"enterLanguageName": "Bitte geben Sie den Sprachennamen ein",
|
||||
"edit": "Bearbeiten",
|
||||
"delete": "Löschen"
|
||||
"delete": "Löschen",
|
||||
"permissionDenied": "Sie haben keine Berechtigung, diese Aktion auszuführen",
|
||||
"error": {
|
||||
"update": "Sie haben keine Berechtigung, dieses Element zu aktualisieren.",
|
||||
"delete": "Sie haben keine Berechtigung, dieses Element zu löschen.",
|
||||
"add": "Sie haben keine Berechtigung, Elemente zu diesem Ordner hinzuzufügen.",
|
||||
"rename": "Sie haben keine Berechtigung, diesen Ordner umzubenennen.",
|
||||
"deleteFolder": "Sie haben keine Berechtigung, diesen Ordner zu löschen."
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "Sprachen lernen",
|
||||
@@ -225,5 +233,26 @@
|
||||
"pleaseCreateFolder": "Bitte erstellen Sie zuerst einen Ordner",
|
||||
"savedToFolder": "Im Ordner gespeichert: {folderName}",
|
||||
"saveFailed": "Speichern fehlgeschlagen, bitte später erneut versuchen"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "Anonym",
|
||||
"email": "E-Mail",
|
||||
"verified": "Verifiziert",
|
||||
"unverified": "Nicht verifiziert",
|
||||
"accountInfo": "Kontoinformationen",
|
||||
"userId": "Benutzer-ID",
|
||||
"username": "Benutzername",
|
||||
"displayName": "Anzeigename",
|
||||
"notSet": "Nicht festgelegt",
|
||||
"memberSince": "Mitglied seit",
|
||||
"folders": {
|
||||
"title": "Ordner",
|
||||
"noFolders": "Noch keine Ordner",
|
||||
"folderName": "Ordnername",
|
||||
"totalPairs": "Anzahl der Paare",
|
||||
"createdAt": "Erstellt am",
|
||||
"actions": "Aktionen",
|
||||
"view": "Ansehen"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "Locale 2",
|
||||
"enterLanguageName": "Please enter language name",
|
||||
"edit": "Edit",
|
||||
"delete": "Delete"
|
||||
"delete": "Delete",
|
||||
"permissionDenied": "You do not have permission to perform this action",
|
||||
"error": {
|
||||
"update": "You do not have permission to update this item.",
|
||||
"delete": "You do not have permission to delete this item.",
|
||||
"add": "You do not have permission to add items to this folder.",
|
||||
"rename": "You do not have permission to rename this folder.",
|
||||
"deleteFolder": "You do not have permission to delete this folder."
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "Learn Languages",
|
||||
@@ -91,6 +99,8 @@
|
||||
"password": "Password",
|
||||
"confirmPassword": "Confirm Password",
|
||||
"name": "Name",
|
||||
"username": "Username",
|
||||
"emailOrUsername": "Email or Username",
|
||||
"signInButton": "Sign In",
|
||||
"signUpButton": "Sign Up",
|
||||
"noAccount": "Don't have an account?",
|
||||
@@ -101,7 +111,11 @@
|
||||
"passwordTooShort": "Password must be at least 8 characters",
|
||||
"passwordsNotMatch": "Passwords do not match",
|
||||
"nameRequired": "Please enter your name",
|
||||
"usernameRequired": "Please enter a username",
|
||||
"usernameTooShort": "Username must be at least 3 characters",
|
||||
"usernameInvalid": "Username can only contain letters, numbers, and underscores",
|
||||
"emailRequired": "Please enter your email",
|
||||
"identifierRequired": "Please enter your email or username",
|
||||
"passwordRequired": "Please enter your password",
|
||||
"confirmPasswordRequired": "Please confirm your password",
|
||||
"loading": "Loading..."
|
||||
@@ -225,5 +239,26 @@
|
||||
"pleaseCreateFolder": "Please create a folder first",
|
||||
"savedToFolder": "Saved to folder: {folderName}",
|
||||
"saveFailed": "Save failed, please try again later"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "Anonymous",
|
||||
"email": "Email",
|
||||
"verified": "Verified",
|
||||
"unverified": "Unverified",
|
||||
"accountInfo": "Account Information",
|
||||
"userId": "User ID",
|
||||
"username": "Username",
|
||||
"displayName": "Display Name",
|
||||
"notSet": "Not Set",
|
||||
"memberSince": "Member Since",
|
||||
"folders": {
|
||||
"title": "Folders",
|
||||
"noFolders": "No folders yet",
|
||||
"folderName": "Folder Name",
|
||||
"totalPairs": "Total Pairs",
|
||||
"createdAt": "Created At",
|
||||
"actions": "Actions",
|
||||
"view": "View"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "Langue 2",
|
||||
"enterLanguageName": "Veuillez entrer le nom de la langue",
|
||||
"edit": "Modifier",
|
||||
"delete": "Supprimer"
|
||||
"delete": "Supprimer",
|
||||
"permissionDenied": "Vous n'avez pas la permission d'effectuer cette action",
|
||||
"error": {
|
||||
"update": "Vous n'avez pas la permission de mettre à jour cet élément.",
|
||||
"delete": "Vous n'avez pas la permission de supprimer cet élément.",
|
||||
"add": "Vous n'avez pas la permission d'ajouter des éléments à ce dossier.",
|
||||
"rename": "Vous n'avez pas la permission de renommer ce dossier.",
|
||||
"deleteFolder": "Vous n'avez pas la permission de supprimer ce dossier."
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "Apprendre les langues",
|
||||
@@ -225,5 +233,26 @@
|
||||
"pleaseCreateFolder": "Veuillez d'abord créer un dossier",
|
||||
"savedToFolder": "Enregistré dans le dossier : {folderName}",
|
||||
"saveFailed": "Échec de l'enregistrement, veuillez réessayer plus tard"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "Anonyme",
|
||||
"email": "E-mail",
|
||||
"verified": "Vérifié",
|
||||
"unverified": "Non vérifié",
|
||||
"accountInfo": "Informations du compte",
|
||||
"userId": "ID utilisateur",
|
||||
"username": "Nom d'utilisateur",
|
||||
"displayName": "Nom d'affichage",
|
||||
"notSet": "Non défini",
|
||||
"memberSince": "Membre depuis",
|
||||
"folders": {
|
||||
"title": "Dossiers",
|
||||
"noFolders": "Aucun dossier pour le moment",
|
||||
"folderName": "Nom du dossier",
|
||||
"totalPairs": "Nombre de paires",
|
||||
"createdAt": "Créé le",
|
||||
"actions": "Actions",
|
||||
"view": "Voir"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "Lingua 2",
|
||||
"enterLanguageName": "Inserisci il nome della lingua",
|
||||
"edit": "Modifica",
|
||||
"delete": "Elimina"
|
||||
"delete": "Elimina",
|
||||
"permissionDenied": "Non hai il permesso di eseguire questa azione",
|
||||
"error": {
|
||||
"update": "Non hai il permesso di aggiornare questo elemento.",
|
||||
"delete": "Non hai il permesso di eliminare questo elemento.",
|
||||
"add": "Non hai il permesso di aggiungere elementi a questa cartella.",
|
||||
"rename": "Non hai il permesso di rinominare questa cartella.",
|
||||
"deleteFolder": "Non hai il permesso di eliminare questa cartella."
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "Impara le lingue",
|
||||
@@ -225,5 +233,26 @@
|
||||
"pleaseCreateFolder": "Crea prima una cartella",
|
||||
"savedToFolder": "Salvato nella cartella: {folderName}",
|
||||
"saveFailed": "Salvataggio fallito, riprova più tardi"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "Anonimo",
|
||||
"email": "Email",
|
||||
"verified": "Verificato",
|
||||
"unverified": "Non verificato",
|
||||
"accountInfo": "Informazioni account",
|
||||
"userId": "ID utente",
|
||||
"username": "Nome utente",
|
||||
"displayName": "Nome visualizzato",
|
||||
"notSet": "Non impostato",
|
||||
"memberSince": "Membro dal",
|
||||
"folders": {
|
||||
"title": "Cartelle",
|
||||
"noFolders": "Nessuna cartella ancora",
|
||||
"folderName": "Nome cartella",
|
||||
"totalPairs": "Numero di coppie",
|
||||
"createdAt": "Creato il",
|
||||
"actions": "Azioni",
|
||||
"view": "Visualizza"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "言語2",
|
||||
"enterLanguageName": "言語名を入力してください",
|
||||
"edit": "編集",
|
||||
"delete": "削除"
|
||||
"delete": "削除",
|
||||
"permissionDenied": "この操作を実行する権限がありません",
|
||||
"error": {
|
||||
"update": "この項目を更新する権限がありません。",
|
||||
"delete": "この項目を削除する権限がありません。",
|
||||
"add": "このフォルダーに項目を追加する権限がありません。",
|
||||
"rename": "このフォルダー名を変更する権限がありません。",
|
||||
"deleteFolder": "このフォルダーを削除する権限がありません。"
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "言語を学ぶ",
|
||||
@@ -225,5 +233,26 @@
|
||||
"pleaseCreateFolder": "まずフォルダを作成してください",
|
||||
"savedToFolder": "フォルダに保存しました:{folderName}",
|
||||
"saveFailed": "保存に失敗しました。後でもう一度お試しください"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "匿名",
|
||||
"email": "メールアドレス",
|
||||
"verified": "認証済み",
|
||||
"unverified": "未認証",
|
||||
"accountInfo": "アカウント情報",
|
||||
"userId": "ユーザーID",
|
||||
"username": "ユーザー名",
|
||||
"displayName": "表示名",
|
||||
"notSet": "未設定",
|
||||
"memberSince": "登録日",
|
||||
"folders": {
|
||||
"title": "フォルダー",
|
||||
"noFolders": "フォルダーがありません",
|
||||
"folderName": "フォルダー名",
|
||||
"totalPairs": "テキストペア数",
|
||||
"createdAt": "作成日",
|
||||
"actions": "操作",
|
||||
"view": "表示"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "언어 2",
|
||||
"enterLanguageName": "언어 이름을 입력하세요",
|
||||
"edit": "편집",
|
||||
"delete": "삭제"
|
||||
"delete": "삭제",
|
||||
"permissionDenied": "이 작업을 수행할 권한이 없습니다",
|
||||
"error": {
|
||||
"update": "이 항목을 업데이트할 권한이 없습니다.",
|
||||
"delete": "이 항목을 삭제할 권한이 없습니다.",
|
||||
"add": "이 폴더에 항목을 추가할 권한이 없습니다.",
|
||||
"rename": "이 폴더 이름을 변경할 권한이 없습니다.",
|
||||
"deleteFolder": "이 폴더를 삭제할 권한이 없습니다."
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "언어 학습",
|
||||
@@ -225,5 +233,26 @@
|
||||
"pleaseCreateFolder": "먼저 폴더를 만드세요",
|
||||
"savedToFolder": "폴더에 저장됨: {folderName}",
|
||||
"saveFailed": "저장 실패, 나중에 다시 시도하세요"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "익명",
|
||||
"email": "이메일",
|
||||
"verified": "인증됨",
|
||||
"unverified": "미인증",
|
||||
"accountInfo": "계정 정보",
|
||||
"userId": "사용자 ID",
|
||||
"username": "사용자명",
|
||||
"displayName": "표시 이름",
|
||||
"notSet": "설정되지 않음",
|
||||
"memberSince": "가입일",
|
||||
"folders": {
|
||||
"title": "폴더",
|
||||
"noFolders": "폴더가 없습니다",
|
||||
"folderName": "폴더 이름",
|
||||
"totalPairs": "텍스트 쌍 수",
|
||||
"createdAt": "생성일",
|
||||
"actions": "작업",
|
||||
"view": "보기"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "تىل 2",
|
||||
"enterLanguageName": "تىل نامىنى كىرگۈزۈڭ",
|
||||
"edit": "تەھرىرلەش",
|
||||
"delete": "ئۆچۈرۈش"
|
||||
"delete": "ئۆچۈرۈش",
|
||||
"permissionDenied": "بۇ مەشغۇلاتنى ئىجرا قىلىش ھوقۇقىڭىز يوق",
|
||||
"error": {
|
||||
"update": "بۇ تۈرنى يېڭىلاش ھوقۇقىڭىز يوق.",
|
||||
"delete": "بۇ تۈرنى ئۆچۈرۈش ھوقۇقىڭىز يوق.",
|
||||
"add": "بۇ قىسقۇچقا تۈر قوشۇش ھوقۇقىڭىز يوق.",
|
||||
"rename": "بۇ قىسقۇچنىڭ نامىنى ئۆزگەرتىش ھوقۇقىڭىز يوق.",
|
||||
"deleteFolder": "بۇ قىسقۇچنى ئۆچۈرۈش ھوقۇقىڭىز يوق."
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "تىل ئۆگىنىڭ",
|
||||
@@ -225,5 +233,26 @@
|
||||
"pleaseCreateFolder": "ئاۋۋال قىسقۇچ قۇرۇڭ",
|
||||
"savedToFolder": "قىسقۇچقا ساقلاندى: {folderName}",
|
||||
"saveFailed": "ساقلاش مەغلۇب بولدى، كېيىنرەك قايتا سىناڭ"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "ئىسىمسىز",
|
||||
"email": "ئېلخەت",
|
||||
"verified": "دەلىللەندى",
|
||||
"unverified": "دەلىتلەنمىدى",
|
||||
"accountInfo": "ھېسابات ئۇچۇرى",
|
||||
"userId": "ئىشلەتكۈچى كودى",
|
||||
"username": "ئىشلەتكۈچى نامى",
|
||||
"displayName": "كۆرسىتىلىدىغان نام",
|
||||
"notSet": "تەڭشەلمىگەن",
|
||||
"memberSince": "تىزىملاتقان ۋاقىت",
|
||||
"folders": {
|
||||
"title": "قىسقۇچلار",
|
||||
"noFolders": "قىسقۇچ يوق",
|
||||
"folderName": "قىسقۇچ نامى",
|
||||
"totalPairs": "تېكىست جۈپ سانى",
|
||||
"createdAt": "قۇرۇلغان ۋاقىت",
|
||||
"actions": "مەشغۇلات",
|
||||
"view": "كۆرۈش"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,15 @@
|
||||
"language2": "语言2",
|
||||
"enterLanguageName": "请输入语言名称",
|
||||
"edit": "编辑",
|
||||
"delete": "删除"
|
||||
"delete": "删除",
|
||||
"permissionDenied": "您没有权限执行此操作",
|
||||
"error": {
|
||||
"update": "您没有权限更新此项目",
|
||||
"delete": "您没有权限删除此项目",
|
||||
"add": "您没有权限向此文件夹添加项目",
|
||||
"rename": "您没有权限重命名此文件夹",
|
||||
"deleteFolder": "您没有权限删除此文件夹"
|
||||
}
|
||||
},
|
||||
"home": {
|
||||
"title": "学语言",
|
||||
@@ -91,6 +99,8 @@
|
||||
"password": "密码",
|
||||
"confirmPassword": "确认密码",
|
||||
"name": "用户名",
|
||||
"username": "用户名",
|
||||
"emailOrUsername": "邮箱或用户名",
|
||||
"signInButton": "登录",
|
||||
"signUpButton": "注册",
|
||||
"noAccount": "还没有账户?",
|
||||
@@ -101,7 +111,11 @@
|
||||
"passwordTooShort": "密码至少需要8个字符",
|
||||
"passwordsNotMatch": "两次输入的密码不匹配",
|
||||
"nameRequired": "请输入用户名",
|
||||
"usernameRequired": "请输入用户名",
|
||||
"usernameTooShort": "用户名至少需要3个字符",
|
||||
"usernameInvalid": "用户名只能包含字母、数字和下划线",
|
||||
"emailRequired": "请输入邮箱",
|
||||
"identifierRequired": "请输入邮箱或用户名",
|
||||
"passwordRequired": "请输入密码",
|
||||
"confirmPasswordRequired": "请确认密码",
|
||||
"loading": "加载中..."
|
||||
@@ -225,5 +239,26 @@
|
||||
"pleaseCreateFolder": "请先创建文件夹",
|
||||
"savedToFolder": "已保存到文件夹:{folderName}",
|
||||
"saveFailed": "保存失败,请稍后重试"
|
||||
},
|
||||
"user_profile": {
|
||||
"anonymous": "匿名",
|
||||
"email": "邮箱",
|
||||
"verified": "已验证",
|
||||
"unverified": "未验证",
|
||||
"accountInfo": "账户信息",
|
||||
"userId": "用户ID",
|
||||
"username": "用户名",
|
||||
"displayName": "显示名称",
|
||||
"notSet": "未设置",
|
||||
"memberSince": "注册时间",
|
||||
"folders": {
|
||||
"title": "文件夹",
|
||||
"noFolders": "还没有文件夹",
|
||||
"folderName": "文件夹名称",
|
||||
"totalPairs": "文本对数量",
|
||||
"createdAt": "创建时间",
|
||||
"actions": "操作",
|
||||
"view": "查看"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
"@prisma/client": "^7.2.0",
|
||||
"bcryptjs": "^3.0.3",
|
||||
"better-auth": "^1.4.10",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"dotenv": "^17.2.3",
|
||||
"lucide-react": "^0.562.0",
|
||||
"next": "16.1.1",
|
||||
@@ -23,6 +25,7 @@
|
||||
"react": "19.2.3",
|
||||
"react-dom": "19.2.3",
|
||||
"sonner": "^2.0.7",
|
||||
"tailwind-merge": "^3.4.0",
|
||||
"unstorage": "^1.17.3",
|
||||
"zod": "^4.3.5"
|
||||
},
|
||||
|
||||
27
pnpm-lock.yaml
generated
27
pnpm-lock.yaml
generated
@@ -24,6 +24,12 @@ importers:
|
||||
better-auth:
|
||||
specifier: ^1.4.10
|
||||
version: 1.4.10(@prisma/client@7.2.0(prisma@7.2.0(@types/react@19.2.7)(better-sqlite3@12.5.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3)(typescript@5.9.3))(typescript@5.9.3))(better-sqlite3@12.5.0)(drizzle-orm@0.33.0(@electric-sql/pglite@0.3.2)(@prisma/client@5.22.0(prisma@7.2.0(@types/react@19.2.7)(better-sqlite3@12.5.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3)(typescript@5.9.3)))(@types/pg@8.15.6)(@types/react@19.2.7)(better-sqlite3@12.5.0)(kysely@0.28.8)(mysql2@3.15.3)(pg@8.16.3)(postgres@3.4.7)(prisma@7.2.0(@types/react@19.2.7)(better-sqlite3@12.5.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3)(typescript@5.9.3))(react@19.2.3))(mysql2@3.15.3)(next@16.1.1(@babel/core@7.28.5)(babel-plugin-react-compiler@1.0.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3))(pg@8.16.3)(prisma@7.2.0(@types/react@19.2.7)(better-sqlite3@12.5.0)(react-dom@19.2.3(react@19.2.3))(react@19.2.3)(typescript@5.9.3))(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
|
||||
class-variance-authority:
|
||||
specifier: ^0.7.1
|
||||
version: 0.7.1
|
||||
clsx:
|
||||
specifier: ^2.1.1
|
||||
version: 2.1.1
|
||||
dotenv:
|
||||
specifier: ^17.2.3
|
||||
version: 17.2.3
|
||||
@@ -48,6 +54,9 @@ importers:
|
||||
sonner:
|
||||
specifier: ^2.0.7
|
||||
version: 2.0.7(react-dom@19.2.3(react@19.2.3))(react@19.2.3)
|
||||
tailwind-merge:
|
||||
specifier: ^3.4.0
|
||||
version: 3.4.0
|
||||
unstorage:
|
||||
specifier: ^1.17.3
|
||||
version: 1.17.3
|
||||
@@ -1480,9 +1489,16 @@ packages:
|
||||
citty@0.1.6:
|
||||
resolution: {integrity: sha512-tskPPKEs8D2KPafUypv2gxwJP8h/OaJmC82QQGGDQcHvXX43xF2VDACcJVmZ0EuSxkpO9Kc4MlrA3q0+FG58AQ==}
|
||||
|
||||
class-variance-authority@0.7.1:
|
||||
resolution: {integrity: sha512-Ka+9Trutv7G8M6WT6SeiRWz792K5qEqIGEGzXKhAE6xOWAY6pPH8U+9IY3oCMv6kqTmLsv7Xh/2w2RigkePMsg==}
|
||||
|
||||
client-only@0.0.1:
|
||||
resolution: {integrity: sha512-IV3Ou0jSMzZrd3pZ48nLkT9DA7Ag1pnPzaiQhpW7c3RbcqqzvzzVu+L8gfqMp/8IM2MQtSiqaCxrrcfu8I8rMA==}
|
||||
|
||||
clsx@2.1.1:
|
||||
resolution: {integrity: sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==}
|
||||
engines: {node: '>=6'}
|
||||
|
||||
color-convert@2.0.1:
|
||||
resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==}
|
||||
engines: {node: '>=7.0.0'}
|
||||
@@ -3024,6 +3040,9 @@ packages:
|
||||
resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==}
|
||||
engines: {node: '>= 0.4'}
|
||||
|
||||
tailwind-merge@3.4.0:
|
||||
resolution: {integrity: sha512-uSaO4gnW+b3Y2aWoWfFpX62vn2sR3skfhbjsEnaBI81WD1wBLlHZe5sWf0AqjksNdYTbGBEd0UasQMT3SNV15g==}
|
||||
|
||||
tailwindcss@4.1.18:
|
||||
resolution: {integrity: sha512-4+Z+0yiYyEtUVCScyfHCxOYP06L5Ne+JiHhY2IjR2KWMIWhJOYZKLSGZaP5HkZ8+bY0cxfzwDE5uOmzFXyIwxw==}
|
||||
|
||||
@@ -4778,8 +4797,14 @@ snapshots:
|
||||
dependencies:
|
||||
consola: 3.4.2
|
||||
|
||||
class-variance-authority@0.7.1:
|
||||
dependencies:
|
||||
clsx: 2.1.1
|
||||
|
||||
client-only@0.0.1: {}
|
||||
|
||||
clsx@2.1.1: {}
|
||||
|
||||
color-convert@2.0.1:
|
||||
dependencies:
|
||||
color-name: 1.1.4
|
||||
@@ -6407,6 +6432,8 @@ snapshots:
|
||||
|
||||
supports-preserve-symlinks-flag@1.0.0: {}
|
||||
|
||||
tailwind-merge@3.4.0: {}
|
||||
|
||||
tailwindcss@4.1.18: {}
|
||||
|
||||
tapable@2.3.0: {}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- A unique constraint covering the columns `[username]` on the table `user` will be added. If there are existing duplicate values, this will fail.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE "user" ADD COLUMN "displayUsername" TEXT,
|
||||
ADD COLUMN "username" TEXT;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "user_username_key" ON "user"("username");
|
||||
@@ -10,25 +10,26 @@ datasource db {
|
||||
model User {
|
||||
id String @id
|
||||
name String
|
||||
email String
|
||||
email String @unique
|
||||
emailVerified Boolean @default(false)
|
||||
image String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
sessions Session[]
|
||||
displayUsername String?
|
||||
username String? @unique
|
||||
accounts Account[]
|
||||
folders Folder[]
|
||||
dictionaryLookUps DictionaryLookUp[]
|
||||
folders Folder[]
|
||||
sessions Session[]
|
||||
translationHistories TranslationHistory[]
|
||||
|
||||
@@unique([email])
|
||||
@@map("user")
|
||||
}
|
||||
|
||||
model Session {
|
||||
id String @id
|
||||
expiresAt DateTime
|
||||
token String
|
||||
token String @unique
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
ipAddress String?
|
||||
@@ -36,7 +37,6 @@ model Session {
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([token])
|
||||
@@index([userId])
|
||||
@@map("session")
|
||||
}
|
||||
@@ -46,7 +46,6 @@ model Account {
|
||||
accountId String
|
||||
providerId String
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
accessToken String?
|
||||
refreshToken String?
|
||||
idToken String?
|
||||
@@ -56,6 +55,7 @@ model Account {
|
||||
password String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([userId])
|
||||
@@map("account")
|
||||
@@ -75,17 +75,16 @@ model Verification {
|
||||
|
||||
model Pair {
|
||||
id Int @id @default(autoincrement())
|
||||
text1 String
|
||||
text2 String
|
||||
language1 String
|
||||
language2 String
|
||||
text1 String
|
||||
text2 String
|
||||
ipa1 String?
|
||||
ipa2 String?
|
||||
folderId Int @map("folder_id")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
folder Folder @relation(fields: [folderId], references: [id], onDelete: Cascade)
|
||||
folder Folder @relation(fields: [folderId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([folderId, language1, language2, text1, text2])
|
||||
@@index([folderId])
|
||||
@@ -98,26 +97,24 @@ model Folder {
|
||||
userId String @map("user_id")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
pairs Pair[]
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
pairs Pair[]
|
||||
|
||||
@@index([userId])
|
||||
@@map("folders")
|
||||
}
|
||||
|
||||
model DictionaryLookUp {
|
||||
id Int @id @default(autoincrement())
|
||||
userId String? @map("user_id")
|
||||
id Int @id @default(autoincrement())
|
||||
userId String? @map("user_id")
|
||||
text String
|
||||
normalizedText String @default("") @map("normalized_text")
|
||||
queryLang String @map("query_lang")
|
||||
definitionLang String @map("definition_lang")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
dictionaryItemId Int? @map("dictionary_item_id")
|
||||
|
||||
user User? @relation(fields: [userId], references: [id])
|
||||
dictionaryItem DictionaryItem? @relation(fields: [dictionaryItemId], references: [id], onDelete: SetNull)
|
||||
queryLang String @map("query_lang")
|
||||
definitionLang String @map("definition_lang")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
dictionaryItemId Int? @map("dictionary_item_id")
|
||||
normalizedText String @default("") @map("normalized_text")
|
||||
dictionaryItem DictionaryItem? @relation(fields: [dictionaryItemId], references: [id])
|
||||
user User? @relation(fields: [userId], references: [id])
|
||||
|
||||
@@index([userId])
|
||||
@@index([createdAt])
|
||||
@@ -126,16 +123,15 @@ model DictionaryLookUp {
|
||||
}
|
||||
|
||||
model DictionaryItem {
|
||||
id Int @id @default(autoincrement())
|
||||
frequency Int @default(1)
|
||||
standardForm String @map("standard_form")
|
||||
queryLang String @map("query_lang")
|
||||
definitionLang String @map("definition_lang")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
lookups DictionaryLookUp[]
|
||||
entries DictionaryEntry[]
|
||||
id Int @id @default(autoincrement())
|
||||
frequency Int @default(1)
|
||||
standardForm String @map("standard_form")
|
||||
queryLang String @map("query_lang")
|
||||
definitionLang String @map("definition_lang")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
entries DictionaryEntry[]
|
||||
lookups DictionaryLookUp[]
|
||||
|
||||
@@unique([standardForm, queryLang, definitionLang])
|
||||
@@index([standardForm])
|
||||
@@ -144,16 +140,15 @@ model DictionaryItem {
|
||||
}
|
||||
|
||||
model DictionaryEntry {
|
||||
id Int @id @default(autoincrement())
|
||||
itemId Int @map("item_id")
|
||||
id Int @id @default(autoincrement())
|
||||
itemId Int @map("item_id")
|
||||
ipa String?
|
||||
definition String
|
||||
partOfSpeech String? @map("part_of_speech")
|
||||
partOfSpeech String? @map("part_of_speech")
|
||||
example String
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
item DictionaryItem @relation(fields: [itemId], references: [id], onDelete: Cascade)
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
item DictionaryItem @relation(fields: [itemId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([itemId])
|
||||
@@index([createdAt])
|
||||
@@ -171,8 +166,7 @@ model TranslationHistory {
|
||||
targetIpa String? @map("target_ipa")
|
||||
createdAt DateTime @default(now()) @map("created_at")
|
||||
updatedAt DateTime @updatedAt @map("updated_at")
|
||||
|
||||
user User? @relation(fields: [userId], references: [id], onDelete: SetNull)
|
||||
user User? @relation(fields: [userId], references: [id])
|
||||
|
||||
@@index([userId])
|
||||
@@index([createdAt])
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Letter, SupportedAlphabets } from "@/lib/interfaces";
|
||||
import { IconClick } from "@/components/ui/buttons";
|
||||
import { IconClick, CircleToggleButton, CircleButton, PrimaryButton } from "@/design-system/base/button";
|
||||
import { IMAGES } from "@/config/images";
|
||||
import { ChevronLeft, ChevronRight } from "lucide-react";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { Card } from "@/design-system/base/card";
|
||||
|
||||
interface AlphabetCardProps {
|
||||
alphabet: Letter[];
|
||||
@@ -97,148 +99,122 @@ export function AlphabetCard({ alphabet, alphabetType, onBack }: AlphabetCardPro
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center px-4 py-8">
|
||||
<div className="w-full max-w-2xl">
|
||||
{/* 右上角返回按钮 */}
|
||||
<div className="flex justify-end mb-4">
|
||||
<IconClick
|
||||
size={32}
|
||||
alt="close"
|
||||
src={IMAGES.close}
|
||||
onClick={onBack}
|
||||
className="bg-white rounded-full shadow-md"
|
||||
/>
|
||||
</div>
|
||||
<PageLayout className="relative">
|
||||
{/* 右上角返回按钮 - outside the white card */}
|
||||
<div className="flex justify-end mb-4">
|
||||
<IconClick
|
||||
size="lg"
|
||||
alt="close"
|
||||
src={IMAGES.close}
|
||||
onClick={onBack}
|
||||
className="bg-white rounded-full shadow-md"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 白色主卡片容器 */}
|
||||
<div className="bg-white rounded-2xl shadow-xl p-8 md:p-12">
|
||||
{/* 顶部进度指示器和显示选项按钮 */}
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
{/* 当前字母进度 */}
|
||||
<span className="text-sm text-gray-500">
|
||||
{currentIndex + 1} / {alphabet.length}
|
||||
</span>
|
||||
{/* 显示选项切换按钮组 */}
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
<button
|
||||
onClick={() => setShowLetter(!showLetter)}
|
||||
className={`px-3 py-1 rounded-full text-sm transition-colors ${
|
||||
showLetter
|
||||
? "bg-[#35786f] text-white"
|
||||
: "bg-gray-200 text-gray-600"
|
||||
}`}
|
||||
>
|
||||
{t("letter")}
|
||||
</button>
|
||||
{/* IPA 音标显示切换 */}
|
||||
<button
|
||||
onClick={() => setShowIPA(!showIPA)}
|
||||
className={`px-3 py-1 rounded-full text-sm transition-colors ${
|
||||
showIPA
|
||||
? "bg-[#35786f] text-white"
|
||||
: "bg-gray-200 text-gray-600"
|
||||
}`}
|
||||
>
|
||||
IPA
|
||||
</button>
|
||||
{/* 罗马音显示切换(仅日语显示) */}
|
||||
{hasRomanization && (
|
||||
<button
|
||||
onClick={() => setShowRoman(!showRoman)}
|
||||
className={`px-3 py-1 rounded-full text-sm transition-colors ${
|
||||
showRoman
|
||||
? "bg-[#35786f] text-white"
|
||||
: "bg-gray-200 text-gray-600"
|
||||
}`}
|
||||
>
|
||||
{t("roman")}
|
||||
</button>
|
||||
)}
|
||||
{/* 随机模式切换 */}
|
||||
<button
|
||||
onClick={() => setIsRandomMode(!isRandomMode)}
|
||||
className={`px-3 py-1 rounded-full text-sm transition-colors ${
|
||||
isRandomMode
|
||||
? "bg-[#35786f] text-white"
|
||||
: "bg-gray-200 text-gray-600"
|
||||
}`}
|
||||
>
|
||||
{t("random")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 字母主要内容显示区域 */}
|
||||
<div className="text-center mb-8">
|
||||
{/* 字母本身(可隐藏) */}
|
||||
{showLetter ? (
|
||||
<div className="text-6xl md:text-8xl font-bold text-gray-800 mb-4">
|
||||
{currentLetter.letter}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-6xl md:text-8xl font-bold text-gray-300 mb-4 h-20 md:h-24 flex items-center justify-center">
|
||||
<span className="text-2xl md:text-3xl text-gray-400">?</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* IPA 音标显示 */}
|
||||
{showIPA && (
|
||||
<div className="text-2xl md:text-3xl text-gray-600 mb-2">
|
||||
{currentLetter.letter_sound_ipa}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 罗马音显示(日语) */}
|
||||
{showRoman && hasRomanization && currentLetter.roman_letter && (
|
||||
<div className="text-lg md:text-xl text-gray-500">
|
||||
{currentLetter.roman_letter}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 底部导航控制区域 */}
|
||||
<div className="flex justify-between items-center">
|
||||
{/* 上一个按钮 */}
|
||||
<button
|
||||
onClick={goToPrevious}
|
||||
className="p-3 rounded-full bg-gray-100 hover:bg-gray-200 transition-colors"
|
||||
aria-label="上一个字母"
|
||||
{/* 白色主卡片容器 */}
|
||||
<Card padding="xl">
|
||||
{/* 顶部进度指示器和显示选项按钮 */}
|
||||
<div className="flex justify-between items-center mb-6">
|
||||
{/* 当前字母进度 */}
|
||||
<span className="text-sm text-gray-500">
|
||||
{currentIndex + 1} / {alphabet.length}
|
||||
</span>
|
||||
{/* 显示选项切换按钮组 */}
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
<CircleToggleButton
|
||||
selected={showLetter}
|
||||
onClick={() => setShowLetter(!showLetter)}
|
||||
>
|
||||
<ChevronLeft size={24} />
|
||||
</button>
|
||||
|
||||
{/* 中间区域:随机按钮 */}
|
||||
<div className="flex gap-2 items-center">
|
||||
{isRandomMode && (
|
||||
<button
|
||||
onClick={goToRandom}
|
||||
className="px-4 py-2 rounded-full bg-[#35786f] text-white text-sm font-medium hover:bg-[#2d5f58] transition-colors"
|
||||
>
|
||||
{t("randomNext")}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 下一个按钮 */}
|
||||
<button
|
||||
onClick={goToNext}
|
||||
className="p-3 rounded-full bg-gray-100 hover:bg-gray-200 transition-colors"
|
||||
aria-label="下一个字母"
|
||||
{t("letter")}
|
||||
</CircleToggleButton>
|
||||
{/* IPA 音标显示切换 */}
|
||||
<CircleToggleButton
|
||||
selected={showIPA}
|
||||
onClick={() => setShowIPA(!showIPA)}
|
||||
>
|
||||
<ChevronRight size={24} />
|
||||
</button>
|
||||
IPA
|
||||
</CircleToggleButton>
|
||||
{/* 罗马音显示切换(仅日语显示) */}
|
||||
{hasRomanization && (
|
||||
<CircleToggleButton
|
||||
selected={showRoman}
|
||||
onClick={() => setShowRoman(!showRoman)}
|
||||
>
|
||||
{t("roman")}
|
||||
</CircleToggleButton>
|
||||
)}
|
||||
{/* 随机模式切换 */}
|
||||
<CircleToggleButton
|
||||
selected={isRandomMode}
|
||||
onClick={() => setIsRandomMode(!isRandomMode)}
|
||||
>
|
||||
{t("random")}
|
||||
</CircleToggleButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 底部操作提示文字 */}
|
||||
<div className="text-center mt-6 text-white text-sm">
|
||||
<p>
|
||||
{isRandomMode
|
||||
? "使用左右箭头键或空格键随机切换字母,ESC键返回"
|
||||
: "使用左右箭头键或滑动切换字母,ESC键返回"
|
||||
}
|
||||
</p>
|
||||
{/* 字母主要内容显示区域 */}
|
||||
<div className="text-center mb-8">
|
||||
{/* 字母本身(可隐藏) */}
|
||||
{showLetter ? (
|
||||
<div className="text-6xl md:text-8xl font-bold text-gray-800 mb-4">
|
||||
{currentLetter.letter}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-6xl md:text-8xl font-bold text-gray-300 mb-4 h-20 md:h-24 flex items-center justify-center">
|
||||
<span className="text-2xl md:text-3xl text-gray-400">?</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* IPA 音标显示 */}
|
||||
{showIPA && (
|
||||
<div className="text-2xl md:text-3xl text-gray-600 mb-2">
|
||||
{currentLetter.letter_sound_ipa}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 罗马音显示(日语) */}
|
||||
{showRoman && hasRomanization && currentLetter.roman_letter && (
|
||||
<div className="text-lg md:text-xl text-gray-500">
|
||||
{currentLetter.roman_letter}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 底部导航控制区域 */}
|
||||
<div className="flex justify-between items-center">
|
||||
{/* 上一个按钮 */}
|
||||
<CircleButton onClick={goToPrevious} aria-label="上一个字母">
|
||||
<ChevronLeft size={20} />
|
||||
</CircleButton>
|
||||
|
||||
{/* 中间区域:随机按钮 */}
|
||||
<div className="flex gap-2 items-center">
|
||||
{isRandomMode && (
|
||||
<PrimaryButton
|
||||
onClick={goToRandom}
|
||||
className="rounded-full px-4 py-2 text-sm"
|
||||
>
|
||||
{t("randomNext")}
|
||||
</PrimaryButton>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 下一个按钮 */}
|
||||
<CircleButton onClick={goToNext} aria-label="下一个字母">
|
||||
<ChevronRight size={20} />
|
||||
</CircleButton>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* 底部操作提示文字 */}
|
||||
<div className="text-center mt-6 text-white text-sm">
|
||||
<p>
|
||||
{isRandomMode
|
||||
? "使用左右箭头键或空格键随机切换字母,ESC键返回"
|
||||
: "使用左右箭头键或滑动切换字母,ESC键返回"
|
||||
}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 全屏触摸事件监听层(用于滑动切换) */}
|
||||
@@ -248,6 +224,6 @@ export function AlphabetCard({ alphabet, alphabetType, onBack }: AlphabetCardPro
|
||||
onTouchMove={onTouchMove}
|
||||
onTouchEnd={onTouchEnd}
|
||||
/>
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { IconClick } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { IconClick } from "@/design-system/base/button";
|
||||
import { IMAGES } from "@/config/images";
|
||||
import { Letter, SupportedAlphabets } from "@/lib/interfaces";
|
||||
import {
|
||||
@@ -45,10 +45,10 @@ export function MemoryCard({
|
||||
className="w-full flex justify-center items-center"
|
||||
onKeyDown={(e: KeyboardEvent<HTMLDivElement>) => e.preventDefault()}
|
||||
>
|
||||
<div className="m-4 p-4 w-full md:w-[60dvw] flex-col rounded-2xl shadow border-gray-200 border flex justify-center items-center">
|
||||
<div className="m-4 p-4 w-full md:w-[60dvw] flex-col rounded-lg shadow border-gray-200 border flex justify-center items-center">
|
||||
<div className="w-full flex justify-end items-center">
|
||||
<IconClick
|
||||
size={32}
|
||||
size="lg"
|
||||
alt="close"
|
||||
src={IMAGES.close}
|
||||
onClick={() => setChosenAlphabet(null)}
|
||||
@@ -64,13 +64,13 @@ export function MemoryCard({
|
||||
</div>
|
||||
<div className="flex flex-row mt-32 items-center justify-center gap-2">
|
||||
<IconClick
|
||||
size={48}
|
||||
size="lg"
|
||||
alt="refresh"
|
||||
src={IMAGES.refresh}
|
||||
onClick={refresh}
|
||||
></IconClick>
|
||||
<IconClick
|
||||
size={48}
|
||||
size="lg"
|
||||
alt="more"
|
||||
src={IMAGES.more_horiz}
|
||||
onClick={() => setMore(!more)}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Letter, SupportedAlphabets } from "@/lib/interfaces";
|
||||
import { Container } from "@/components/ui/Container";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { AlphabetCard } from "./AlphabetCard";
|
||||
|
||||
export default function Alphabet() {
|
||||
@@ -48,87 +48,81 @@ export default function Alphabet() {
|
||||
// 语言选择界面
|
||||
if (!chosenAlphabet) {
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f] flex flex-col items-center justify-center px-4">
|
||||
<Container className="p-8 max-w-2xl w-full text-center">
|
||||
{/* 页面标题 */}
|
||||
<h1 className="text-4xl md:text-5xl font-bold text-gray-800 mb-4">
|
||||
{t("chooseCharacters")}
|
||||
</h1>
|
||||
{/* 副标题说明 */}
|
||||
<p className="text-gray-600 mb-8 text-lg">
|
||||
选择一种语言的字母表开始学习
|
||||
</p>
|
||||
<PageLayout>
|
||||
{/* 页面标题 */}
|
||||
<h1 className="text-4xl md:text-5xl font-bold text-gray-800 mb-4">
|
||||
{t("chooseCharacters")}
|
||||
</h1>
|
||||
{/* 副标题说明 */}
|
||||
<p className="text-gray-600 mb-8 text-lg">
|
||||
选择一种语言的字母表开始学习
|
||||
</p>
|
||||
|
||||
{/* 语言选择按钮网格 */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{/* 日语假名选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("japanese")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">あいうえお</span>
|
||||
<span>{t("japanese")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
{/* 语言选择按钮网格 */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{/* 日语假名选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("japanese")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">あいうえお</span>
|
||||
<span>{t("japanese")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
|
||||
{/* 英语字母选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("english")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">ABC</span>
|
||||
<span>{t("english")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
{/* 英语字母选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("english")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">ABC</span>
|
||||
<span>{t("english")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
|
||||
{/* 维吾尔语字母选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("uyghur")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">ئۇيغۇر</span>
|
||||
<span>{t("uyghur")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
{/* 维吾尔语字母选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("uyghur")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">ئۇيغۇر</span>
|
||||
<span>{t("uyghur")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
|
||||
{/* 世界语字母选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("esperanto")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">ABCĜĤ</span>
|
||||
<span>{t("esperanto")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
</div>
|
||||
</Container>
|
||||
</div>
|
||||
{/* 世界语字母选项 */}
|
||||
<LightButton
|
||||
onClick={() => setChosenAlphabet("esperanto")}
|
||||
className="p-6 text-lg font-medium hover:scale-105 transition-transform"
|
||||
>
|
||||
<div className="flex flex-col items-center">
|
||||
<span className="text-2xl mb-2">ABCĜĤ</span>
|
||||
<span>{t("esperanto")}</span>
|
||||
</div>
|
||||
</LightButton>
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
|
||||
// 加载状态
|
||||
if (loadingState === "loading") {
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center">
|
||||
<Container className="p-8 text-center">
|
||||
<div className="text-2xl text-gray-600">{t("loading")}</div>
|
||||
</Container>
|
||||
</div>
|
||||
<PageLayout>
|
||||
<div className="text-2xl text-gray-600 text-center">{t("loading")}</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
|
||||
// 错误状态
|
||||
if (loadingState === "error") {
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center">
|
||||
<Container className="p-8 text-center">
|
||||
<div className="text-2xl text-red-600">{t("loadFailed")}</div>
|
||||
</Container>
|
||||
</div>
|
||||
<PageLayout>
|
||||
<div className="text-2xl text-red-600 text-center">{t("loadFailed")}</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +1,38 @@
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { POPULAR_LANGUAGES } from "./constants";
|
||||
"use client";
|
||||
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { Input } from "@/design-system/base/input";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { POPULAR_LANGUAGES } from "./constants";
|
||||
|
||||
interface SearchFormProps {
|
||||
searchQuery: string;
|
||||
onSearchQueryChange: (query: string) => void;
|
||||
isSearching: boolean;
|
||||
onSearch: (e: React.FormEvent) => void;
|
||||
queryLang: string;
|
||||
onQueryLangChange: (lang: string) => void;
|
||||
definitionLang: string;
|
||||
onDefinitionLangChange: (lang: string) => void;
|
||||
defaultQueryLang?: string;
|
||||
defaultDefinitionLang?: string;
|
||||
}
|
||||
|
||||
export function SearchForm({
|
||||
searchQuery,
|
||||
onSearchQueryChange,
|
||||
isSearching,
|
||||
onSearch,
|
||||
queryLang,
|
||||
onQueryLangChange,
|
||||
definitionLang,
|
||||
onDefinitionLangChange,
|
||||
}: SearchFormProps) {
|
||||
export function SearchForm({ defaultQueryLang = "english", defaultDefinitionLang = "chinese" }: SearchFormProps) {
|
||||
const t = useTranslations("dictionary");
|
||||
const [queryLang, setQueryLang] = useState(defaultQueryLang);
|
||||
const [definitionLang, setDefinitionLang] = useState(defaultDefinitionLang);
|
||||
const router = useRouter();
|
||||
|
||||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
const formData = new FormData(e.currentTarget);
|
||||
const searchQuery = formData.get("searchQuery") as string;
|
||||
|
||||
if (!searchQuery?.trim()) return;
|
||||
|
||||
const params = new URLSearchParams({
|
||||
q: searchQuery,
|
||||
ql: queryLang,
|
||||
dl: definitionLang,
|
||||
});
|
||||
|
||||
router.push(`/dictionary?${params.toString()}`);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -38,20 +47,20 @@ export function SearchForm({
|
||||
</div>
|
||||
|
||||
{/* 搜索表单 */}
|
||||
<form onSubmit={onSearch} className="flex flex-col sm:flex-row gap-2">
|
||||
<input
|
||||
<form onSubmit={handleSubmit} className="flex flex-col sm:flex-row gap-2">
|
||||
<Input
|
||||
type="text"
|
||||
value={searchQuery}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) => onSearchQueryChange(e.target.value)}
|
||||
name="searchQuery"
|
||||
defaultValue=""
|
||||
placeholder={t("searchPlaceholder")}
|
||||
className="flex-1 min-w-0 px-4 py-3 text-lg text-gray-800 focus:outline-none border-b-2 border-gray-600 bg-white/90 rounded"
|
||||
variant="search"
|
||||
required
|
||||
/>
|
||||
<LightButton
|
||||
type="submit"
|
||||
disabled={isSearching || !searchQuery.trim()}
|
||||
className="px-6 py-3 whitespace-nowrap text-center sm:min-w-30"
|
||||
>
|
||||
{isSearching ? t("searching") : t("search")}
|
||||
{t("search")}
|
||||
</LightButton>
|
||||
</form>
|
||||
|
||||
@@ -62,68 +71,47 @@ export function SearchForm({
|
||||
</div>
|
||||
|
||||
<div className="space-y-4">
|
||||
{/* 查询语言 */}
|
||||
<div>
|
||||
<label className="block text-gray-700 text-sm mb-2">
|
||||
{t("queryLanguage")} ({t("queryLanguageHint")})
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-2 mb-2">
|
||||
{POPULAR_LANGUAGES.map((lang) => (
|
||||
<LightButton
|
||||
key={lang.code}
|
||||
selected={queryLang === lang.code}
|
||||
onClick={() => onQueryLangChange(lang.code)}
|
||||
className="text-sm px-3 py-1"
|
||||
>
|
||||
{lang.nativeName}
|
||||
</LightButton>
|
||||
))}
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
value={queryLang}
|
||||
onChange={(e) => onQueryLangChange(e.target.value)}
|
||||
placeholder={t("otherLanguagePlaceholder")}
|
||||
className="w-full px-3 py-2 text-sm text-gray-800 focus:outline-none border-b-2 border-gray-600 bg-white/90 rounded"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 释义语言 */}
|
||||
<div>
|
||||
<label className="block text-gray-700 text-sm mb-2">
|
||||
{t("definitionLanguage")} ({t("definitionLanguageHint")})
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-2 mb-2">
|
||||
{POPULAR_LANGUAGES.map((lang) => (
|
||||
<LightButton
|
||||
key={lang.code}
|
||||
selected={definitionLang === lang.code}
|
||||
onClick={() => onDefinitionLangChange(lang.code)}
|
||||
className="text-sm px-3 py-1"
|
||||
>
|
||||
{lang.nativeName}
|
||||
</LightButton>
|
||||
))}
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
value={definitionLang}
|
||||
onChange={(e) => onDefinitionLangChange(e.target.value)}
|
||||
placeholder={t("otherLanguagePlaceholder")}
|
||||
className="w-full px-3 py-2 text-sm text-gray-800 focus:outline-none border-b-2 border-gray-600 bg-white/90 rounded"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 当前设置显示 */}
|
||||
<div className="text-center text-gray-700 text-sm pt-2 border-t border-gray-300">
|
||||
{t("currentSettings", {
|
||||
queryLang: POPULAR_LANGUAGES.find(l => l.code === queryLang)?.nativeName || queryLang,
|
||||
definitionLang: POPULAR_LANGUAGES.find(l => l.code === definitionLang)?.nativeName || definitionLang
|
||||
})}
|
||||
{/* 查询语言 */}
|
||||
<div>
|
||||
<label className="block text-gray-700 text-sm mb-2">
|
||||
{t("queryLanguage")} ({t("queryLanguageHint")})
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{POPULAR_LANGUAGES.map((lang) => (
|
||||
<LightButton
|
||||
key={lang.code}
|
||||
type="button"
|
||||
selected={queryLang === lang.code}
|
||||
onClick={() => setQueryLang(lang.code)}
|
||||
className="text-sm px-3 py-1"
|
||||
>
|
||||
{lang.nativeName}
|
||||
</LightButton>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 释义语言 */}
|
||||
<div>
|
||||
<label className="block text-gray-700 text-sm mb-2">
|
||||
{t("definitionLanguage")} ({t("definitionLanguageHint")})
|
||||
</label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{POPULAR_LANGUAGES.map((lang) => (
|
||||
<LightButton
|
||||
key={lang.code}
|
||||
type="button"
|
||||
selected={definitionLang === lang.code}
|
||||
onClick={() => setDefinitionLang(lang.code)}
|
||||
className="text-sm px-3 py-1"
|
||||
>
|
||||
{lang.nativeName}
|
||||
</LightButton>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
122
src/app/(features)/dictionary/SearchResult.client.tsx
Normal file
122
src/app/(features)/dictionary/SearchResult.client.tsx
Normal file
@@ -0,0 +1,122 @@
|
||||
"use client";
|
||||
|
||||
import { Plus, RefreshCw } from "lucide-react";
|
||||
import { CircleButton, LightButton } from "@/design-system/base/button";
|
||||
import { toast } from "sonner";
|
||||
import { actionCreatePair } from "@/modules/folder/folder-aciton";
|
||||
import { TSharedItem } from "@/shared/dictionary-type";
|
||||
import { TSharedFolder } from "@/shared/folder-type";
|
||||
import { actionLookUpDictionary } from "@/modules/dictionary/dictionary-action";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
type Session = {
|
||||
user: {
|
||||
id: string;
|
||||
name?: string | null;
|
||||
email?: string | null;
|
||||
image?: string | null;
|
||||
};
|
||||
} | null;
|
||||
|
||||
interface SaveButtonClientProps {
|
||||
session: Session;
|
||||
folders: TSharedFolder[];
|
||||
searchResult: TSharedItem;
|
||||
queryLang: string;
|
||||
definitionLang: string;
|
||||
}
|
||||
|
||||
export function SaveButtonClient({ session, folders, searchResult, queryLang, definitionLang }: SaveButtonClientProps) {
|
||||
const handleSave = async () => {
|
||||
if (!session) {
|
||||
toast.error("Please login first");
|
||||
return;
|
||||
}
|
||||
if (folders.length === 0) {
|
||||
toast.error("Please create a folder first");
|
||||
return;
|
||||
}
|
||||
|
||||
const folderSelect = document.getElementById("folder-select") as HTMLSelectElement;
|
||||
const folderId = folderSelect?.value ? Number(folderSelect.value) : folders[0]?.id;
|
||||
|
||||
const definition = searchResult.entries.reduce((p, e) => {
|
||||
return { ...p, definition: p.definition + ' | ' + e.definition };
|
||||
}).definition;
|
||||
|
||||
try {
|
||||
await actionCreatePair({
|
||||
text1: searchResult.standardForm,
|
||||
text2: definition,
|
||||
language1: queryLang,
|
||||
language2: definitionLang,
|
||||
ipa1: searchResult.entries[0].ipa,
|
||||
folderId: folderId,
|
||||
});
|
||||
|
||||
const folderName = folders.find((f) => f.id === folderId)?.name || "Unknown";
|
||||
toast.success(`Saved to ${folderName}`);
|
||||
} catch (error) {
|
||||
toast.error("Save failed");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<CircleButton
|
||||
onClick={handleSave}
|
||||
className="w-10 h-10 shrink-0"
|
||||
title="Save to folder"
|
||||
>
|
||||
<Plus />
|
||||
</CircleButton>
|
||||
);
|
||||
}
|
||||
|
||||
interface ReLookupButtonClientProps {
|
||||
searchQuery: string;
|
||||
queryLang: string;
|
||||
definitionLang: string;
|
||||
}
|
||||
|
||||
export function ReLookupButtonClient({ searchQuery, queryLang, definitionLang }: ReLookupButtonClientProps) {
|
||||
const router = useRouter();
|
||||
|
||||
const handleRelookup = async () => {
|
||||
const getNativeName = (code: string): string => {
|
||||
const popularLanguages: Record<string, string> = {
|
||||
english: "English",
|
||||
chinese: "中文",
|
||||
japanese: "日本語",
|
||||
korean: "한국어",
|
||||
italian: "Italiano",
|
||||
uyghur: "ئۇيغۇرچە",
|
||||
};
|
||||
return popularLanguages[code] || code;
|
||||
};
|
||||
|
||||
try {
|
||||
await actionLookUpDictionary({
|
||||
text: searchQuery,
|
||||
queryLang: getNativeName(queryLang),
|
||||
definitionLang: getNativeName(definitionLang),
|
||||
forceRelook: true
|
||||
});
|
||||
|
||||
toast.success("Re-lookup successful");
|
||||
// 刷新页面以显示新结果
|
||||
router.refresh();
|
||||
} catch (error) {
|
||||
toast.error("Re-lookup failed");
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<LightButton
|
||||
onClick={handleRelookup}
|
||||
className="flex items-center gap-2 px-4 py-2 text-sm"
|
||||
leftIcon={<RefreshCw className="w-4 h-4" />}
|
||||
>
|
||||
Re-lookup
|
||||
</LightButton>
|
||||
);
|
||||
}
|
||||
@@ -1,143 +1,93 @@
|
||||
import { Plus, RefreshCw } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { auth } from "@/auth";
|
||||
import { DictionaryEntry } from "./DictionaryEntry";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { performDictionaryLookup } from "./utils";
|
||||
import { TSharedItem } from "@/shared/dictionary-type";
|
||||
import { SaveButtonClient, ReLookupButtonClient } from "./SearchResult.client";
|
||||
import { headers } from "next/headers";
|
||||
import { actionGetFoldersByUserId } from "@/modules/folder/folder-aciton";
|
||||
import { TSharedFolder } from "@/shared/folder-type";
|
||||
import { actionCreatePair } from "@/modules/folder/folder-aciton";
|
||||
|
||||
interface SearchResultProps {
|
||||
searchResult: TSharedItem;
|
||||
searchResult: TSharedItem | null;
|
||||
searchQuery: string;
|
||||
queryLang: string;
|
||||
definitionLang: string;
|
||||
folders: TSharedFolder[];
|
||||
selectedFolderId: number | null;
|
||||
onFolderSelect: (folderId: number | null) => void;
|
||||
onResultUpdate: (newResult: TSharedItem) => void;
|
||||
onSearchingChange: (isSearching: boolean) => void;
|
||||
getNativeName: (code: string) => string;
|
||||
}
|
||||
|
||||
export function SearchResult({
|
||||
export async function SearchResult({
|
||||
searchResult,
|
||||
searchQuery,
|
||||
queryLang,
|
||||
definitionLang,
|
||||
folders,
|
||||
selectedFolderId,
|
||||
onFolderSelect,
|
||||
onResultUpdate,
|
||||
onSearchingChange,
|
||||
getNativeName,
|
||||
definitionLang
|
||||
}: SearchResultProps) {
|
||||
const t = useTranslations("dictionary");
|
||||
const { data: session } = authClient.useSession();
|
||||
// 获取用户会话和文件夹
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
let folders: TSharedFolder[] = [];
|
||||
|
||||
const handleRelookup = async () => {
|
||||
onSearchingChange(true);
|
||||
|
||||
const result = await performDictionaryLookup(
|
||||
{
|
||||
text: searchQuery,
|
||||
queryLang: getNativeName(queryLang),
|
||||
definitionLang: getNativeName(definitionLang),
|
||||
forceRelook: true
|
||||
},
|
||||
t
|
||||
);
|
||||
|
||||
if (result) {
|
||||
onResultUpdate(result);
|
||||
if (session?.user?.id) {
|
||||
const result = await actionGetFoldersByUserId(session.user.id as string);
|
||||
if (result.success && result.data) {
|
||||
folders = result.data;
|
||||
}
|
||||
|
||||
onSearchingChange(false);
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
if (!session) {
|
||||
toast.error(t("pleaseLogin"));
|
||||
return;
|
||||
}
|
||||
if (!selectedFolderId) {
|
||||
toast.error(t("pleaseCreateFolder"));
|
||||
return;
|
||||
}
|
||||
|
||||
const entry = searchResult.entries[0];
|
||||
actionCreatePair({
|
||||
text1: searchResult.standardForm,
|
||||
text2: entry.definition,
|
||||
language1: queryLang,
|
||||
language2: definitionLang,
|
||||
ipa1: entry.ipa,
|
||||
folderId: selectedFolderId,
|
||||
})
|
||||
.then(() => {
|
||||
const folderName = folders.find(f => f.id === selectedFolderId)?.name || "Unknown";
|
||||
toast.success(t("savedToFolder", { folderName }));
|
||||
})
|
||||
.catch(() => {
|
||||
toast.error(t("saveFailed"));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="bg-white rounded-lg p-6 shadow-lg">
|
||||
{/* 标题和保存按钮 */}
|
||||
<div className="flex items-start justify-between mb-6">
|
||||
<div className="flex-1">
|
||||
<h2 className="text-3xl font-bold text-gray-800 mb-2">
|
||||
{searchResult.standardForm}
|
||||
</h2>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 ml-4">
|
||||
{session && folders.length > 0 && (
|
||||
<select
|
||||
value={selectedFolderId || ""}
|
||||
onChange={(e) => onFolderSelect(e.target.value ? Number(e.target.value) : null)}
|
||||
className="px-3 py-2 text-sm border border-gray-300 rounded-lg bg-white focus:outline-none focus:ring-2 focus:ring-[#35786f]"
|
||||
>
|
||||
{folders.map((folder) => (
|
||||
<option key={folder.id} value={folder.id}>
|
||||
{folder.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
<button
|
||||
onClick={handleSave}
|
||||
className="hover:bg-gray-200 hover:cursor-pointer rounded-4xl border border-gray-200 w-10 h-10 flex justify-center items-center shrink-0"
|
||||
title={t("saveToFolder")}
|
||||
>
|
||||
<Plus />
|
||||
</button>
|
||||
</div>
|
||||
{!searchResult ? (
|
||||
<div className="text-center py-12 bg-white/20 rounded-lg">
|
||||
<p className="text-gray-800 text-xl">No results found</p>
|
||||
<p className="text-gray-600 mt-2">Try other words</p>
|
||||
</div>
|
||||
|
||||
{/* 条目列表 */}
|
||||
<div className="space-y-6">
|
||||
{searchResult.entries.map((entry, index) => (
|
||||
<div key={index} className="border-t border-gray-200 pt-4">
|
||||
<DictionaryEntry entry={entry} />
|
||||
) : (
|
||||
<div className="bg-white rounded-lg p-6 shadow-lg">
|
||||
{/* 标题和保存按钮 */}
|
||||
<div className="flex items-start justify-between mb-6">
|
||||
<div className="flex-1">
|
||||
<h2 className="text-3xl font-bold text-gray-800 mb-2">
|
||||
{searchResult.standardForm}
|
||||
</h2>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 ml-4">
|
||||
{session && folders.length > 0 && (
|
||||
<select
|
||||
id="folder-select"
|
||||
className="px-3 py-2 text-sm border border-gray-300 rounded-lg bg-white focus:outline-none focus:ring-2 focus:ring-[#35786f]"
|
||||
>
|
||||
{folders.map((folder) => (
|
||||
<option key={folder.id} value={folder.id}>
|
||||
{folder.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
<SaveButtonClient
|
||||
session={session}
|
||||
folders={folders}
|
||||
searchResult={searchResult}
|
||||
queryLang={queryLang}
|
||||
definitionLang={definitionLang}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 重新查询按钮 */}
|
||||
<div className="border-t border-gray-200 pt-4 mt-4">
|
||||
<button
|
||||
onClick={handleRelookup}
|
||||
className="flex items-center gap-2 px-4 py-2 text-sm text-gray-700 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors"
|
||||
>
|
||||
<RefreshCw className="w-4 h-4" />
|
||||
{t("relookup")}
|
||||
</button>
|
||||
{/* 条目列表 */}
|
||||
<div className="space-y-6">
|
||||
{searchResult.entries.map((entry, index) => (
|
||||
<div key={index} className="border-t border-gray-200 pt-4">
|
||||
<DictionaryEntry entry={entry} />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* 重新查询按钮 */}
|
||||
<div className="border-t border-gray-200 pt-4 mt-4">
|
||||
<ReLookupButtonClient
|
||||
searchQuery={searchQuery}
|
||||
queryLang={queryLang}
|
||||
definitionLang={definitionLang}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,133 +1,75 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { Container } from "@/components/ui/Container";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { SearchForm } from "./SearchForm";
|
||||
import { SearchResult } from "./SearchResult";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { POPULAR_LANGUAGES } from "./constants";
|
||||
import { performDictionaryLookup } from "./utils";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { actionLookUpDictionary } from "@/modules/dictionary/dictionary-action";
|
||||
import { TSharedItem } from "@/shared/dictionary-type";
|
||||
import { actionGetFoldersByUserId } from "@/modules/folder/folder-aciton";
|
||||
import { TSharedFolder } from "@/shared/folder-type";
|
||||
import { toast } from "sonner";
|
||||
|
||||
export default function DictionaryPage() {
|
||||
const t = useTranslations("dictionary");
|
||||
const [searchQuery, setSearchQuery] = useState("");
|
||||
const [searchResult, setSearchResult] = useState<TSharedItem | null>(null);
|
||||
const [isSearching, setIsSearching] = useState(false);
|
||||
const [hasSearched, setHasSearched] = useState(false);
|
||||
const [queryLang, setQueryLang] = useState("english");
|
||||
const [definitionLang, setDefinitionLang] = useState("chinese");
|
||||
const [selectedFolderId, setSelectedFolderId] = useState<number | null>(null);
|
||||
const [folders, setFolders] = useState<TSharedFolder[]>([]);
|
||||
const { data: session } = authClient.useSession();
|
||||
interface DictionaryPageProps {
|
||||
searchParams: Promise<{ q?: string; ql?: string; dl?: string; }>;
|
||||
}
|
||||
|
||||
// 加载用户的文件夹列表
|
||||
useEffect(() => {
|
||||
if (session) {
|
||||
actionGetFoldersByUserId(session.user.id as string)
|
||||
.then(result => {
|
||||
if (!result.success || !result.data) throw result.message;
|
||||
return result.data;
|
||||
})
|
||||
.then((loadedFolders) => {
|
||||
setFolders(loadedFolders);
|
||||
// 如果有文件夹且未选择,默认选择第一个
|
||||
if (loadedFolders.length > 0 && !selectedFolderId) {
|
||||
setSelectedFolderId(loadedFolders[0].id);
|
||||
}
|
||||
}).catch(e => toast.error);
|
||||
export default async function DictionaryPage({ searchParams }: DictionaryPageProps) {
|
||||
const t = await getTranslations("dictionary");
|
||||
|
||||
// 从 searchParams 获取搜索参数
|
||||
const { q: searchQuery, ql: queryLang = "english", dl: definitionLang = "chinese" } = await searchParams;
|
||||
|
||||
// 如果有搜索查询,获取搜索结果
|
||||
let searchResult: TSharedItem | undefined | null = null;
|
||||
if (searchQuery) {
|
||||
const getNativeName = (code: string): string => {
|
||||
const popularLanguages: Record<string, string> = {
|
||||
english: "English",
|
||||
chinese: "中文",
|
||||
japanese: "日本語",
|
||||
korean: "한국어",
|
||||
italian: "Italiano",
|
||||
uyghur: "ئۇيغۇرچە",
|
||||
};
|
||||
return popularLanguages[code] || code;
|
||||
};
|
||||
|
||||
const result = await actionLookUpDictionary({
|
||||
text: searchQuery,
|
||||
queryLang: getNativeName(queryLang),
|
||||
definitionLang: getNativeName(definitionLang),
|
||||
forceRelook: false
|
||||
});
|
||||
|
||||
if (result.success && result.data) {
|
||||
searchResult = result.data;
|
||||
}
|
||||
}, [session, selectedFolderId]);
|
||||
|
||||
// 将 code 转换为 nativeName
|
||||
const getNativeName = (code: string) => {
|
||||
return POPULAR_LANGUAGES.find(l => l.code === code)?.nativeName || code;
|
||||
};
|
||||
|
||||
const handleSearch = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!searchQuery.trim()) return;
|
||||
|
||||
setIsSearching(true);
|
||||
setHasSearched(true);
|
||||
setSearchResult(null);
|
||||
|
||||
const result = await performDictionaryLookup(
|
||||
{
|
||||
text: searchQuery,
|
||||
queryLang: getNativeName(queryLang),
|
||||
definitionLang: getNativeName(definitionLang),
|
||||
forceRelook: false
|
||||
},
|
||||
t
|
||||
);
|
||||
setSearchResult(result);
|
||||
setIsSearching(false);
|
||||
};
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f]">
|
||||
<PageLayout>
|
||||
{/* 搜索区域 */}
|
||||
<div className="flex items-center justify-center px-4 py-12">
|
||||
<Container className="max-w-3xl w-full p-4">
|
||||
<SearchForm
|
||||
searchQuery={searchQuery}
|
||||
onSearchQueryChange={setSearchQuery}
|
||||
isSearching={isSearching}
|
||||
onSearch={handleSearch}
|
||||
queryLang={queryLang}
|
||||
onQueryLangChange={setQueryLang}
|
||||
definitionLang={definitionLang}
|
||||
onDefinitionLangChange={setDefinitionLang}
|
||||
/>
|
||||
</Container>
|
||||
<div className="mb-8">
|
||||
<SearchForm
|
||||
defaultQueryLang={queryLang}
|
||||
defaultDefinitionLang={definitionLang}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* 搜索结果区域 */}
|
||||
<div className="flex-1 px-4 pb-12">
|
||||
<Container className="max-w-3xl w-full p-4">
|
||||
{isSearching && (
|
||||
<div className="text-center py-8">
|
||||
<div className="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-white"></div>
|
||||
<p className="mt-4 text-white">{t("loading")}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isSearching && hasSearched && !searchResult && (
|
||||
<div className="text-center py-12 bg-white/20 rounded-lg">
|
||||
<p className="text-gray-800 text-xl">{t("noResults")}</p>
|
||||
<p className="text-gray-600 mt-2">{t("tryOtherWords")}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isSearching && searchResult && (
|
||||
<SearchResult
|
||||
searchResult={searchResult}
|
||||
searchQuery={searchQuery}
|
||||
queryLang={queryLang}
|
||||
definitionLang={definitionLang}
|
||||
folders={folders}
|
||||
selectedFolderId={selectedFolderId}
|
||||
onFolderSelect={setSelectedFolderId}
|
||||
onResultUpdate={setSearchResult}
|
||||
onSearchingChange={setIsSearching}
|
||||
getNativeName={getNativeName}
|
||||
/>
|
||||
)}
|
||||
|
||||
{!hasSearched && (
|
||||
<div className="text-center py-12 bg-white/20 rounded-lg">
|
||||
<div className="text-6xl mb-4">📚</div>
|
||||
<p className="text-gray-800 text-xl mb-2">{t("welcomeTitle")}</p>
|
||||
<p className="text-gray-600">{t("welcomeHint")}</p>
|
||||
</div>
|
||||
)}
|
||||
</Container>
|
||||
<div>
|
||||
{searchQuery && (
|
||||
<SearchResult
|
||||
searchResult={searchResult}
|
||||
searchQuery={searchQuery}
|
||||
queryLang={queryLang}
|
||||
definitionLang={definitionLang}
|
||||
/>
|
||||
)}
|
||||
{!searchQuery && (
|
||||
<div className="text-center py-12">
|
||||
<div className="text-6xl mb-4">📚</div>
|
||||
<p className="text-gray-800 text-xl mb-2">{t("welcomeTitle")}</p>
|
||||
<p className="text-gray-600">{t("welcomeHint")}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import { toast } from "sonner";
|
||||
import { actionLookUpDictionary } from "@/modules/dictionary/dictionary-action";
|
||||
import { ActionInputLookUpDictionary, ActionOutputLookUpDictionary } from "@/modules/dictionary/dictionary-action-dto";
|
||||
import { TSharedItem } from "@/shared/dictionary-type";
|
||||
|
||||
export async function performDictionaryLookup(
|
||||
options: ActionInputLookUpDictionary,
|
||||
t?: (key: string) => string
|
||||
): Promise<TSharedItem | null> {
|
||||
const { text, queryLang, definitionLang, forceRelook = false, userId } = options;
|
||||
const result = await actionLookUpDictionary({
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang,
|
||||
forceRelook,
|
||||
userId
|
||||
});
|
||||
|
||||
if (!result.success || !result.data) return null;
|
||||
|
||||
if (forceRelook && t) {
|
||||
toast.success(t("relookupSuccess"));
|
||||
}
|
||||
return result.data;
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import { useTranslations } from "next-intl";
|
||||
import Link from "next/link";
|
||||
import { Folder as Fd } from "lucide-react";
|
||||
import { TSharedFolderWithTotalPairs } from "@/shared/folder-type";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { PrimaryButton } from "@/design-system/base/button";
|
||||
|
||||
interface FolderSelectorProps {
|
||||
folders: TSharedFolderWithTotalPairs[];
|
||||
@@ -15,81 +17,76 @@ const FolderSelector: React.FC<FolderSelectorProps> = ({ folders }) => {
|
||||
const router = useRouter();
|
||||
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center px-4 py-8">
|
||||
<div className="w-full max-w-2xl">
|
||||
<div className="bg-white rounded-2xl shadow-xl p-6 md:p-8">
|
||||
{folders.length === 0 ? (
|
||||
// 空状态 - 显示提示和跳转按钮
|
||||
<div className="text-center">
|
||||
<h1 className="text-2xl md:text-3xl font-bold text-gray-800 mb-4">
|
||||
{t("noFolders")}
|
||||
</h1>
|
||||
<Link
|
||||
className="inline-block px-6 py-2 bg-[#35786f] text-white rounded-full hover:bg-[#2d5f58] transition-colors"
|
||||
href="/folders"
|
||||
>
|
||||
Go to Folders
|
||||
</Link>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{/* 页面标题 */}
|
||||
<h1 className="text-2xl md:text-3xl font-bold text-gray-800 mb-6">
|
||||
{t("selectFolder")}
|
||||
</h1>
|
||||
{/* 文件夹列表 */}
|
||||
<div className="border border-gray-200 rounded-2xl max-h-96 overflow-y-auto">
|
||||
{folders
|
||||
.toSorted((a, b) => a.id - b.id)
|
||||
.map((folder) => (
|
||||
<div
|
||||
key={folder.id}
|
||||
onClick={() =>
|
||||
router.push(`/memorize?folder_id=${folder.id}`)
|
||||
}
|
||||
className="flex flex-row items-center p-4 gap-3 hover:cursor-pointer hover:bg-gray-50 transition-colors border-b border-gray-100 last:border-b-0"
|
||||
>
|
||||
{/* 文件夹图标 */}
|
||||
<div className="shrink-0">
|
||||
<Fd className="text-gray-600" size={24} />
|
||||
</div>
|
||||
{/* 文件夹信息 */}
|
||||
<div className="flex-1">
|
||||
<div className="font-medium text-gray-900">
|
||||
{folder.name}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500">
|
||||
{t("folderInfo", {
|
||||
id: folder.id,
|
||||
name: folder.name,
|
||||
count: folder.total,
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
{/* 右箭头 */}
|
||||
<div className="text-gray-400">
|
||||
<svg
|
||||
className="w-5 h-5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M9 5l7 7-7 7"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<PageLayout>
|
||||
{folders.length === 0 ? (
|
||||
// 空状态 - 显示提示和跳转按钮
|
||||
<div className="text-center">
|
||||
<h1 className="text-2xl md:text-3xl font-bold text-gray-800 mb-4">
|
||||
{t("noFolders")}
|
||||
</h1>
|
||||
<Link href="/folders">
|
||||
<PrimaryButton className="px-6 py-2">
|
||||
Go to Folders
|
||||
</PrimaryButton>
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{/* 页面标题 */}
|
||||
<h1 className="text-2xl md:text-3xl font-bold text-gray-800 mb-6">
|
||||
{t("selectFolder")}
|
||||
</h1>
|
||||
{/* 文件夹列表 */}
|
||||
<div className="border border-gray-200 rounded-lg max-h-96 overflow-y-auto">
|
||||
{folders
|
||||
.toSorted((a, b) => a.id - b.id)
|
||||
.map((folder) => (
|
||||
<div
|
||||
key={folder.id}
|
||||
onClick={() =>
|
||||
router.push(`/memorize?folder_id=${folder.id}`)
|
||||
}
|
||||
className="flex flex-row items-center p-4 gap-3 hover:cursor-pointer hover:bg-gray-50 transition-colors border-b border-gray-100 last:border-b-0"
|
||||
>
|
||||
{/* 文件夹图标 */}
|
||||
<div className="shrink-0">
|
||||
<Fd className="text-gray-600" size="md" />
|
||||
</div>
|
||||
{/* 文件夹信息 */}
|
||||
<div className="flex-1">
|
||||
<div className="font-medium text-gray-900">
|
||||
{folder.name}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500">
|
||||
{t("folderInfo", {
|
||||
id: folder.id,
|
||||
name: folder.name,
|
||||
count: folder.total,
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
{/* 右箭头 */}
|
||||
<div className="text-gray-400">
|
||||
<svg
|
||||
className="w-5 h-5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M9 5l7 7-7 7"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</PageLayout>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { LinkButton, CircleToggleButton, LightButton } from "@/design-system/base/button";
|
||||
import { useAudioPlayer } from "@/hooks/useAudioPlayer";
|
||||
import { getTTSUrl, TTS_SUPPORTED_LANGUAGES } from "@/lib/bigmodel/tts";
|
||||
import { useTranslations } from "next-intl";
|
||||
import localFont from "next/font/local";
|
||||
import { isNonNegativeInteger, SeededRandom } from "@/utils/random";
|
||||
import { TSharedPair } from "@/shared/folder-type";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
|
||||
const myFont = localFont({
|
||||
src: "../../../../public/fonts/NotoNaskhArabic-VariableFont_wght.ttf",
|
||||
@@ -27,11 +29,9 @@ const Memorize: React.FC<MemorizeProps> = ({ textPairs }) => {
|
||||
|
||||
if (textPairs.length === 0) {
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center px-4">
|
||||
<div className="bg-white rounded-2xl shadow-xl p-8 max-w-md w-full text-center">
|
||||
<p className="text-gray-700">{t("noTextPairs")}</p>
|
||||
</div>
|
||||
</div>
|
||||
<PageLayout>
|
||||
<p className="text-gray-700 text-center">{t("noTextPairs")}</p>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -112,99 +112,83 @@ const Memorize: React.FC<MemorizeProps> = ({ textPairs }) => {
|
||||
: [getTextPairs()[index].text1, getTextPairs()[index].text2];
|
||||
|
||||
return (
|
||||
<div className="min-h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center px-4 py-8">
|
||||
<div className="w-full max-w-2xl">
|
||||
<div className="bg-white rounded-2xl shadow-xl p-6 md:p-8">
|
||||
{/* 进度指示器 */}
|
||||
<div className="flex justify-center mb-4">
|
||||
<button
|
||||
onClick={handleIndexClick}
|
||||
className="text-sm text-gray-500 hover:text-gray-700 transition-colors"
|
||||
>
|
||||
{index + 1} / {getTextPairs().length}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 文本显示区域 */}
|
||||
<div className={`h-[40dvh] ${myFont.className} mb-4`}>
|
||||
{(() => {
|
||||
if (dictation) {
|
||||
if (show === "question") {
|
||||
return (
|
||||
<div className="h-full flex items-center justify-center">
|
||||
<div className="text-gray-400 text-4xl">?</div>
|
||||
</div>
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{createText(text1)}
|
||||
<div className="border-t border-gray-200"></div>
|
||||
{createText(text2)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (show === "question") {
|
||||
return createText(text1);
|
||||
} else {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{createText(text1)}
|
||||
<div className="border-t border-gray-200"></div>
|
||||
{createText(text2)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
})()}
|
||||
</div>
|
||||
|
||||
{/* 底部按钮 */}
|
||||
<div className="flex flex-row gap-2 items-center justify-center flex-wrap">
|
||||
<button
|
||||
onClick={handleNext}
|
||||
className="px-4 py-2 rounded-full bg-gray-200 text-gray-700 hover:bg-gray-300 transition-colors text-sm"
|
||||
>
|
||||
{show === "question" ? t("answer") : t("next")}
|
||||
</button>
|
||||
<button
|
||||
onClick={handlePrevious}
|
||||
className="px-4 py-2 rounded-full bg-gray-200 text-gray-700 hover:bg-gray-300 transition-colors text-sm"
|
||||
>
|
||||
{t("previous")}
|
||||
</button>
|
||||
<button
|
||||
onClick={toggleReverse}
|
||||
className={`px-4 py-2 rounded-full transition-colors text-sm ${reverse
|
||||
? "bg-[#35786f] text-white hover:bg-[#2d5f58]"
|
||||
: "bg-gray-200 text-gray-700 hover:bg-gray-300"
|
||||
}`}
|
||||
>
|
||||
{t("reverse")}
|
||||
</button>
|
||||
<button
|
||||
onClick={toggleDictation}
|
||||
className={`px-4 py-2 rounded-full transition-colors text-sm ${dictation
|
||||
? "bg-[#35786f] text-white hover:bg-[#2d5f58]"
|
||||
: "bg-gray-200 text-gray-700 hover:bg-gray-300"
|
||||
}`}
|
||||
>
|
||||
{t("dictation")}
|
||||
</button>
|
||||
<button
|
||||
onClick={toggleDisorder}
|
||||
className={`px-4 py-2 rounded-full transition-colors text-sm ${disorder
|
||||
? "bg-[#35786f] text-white hover:bg-[#2d5f58]"
|
||||
: "bg-gray-200 text-gray-700 hover:bg-gray-300"
|
||||
}`}
|
||||
>
|
||||
{t("disorder")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<PageLayout>
|
||||
{/* 进度指示器 */}
|
||||
<div className="flex justify-center mb-4">
|
||||
<LinkButton onClick={handleIndexClick} className="text-sm">
|
||||
{index + 1} / {getTextPairs().length}
|
||||
</LinkButton>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 文本显示区域 */}
|
||||
<div className={`h-[40dvh] ${myFont.className} mb-4`}>
|
||||
{(() => {
|
||||
if (dictation) {
|
||||
if (show === "question") {
|
||||
return (
|
||||
<div className="h-full flex items-center justify-center">
|
||||
<div className="text-gray-400 text-4xl">?</div>
|
||||
</div>
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{createText(text1)}
|
||||
<div className="border-t border-gray-200"></div>
|
||||
{createText(text2)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (show === "question") {
|
||||
return createText(text1);
|
||||
} else {
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
{createText(text1)}
|
||||
<div className="border-t border-gray-200"></div>
|
||||
{createText(text2)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
})()}
|
||||
</div>
|
||||
|
||||
{/* 底部按钮 */}
|
||||
<div className="flex flex-row gap-2 items-center justify-center flex-wrap">
|
||||
<LightButton
|
||||
onClick={handleNext}
|
||||
className="px-4 py-2 rounded-full text-sm"
|
||||
>
|
||||
{show === "question" ? t("answer") : t("next")}
|
||||
</LightButton>
|
||||
<LightButton
|
||||
onClick={handlePrevious}
|
||||
className="px-4 py-2 rounded-full text-sm"
|
||||
>
|
||||
{t("previous")}
|
||||
</LightButton>
|
||||
<CircleToggleButton
|
||||
selected={reverse}
|
||||
onClick={toggleReverse}
|
||||
>
|
||||
{t("reverse")}
|
||||
</CircleToggleButton>
|
||||
<CircleToggleButton
|
||||
selected={dictation}
|
||||
onClick={toggleDictation}
|
||||
>
|
||||
{t("dictation")}
|
||||
</CircleToggleButton>
|
||||
<CircleToggleButton
|
||||
selected={disorder}
|
||||
onClick={toggleDisorder}
|
||||
>
|
||||
{t("disorder")}
|
||||
</CircleToggleButton>
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState, useRef, forwardRef, useEffect, useCallback } from "react";
|
||||
import { SubtitleDisplay } from "./SubtitleDisplay";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { RangeInput } from "@/components/ui/RangeInput";
|
||||
import { getIndex, parseSrt, getNearistIndex } from "../subtitle";
|
||||
import { useTranslations } from "next-intl";
|
||||
|
||||
@@ -196,15 +197,18 @@ const VideoPanel = forwardRef<HTMLVideoElement, VideoPanelProps>(
|
||||
{t("autoPause", { enabled: autoPause ? "Yes" : "No" })}
|
||||
</LightButton>
|
||||
</div>
|
||||
<input
|
||||
<RangeInput
|
||||
className="seekbar"
|
||||
type="range"
|
||||
min={0}
|
||||
max={srtLength}
|
||||
onChange={handleSeek}
|
||||
step={1}
|
||||
onChange={(value) => {
|
||||
if (videoRef.current && parsedSrtRef.current) {
|
||||
videoRef.current.currentTime = parsedSrtRef.current[value]?.start || 0;
|
||||
setProgress(value);
|
||||
}
|
||||
}}
|
||||
value={progress}
|
||||
></input>
|
||||
/>
|
||||
<span>{spanText}</span>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import React, { useRef } from "react";
|
||||
import { Button } from "@/design-system/base/button";
|
||||
import { FileInputProps } from "../../types/controls";
|
||||
|
||||
interface FileInputComponentProps extends FileInputProps {
|
||||
@@ -33,13 +34,15 @@ export function FileInput({ accept, onFileSelect, disabled, className, children
|
||||
disabled={disabled}
|
||||
className="hidden"
|
||||
/>
|
||||
<button
|
||||
<Button
|
||||
onClick={handleClick}
|
||||
disabled={disabled}
|
||||
className={`px-2 py-1 rounded shadow font-bold hover:cursor-pointer hover:bg-gray-200 text-gray-800 bg-white ${disabled ? 'opacity-50 cursor-not-allowed' : ''} ${className || ''}`}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
className={className}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import React from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { PlayButtonProps } from "../../types/player";
|
||||
|
||||
export function PlayButton({ isPlaying, onToggle, disabled, className }: PlayButtonProps) {
|
||||
|
||||
@@ -2,25 +2,16 @@
|
||||
|
||||
import React from "react";
|
||||
import { SeekBarProps } from "../../types/player";
|
||||
import { RangeInput } from "@/components/ui/RangeInput";
|
||||
|
||||
export function SeekBar({ value, max, onChange, disabled, className }: SeekBarProps) {
|
||||
const handleChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const newValue = parseInt(event.target.value);
|
||||
onChange(newValue);
|
||||
}, [onChange]);
|
||||
|
||||
return (
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={max}
|
||||
<RangeInput
|
||||
value={value}
|
||||
onChange={handleChange}
|
||||
max={max}
|
||||
onChange={onChange}
|
||||
disabled={disabled}
|
||||
className={`w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer ${disabled ? 'opacity-50 cursor-not-allowed' : ''} ${className || ''}`}
|
||||
style={{
|
||||
background: `linear-gradient(to right, #374151 0%, #374151 ${(value / max) * 100}%, #e5e7eb ${(value / max) * 100}%, #e5e7eb 100%)`
|
||||
}}
|
||||
className={className}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { SpeedControlProps } from "../../types/player";
|
||||
import { getPlaybackRateOptions, getPlaybackRateLabel } from "../../utils/timeUtils";
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import React from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { ChevronLeft, ChevronRight, RotateCcw, Pause } from "lucide-react";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { ControlBarProps } from "../../types/controls";
|
||||
import { PlayButton } from "../atoms/PlayButton";
|
||||
import { SpeedControl } from "../atoms/SpeedControl";
|
||||
|
||||
@@ -4,7 +4,7 @@ import React from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { toast } from "sonner";
|
||||
import { Video, FileText } from "lucide-react";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { FileUploadProps } from "../../types/controls";
|
||||
import { useFileUpload } from "../../hooks/useFileUpload";
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import React from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { toast } from "sonner";
|
||||
import { Video, FileText } from "lucide-react";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { useSrtPlayer } from "./hooks/useSrtPlayer";
|
||||
import { useSubtitleSync } from "./hooks/useSubtitleSync";
|
||||
import { useKeyboardShortcuts, createSrtPlayerShortcuts } from "./hooks/useKeyboardShortcuts";
|
||||
@@ -14,7 +15,7 @@ import { SubtitleArea } from "./components/compounds/SubtitleArea";
|
||||
import { ControlBar } from "./components/compounds/ControlBar";
|
||||
import { UploadZone } from "./components/compounds/UploadZone";
|
||||
import { SeekBar } from "./components/atoms/SeekBar";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
|
||||
export default function SrtPlayerPage() {
|
||||
const t = useTranslations("home");
|
||||
@@ -106,23 +107,19 @@ export default function SrtPlayerPage() {
|
||||
const canPlay = state.video.url && state.subtitle.url && state.subtitle.data.length > 0;
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<div className="container mx-auto px-4 py-8">
|
||||
<div className="max-w-6xl mx-auto">
|
||||
{/* 标题区域 */}
|
||||
<div className="text-center mb-8">
|
||||
<h1 className="text-4xl font-bold text-gray-800 mb-2">
|
||||
{t("srtPlayer.name")}
|
||||
</h1>
|
||||
<p className="text-lg text-gray-600">
|
||||
{t("srtPlayer.description")}
|
||||
</p>
|
||||
</div>
|
||||
<PageLayout>
|
||||
{/* 标题区域 */}
|
||||
<div className="text-center mb-8">
|
||||
<h1 className="text-4xl font-bold text-gray-800 mb-2">
|
||||
{t("srtPlayer.name")}
|
||||
</h1>
|
||||
<p className="text-lg text-gray-600">
|
||||
{t("srtPlayer.description")}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 主要内容区域 */}
|
||||
<div className="bg-white rounded-xl shadow-lg overflow-hidden">
|
||||
{/* 视频播放器区域 */}
|
||||
<div className="aspect-video bg-black relative">
|
||||
{/* 视频播放器区域 */}
|
||||
<div className="aspect-video bg-black relative rounded-md overflow-hidden">
|
||||
{(!state.video.url || !state.subtitle.url || state.subtitle.data.length === 0) && (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-gray-900 bg-opacity-75 z-10">
|
||||
<div className="text-center text-white">
|
||||
@@ -163,10 +160,10 @@ export default function SrtPlayerPage() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 控制面板 */}
|
||||
<div className="p-3 bg-gray-50 border-t">
|
||||
{/* 上传区域和状态指示器 */}
|
||||
<div className="mb-3">
|
||||
{/* 控制面板 */}
|
||||
<div className="p-3 bg-gray-50 border-t rounded-b-xl">
|
||||
{/* 上传区域和状态指示器 */}
|
||||
<div className="mb-3">
|
||||
<div className="flex gap-3">
|
||||
<div className={`flex-1 p-2 rounded-lg border-2 transition-all ${state.video.url
|
||||
? 'border-gray-800 bg-gray-100'
|
||||
@@ -269,12 +266,9 @@ export default function SrtPlayerPage() {
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { SubtitleEntry } from "../types/subtitle";
|
||||
import { logger } from "@/lib/logger";
|
||||
|
||||
export function parseSrt(data: string): SubtitleEntry[] {
|
||||
const lines = data.split(/\r?\n/);
|
||||
@@ -94,7 +93,7 @@ export async function loadSubtitle(url: string): Promise<SubtitleEntry[]> {
|
||||
const data = await response.text();
|
||||
return parseSrt(data);
|
||||
} catch (error) {
|
||||
logger.error('加载字幕失败', error);
|
||||
console.error('加载字幕失败', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
TextSpeakerArraySchema,
|
||||
TextSpeakerItemSchema,
|
||||
} from "@/lib/interfaces";
|
||||
import { IconClick } from "@/components/ui/buttons";
|
||||
import { IconClick } from "@/design-system/base/button";
|
||||
import { IMAGES } from "@/config/images";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { getLocalStorageOperator } from "@/lib/browser/localStorageOperators";
|
||||
@@ -24,7 +24,7 @@ function TextCard({ item, handleUse, handleDel }: TextCardProps) {
|
||||
handleDel(item);
|
||||
};
|
||||
return (
|
||||
<div className="p-2 border-b border-gray-200 rounded-2xl bg-gray-100 m-2 grid grid-cols-8">
|
||||
<div className="p-2 border-b border-gray-200 rounded-lg bg-gray-100 m-2 grid grid-cols-8">
|
||||
<div className="col-span-7" onClick={onUseClick}>
|
||||
<div className="max-h-26 hover:cursor-pointer text-3xl overflow-y-auto">
|
||||
{item.text}
|
||||
@@ -39,7 +39,7 @@ function TextCard({ item, handleUse, handleDel }: TextCardProps) {
|
||||
alt="delete"
|
||||
onClick={onDelClick}
|
||||
className="place-self-center"
|
||||
size={42}
|
||||
size="lg"
|
||||
></IconClick>
|
||||
</div>
|
||||
</div>
|
||||
@@ -81,7 +81,7 @@ export function SaveList({ show = false, handleUse }: SaveListProps) {
|
||||
if (show)
|
||||
return (
|
||||
<div
|
||||
className="my-4 p-2 mx-4 md:mx-32 border border-gray-200 rounded-2xl"
|
||||
className="my-4 p-2 mx-4 md:mx-32 border border-gray-200 rounded-lg"
|
||||
style={{ fontFamily: "Times New Roman, serif" }}
|
||||
>
|
||||
<div className="flex flex-row justify-center gap-8 items-center">
|
||||
@@ -89,14 +89,14 @@ export function SaveList({ show = false, handleUse }: SaveListProps) {
|
||||
src={IMAGES.refresh}
|
||||
alt="refresh"
|
||||
onClick={refresh}
|
||||
size={48}
|
||||
size="lg"
|
||||
className=""
|
||||
></IconClick>
|
||||
<IconClick
|
||||
src={IMAGES.delete}
|
||||
alt="delete"
|
||||
onClick={handleDeleteAll}
|
||||
size={48}
|
||||
size="lg"
|
||||
className=""
|
||||
></IconClick>
|
||||
</div>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { IconClick } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { IconClick } from "@/design-system/base/button";
|
||||
import { IMAGES } from "@/config/images";
|
||||
import { useAudioPlayer } from "@/hooks/useAudioPlayer";
|
||||
import {
|
||||
@@ -15,7 +15,6 @@ import { SaveList } from "./SaveList";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { getLocalStorageOperator } from "@/lib/browser/localStorageOperators";
|
||||
import { genIPA, genLanguage } from "@/modules/translator/translator-action";
|
||||
import { logger } from "@/lib/logger";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { getTTSUrl, TTS_SUPPORTED_LANGUAGES } from "@/lib/bigmodel/tts";
|
||||
|
||||
@@ -75,7 +74,7 @@ export default function TextSpeakerPage() {
|
||||
setIPA(data.ipa);
|
||||
})
|
||||
.catch((e) => {
|
||||
logger.error("生成 IPA 失败", e);
|
||||
console.error("生成 IPA 失败", e);
|
||||
setIPA("");
|
||||
});
|
||||
}
|
||||
@@ -120,7 +119,7 @@ export default function TextSpeakerPage() {
|
||||
load(objurlRef.current);
|
||||
play();
|
||||
} catch (e) {
|
||||
logger.error("播放音频失败", e);
|
||||
console.error("播放音频失败", e);
|
||||
setPause(true);
|
||||
setLanguage(null);
|
||||
setProcessing(false);
|
||||
@@ -212,7 +211,7 @@ export default function TextSpeakerPage() {
|
||||
}
|
||||
setIntoLocalStorage(save);
|
||||
} catch (e) {
|
||||
logger.error("保存到本地存储失败", e);
|
||||
console.error("保存到本地存储失败", e);
|
||||
setLanguage(null);
|
||||
} finally {
|
||||
setSaving(false);
|
||||
@@ -223,7 +222,7 @@ export default function TextSpeakerPage() {
|
||||
<PageLayout className="items-start py-4">
|
||||
{/* 文本输入区域 */}
|
||||
<div
|
||||
className="border border-gray-200 rounded-2xl"
|
||||
className="border border-gray-200 rounded-lg"
|
||||
style={{ fontFamily: "Times New Roman, serif" }}
|
||||
>
|
||||
{/* 文本输入框 */}
|
||||
@@ -243,37 +242,37 @@ export default function TextSpeakerPage() {
|
||||
<div className="p-4 relative w-full flex flex-row flex-wrap gap-2 justify-center items-center">
|
||||
{/* 速度调节面板 */}
|
||||
{showSpeedAdjust && (
|
||||
<div className="bg-white p-6 rounded-2xl border-gray-200 border-2 shadow-2xl absolute left-1/2 -translate-x-1/2 -translate-y-full -top-4 flex flex-row flex-wrap gap-2 justify-center items-center z-10">
|
||||
<div className="bg-white p-6 rounded-lg border-gray-200 border-2 shadow-2xl absolute left-1/2 -translate-x-1/2 -translate-y-full -top-4 flex flex-row flex-wrap gap-2 justify-center items-center z-10">
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={letMeSetSpeed(0.5)}
|
||||
src={IMAGES.speed_0_5x}
|
||||
alt="0.5x"
|
||||
className={speed === 0.5 ? "bg-gray-200" : ""}
|
||||
></IconClick>
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={letMeSetSpeed(0.7)}
|
||||
src={IMAGES.speed_0_7x}
|
||||
alt="0.7x"
|
||||
className={speed === 0.7 ? "bg-gray-200" : ""}
|
||||
></IconClick>
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={letMeSetSpeed(1)}
|
||||
src={IMAGES.speed_1x}
|
||||
alt="1x"
|
||||
className={speed === 1 ? "bg-gray-200" : ""}
|
||||
></IconClick>
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={letMeSetSpeed(1.2)}
|
||||
src={IMAGES.speed_1_2_x}
|
||||
alt="1.2x"
|
||||
className={speed === 1.2 ? "bg-gray-200" : ""}
|
||||
></IconClick>
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={letMeSetSpeed(1.5)}
|
||||
src={IMAGES.speed_1_5x}
|
||||
alt="1.5x"
|
||||
@@ -283,7 +282,7 @@ export default function TextSpeakerPage() {
|
||||
)}
|
||||
{/* 播放/暂停按钮 */}
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={speak}
|
||||
src={pause ? IMAGES.play_arrow : IMAGES.pause}
|
||||
alt="playorpause"
|
||||
@@ -291,7 +290,7 @@ export default function TextSpeakerPage() {
|
||||
></IconClick>
|
||||
{/* 自动暂停按钮 */}
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={() => {
|
||||
setAutopause(!autopause);
|
||||
if (objurlRef) {
|
||||
@@ -304,7 +303,7 @@ export default function TextSpeakerPage() {
|
||||
></IconClick>
|
||||
{/* 速度调节按钮 */}
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={() => setShowSpeedAdjust(!showSpeedAdjust)}
|
||||
src={IMAGES.speed}
|
||||
alt="speed"
|
||||
@@ -312,7 +311,7 @@ export default function TextSpeakerPage() {
|
||||
></IconClick>
|
||||
{/* 保存按钮 */}
|
||||
<IconClick
|
||||
size={45}
|
||||
size="lg"
|
||||
onClick={save}
|
||||
src={IMAGES.save}
|
||||
alt="save"
|
||||
@@ -339,7 +338,7 @@ export default function TextSpeakerPage() {
|
||||
</div>
|
||||
{/* 保存列表 */}
|
||||
{showSaveList && (
|
||||
<div className="mt-4 border border-gray-200 rounded-2xl overflow-hidden">
|
||||
<div className="mt-4 border border-gray-200 rounded-lg overflow-hidden">
|
||||
<SaveList show={showSaveList} handleUse={handleUseItem}></SaveList>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { IconClick } from "@/components/ui/buttons";
|
||||
import { LightButton, PrimaryButton, IconClick } from "@/design-system/base/button";
|
||||
import { IMAGES } from "@/config/images";
|
||||
import { useAudioPlayer } from "@/hooks/useAudioPlayer";
|
||||
import { useTranslations } from "next-intl";
|
||||
@@ -95,13 +94,13 @@ export default function TranslatorPage() {
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="min-h-[calc(100vh-64px)] bg-white">
|
||||
{/* TCard Component */}
|
||||
<div className="w-screen flex flex-col md:flex-row md:justify-between gap-2 p-2">
|
||||
{/* Card Component - Left Side */}
|
||||
<div className="w-full md:w-1/2 flex flex-col-reverse gap-2">
|
||||
{/* ICard1 Component */}
|
||||
<div className="border border-gray-200 rounded-2xl w-full h-64 p-2">
|
||||
<div className="border border-gray-200 rounded-lg w-full h-64 p-2">
|
||||
<textarea
|
||||
className="resize-none h-8/12 w-full focus:outline-0"
|
||||
ref={taref}
|
||||
@@ -147,7 +146,7 @@ export default function TranslatorPage() {
|
||||
{/* Card Component - Right Side */}
|
||||
<div className="w-full md:w-1/2 flex flex-col-reverse gap-2">
|
||||
{/* ICard2 Component */}
|
||||
<div className="bg-gray-100 rounded-2xl w-full h-64 p-2">
|
||||
<div className="bg-gray-100 rounded-lg w-full h-64 p-2">
|
||||
<div className="h-2/3 w-full overflow-y-auto">{translationResult?.translatedText || ""}</div>
|
||||
<div className="ipa w-full h-1/6 overflow-y-auto text-gray-600">
|
||||
{translationResult?.targetIpa || ""}
|
||||
@@ -210,13 +209,15 @@ export default function TranslatorPage() {
|
||||
|
||||
{/* TranslateButton Component */}
|
||||
<div className="w-screen flex justify-center items-center">
|
||||
<button
|
||||
className={`duration-150 ease-in text-xl font-extrabold border rounded-4xl p-3 border-gray-200 h-16 ${processing ? "bg-gray-200" : "bg-white hover:bg-gray-200 hover:cursor-pointer"}`}
|
||||
<PrimaryButton
|
||||
onClick={translate}
|
||||
disabled={processing}
|
||||
size="lg"
|
||||
className="text-xl"
|
||||
>
|
||||
{t("translate")}
|
||||
</button>
|
||||
</PrimaryButton>
|
||||
</div>
|
||||
</>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
import { useState, useActionState, startTransition } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { Container } from "@/components/ui/Container";
|
||||
import { Input } from "@/components/ui/Input";
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { Input } from "@/design-system/base/input";
|
||||
import { LightButton, LinkButton } from "@/design-system/base/button";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { signInAction, signUpAction, SignUpState } from "@/modules/auth/auth-action";
|
||||
import { actionSignIn, actionSignUp, ActionOutputAuth } from "@/modules/auth/auth-action";
|
||||
|
||||
interface AuthFormProps {
|
||||
redirectTo?: string;
|
||||
@@ -19,22 +19,22 @@ export function AuthForm({ redirectTo }: AuthFormProps) {
|
||||
const [clearSignUp, setClearSignUp] = useState(false);
|
||||
|
||||
const [signInState, signInActionForm, isSignInPending] = useActionState(
|
||||
async (prevState: SignUpState | undefined, formData: FormData) => {
|
||||
async (_prevState: ActionOutputAuth | undefined, formData: FormData) => {
|
||||
if (clearSignIn) {
|
||||
setClearSignIn(false);
|
||||
return undefined;
|
||||
}
|
||||
return signInAction(prevState || {}, formData);
|
||||
return actionSignIn(undefined, formData);
|
||||
},
|
||||
undefined
|
||||
);
|
||||
const [signUpState, signUpActionForm, isSignUpPending] = useActionState(
|
||||
async (prevState: SignUpState | undefined, formData: FormData) => {
|
||||
async (_prevState: ActionOutputAuth | undefined, formData: FormData) => {
|
||||
if (clearSignUp) {
|
||||
setClearSignUp(false);
|
||||
return undefined;
|
||||
}
|
||||
return signUpAction(prevState || {}, formData);
|
||||
return actionSignUp(undefined, formData);
|
||||
},
|
||||
undefined
|
||||
);
|
||||
@@ -44,15 +44,32 @@ export function AuthForm({ redirectTo }: AuthFormProps) {
|
||||
const validateForm = (formData: FormData): boolean => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
|
||||
const identifier = formData.get("identifier") as string;
|
||||
const email = formData.get("email") as string;
|
||||
const username = formData.get("username") as string;
|
||||
const password = formData.get("password") as string;
|
||||
const name = formData.get("name") as string;
|
||||
const confirmPassword = formData.get("confirmPassword") as string;
|
||||
|
||||
if (!email) {
|
||||
newErrors.email = t("emailRequired");
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
newErrors.email = t("invalidEmail");
|
||||
// 登录模式验证
|
||||
if (mode === 'signin') {
|
||||
if (!identifier) {
|
||||
newErrors.identifier = t("identifierRequired");
|
||||
}
|
||||
} else {
|
||||
// 注册模式验证
|
||||
if (!email) {
|
||||
newErrors.email = t("emailRequired");
|
||||
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
newErrors.email = t("invalidEmail");
|
||||
}
|
||||
|
||||
if (!username) {
|
||||
newErrors.username = t("usernameRequired");
|
||||
} else if (username.length < 3) {
|
||||
newErrors.username = t("usernameTooShort");
|
||||
} else if (!/^[a-zA-Z0-9_]+$/.test(username)) {
|
||||
newErrors.username = t("usernameInvalid");
|
||||
}
|
||||
}
|
||||
|
||||
if (!password) {
|
||||
@@ -62,10 +79,6 @@ export function AuthForm({ redirectTo }: AuthFormProps) {
|
||||
}
|
||||
|
||||
if (mode === 'signup') {
|
||||
if (!name) {
|
||||
newErrors.name = t("nameRequired");
|
||||
}
|
||||
|
||||
if (!confirmPassword) {
|
||||
newErrors.confirmPassword = t("confirmPasswordRequired");
|
||||
} else if (password !== confirmPassword) {
|
||||
@@ -112,8 +125,7 @@ export function AuthForm({ redirectTo }: AuthFormProps) {
|
||||
const currentError = mode === 'signin' ? signInState : signUpState;
|
||||
|
||||
return (
|
||||
<div className="h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center px-4">
|
||||
<Container className="p-8 max-w-md w-full">
|
||||
<PageLayout>
|
||||
{/* 页面标题 */}
|
||||
<div className="text-center mb-8">
|
||||
<h1 className="text-3xl font-bold text-gray-800 mb-2">{t(mode === 'signin' ? 'signIn' : 'signUp')}</h1>
|
||||
@@ -128,41 +140,57 @@ export function AuthForm({ redirectTo }: AuthFormProps) {
|
||||
|
||||
{/* 登录/注册表单 */}
|
||||
<form onSubmit={handleFormSubmit} className="space-y-4">
|
||||
{/* 用户名输入(仅注册模式显示) */}
|
||||
{mode === 'signup' && (
|
||||
{/* 邮箱/用户名输入(登录模式)或 用户名输入(注册模式) */}
|
||||
{mode === 'signin' ? (
|
||||
<div>
|
||||
<Input
|
||||
type="text"
|
||||
name="name"
|
||||
placeholder={t("name")}
|
||||
name="identifier"
|
||||
placeholder={t("emailOrUsername")}
|
||||
className="w-full px-3 py-2"
|
||||
/>
|
||||
{/* 客户端验证错误 */}
|
||||
{errors.name && (
|
||||
<p className="text-red-500 text-sm mt-1">{errors.name}</p>
|
||||
{errors.identifier && (
|
||||
<p className="text-red-500 text-sm mt-1">{errors.identifier}</p>
|
||||
)}
|
||||
{/* 服务器端验证错误 */}
|
||||
{currentError?.errors?.username && (
|
||||
<p className="text-red-500 text-sm mt-1">{currentError.errors.username[0]}</p>
|
||||
{currentError?.errors?.email && (
|
||||
<p className="text-red-500 text-sm mt-1">{currentError.errors.email[0]}</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
) : (
|
||||
<>
|
||||
{/* 用户名输入(仅注册模式) */}
|
||||
<div>
|
||||
<Input
|
||||
type="text"
|
||||
name="username"
|
||||
placeholder={t("username")}
|
||||
className="w-full px-3 py-2"
|
||||
/>
|
||||
{errors.username && (
|
||||
<p className="text-red-500 text-sm mt-1">{errors.username}</p>
|
||||
)}
|
||||
{currentError?.errors?.username && (
|
||||
<p className="text-red-500 text-sm mt-1">{currentError.errors.username[0]}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 邮箱输入 */}
|
||||
<div>
|
||||
<Input
|
||||
type="email"
|
||||
name="email"
|
||||
placeholder={t("email")}
|
||||
className="w-full px-3 py-2"
|
||||
/>
|
||||
{errors.email && (
|
||||
<p className="text-red-500 text-sm mt-1">{errors.email}</p>
|
||||
)}
|
||||
{currentError?.errors?.email && (
|
||||
<p className="text-red-500 text-sm mt-1">{currentError.errors.email[0]}</p>
|
||||
)}
|
||||
</div>
|
||||
{/* 邮箱输入(仅注册模式) */}
|
||||
<div>
|
||||
<Input
|
||||
type="email"
|
||||
name="email"
|
||||
placeholder={t("email")}
|
||||
className="w-full px-3 py-2"
|
||||
/>
|
||||
{errors.email && (
|
||||
<p className="text-red-500 text-sm mt-1">{errors.email}</p>
|
||||
)}
|
||||
{currentError?.errors?.email && (
|
||||
<p className="text-red-500 text-sm mt-1">{currentError.errors.email[0]}</p>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 密码输入 */}
|
||||
<div>
|
||||
@@ -233,7 +261,7 @@ export function AuthForm({ redirectTo }: AuthFormProps) {
|
||||
|
||||
{/* 模式切换链接 */}
|
||||
<div className="mt-6 text-center">
|
||||
<button
|
||||
<LinkButton
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setMode(mode === 'signin' ? 'signup' : 'signin');
|
||||
@@ -245,15 +273,13 @@ export function AuthForm({ redirectTo }: AuthFormProps) {
|
||||
setClearSignUp(true);
|
||||
}
|
||||
}}
|
||||
className="text-[#35786f] hover:underline"
|
||||
>
|
||||
{mode === 'signin'
|
||||
? `${t("noAccount")} ${t("signUp")}`
|
||||
: `${t("hasAccount")} ${t("signIn")}`
|
||||
}
|
||||
</button>
|
||||
</LinkButton>
|
||||
</div>
|
||||
</Container>
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
FolderPlus,
|
||||
Trash2,
|
||||
} from "lucide-react";
|
||||
import { CircleButton, DashedButton } from "@/design-system/base/button";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useTranslations } from "next-intl";
|
||||
@@ -35,7 +36,7 @@ const FolderCard = ({ folder, refresh }: FolderProps) => {
|
||||
>
|
||||
<div className="flex items-center gap-3 flex-1">
|
||||
<div className="shrink-0">
|
||||
<Fd className="text-gray-600" size={24} />
|
||||
<Fd className="text-gray-600" size="md" />
|
||||
</div>
|
||||
|
||||
<div className="flex-1">
|
||||
@@ -51,8 +52,8 @@ const FolderCard = ({ folder, refresh }: FolderProps) => {
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={(e) => {
|
||||
<CircleButton
|
||||
onClick={(e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
const newName = prompt("Input a new name.")?.trim();
|
||||
if (newName && newName.length > 0) {
|
||||
@@ -67,12 +68,11 @@ const FolderCard = ({ folder, refresh }: FolderProps) => {
|
||||
});
|
||||
}
|
||||
}}
|
||||
className="p-2 text-gray-400 hover:text-gray-600 hover:bg-gray-100 rounded-lg transition-colors"
|
||||
>
|
||||
<FolderPen size={16} />
|
||||
</button>
|
||||
<button
|
||||
onClick={(e) => {
|
||||
</CircleButton>
|
||||
<CircleButton
|
||||
onClick={(e: React.MouseEvent) => {
|
||||
e.stopPropagation();
|
||||
const confirm = prompt(t("confirmDelete", { name: folder.name }));
|
||||
if (confirm === folder.name) {
|
||||
@@ -87,10 +87,10 @@ const FolderCard = ({ folder, refresh }: FolderProps) => {
|
||||
});
|
||||
}
|
||||
}}
|
||||
className="p-2 text-gray-400 hover:text-red-500 hover:bg-red-50 rounded-lg transition-colors"
|
||||
className="text-gray-400 hover:text-red-500 hover:bg-red-50"
|
||||
>
|
||||
<Trash2 size={16} />
|
||||
</button>
|
||||
</CircleButton>
|
||||
<ChevronRight size={18} className="text-gray-400" />
|
||||
</div>
|
||||
</div>
|
||||
@@ -135,7 +135,7 @@ export function FoldersClient({ userId }: { userId: string; }) {
|
||||
<PageHeader title={t("title")} subtitle={t("subtitle")} />
|
||||
|
||||
{/* 新建文件夹按钮 */}
|
||||
<button
|
||||
<DashedButton
|
||||
onClick={async () => {
|
||||
const folderName = prompt(t("enterFolderName"));
|
||||
if (!folderName) return;
|
||||
@@ -154,11 +154,11 @@ export function FoldersClient({ userId }: { userId: string; }) {
|
||||
}
|
||||
}}
|
||||
disabled={loading}
|
||||
className="w-full p-3 border-2 border-dashed border-gray-300 rounded-xl text-gray-500 hover:border-gray-400 hover:text-gray-600 transition-colors flex items-center justify-center gap-2"
|
||||
className="w-full"
|
||||
>
|
||||
<FolderPlus size={18} />
|
||||
<span>{loading ? t("creating") : t("newFolder")}</span>
|
||||
</button>
|
||||
</DashedButton>
|
||||
|
||||
{/* 文件夹列表 */}
|
||||
<div className="mt-4">
|
||||
@@ -167,13 +167,13 @@ export function FoldersClient({ userId }: { userId: string; }) {
|
||||
// 空状态
|
||||
<div className="text-center py-12 text-gray-400">
|
||||
<div className="w-16 h-16 mx-auto mb-3 rounded-full bg-gray-100 flex items-center justify-center">
|
||||
<FolderPlus size={24} className="text-gray-400" />
|
||||
<FolderPlus size="md" className="text-gray-400" />
|
||||
</div>
|
||||
<p className="text-sm">{t("noFoldersYet")}</p>
|
||||
</div>
|
||||
) : (
|
||||
// 文件夹卡片列表
|
||||
<div className="rounded-xl border border-gray-200 overflow-hidden">
|
||||
<div className="rounded-md border border-gray-200 overflow-hidden">
|
||||
{folders
|
||||
.toSorted((a, b) => a.id - b.id)
|
||||
.map((folder) => (
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { Input } from "@/components/ui/Input";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { Input } from "@/design-system/base/input";
|
||||
import { LocaleSelector } from "@/components/ui/LocaleSelector";
|
||||
import { X } from "lucide-react";
|
||||
import { useRef, useState } from "react";
|
||||
@@ -67,7 +67,7 @@ export function AddTextPairModal({
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="bg-white rounded-xl p-6 w-full max-w-md mx-4">
|
||||
<div className="bg-white rounded-md p-6 w-full max-w-md mx-4">
|
||||
<div className="flex">
|
||||
<h2 className="flex-1 text-xl font-light mb-4 text-center">
|
||||
{t("addNewTextPair")}
|
||||
|
||||
@@ -7,15 +7,14 @@ import { AddTextPairModal } from "./AddTextPairModal";
|
||||
import { TextPairCard } from "./TextPairCard";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { GreenButton } from "@/components/ui/buttons";
|
||||
import { IconButton } from "@/components/ui/buttons";
|
||||
import { PrimaryButton, IconButton, LinkButton } from "@/design-system/base/button";
|
||||
import { CardList } from "@/components/ui/CardList";
|
||||
import { actionCreatePair, actionDeletePairById, actionGetPairsByFolderId } from "@/modules/folder/folder-aciton";
|
||||
import { TSharedPair } from "@/shared/folder-type";
|
||||
import { toast } from "sonner";
|
||||
|
||||
|
||||
export function InFolder({ folderId }: { folderId: number; }) {
|
||||
export function InFolder({ folderId, isReadOnly }: { folderId: number; isReadOnly: boolean; }) {
|
||||
const [textPairs, setTextPairs] = useState<TSharedPair[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [openAddModal, setAddModal] = useState(false);
|
||||
@@ -52,13 +51,13 @@ export function InFolder({ folderId }: { folderId: number; }) {
|
||||
{/* 顶部导航和标题栏 */}
|
||||
<div className="mb-6">
|
||||
{/* 返回按钮 */}
|
||||
<button
|
||||
<LinkButton
|
||||
onClick={router.back}
|
||||
className="flex items-center gap-2 text-gray-500 hover:text-gray-700 transition-colors mb-4"
|
||||
className="flex items-center gap-2 mb-4"
|
||||
>
|
||||
<ArrowLeft size={16} />
|
||||
<span className="text-sm">{t("back")}</span>
|
||||
</button>
|
||||
</LinkButton>
|
||||
|
||||
{/* 页面标题和操作按钮 */}
|
||||
<div className="flex items-center justify-between">
|
||||
@@ -74,19 +73,21 @@ export function InFolder({ folderId }: { folderId: number; }) {
|
||||
|
||||
{/* 操作按钮区域 */}
|
||||
<div className="flex items-center gap-2">
|
||||
<GreenButton
|
||||
<PrimaryButton
|
||||
onClick={() => {
|
||||
redirect(`/memorize?folder_id=${folderId}`);
|
||||
}}
|
||||
>
|
||||
{t("memorize")}
|
||||
</GreenButton>
|
||||
<IconButton
|
||||
onClick={() => {
|
||||
setAddModal(true);
|
||||
}}
|
||||
icon={<Plus size={18} className="text-gray-700" />}
|
||||
/>
|
||||
</PrimaryButton>
|
||||
{!isReadOnly && (
|
||||
<IconButton
|
||||
onClick={() => {
|
||||
setAddModal(true);
|
||||
}}
|
||||
icon={<Plus size={18} className="text-gray-700" />}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -113,6 +114,7 @@ export function InFolder({ folderId }: { folderId: number; }) {
|
||||
<TextPairCard
|
||||
key={textPair.id}
|
||||
textPair={textPair}
|
||||
isReadOnly={isReadOnly}
|
||||
onDel={() => {
|
||||
actionDeletePairById(textPair.id)
|
||||
.then(result => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Edit, Trash2 } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { CircleButton } from "@/design-system/base/button";
|
||||
import { UpdateTextPairModal } from "./UpdateTextPairModal";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { TSharedPair } from "@/shared/folder-type";
|
||||
@@ -9,12 +10,14 @@ import { toast } from "sonner";
|
||||
|
||||
interface TextPairCardProps {
|
||||
textPair: TSharedPair;
|
||||
isReadOnly: boolean;
|
||||
onDel: () => void;
|
||||
refreshTextPairs: () => void;
|
||||
}
|
||||
|
||||
export function TextPairCard({
|
||||
textPair,
|
||||
isReadOnly,
|
||||
onDel,
|
||||
refreshTextPairs,
|
||||
}: TextPairCardProps) {
|
||||
@@ -35,20 +38,24 @@ export function TextPairCard({
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-1 opacity-50 group-hover:opacity-100 transition-opacity">
|
||||
<button
|
||||
className="p-1.5 text-gray-400 hover:text-gray-600 hover:bg-gray-100 rounded-md transition-colors"
|
||||
onClick={() => setOpenUpdateModal(true)}
|
||||
title={t("edit")}
|
||||
>
|
||||
<Edit size={14} />
|
||||
</button>
|
||||
<button
|
||||
className="p-1.5 text-gray-400 hover:text-red-500 hover:bg-red-50 rounded-md transition-colors"
|
||||
onClick={onDel}
|
||||
title={t("delete")}
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
{!isReadOnly && (
|
||||
<>
|
||||
<CircleButton
|
||||
onClick={() => setOpenUpdateModal(true)}
|
||||
title={t("edit")}
|
||||
className="text-gray-400 hover:text-gray-600"
|
||||
>
|
||||
<Edit size={14} />
|
||||
</CircleButton>
|
||||
<CircleButton
|
||||
onClick={onDel}
|
||||
title={t("delete")}
|
||||
className="text-gray-400 hover:text-red-500 hover:bg-red-50"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</CircleButton>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-gray-900 grid grid-cols-2 gap-4 w-3/4">
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { Input } from "@/components/ui/Input";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { Input } from "@/design-system/base/input";
|
||||
import { LocaleSelector } from "@/components/ui/LocaleSelector";
|
||||
import { X } from "lucide-react";
|
||||
import { useRef, useState } from "react";
|
||||
@@ -63,7 +63,7 @@ export function UpdateTextPairModal({
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="bg-white rounded-xl p-6 w-full max-w-md mx-4">
|
||||
<div className="bg-white rounded-md p-6 w-full max-w-md mx-4">
|
||||
<div className="flex">
|
||||
<h2 className="flex-1 text-xl font-light mb-4 text-center">
|
||||
{t("updateTextPair")}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { InFolder } from "./InFolder";
|
||||
import { auth } from "@/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { actionGetUserIdByFolderId } from "@/modules/folder/folder-aciton";
|
||||
|
||||
export default async function FoldersPage({
|
||||
params,
|
||||
}: {
|
||||
@@ -16,9 +17,11 @@ export default async function FoldersPage({
|
||||
if (!folder_id) {
|
||||
redirect("/folders");
|
||||
}
|
||||
if (!session) redirect(`/auth?redirect=/folders/${folder_id}`);
|
||||
if ((await actionGetUserIdByFolderId(Number(folder_id))).data !== session.user.id) {
|
||||
return <p>{t("unauthorized")}</p>;
|
||||
}
|
||||
return <InFolder folderId={Number(folder_id)} />;
|
||||
|
||||
// Allow non-authenticated users to view folders (read-only mode)
|
||||
const folderUserId = (await actionGetUserIdByFolderId(Number(folder_id))).data;
|
||||
const isOwner = session?.user?.id === folderUserId;
|
||||
const isReadOnly = !isOwner;
|
||||
|
||||
return <InFolder folderId={Number(folder_id)} isReadOnly={isReadOnly} />;
|
||||
}
|
||||
|
||||
@@ -1,30 +1,230 @@
|
||||
@import "tailwindcss";
|
||||
|
||||
/**
|
||||
* Tailwind CSS v4 主题配置
|
||||
* 使用 @theme 指令定义主题变量
|
||||
*/
|
||||
@theme {
|
||||
/* 主色 - Teal */
|
||||
--color-primary-50: #f0f9f8;
|
||||
--color-primary-100: #e0f2f0;
|
||||
--color-primary-200: #bce6e1;
|
||||
--color-primary-300: #8dd4cc;
|
||||
--color-primary-400: #5ec2b7;
|
||||
--color-primary-500: #35786f;
|
||||
--color-primary-600: #2a605b;
|
||||
--color-primary-700: #1f4844;
|
||||
--color-primary-800: #183835;
|
||||
--color-primary-900: #122826;
|
||||
--color-primary-950: #0a1413;
|
||||
|
||||
/* 中性色 */
|
||||
--color-gray-50: #f9fafb;
|
||||
--color-gray-100: #f3f4f6;
|
||||
--color-gray-200: #e5e7eb;
|
||||
--color-gray-300: #d1d5db;
|
||||
--color-gray-400: #9ca3af;
|
||||
--color-gray-500: #6b7280;
|
||||
--color-gray-600: #4b5563;
|
||||
--color-gray-700: #374151;
|
||||
--color-gray-800: #1f2937;
|
||||
--color-gray-900: #111827;
|
||||
--color-gray-950: #030712;
|
||||
|
||||
/* 语义色 - Success */
|
||||
--color-success-50: #f0fdf4;
|
||||
--color-success-100: #dcfce7;
|
||||
--color-success-200: #bbf7d0;
|
||||
--color-success-300: #86efac;
|
||||
--color-success-400: #4ade80;
|
||||
--color-success-500: #22c55e;
|
||||
--color-success-600: #16a34a;
|
||||
--color-success-700: #15803d;
|
||||
--color-success-800: #166534;
|
||||
--color-success-900: #14532d;
|
||||
--color-success-950: #052e16;
|
||||
|
||||
/* 语义色 - Warning */
|
||||
--color-warning-50: #fffbeb;
|
||||
--color-warning-100: #fef3c7;
|
||||
--color-warning-200: #fde68a;
|
||||
--color-warning-300: #fcd34d;
|
||||
--color-warning-400: #fbbf24;
|
||||
--color-warning-500: #f59e0b;
|
||||
--color-warning-600: #d97706;
|
||||
--color-warning-700: #b45309;
|
||||
--color-warning-800: #92400e;
|
||||
--color-warning-900: #78350f;
|
||||
--color-warning-950: #451a03;
|
||||
|
||||
/* 语义色 - Error */
|
||||
--color-error-50: #fef2f2;
|
||||
--color-error-100: #fee2e2;
|
||||
--color-error-200: #fecaca;
|
||||
--color-error-300: #fca5a5;
|
||||
--color-error-400: #f87171;
|
||||
--color-error-500: #ef4444;
|
||||
--color-error-600: #dc2626;
|
||||
--color-error-700: #b91c1c;
|
||||
--color-error-800: #991b1b;
|
||||
--color-error-900: #7f1d1d;
|
||||
--color-error-950: #450a0a;
|
||||
|
||||
/* 语义色 - Info */
|
||||
--color-info-50: #eff6ff;
|
||||
--color-info-100: #dbeafe;
|
||||
--color-info-200: #bfdbfe;
|
||||
--color-info-300: #93c5fd;
|
||||
--color-info-400: #60a5fa;
|
||||
--color-info-500: #3b82f6;
|
||||
--color-info-600: #2563eb;
|
||||
--color-info-700: #1d4ed8;
|
||||
--color-info-800: #1e40af;
|
||||
--color-info-900: #1e3a8a;
|
||||
--color-info-950: #172554;
|
||||
|
||||
/* 圆角 - 更小的圆角 */
|
||||
--radius-xs: 0.125rem;
|
||||
--radius-sm: 0.25rem;
|
||||
--radius-md: 0.375rem;
|
||||
--radius-lg: 0.5rem;
|
||||
--radius-xl: 0.625rem;
|
||||
--radius-2xl: 0.75rem;
|
||||
--radius-3xl: 1rem;
|
||||
--radius-full: 9999px;
|
||||
}
|
||||
|
||||
/**
|
||||
* Design System CSS 变量
|
||||
*
|
||||
* 定义全局 CSS 变量用于主题切换和动态样式
|
||||
*/
|
||||
:root {
|
||||
--background: #ffffff;
|
||||
--foreground: #171717;
|
||||
/* 基础颜色 */
|
||||
--background: #ffffff;
|
||||
--foreground: #111827;
|
||||
--foreground-secondary: #4b5563;
|
||||
--foreground-tertiary: #6b7280;
|
||||
--foreground-disabled: #9ca3af;
|
||||
|
||||
/* 背景 */
|
||||
--background-secondary: #f3f4f6;
|
||||
--background-tertiary: #e5e7eb;
|
||||
|
||||
/* 边框 */
|
||||
--border: #d1d5db;
|
||||
--border-secondary: #e5e7eb;
|
||||
--border-focus: #35786f;
|
||||
|
||||
/* 圆角 - 更小的圆角 */
|
||||
--radius-xs: 0.125rem;
|
||||
--radius-sm: 0.25rem;
|
||||
--radius-md: 0.375rem;
|
||||
--radius-lg: 0.5rem;
|
||||
--radius-xl: 0.625rem;
|
||||
--radius-2xl: 0.75rem;
|
||||
--radius-3xl: 1rem;
|
||||
--radius-full: 9999px;
|
||||
|
||||
/* 阴影 */
|
||||
--shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
||||
--shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
|
||||
--shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
|
||||
--shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
|
||||
--shadow-2xl: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
|
||||
--shadow-primary: 0 4px 14px 0 rgba(53, 120, 111, 0.39);
|
||||
|
||||
/* 间距 */
|
||||
--spacing-xs: 0.25rem;
|
||||
--spacing-sm: 0.5rem;
|
||||
--spacing-md: 1rem;
|
||||
--spacing-lg: 1.5rem;
|
||||
--spacing-xl: 2rem;
|
||||
--spacing-2xl: 3rem;
|
||||
|
||||
/* 过渡 */
|
||||
--transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
|
||||
--transition-base: 250ms cubic-bezier(0.4, 0, 0.2, 1);
|
||||
--transition-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);
|
||||
}
|
||||
|
||||
@theme inline {
|
||||
--color-background: var(--background);
|
||||
--color-foreground: var(--foreground);
|
||||
--font-sans: var(--font-geist-sans);
|
||||
--font-mono: var(--font-geist-mono);
|
||||
/**
|
||||
* 全局基础样式
|
||||
*/
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
/* @media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--background: #0a0a0a;
|
||||
--foreground: #ededed;
|
||||
}
|
||||
} */
|
||||
html {
|
||||
height: 100%;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
body {
|
||||
background: var(--background);
|
||||
color: var(--foreground);
|
||||
font-family: var(--font-geist-sans), Arial, Helvetica, sans-serif;
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: var(--background);
|
||||
color: var(--foreground);
|
||||
font-family: var(--font-geist-sans), -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
|
||||
font-size: 1rem;
|
||||
line-height: 1.5;
|
||||
text-rendering: optimizeLegibility;
|
||||
}
|
||||
|
||||
.code-block {
|
||||
font-family: var(--font-geist-mono), monospace;
|
||||
/**
|
||||
* 代码块字体
|
||||
*/
|
||||
.code-block,
|
||||
code,
|
||||
kbd,
|
||||
pre,
|
||||
samp {
|
||||
font-family: var(--font-geist-mono), ui-monospace, SFMono-Regular, Monaco, Consolas, monospace;
|
||||
}
|
||||
|
||||
/**
|
||||
* 导航栏按钮样式
|
||||
*/
|
||||
.navbar-btn {
|
||||
@apply border-0 bg-transparent hover:bg-black/30 shadow-none;
|
||||
transition: background-color var(--transition-fast);
|
||||
}
|
||||
|
||||
/**
|
||||
* 焦点可见性优化
|
||||
*/
|
||||
:focus-visible {
|
||||
outline: 2px solid var(--border-focus);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择文本样式
|
||||
*/
|
||||
::selection {
|
||||
background-color: var(--color-primary-200);
|
||||
color: var(--color-primary-900);
|
||||
}
|
||||
|
||||
/**
|
||||
* 滚动条样式
|
||||
*/
|
||||
::-webkit-scrollbar {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-track {
|
||||
background: var(--background-secondary);
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb {
|
||||
background: var(--foreground-tertiary);
|
||||
border-radius: var(--radius-full);
|
||||
}
|
||||
|
||||
::-webkit-scrollbar-thumb:hover {
|
||||
background: var(--foreground-secondary);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ function LinkArea({ href, name, description, color }: LinkAreaProps) {
|
||||
<Link
|
||||
href={href}
|
||||
style={{ backgroundColor: color }}
|
||||
className={`h-32 md:h-64 flex md:justify-center items-center`}
|
||||
className={`hover:scale-105 transition-transform duration-200 h-32 md:h-64 flex md:justify-center items-center`}
|
||||
>
|
||||
<div className="text-white m-8">
|
||||
<h1 className="md:text-4xl text-3xl">{name}</h1>
|
||||
|
||||
@@ -1,49 +1,16 @@
|
||||
import Image from "next/image";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { PageHeader } from "@/components/ui/PageHeader";
|
||||
import { auth } from "@/auth";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { redirect } from "next/navigation";
|
||||
import { headers } from "next/headers";
|
||||
import { LogoutButton } from "./LogoutButton";
|
||||
|
||||
export default async function ProfilePage() {
|
||||
const t = await getTranslations("profile");
|
||||
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
|
||||
if (!session) {
|
||||
redirect("/auth?redirect=/profile");
|
||||
}
|
||||
|
||||
return (
|
||||
<PageLayout>
|
||||
<PageHeader title={t("myProfile")} />
|
||||
|
||||
{/* 用户信息区域 */}
|
||||
<div className="flex flex-col items-center gap-4">
|
||||
{/* 用户头像 */}
|
||||
{session.user.image && (
|
||||
<Image
|
||||
width={80}
|
||||
height={80}
|
||||
alt="User Avatar"
|
||||
src={session.user.image as string}
|
||||
className="rounded-full"
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 用户名和邮箱 */}
|
||||
<div className="text-center">
|
||||
<h2 className="text-xl font-semibold text-gray-800">
|
||||
{session.user.name}
|
||||
</h2>
|
||||
<p className="text-gray-600">{t("email", { email: session.user.email })}</p>
|
||||
</div>
|
||||
|
||||
{/* 登出按钮 */}
|
||||
<LogoutButton />
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
// 已登录,跳转到用户资料页面
|
||||
// 优先使用 username,如果没有则使用 email
|
||||
const username = (session.user.username as string) || (session.user.email as string);
|
||||
redirect(`/users/${username}`);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { LightButton } from "@/components/ui/buttons";
|
||||
import { LightButton } from "@/design-system/base/button";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useRouter } from "next/navigation";
|
||||
195
src/app/users/[username]/page.tsx
Normal file
195
src/app/users/[username]/page.tsx
Normal file
@@ -0,0 +1,195 @@
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import { PageLayout } from "@/components/ui/PageLayout";
|
||||
import { LinkButton } from "@/design-system/base/button";
|
||||
import { actionGetUserProfileByUsername } from "@/modules/auth/auth-action";
|
||||
import { repoGetFoldersWithTotalPairsByUserId } from "@/modules/folder/folder-repository";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { auth } from "@/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { LogoutButton } from "@/app/users/[username]/LogoutButton";
|
||||
|
||||
interface UserPageProps {
|
||||
params: Promise<{ username: string; }>;
|
||||
}
|
||||
|
||||
export default async function UserPage({ params }: UserPageProps) {
|
||||
const { username } = await params;
|
||||
const t = await getTranslations("user_profile");
|
||||
|
||||
// Get current session
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
|
||||
// Get user profile
|
||||
const result = await actionGetUserProfileByUsername({ username });
|
||||
|
||||
if (!result.success || !result.data) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
const user = result.data;
|
||||
|
||||
// Get user's folders
|
||||
const folders = await repoGetFoldersWithTotalPairsByUserId(user.id);
|
||||
|
||||
// Check if viewing own profile
|
||||
const isOwnProfile = session?.user?.username === username || session?.user?.email === username;
|
||||
|
||||
return (
|
||||
<PageLayout>
|
||||
{/* Header */}
|
||||
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div></div>
|
||||
{isOwnProfile && <LogoutButton />}
|
||||
</div>
|
||||
<div className="flex items-center space-x-6">
|
||||
{/* Avatar */}
|
||||
{user.image ? (
|
||||
<div className="relative w-24 h-24 rounded-full border-4 border-[#35786f] overflow-hidden">
|
||||
<Image
|
||||
src={user.image}
|
||||
alt={user.displayUsername || user.username || user.email}
|
||||
fill
|
||||
className="object-cover"
|
||||
unoptimized
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className="w-24 h-24 rounded-full bg-[#35786f] border-4 border-[#35786f] flex items-center justify-center">
|
||||
<span className="text-3xl font-bold text-white">
|
||||
{(user.displayUsername || user.username || user.email)[0].toUpperCase()}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* User Info */}
|
||||
<div className="flex-1">
|
||||
<h1 className="text-3xl font-bold text-gray-800 mb-2">
|
||||
{user.displayUsername || user.username || t("anonymous")}
|
||||
</h1>
|
||||
{user.username && (
|
||||
<p className="text-gray-600 text-sm mb-1">
|
||||
@{user.username}
|
||||
</p>
|
||||
)}
|
||||
<div className="flex items-center space-x-4 text-sm">
|
||||
<span className="text-gray-500">
|
||||
Joined: {new Date(user.createdAt).toLocaleDateString()}
|
||||
</span>
|
||||
{user.emailVerified && (
|
||||
<span className="flex items-center text-green-600">
|
||||
<svg className="w-4 h-4 mr-1" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 00016zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.293 12.293a1 1 0 101.414 1.414l2 2a1 1 0 001.414 0l4-4z" clipRule="evenodd" />
|
||||
</svg>
|
||||
Verified
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Email Section */}
|
||||
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
||||
<h2 className="text-xl font-semibold text-gray-800 mb-4">{t("email")}</h2>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center space-x-3">
|
||||
<span className="text-gray-700">{user.email}</span>
|
||||
</div>
|
||||
{user.emailVerified ? (
|
||||
<span className="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-800">
|
||||
✓ {t("verified")}
|
||||
</span>
|
||||
) : (
|
||||
<span className="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-800">
|
||||
{t("unverified")}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Account Info */}
|
||||
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
||||
<h2 className="text-xl font-semibold text-gray-800 mb-4">{t("accountInfo")}</h2>
|
||||
<dl className="grid grid-cols-1 gap-4 sm:grid-cols-2">
|
||||
<div>
|
||||
<dt className="text-sm font-medium text-gray-500">{t("userId")}</dt>
|
||||
<dd className="mt-1 text-sm text-gray-900 font-mono break-all">{user.id}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-sm font-medium text-gray-500">{t("username")}</dt>
|
||||
<dd className="mt-1 text-sm text-gray-900">
|
||||
{user.username || <span className="text-gray-400">{t("notSet")}</span>}
|
||||
</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-sm font-medium text-gray-500">{t("displayName")}</dt>
|
||||
<dd className="mt-1 text-sm text-gray-900">
|
||||
{user.displayUsername || <span className="text-gray-400">{t("notSet")}</span>}
|
||||
</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-sm font-medium text-gray-500">{t("memberSince")}</dt>
|
||||
<dd className="mt-1 text-sm text-gray-900">
|
||||
{new Date(user.createdAt).toLocaleDateString()}
|
||||
</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
{/* Folders Section */}
|
||||
<div className="bg-white rounded-lg shadow-md p-6">
|
||||
<h2 className="text-xl font-semibold text-gray-800 mb-4">{t("folders.title")}</h2>
|
||||
{folders.length === 0 ? (
|
||||
<p className="text-gray-500 text-center py-8">{t("folders.noFolders")}</p>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="min-w-full divide-y divide-gray-200">
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
{t("folders.folderName")}
|
||||
</th>
|
||||
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
{t("folders.totalPairs")}
|
||||
</th>
|
||||
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
{t("folders.createdAt")}
|
||||
</th>
|
||||
<th scope="col" className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
|
||||
{t("folders.actions")}
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="bg-white divide-y divide-gray-200">
|
||||
{folders.map((folder) => (
|
||||
<tr key={folder.id} className="hover:bg-gray-50">
|
||||
<td className="px-6 py-4 whitespace-nowrap">
|
||||
<div className="text-sm font-medium text-gray-900">{folder.name}</div>
|
||||
<div className="text-sm text-gray-500">ID: {folder.id}</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap">
|
||||
<div className="text-sm text-gray-900">{folder.total}</div>
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
|
||||
{new Date(folder.createdAt).toLocaleDateString()}
|
||||
</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
||||
<Link href={`/folders/${folder.id}`}>
|
||||
<LinkButton>
|
||||
{t("folders.view")}
|
||||
</LinkButton>
|
||||
</Link>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { betterAuth } from "better-auth";
|
||||
import { prismaAdapter } from "better-auth/adapters/prisma";
|
||||
import { nextCookies } from "better-auth/next-js";
|
||||
import { prisma } from "./lib/db";
|
||||
import { username } from "better-auth/plugins";
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: prismaAdapter(prisma, {
|
||||
@@ -16,5 +17,5 @@ export const auth = betterAuth({
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET as string
|
||||
},
|
||||
},
|
||||
plugins: [nextCookies()]
|
||||
plugins: [nextCookies(), username()]
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { GhostButton } from "./ui/buttons";
|
||||
import { GhostLightButton } from "@/design-system/base/button";
|
||||
import { useState } from "react";
|
||||
import { Languages } from "lucide-react";
|
||||
|
||||
@@ -15,59 +15,59 @@ export function LanguageSettings() {
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<Languages />
|
||||
<Languages onClick={handleLanguageClick} size={28} className="text-white hover:text-white/80" />
|
||||
<div className="relative">
|
||||
{showLanguageMenu && (
|
||||
<div>
|
||||
<div className="absolute top-10 right-0 rounded-md shadow-md flex flex-col gap-2">
|
||||
<GhostButton
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("en-US")}
|
||||
>
|
||||
English
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("zh-CN")}
|
||||
>
|
||||
中文
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("ja-JP")}
|
||||
>
|
||||
日本語
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("ko-KR")}
|
||||
>
|
||||
한국어
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("de-DE")}
|
||||
>
|
||||
Deutsch
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("fr-FR")}
|
||||
>
|
||||
Français
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("it-IT")}
|
||||
>
|
||||
Italiano
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="w-full bg-[#35786f]"
|
||||
onClick={() => setLocale("ug-CN")}
|
||||
>
|
||||
ئۇيغۇرچە
|
||||
</GhostButton>
|
||||
</GhostLightButton>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
@@ -1,11 +1,11 @@
|
||||
import Image from "next/image";
|
||||
import { IMAGES } from "@/config/images";
|
||||
import { Folder, Home, User } from "lucide-react";
|
||||
import { LanguageSettings } from "../LanguageSettings";
|
||||
import { LanguageSettings } from "./LanguageSettings";
|
||||
import { auth } from "@/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
import { GhostButton } from "../ui/buttons";
|
||||
import { GhostLightButton } from "@/design-system/base/button";
|
||||
|
||||
export async function Navbar() {
|
||||
const t = await getTranslations("navbar");
|
||||
@@ -15,16 +15,17 @@ export async function Navbar() {
|
||||
|
||||
return (
|
||||
<div className="flex justify-between items-center w-full h-16 px-4 md:px-8 bg-[#35786f] text-white">
|
||||
<GhostButton href="/" className="text-lg md:text-xl border-b hidden! md:block!">
|
||||
<GhostLightButton href="/" className="border-b hidden! md:block!" size="md">
|
||||
{t("title")}
|
||||
</GhostButton>
|
||||
<GhostButton className="block! md:hidden!" href={"/"}>
|
||||
</GhostLightButton>
|
||||
<GhostLightButton className="block! md:hidden!" size="md" href={"/"}>
|
||||
<Home size={20} />
|
||||
</GhostButton>
|
||||
<div className="flex text-base md:text-xl gap-0.5 justify-center items-center flex-wrap">
|
||||
</GhostLightButton>
|
||||
<div className="flex gap-0.5 justify-center items-center flex-wrap">
|
||||
<LanguageSettings />
|
||||
<GhostButton
|
||||
className="md:hidden! block! border-0 bg-transparent hover:bg-black/30 shadow-none p-2"
|
||||
<GhostLightButton
|
||||
className="md:hidden! block!"
|
||||
size="md"
|
||||
href="https://github.com/GoddoNebianU/learn-languages"
|
||||
>
|
||||
<Image
|
||||
@@ -33,35 +34,35 @@ export async function Navbar() {
|
||||
width={20}
|
||||
height={20}
|
||||
/>
|
||||
</GhostButton>
|
||||
<GhostButton href="/folders" className="md:block! hidden! border-0 bg-transparent hover:bg-black/30 shadow-none">
|
||||
</GhostLightButton>
|
||||
<GhostLightButton href="/folders" className="md:block! hidden!" size="md">
|
||||
{t("folders")}
|
||||
</GhostButton>
|
||||
<GhostButton href="/folders" className="md:hidden! block! border-0 bg-transparent hover:bg-black/30 shadow-none p-2">
|
||||
</GhostLightButton>
|
||||
<GhostLightButton href="/folders" className="md:hidden! block!" size="md">
|
||||
<Folder size={20} />
|
||||
</GhostButton>
|
||||
<GhostButton
|
||||
className="hidden! md:block! border-0 bg-transparent hover:bg-black/30 shadow-none"
|
||||
</GhostLightButton>
|
||||
<GhostLightButton
|
||||
className="hidden! md:block!"
|
||||
size="md"
|
||||
href="https://github.com/GoddoNebianU/learn-languages"
|
||||
>
|
||||
{t("sourceCode")}
|
||||
</GhostButton>
|
||||
</GhostLightButton>
|
||||
{
|
||||
(() => {
|
||||
return session &&
|
||||
<>
|
||||
<GhostButton href="/profile" className="hidden! md:block! text-sm md:text-base border-0 bg-transparent hover:bg-black/30 shadow-none px-2 py-1">{t("profile")}</GhostButton>
|
||||
<GhostButton href="/profile" className="md:hidden! block! border-0 bg-transparent hover:bg-black/30 shadow-none p-2">
|
||||
<GhostLightButton href="/profile" className="hidden! md:block!" size="md">{t("profile")}</GhostLightButton>
|
||||
<GhostLightButton href="/profile" className="md:hidden! block!" size="md">
|
||||
<User size={20} />
|
||||
</GhostButton>
|
||||
</GhostLightButton>
|
||||
</>
|
||||
|| <>
|
||||
<GhostButton href="/auth" className="hidden! md:block! text-sm md:text-base border-0 bg-transparent hover:bg-black/30 shadow-none px-2 py-1">{t("sign_in")}</GhostButton>
|
||||
<GhostButton href="/auth" className="md:hidden! block! border-0 bg-transparent hover:bg-black/30 shadow-none p-2">
|
||||
<GhostLightButton href="/auth" className="hidden! md:block!" size="md">{t("sign_in")}</GhostLightButton>
|
||||
<GhostLightButton href="/auth" className="md:hidden! block!" size="md">
|
||||
<User size={20} />
|
||||
</GhostButton>
|
||||
</GhostLightButton>
|
||||
</>;
|
||||
|
||||
})()
|
||||
}
|
||||
</div>
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import Link from "next/link";
|
||||
import Image from "next/image";
|
||||
import { COLORS } from "@/lib/theme/colors";
|
||||
|
||||
export type ButtonVariant = "primary" | "secondary" | "ghost" | "icon";
|
||||
export type ButtonSize = "sm" | "md" | "lg";
|
||||
|
||||
export interface ButtonProps {
|
||||
// Content
|
||||
children?: React.ReactNode;
|
||||
|
||||
// Behavior
|
||||
onClick?: () => void;
|
||||
disabled?: boolean;
|
||||
type?: "button" | "submit" | "reset";
|
||||
|
||||
// Styling
|
||||
variant?: ButtonVariant;
|
||||
size?: ButtonSize;
|
||||
className?: string;
|
||||
selected?: boolean;
|
||||
style?: React.CSSProperties;
|
||||
|
||||
// Icons
|
||||
leftIcon?: React.ReactNode;
|
||||
rightIcon?: React.ReactNode;
|
||||
iconSrc?: string; // For Next.js Image icons
|
||||
iconAlt?: string;
|
||||
|
||||
// Navigation
|
||||
href?: string;
|
||||
}
|
||||
|
||||
export function Button({
|
||||
variant = "secondary",
|
||||
size = "md",
|
||||
selected = false,
|
||||
href,
|
||||
iconSrc,
|
||||
iconAlt,
|
||||
leftIcon,
|
||||
rightIcon,
|
||||
children,
|
||||
className = "",
|
||||
style,
|
||||
type = "button",
|
||||
disabled = false,
|
||||
...props
|
||||
}: ButtonProps) {
|
||||
// Base classes
|
||||
const baseClasses = "inline-flex items-center justify-center gap-2 rounded font-bold shadow hover:cursor-pointer transition-colors";
|
||||
|
||||
// Variant-specific classes
|
||||
const variantStyles: Record<ButtonVariant, string> = {
|
||||
primary: `
|
||||
text-white
|
||||
hover:opacity-90
|
||||
`,
|
||||
secondary: `
|
||||
text-black
|
||||
hover:bg-gray-100
|
||||
`,
|
||||
ghost: `
|
||||
hover:bg-black/30
|
||||
p-2
|
||||
`,
|
||||
icon: `
|
||||
p-2 bg-gray-200 rounded-full
|
||||
hover:bg-gray-300
|
||||
`
|
||||
};
|
||||
|
||||
// Size-specific classes
|
||||
const sizeStyles: Record<ButtonSize, string> = {
|
||||
sm: "px-3 py-1 text-sm",
|
||||
md: "px-4 py-2",
|
||||
lg: "px-6 py-3 text-lg"
|
||||
};
|
||||
|
||||
const variantClass = variantStyles[variant];
|
||||
const sizeClass = sizeStyles[size];
|
||||
|
||||
// Selected state for secondary variant
|
||||
const selectedClass = variant === "secondary" && selected ? "bg-gray-100" : "";
|
||||
|
||||
// Background color for primary variant
|
||||
const backgroundColor = variant === "primary" ? '#35786f' : undefined;
|
||||
|
||||
// Combine all classes
|
||||
const combinedClasses = `
|
||||
${baseClasses}
|
||||
${variantClass}
|
||||
${sizeClass}
|
||||
${selectedClass}
|
||||
${disabled ? 'opacity-50 cursor-not-allowed' : ''}
|
||||
${className}
|
||||
`.trim().replace(/\s+/g, " ");
|
||||
|
||||
// Icon rendering helper for SVG icons
|
||||
const renderSvgIcon = (icon: React.ReactNode, position: "left" | "right") => {
|
||||
if (!icon) return null;
|
||||
return (
|
||||
<span className={`flex items-center ${position === "left" ? "-ml-1 mr-2" : "-mr-1 ml-2"}`}>
|
||||
{icon}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
// Image icon rendering for Next.js Image
|
||||
const renderImageIcon = () => {
|
||||
if (!iconSrc) return null;
|
||||
const sizeMap = { sm: 16, md: 20, lg: 24 };
|
||||
const imgSize = sizeMap[size] || 20;
|
||||
|
||||
return (
|
||||
<Image
|
||||
src={iconSrc}
|
||||
width={imgSize}
|
||||
height={imgSize}
|
||||
alt={iconAlt || "icon"}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
// Content assembly
|
||||
const content = (
|
||||
<>
|
||||
{renderImageIcon()}
|
||||
{renderSvgIcon(leftIcon, "left")}
|
||||
{children}
|
||||
{renderSvgIcon(rightIcon, "right")}
|
||||
</>
|
||||
);
|
||||
|
||||
// If href is provided, render as Link
|
||||
if (href) {
|
||||
return (
|
||||
<Link
|
||||
href={href}
|
||||
className={combinedClasses}
|
||||
style={{ ...style, backgroundColor }}
|
||||
>
|
||||
{content}
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
// Otherwise render as button
|
||||
return (
|
||||
<button
|
||||
type={type}
|
||||
disabled={disabled}
|
||||
className={combinedClasses}
|
||||
style={{ ...style, backgroundColor }}
|
||||
{...props}
|
||||
>
|
||||
{content}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -1,30 +1,21 @@
|
||||
/**
|
||||
* CardList - 可滚动的卡片列表容器
|
||||
*
|
||||
* 用于显示可滚动的列表内容,如文件夹列表、文本对列表等
|
||||
* - 最大高度 96 (24rem)
|
||||
* - 垂直滚动
|
||||
* - 圆角边框
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* <CardList>
|
||||
* {items.map(item => (
|
||||
* <div key={item.id}>{item.name}</div>
|
||||
* ))}
|
||||
* </CardList>
|
||||
* ```
|
||||
* 使用 Design System 重写的卡片列表组件
|
||||
*/
|
||||
import { VStack } from "@/design-system/layout/stack";
|
||||
|
||||
interface CardListProps {
|
||||
children: React.ReactNode;
|
||||
/** 额外的 CSS 类名 */
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function CardList({ children, className = "" }: CardListProps) {
|
||||
return (
|
||||
<div className={`max-h-96 overflow-y-auto rounded-xl border border-gray-200 overflow-hidden ${className}`}>
|
||||
{children}
|
||||
<div className={`max-h-96 overflow-y-auto rounded-lg border-2 border-gray-200 ${className}`}>
|
||||
<VStack gap={0}>
|
||||
{children}
|
||||
</VStack>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
/**
|
||||
* Container - 容器组件
|
||||
*
|
||||
* 使用 Design System 重写的容器组件
|
||||
*/
|
||||
import { Container as DSContainer } from "@/design-system/layout/container";
|
||||
import { Card } from "@/design-system/base/card";
|
||||
|
||||
interface ContainerProps {
|
||||
children?: React.ReactNode;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function Container({ children, className }: ContainerProps) {
|
||||
export function Container({ children, className = "" }: ContainerProps) {
|
||||
return (
|
||||
<div
|
||||
className={`w-full max-w-2xl mx-auto bg-white border border-gray-200 rounded-2xl ${className}`}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
<DSContainer size="2xl" className={`mx-auto ${className}`}>
|
||||
<Card variant="bordered" padding="md">
|
||||
{children}
|
||||
</Card>
|
||||
</DSContainer>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
interface Props {
|
||||
ref?: React.Ref<HTMLInputElement>;
|
||||
placeholder?: string;
|
||||
type?: string;
|
||||
className?: string;
|
||||
name?: string;
|
||||
defaultValue?: string;
|
||||
}
|
||||
|
||||
export function Input({
|
||||
ref,
|
||||
placeholder = "",
|
||||
type = "text",
|
||||
className = "",
|
||||
name = "",
|
||||
defaultValue = "",
|
||||
}: Props) {
|
||||
return (
|
||||
<input
|
||||
ref={ref}
|
||||
placeholder={placeholder}
|
||||
type={type}
|
||||
className={`block focus:outline-none border-b-2 border-gray-600 ${className}`}
|
||||
name={name}
|
||||
defaultValue={defaultValue}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,13 @@
|
||||
/**
|
||||
* LocaleSelector - 语言选择器组件
|
||||
*
|
||||
* 使用 Design System 重写的语言选择器组件
|
||||
*/
|
||||
import { useTranslations } from "next-intl";
|
||||
import { useState } from "react";
|
||||
import { Input } from "@/design-system/base/input";
|
||||
import { Select } from "@/design-system/base/select";
|
||||
import { VStack } from "@/design-system/layout/stack";
|
||||
|
||||
const COMMON_LANGUAGES = [
|
||||
{ label: "chinese", value: "chinese" },
|
||||
@@ -36,7 +44,8 @@ export function LocaleSelector({ value, onChange }: LocaleSelectorProps) {
|
||||
};
|
||||
|
||||
// 当选择常见语言或"其他"时
|
||||
const handleSelectChange = (selectedValue: string) => {
|
||||
const handleSelectChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||
const selectedValue = e.target.value;
|
||||
if (selectedValue === "other") {
|
||||
setCustomInput("");
|
||||
onChange("other");
|
||||
@@ -46,27 +55,26 @@ export function LocaleSelector({ value, onChange }: LocaleSelectorProps) {
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<select
|
||||
<VStack gap={2}>
|
||||
<Select
|
||||
value={isCommonLanguage ? value : "other"}
|
||||
onChange={(e) => handleSelectChange(e.target.value)}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-[#35786f]"
|
||||
onChange={handleSelectChange}
|
||||
>
|
||||
{COMMON_LANGUAGES.map((lang) => (
|
||||
<option key={lang.value} value={lang.value}>
|
||||
{t(`translator.${lang.label}`)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</Select>
|
||||
{showCustomInput && (
|
||||
<input
|
||||
<Input
|
||||
type="text"
|
||||
value={inputValue}
|
||||
onChange={(e) => handleCustomInputChange(e.target.value)}
|
||||
placeholder={t("folder_id.enterLanguageName")}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-[#35786f] mt-2"
|
||||
variant="bordered"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</VStack>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,29 +1,25 @@
|
||||
/**
|
||||
* PageHeader - 页面标题组件
|
||||
*
|
||||
* 用于 PageLayout 内的页面标题,支持主标题和可选的副标题
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* <PageHeader title="我的文件夹" subtitle="管理和组织你的学习内容" />
|
||||
* ```
|
||||
* 使用 Design System 重写的页面标题组件
|
||||
*/
|
||||
import { VStack } from "@/design-system/layout/stack";
|
||||
|
||||
interface PageHeaderProps {
|
||||
/** 页面主标题 */
|
||||
title: string;
|
||||
/** 可选的副标题/描述 */
|
||||
subtitle?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function PageHeader({ title, subtitle }: PageHeaderProps) {
|
||||
export function PageHeader({ title, subtitle, className = "" }: PageHeaderProps) {
|
||||
return (
|
||||
<div className="mb-6">
|
||||
<h1 className="text-2xl md:text-3xl font-bold text-gray-800 mb-2">
|
||||
<VStack gap={2} className={`mb-6 ${className}`}>
|
||||
<h1 className="text-2xl md:text-3xl font-bold text-gray-800">
|
||||
{title}
|
||||
</h1>
|
||||
{subtitle && (
|
||||
<p className="text-sm text-gray-500">{subtitle}</p>
|
||||
)}
|
||||
</div>
|
||||
</VStack>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,33 +1,64 @@
|
||||
/**
|
||||
* PageLayout - 统一的页面布局组件
|
||||
* PageLayout - 页面布局组件
|
||||
*
|
||||
* 提供应用统一的标准页面布局:
|
||||
* - 绿色背景 (#35786f)
|
||||
* - 居中的白色圆角卡片
|
||||
* - 响应式内边距
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* <PageLayout>
|
||||
* <PageHeader title="标题" subtitle="副标题" />
|
||||
* <div>页面内容</div>
|
||||
* </PageLayout>
|
||||
* ```
|
||||
* 使用 Design System 重写的页面布局组件
|
||||
*/
|
||||
import { Card } from "@/design-system/base/card";
|
||||
import { Container } from "@/design-system/layout/container";
|
||||
|
||||
type PageLayoutVariant = "centered-card" | "full-width" | "fullscreen";
|
||||
|
||||
interface PageLayoutProps {
|
||||
children: React.ReactNode;
|
||||
/** 额外的 CSS 类名,用于自定义布局行为 */
|
||||
className?: string;
|
||||
variant?: PageLayoutVariant;
|
||||
align?: "center" | "start" | "end";
|
||||
}
|
||||
|
||||
export function PageLayout({ children, className = "" }: PageLayoutProps) {
|
||||
return (
|
||||
<div className={`min-h-[calc(100vh-64px)] bg-[#35786f] flex items-center justify-center px-4 py-8 ${className}`}>
|
||||
<div className="w-full max-w-2xl">
|
||||
<div className="bg-white rounded-2xl shadow-xl p-6 md:p-8">
|
||||
{children}
|
||||
const alignClasses = {
|
||||
center: "items-center",
|
||||
start: "items-start",
|
||||
end: "items-end",
|
||||
};
|
||||
|
||||
export function PageLayout({
|
||||
children,
|
||||
className = "",
|
||||
variant = "centered-card",
|
||||
align = "center",
|
||||
}: PageLayoutProps) {
|
||||
// 居中卡片布局
|
||||
if (variant === "centered-card") {
|
||||
return (
|
||||
<div className={`min-h-[calc(100vh-64px)] bg-primary-500 flex ${alignClasses[align]} justify-center px-4 py-8 ${className}`}>
|
||||
<div className="w-full max-w-2xl">
|
||||
<Card padding="lg" className="p-6 md:p-8">
|
||||
{children}
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
// 全宽布局
|
||||
if (variant === "full-width") {
|
||||
return (
|
||||
<div className={`min-h-[calc(100vh-64px)] bg-primary-500 px-4 py-8 ${className}`}>
|
||||
<Container size="2xl">
|
||||
{children}
|
||||
</Container>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// 全屏布局
|
||||
if (variant === "fullscreen") {
|
||||
return (
|
||||
<div className={`min-h-[calc(100vh-64px)] bg-primary-500 ${className}`}>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
45
src/components/ui/RangeInput.tsx
Normal file
45
src/components/ui/RangeInput.tsx
Normal file
@@ -0,0 +1,45 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
|
||||
interface RangeInputProps {
|
||||
value: number;
|
||||
max: number;
|
||||
onChange: (value: number) => void;
|
||||
disabled?: boolean;
|
||||
className?: string;
|
||||
min?: number;
|
||||
}
|
||||
|
||||
export function RangeInput({
|
||||
value,
|
||||
max,
|
||||
onChange,
|
||||
disabled = false,
|
||||
className = "",
|
||||
min = 0,
|
||||
}: RangeInputProps) {
|
||||
const handleChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const newValue = parseInt(event.target.value);
|
||||
onChange(newValue);
|
||||
}, [onChange]);
|
||||
|
||||
const progressPercentage = ((value - min) / (max - min)) * 100;
|
||||
|
||||
return (
|
||||
<input
|
||||
type="range"
|
||||
min={min}
|
||||
max={max}
|
||||
value={value}
|
||||
onChange={handleChange}
|
||||
disabled={disabled}
|
||||
className={`w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer focus:outline-none focus:ring-2 focus:ring-primary-500 ${
|
||||
disabled ? "opacity-50 cursor-not-allowed" : ""
|
||||
} ${className}`}
|
||||
style={{
|
||||
background: `linear-gradient(to right, #374151 0%, #374151 ${progressPercentage}%, #e5e7eb ${progressPercentage}%, #e5e7eb 100%)`
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
// 向后兼容的按钮组件包装器
|
||||
// 这些组件将新 Button 组件包装,以保持向后兼容
|
||||
|
||||
import { Button } from "../Button";
|
||||
|
||||
// LightButton: 次要按钮,支持 selected 状态
|
||||
export const LightButton = (props: any) => <Button variant="secondary" {...props} />;
|
||||
|
||||
// GreenButton: 主题色主要按钮
|
||||
export const GreenButton = (props: any) => <Button variant="primary" {...props} />;
|
||||
|
||||
// IconButton: SVG 图标按钮
|
||||
export const IconButton = (props: any) => {
|
||||
const { icon, ...rest } = props;
|
||||
return <Button variant="icon" leftIcon={icon} {...rest} />;
|
||||
};
|
||||
|
||||
// GhostButton: 透明导航按钮
|
||||
export const GhostButton = (props: any) => {
|
||||
const { className, children, ...rest } = props;
|
||||
return (
|
||||
<Button variant="ghost" className={className} {...rest}>
|
||||
{children}
|
||||
</Button>
|
||||
);
|
||||
};
|
||||
|
||||
// IconClick: 图片图标按钮
|
||||
export const IconClick = (props: any) => {
|
||||
// IconClick 使用 src/alt 属性,需要映射到 Button 的 iconSrc/iconAlt
|
||||
const { src, alt, size, disableOnHoverBgChange, className, ...rest } = props;
|
||||
let buttonSize: "sm" | "md" | "lg" = "md";
|
||||
if (typeof size === "number") {
|
||||
if (size <= 20) buttonSize = "sm";
|
||||
else if (size >= 32) buttonSize = "lg";
|
||||
} else if (typeof size === "string") {
|
||||
buttonSize = (size === "sm" || size === "md" || size === "lg") ? size : "md";
|
||||
}
|
||||
|
||||
// 如果禁用悬停背景变化,通过 className 覆盖
|
||||
const hoverClass = disableOnHoverBgChange ? "hover:bg-black/30 hover:cursor-pointer border-0 bg-transparent shadow-none" : "";
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="icon"
|
||||
iconSrc={src}
|
||||
iconAlt={alt}
|
||||
size={buttonSize}
|
||||
className={`${hoverClass} ${className || ""}`}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
// PlainButton: 基础小按钮
|
||||
export const PlainButton = (props: any) => <Button variant="secondary" size="sm" {...props} />;
|
||||
37
src/components/ui/index.ts
Normal file
37
src/components/ui/index.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
// 统一的 UI 组件导出
|
||||
// 可以从 '@/components/ui' 导入所有组件
|
||||
|
||||
// Design System 组件(向后兼容)
|
||||
export { Input, type InputVariant, type InputProps } from '@/design-system/base/input';
|
||||
export { Select, type SelectVariant, type SelectSize, type SelectProps } from '@/design-system/base/select';
|
||||
export { Textarea, type TextareaVariant, type TextareaProps } from '@/design-system/base/textarea';
|
||||
export { Card, type CardVariant, type CardPadding, type CardProps } from '@/design-system/base/card';
|
||||
export {
|
||||
Button,
|
||||
PrimaryButton,
|
||||
SecondaryButton,
|
||||
LightButton,
|
||||
SuccessButton,
|
||||
WarningButton,
|
||||
ErrorButton,
|
||||
GhostButton,
|
||||
GhostLightButton,
|
||||
OutlineButton,
|
||||
LinkButton,
|
||||
IconButton,
|
||||
IconClick,
|
||||
CircleButton,
|
||||
CircleToggleButton,
|
||||
DashedButton,
|
||||
type ButtonVariant,
|
||||
type ButtonSize,
|
||||
type ButtonProps
|
||||
} from '@/design-system/base/button';
|
||||
|
||||
// 业务特定组件
|
||||
export { RangeInput } from './RangeInput';
|
||||
export { Container } from './Container';
|
||||
export { PageLayout } from './PageLayout';
|
||||
export { PageHeader } from './PageHeader';
|
||||
export { CardList } from './CardList';
|
||||
export { LocaleSelector } from './LocaleSelector';
|
||||
585
src/design-system/README.md
Normal file
585
src/design-system/README.md
Normal file
@@ -0,0 +1,585 @@
|
||||
# Design System
|
||||
|
||||
完整的设计系统,提供可复用的 UI 组件和设计令牌,确保整个应用的一致性。
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
src/design-system/
|
||||
├── tokens/ # 设计令牌(颜色、间距、字体等)
|
||||
├── lib/ # 工具函数
|
||||
├── base/ # 基础组件
|
||||
│ ├── button/
|
||||
│ ├── input/
|
||||
│ ├── textarea/
|
||||
│ ├── card/
|
||||
│ ├── checkbox/
|
||||
│ ├── radio/
|
||||
│ ├── switch/
|
||||
│ └── select/
|
||||
├── feedback/ # 反馈组件
|
||||
│ ├── alert/
|
||||
│ ├── progress/
|
||||
│ ├── skeleton/
|
||||
│ └── toast/
|
||||
├── overlay/ # 覆盖组件
|
||||
│ └── modal/
|
||||
├── data-display/ # 数据展示组件
|
||||
│ ├── badge/
|
||||
│ └── divider/
|
||||
├── layout/ # 布局组件
|
||||
│ ├── container/
|
||||
│ ├── grid/
|
||||
│ └── stack/
|
||||
├── navigation/ # 导航组件
|
||||
│ └── tabs/
|
||||
└── index.ts # 统一导出
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 安装依赖
|
||||
|
||||
```bash
|
||||
pnpm add class-variance-authority clsx tailwind-merge
|
||||
```
|
||||
|
||||
### 导入组件
|
||||
|
||||
```tsx
|
||||
// 方式 1: 从主入口导入(简单但 tree-shaking 较差)
|
||||
import { Button, Input, Card } from '@/design-system';
|
||||
|
||||
// 方式 2: 从子路径导入(更好的 tree-shaking)
|
||||
import { Button } from '@/design-system/base/button';
|
||||
import { Input } from '@/design-system/base/input';
|
||||
import { Card } from '@/design-system/base/card';
|
||||
```
|
||||
|
||||
### 使用组件
|
||||
|
||||
```tsx
|
||||
import { Button, Card } from '@/design-system';
|
||||
|
||||
export function MyComponent() {
|
||||
return (
|
||||
<Card>
|
||||
<h1>标题</h1>
|
||||
<p>内容</p>
|
||||
<Button variant="primary">点击我</Button>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## 组件列表
|
||||
|
||||
### 基础组件
|
||||
|
||||
| 组件 | 说明 | 状态 |
|
||||
|------|------|------|
|
||||
| [Button](#button) | 按钮 | ✅ |
|
||||
| [Input](#input) | 输入框 | ✅ |
|
||||
| [Textarea](#textarea) | 多行文本输入 | ✅ |
|
||||
| [Card](#card) | 卡片容器 | ✅ |
|
||||
| [Checkbox](#checkbox) | 复选框 | ✅ |
|
||||
| [Radio](#radio) | 单选按钮 | ✅ |
|
||||
| [Switch](#switch) | 开关 | ✅ |
|
||||
| [Select](#select) | 下拉选择框 | ✅ |
|
||||
|
||||
### 反馈组件
|
||||
|
||||
| 组件 | 说明 | 状态 |
|
||||
|------|------|------|
|
||||
| [Alert](#alert) | 警告提示 | ✅ |
|
||||
| [Progress](#progress) | 进度条 | ✅ |
|
||||
| [Skeleton](#skeleton) | 骨架屏 | ✅ |
|
||||
| [Toast](#toast) | 通知提示 | ✅ |
|
||||
|
||||
### 覆盖组件
|
||||
|
||||
| 组件 | 说明 | 状态 |
|
||||
|------|------|------|
|
||||
| [Modal](#modal) | 模态框 | ✅ |
|
||||
|
||||
### 数据展示组件
|
||||
|
||||
| 组件 | 说明 | 状态 |
|
||||
|------|------|------|
|
||||
| [Badge](#badge) | 徽章 | ✅ |
|
||||
| [Divider](#divider) | 分隔线 | ✅ |
|
||||
|
||||
### 布局组件
|
||||
|
||||
| 组件 | 说明 | 状态 |
|
||||
|------|------|------|
|
||||
| [Container](#container) | 容器 | ✅ |
|
||||
| [Grid](#grid) | 网格布局 | ✅ |
|
||||
| [Stack](#stack) | 堆叠布局 | ✅ |
|
||||
|
||||
### 导航组件
|
||||
|
||||
| 组件 | 说明 | 状态 |
|
||||
|------|------|------|
|
||||
| [Tabs](#tabs) | 标签页 | ✅ |
|
||||
|
||||
## 组件 API
|
||||
|
||||
### Button
|
||||
|
||||
按钮组件,支持多种变体和尺寸。
|
||||
|
||||
```tsx
|
||||
import { Button } from '@/design-system';
|
||||
|
||||
<Button variant="primary" size="md" onClick={handleClick}>
|
||||
点击我
|
||||
</Button>
|
||||
```
|
||||
|
||||
**变体 (variant)**: `primary` | `secondary` | `success` | `warning` | `error` | `ghost` | `outline` | `link`
|
||||
|
||||
**尺寸 (size)**: `sm` | `md` | `lg`
|
||||
|
||||
**快捷组件**: `PrimaryButton`, `SecondaryButton`, `SuccessButton`, `WarningButton`, `ErrorButton`, `GhostButton`, `OutlineButton`, `LinkButton`
|
||||
|
||||
### Input
|
||||
|
||||
输入框组件。
|
||||
|
||||
```tsx
|
||||
import { Input } from '@/design-system';
|
||||
|
||||
<Input
|
||||
variant="bordered"
|
||||
placeholder="请输入内容"
|
||||
error={hasError}
|
||||
/>
|
||||
```
|
||||
|
||||
**变体 (variant)**: `default` | `bordered` | `filled` | `search`
|
||||
|
||||
**尺寸 (size)**: `sm` | `md` | `lg`
|
||||
|
||||
### Textarea
|
||||
|
||||
多行文本输入组件。
|
||||
|
||||
```tsx
|
||||
import { Textarea } from '@/design-system';
|
||||
|
||||
<Textarea
|
||||
variant="bordered"
|
||||
placeholder="请输入内容"
|
||||
rows={4}
|
||||
/>
|
||||
```
|
||||
|
||||
**变体 (variant)**: `default` | `bordered` | `filled`
|
||||
|
||||
### Card
|
||||
|
||||
卡片容器组件。
|
||||
|
||||
```tsx
|
||||
import { Card, CardHeader, CardTitle, CardBody, CardFooter } from '@/design-system';
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>标题</CardTitle>
|
||||
</CardHeader>
|
||||
<CardBody>
|
||||
<p>内容</p>
|
||||
</CardBody>
|
||||
<CardFooter>
|
||||
<Button>确定</Button>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
```
|
||||
|
||||
**变体 (variant)**: `default` | `bordered` | `elevated` | `flat`
|
||||
|
||||
**内边距 (padding)**: `none` | `xs` | `sm` | `md` | `lg` | `xl`
|
||||
|
||||
### Checkbox
|
||||
|
||||
复选框组件。
|
||||
|
||||
```tsx
|
||||
import { Checkbox } from '@/design-system';
|
||||
|
||||
<Checkbox checked={checked} onChange={setChecked}>
|
||||
同意条款
|
||||
</Checkbox>
|
||||
```
|
||||
|
||||
### Radio
|
||||
|
||||
单选按钮组件。
|
||||
|
||||
```tsx
|
||||
import { Radio, RadioGroup } from '@/design-system';
|
||||
|
||||
<RadioGroup name="choice" value={value} onChange={setValue}>
|
||||
<Radio value="1">选项 1</Radio>
|
||||
<Radio value="2">选项 2</Radio>
|
||||
</RadioGroup>
|
||||
```
|
||||
|
||||
### Switch
|
||||
|
||||
开关组件。
|
||||
|
||||
```tsx
|
||||
import { Switch } from '@/design-system';
|
||||
|
||||
<Switch checked={enabled} onChange={setEnabled} />
|
||||
```
|
||||
|
||||
### Alert
|
||||
|
||||
警告提示组件。
|
||||
|
||||
```tsx
|
||||
import { Alert } from '@/design-system';
|
||||
|
||||
<Alert variant="success" title="成功">
|
||||
操作成功完成
|
||||
</Alert>
|
||||
```
|
||||
|
||||
**变体 (variant)**: `info` | `success` | `warning` | `error`
|
||||
|
||||
### Progress
|
||||
|
||||
进度条组件。
|
||||
|
||||
```tsx
|
||||
import { Progress } from '@/design-system';
|
||||
|
||||
<Progress value={60} showLabel />
|
||||
```
|
||||
|
||||
### Skeleton
|
||||
|
||||
骨架屏组件。
|
||||
|
||||
```tsx
|
||||
import { Skeleton, TextSkeleton, CardSkeleton } from '@/design-system';
|
||||
|
||||
<Skeleton className="h-4 w-32" />
|
||||
<TextSkeleton lines={3} />
|
||||
<CardSkeleton />
|
||||
```
|
||||
|
||||
### Toast
|
||||
|
||||
通知提示组件(基于 sonner)。
|
||||
|
||||
```tsx
|
||||
import { toast } from '@/design-system';
|
||||
|
||||
toast.success("操作成功!");
|
||||
toast.error("发生错误");
|
||||
toast.promise(promise, {
|
||||
loading: "加载中...",
|
||||
success: "加载成功",
|
||||
error: "加载失败",
|
||||
});
|
||||
```
|
||||
|
||||
### Modal
|
||||
|
||||
模态框组件。
|
||||
|
||||
```tsx
|
||||
import { Modal } from '@/design-system';
|
||||
|
||||
<Modal open={open} onClose={() => setOpen(false)}>
|
||||
<Modal.Header>
|
||||
<Modal.Title>标题</Modal.Title>
|
||||
</Modal.Header>
|
||||
<Modal.Body>
|
||||
<p>内容</p>
|
||||
</Modal.Body>
|
||||
<Modal.Footer>
|
||||
<Button variant="secondary" onClick={() => setOpen(false)}>
|
||||
取消
|
||||
</Button>
|
||||
<Button variant="primary">确定</Button>
|
||||
</Modal.Footer>
|
||||
</Modal>
|
||||
```
|
||||
|
||||
### Badge
|
||||
|
||||
徽章组件。
|
||||
|
||||
```tsx
|
||||
import { Badge } from '@/design-system';
|
||||
|
||||
<Badge variant="success">成功</Badge>
|
||||
<Badge dot />
|
||||
```
|
||||
|
||||
**变体 (variant)**: `default` | `primary` | `success` | `warning` | `error` | `info`
|
||||
|
||||
### Divider
|
||||
|
||||
分隔线组件。
|
||||
|
||||
```tsx
|
||||
import { Divider } from '@/design-system';
|
||||
|
||||
<Divider />
|
||||
<Divider>或者</Divider>
|
||||
<Divider orientation="vertical" />
|
||||
```
|
||||
|
||||
### Container
|
||||
|
||||
容器组件。
|
||||
|
||||
```tsx
|
||||
import { Container } from '@/design-system';
|
||||
|
||||
<Container size="lg" padding="xl">
|
||||
<p>内容</p>
|
||||
</Container>
|
||||
```
|
||||
|
||||
### Grid
|
||||
|
||||
网格布局组件。
|
||||
|
||||
```tsx
|
||||
import { Grid } from '@/design-system';
|
||||
|
||||
<Grid cols={3} gap={4}>
|
||||
<div>项目 1</div>
|
||||
<div>项目 2</div>
|
||||
<div>项目 3</div>
|
||||
</Grid>
|
||||
```
|
||||
|
||||
### Stack
|
||||
|
||||
堆叠布局组件。
|
||||
|
||||
```tsx
|
||||
import { Stack, VStack, HStack } from '@/design-system';
|
||||
|
||||
<VStack gap={4}>
|
||||
<div>项目 1</div>
|
||||
<div>项目 2</div>
|
||||
</VStack>
|
||||
```
|
||||
|
||||
### Tabs
|
||||
|
||||
标签页组件。
|
||||
|
||||
```tsx
|
||||
import { Tabs } from '@/design-system';
|
||||
|
||||
<Tabs value={activeTab} onValueChange={setActiveTab}>
|
||||
<Tabs.List>
|
||||
<Tabs.Trigger value="tab1">标签 1</Tabs.Trigger>
|
||||
<Tabs.Trigger value="tab2">标签 2</Tabs.Trigger>
|
||||
</Tabs.List>
|
||||
<Tabs.Content value="tab1">
|
||||
<p>内容 1</p>
|
||||
</Tabs.Content>
|
||||
<Tabs.Content value="tab2">
|
||||
<p>内容 2</p>
|
||||
</Tabs.Content>
|
||||
</Tabs>
|
||||
```
|
||||
|
||||
## 设计令牌
|
||||
|
||||
### 颜色
|
||||
|
||||
```tsx
|
||||
import { colors } from '@/design-system/tokens';
|
||||
|
||||
// 主色
|
||||
colors.primary.500 // #35786f
|
||||
|
||||
// 语义色
|
||||
colors.success.500 // #22c55e
|
||||
colors.warning.500 // #f59e0b
|
||||
colors.error.500 // #ef4444
|
||||
colors.info.500 // #3b82f6
|
||||
```
|
||||
|
||||
在组件中使用:
|
||||
|
||||
```tsx
|
||||
<div className="bg-primary-500 text-white">主色背景</div>
|
||||
<div className="text-success-600">成功文本</div>
|
||||
```
|
||||
|
||||
### 间距
|
||||
|
||||
基于 8pt 网格系统:
|
||||
|
||||
```tsx
|
||||
<div className="p-4"> // 16px
|
||||
<div className="p-6"> // 24px
|
||||
<div className="p-8"> // 32px
|
||||
```
|
||||
|
||||
### 字体
|
||||
|
||||
```tsx
|
||||
<div className="text-sm">小文本</div>
|
||||
<div className="text-base">正常文本</div>
|
||||
<div className="text-lg">大文本</div>
|
||||
<div className="font-semibold">半粗体</div>
|
||||
<div className="font-bold">粗体</div>
|
||||
```
|
||||
|
||||
### 圆角
|
||||
|
||||
```tsx
|
||||
<div className="rounded-lg"> // 8px
|
||||
<div className="rounded-xl"> // 12px
|
||||
<div className="rounded-2xl"> // 16px
|
||||
```
|
||||
|
||||
### 阴影
|
||||
|
||||
```tsx
|
||||
<div className="shadow-sm"> // 小阴影
|
||||
<div className="shadow-md"> // 中阴影
|
||||
<div className="shadow-lg"> // 大阴影
|
||||
<div className="shadow-xl"> // 超大阴影
|
||||
```
|
||||
|
||||
## 工具函数
|
||||
|
||||
### cn
|
||||
|
||||
合并 Tailwind CSS 类名的工具函数。
|
||||
|
||||
```tsx
|
||||
import { cn } from '@/design-system';
|
||||
|
||||
const className = cn(
|
||||
'base-class',
|
||||
isActive && 'active-class',
|
||||
'another-class'
|
||||
);
|
||||
```
|
||||
|
||||
## 最佳实践
|
||||
|
||||
### 1. 组件导入
|
||||
|
||||
对于更好的 tree-shaking,建议从子路径导入:
|
||||
|
||||
```tsx
|
||||
// ✅ 推荐
|
||||
import { Button } from '@/design-system/base/button';
|
||||
|
||||
// ❌ 不推荐(但也可以)
|
||||
import { Button } from '@/design-system';
|
||||
```
|
||||
|
||||
### 2. 样式覆盖
|
||||
|
||||
使用 `className` 属性覆盖样式:
|
||||
|
||||
```tsx
|
||||
<Button className="w-full">全宽按钮</Button>
|
||||
```
|
||||
|
||||
### 3. 组合组件
|
||||
|
||||
利用组件组合来构建复杂 UI:
|
||||
|
||||
```tsx
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>标题</CardTitle>
|
||||
</CardHeader>
|
||||
<CardBody>
|
||||
<VStack gap={4}>
|
||||
<Input placeholder="输入框" />
|
||||
<Button>提交</Button>
|
||||
</VStack>
|
||||
</CardBody>
|
||||
</Card>
|
||||
```
|
||||
|
||||
### 4. 可访问性
|
||||
|
||||
所有组件都内置了可访问性支持:
|
||||
|
||||
- 正确的 ARIA 属性
|
||||
- 键盘导航支持
|
||||
- 焦点管理
|
||||
- 屏幕阅读器友好
|
||||
|
||||
## 迁移指南
|
||||
|
||||
### 从旧组件迁移
|
||||
|
||||
旧的组件路径:
|
||||
|
||||
```tsx
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Input } from '@/components/ui/Input';
|
||||
```
|
||||
|
||||
新的组件路径:
|
||||
|
||||
```tsx
|
||||
import { Button } from '@/design-system/base/button';
|
||||
import { Input } from '@/design-system/base/input';
|
||||
```
|
||||
|
||||
### API 变化
|
||||
|
||||
大部分 API 保持兼容,但有以下变化:
|
||||
|
||||
1. **颜色不再使用硬编码值**
|
||||
```tsx
|
||||
// 旧
|
||||
style={{ backgroundColor: '#35786f' }}
|
||||
|
||||
// 新
|
||||
className="bg-primary-500"
|
||||
```
|
||||
|
||||
2. **变体命名更加一致**
|
||||
```tsx
|
||||
// 旧
|
||||
<Button variant="icon" />
|
||||
|
||||
// 新
|
||||
<Button variant="ghost" />
|
||||
```
|
||||
|
||||
3. **新增语义色变体**
|
||||
```tsx
|
||||
<Button variant="success">成功</Button>
|
||||
<Button variant="warning">警告</Button>
|
||||
<Button variant="error">错误</Button>
|
||||
```
|
||||
|
||||
## 贡献
|
||||
|
||||
添加新组件时,请遵循以下规范:
|
||||
|
||||
1. 在对应的目录下创建组件
|
||||
2. 使用 `cva` 定义变体样式
|
||||
3. 使用 `forwardRef` 支持 ref 转发
|
||||
4. 添加完整的 TypeScript 类型
|
||||
5. 编写详细的 JSDoc 注释和示例
|
||||
6. 在导出文件中添加导出
|
||||
|
||||
## 许可证
|
||||
|
||||
AGPL-3.0-only
|
||||
351
src/design-system/base/button/button.tsx
Normal file
351
src/design-system/base/button/button.tsx
Normal file
@@ -0,0 +1,351 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import Link from "next/link";
|
||||
import Image from "next/image";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Button 组件
|
||||
*
|
||||
* Design System 中的按钮组件,支持多种变体、尺寸和状态。
|
||||
* 自动处理 Link/button 切换,支持图标和加载状态。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // Primary 按钮
|
||||
* <Button variant="primary" onClick={handleClick}>
|
||||
* 点击我
|
||||
* </Button>
|
||||
*
|
||||
* // 带图标的按钮
|
||||
* <Button variant="secondary" leftIcon={<Icon />}>
|
||||
* 带图标
|
||||
* </Button>
|
||||
*
|
||||
* // 作为链接使用
|
||||
* <Button variant="primary" href="/path">
|
||||
* 链接按钮
|
||||
* </Button>
|
||||
*
|
||||
* // 加载状态
|
||||
* <Button variant="primary" loading>
|
||||
* 提交中...
|
||||
* </Button>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* 按钮变体样式
|
||||
*/
|
||||
const buttonVariants = cva(
|
||||
// 基础样式
|
||||
"inline-flex items-center justify-center gap-2 rounded-md font-semibold shadow transition-all duration-250 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
primary: "bg-primary-500 text-white hover:bg-primary-600 shadow-md",
|
||||
secondary: "bg-gray-100 text-gray-900 hover:bg-gray-200 shadow-sm",
|
||||
success: "bg-success-500 text-white hover:bg-success-600 shadow-md",
|
||||
warning: "bg-warning-500 text-white hover:bg-warning-600 shadow-md",
|
||||
error: "bg-error-500 text-white hover:bg-error-600 shadow-md",
|
||||
ghost: "bg-transparent text-gray-700 hover:bg-gray-100 shadow-none",
|
||||
"ghost-light": "bg-transparent text-white hover:bg-white/10 shadow-none",
|
||||
outline: "border-2 border-gray-300 text-gray-700 hover:bg-gray-50 shadow-none",
|
||||
link: "text-primary-500 hover:text-primary-600 hover:underline shadow-none px-0",
|
||||
},
|
||||
size: {
|
||||
sm: "h-8 px-3 text-sm",
|
||||
md: "h-10 px-4 text-base",
|
||||
lg: "h-12 px-6 text-lg",
|
||||
},
|
||||
fullWidth: {
|
||||
true: "w-full",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
compoundVariants: [
|
||||
// 链接变体不应用高度和圆角
|
||||
{
|
||||
variant: "link",
|
||||
size: "sm",
|
||||
className: "h-auto px-0",
|
||||
},
|
||||
{
|
||||
variant: "link",
|
||||
size: "md",
|
||||
className: "h-auto px-0",
|
||||
},
|
||||
{
|
||||
variant: "link",
|
||||
size: "lg",
|
||||
className: "h-auto px-0",
|
||||
},
|
||||
],
|
||||
defaultVariants: {
|
||||
variant: "secondary",
|
||||
size: "md",
|
||||
fullWidth: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type ButtonVariant = VariantProps<typeof buttonVariants>["variant"];
|
||||
export type ButtonSize = VariantProps<typeof buttonVariants>["size"];
|
||||
|
||||
export interface ButtonProps
|
||||
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
||||
VariantProps<typeof buttonVariants> {
|
||||
// 内容
|
||||
children?: React.ReactNode;
|
||||
|
||||
// 导航
|
||||
href?: string;
|
||||
openInNewTab?: boolean;
|
||||
|
||||
// 图标
|
||||
leftIcon?: React.ReactNode;
|
||||
rightIcon?: React.ReactNode;
|
||||
iconSrc?: string; // For Next.js Image icons
|
||||
iconAlt?: string;
|
||||
|
||||
// 状态
|
||||
loading?: boolean;
|
||||
selected?: boolean;
|
||||
|
||||
// 样式
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Button 组件
|
||||
*/
|
||||
export function Button({
|
||||
variant = "secondary",
|
||||
size = "md",
|
||||
fullWidth = false,
|
||||
href,
|
||||
openInNewTab = false,
|
||||
iconSrc,
|
||||
iconAlt,
|
||||
leftIcon,
|
||||
rightIcon,
|
||||
children,
|
||||
className,
|
||||
loading = false,
|
||||
selected = false,
|
||||
disabled,
|
||||
type = "button",
|
||||
...props
|
||||
}: ButtonProps) {
|
||||
// 确保 size 有默认值
|
||||
const actualSize = size ?? "md";
|
||||
|
||||
// 计算样式
|
||||
const computedClass = cn(
|
||||
buttonVariants({ variant, size: actualSize, fullWidth }),
|
||||
selected && variant === "secondary" && "bg-gray-200",
|
||||
className
|
||||
);
|
||||
|
||||
// 图标尺寸映射
|
||||
const iconSize = { sm: 14, md: 16, lg: 20 }[actualSize];
|
||||
|
||||
// 渲染 SVG 图标
|
||||
const renderSvgIcon = (icon: React.ReactNode, position: "left" | "right") => {
|
||||
if (!icon) return null;
|
||||
return (
|
||||
<span className={`flex items-center shrink-0 ${position === "left" ? "-ml-1 mr-2" : "-mr-1 ml-2"}`}>
|
||||
{icon}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
|
||||
// 渲染 Next.js Image 图标
|
||||
const renderImageIcon = () => {
|
||||
if (!iconSrc) return null;
|
||||
return (
|
||||
<Image
|
||||
src={iconSrc}
|
||||
width={iconSize}
|
||||
height={iconSize}
|
||||
alt={iconAlt || "icon"}
|
||||
className="shrink-0"
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
// 渲染加载图标
|
||||
const renderLoadingIcon = () => {
|
||||
if (!loading) return null;
|
||||
return (
|
||||
<svg
|
||||
className="animate-spin h-4 w-4"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle
|
||||
className="opacity-25"
|
||||
cx="12"
|
||||
cy="12"
|
||||
r="10"
|
||||
stroke="currentColor"
|
||||
strokeWidth="4"
|
||||
/>
|
||||
<path
|
||||
className="opacity-75"
|
||||
fill="currentColor"
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
};
|
||||
|
||||
// 组装内容
|
||||
const content = (
|
||||
<>
|
||||
{loading && renderLoadingIcon()}
|
||||
{renderImageIcon()}
|
||||
{renderSvgIcon(leftIcon, "left")}
|
||||
{children}
|
||||
{renderSvgIcon(rightIcon, "right")}
|
||||
</>
|
||||
);
|
||||
|
||||
// 如果提供了 href,渲染为 Link
|
||||
if (href) {
|
||||
return (
|
||||
<Link
|
||||
href={href}
|
||||
className={computedClass}
|
||||
target={openInNewTab ? "_blank" : undefined}
|
||||
rel={openInNewTab ? "noopener noreferrer" : undefined}
|
||||
>
|
||||
{content}
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
|
||||
// 否则渲染为 button
|
||||
return (
|
||||
<button
|
||||
type={type}
|
||||
disabled={disabled || loading}
|
||||
className={computedClass}
|
||||
{...props}
|
||||
>
|
||||
{content}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 预定义的按钮快捷组件
|
||||
*/
|
||||
export const PrimaryButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="primary" {...props} />
|
||||
);
|
||||
|
||||
export const SecondaryButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="secondary" {...props} />
|
||||
);
|
||||
|
||||
// LightButton: 次要按钮的别名(向后兼容)
|
||||
export const LightButton = SecondaryButton;
|
||||
|
||||
export const SuccessButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="success" {...props} />
|
||||
);
|
||||
|
||||
export const WarningButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="warning" {...props} />
|
||||
);
|
||||
|
||||
export const ErrorButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="error" {...props} />
|
||||
);
|
||||
|
||||
export const GhostButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="ghost" {...props} />
|
||||
);
|
||||
|
||||
// GhostLightButton: 透明按钮(白色文字,用于深色背景)
|
||||
export const GhostLightButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="ghost-light" {...props} />
|
||||
);
|
||||
|
||||
export const OutlineButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="outline" {...props} />
|
||||
);
|
||||
|
||||
export const LinkButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="link" {...props} />
|
||||
);
|
||||
|
||||
// ========== 其他便捷组件 ==========
|
||||
|
||||
// IconButton: SVG 图标按钮(使用 ghost 变体)
|
||||
export const IconButton = (props: Omit<ButtonProps, "variant"> & { icon?: React.ReactNode }) => {
|
||||
const { icon, ...rest } = props;
|
||||
return <Button variant="ghost" leftIcon={icon} {...rest} />;
|
||||
};
|
||||
|
||||
// IconClick: 图片图标按钮(支持 Next.js Image)
|
||||
export const IconClick = (props: Omit<ButtonProps, "variant"> & {
|
||||
src?: string;
|
||||
alt?: string;
|
||||
size?: number | "sm" | "md" | "lg";
|
||||
disableOnHoverBgChange?: boolean;
|
||||
}) => {
|
||||
const { src, alt, size, disableOnHoverBgChange, className, ...rest } = props;
|
||||
let buttonSize: "sm" | "md" | "lg" = "md";
|
||||
if (typeof size === "number") {
|
||||
if (size <= 20) buttonSize = "sm";
|
||||
else if (size >= 32) buttonSize = "lg";
|
||||
} else if (typeof size === "string") {
|
||||
buttonSize = (size === "sm" || size === "md" || size === "lg") ? size : "md";
|
||||
}
|
||||
|
||||
const hoverClass = disableOnHoverBgChange ? "hover:bg-black/30" : "";
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="ghost"
|
||||
iconSrc={src}
|
||||
iconAlt={alt}
|
||||
size={buttonSize}
|
||||
className={`${hoverClass} ${className || ""}`}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
// CircleButton: 圆形图标按钮
|
||||
export const CircleButton = (props: Omit<ButtonProps, "variant"> & { icon?: React.ReactNode }) => {
|
||||
const { icon, className, ...rest } = props;
|
||||
return <Button variant="ghost" leftIcon={icon} className={`rounded-full ${className || ""}`} {...rest} />;
|
||||
};
|
||||
|
||||
// CircleToggleButton: 带选中状态的圆形切换按钮
|
||||
export const CircleToggleButton = (props: Omit<ButtonProps, "variant"> & { selected?: boolean }) => {
|
||||
const { selected, className, children, ...rest } = props;
|
||||
const selectedClass = selected
|
||||
? "bg-primary-500 text-white"
|
||||
: "bg-gray-200 text-gray-600 hover:bg-gray-300";
|
||||
return (
|
||||
<Button
|
||||
variant="secondary"
|
||||
className={`rounded-full px-3 py-1 text-sm transition-colors ${selectedClass} ${className || ""}`}
|
||||
{...rest}
|
||||
>
|
||||
{children}
|
||||
</Button>
|
||||
);
|
||||
};
|
||||
|
||||
// DashedButton: 虚线边框按钮(使用 outline 变体近似)
|
||||
export const DashedButton = (props: Omit<ButtonProps, "variant">) => (
|
||||
<Button variant="outline" className="border-dashed" {...props} />
|
||||
);
|
||||
1
src/design-system/base/button/index.ts
Normal file
1
src/design-system/base/button/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './button';
|
||||
198
src/design-system/base/card/card.tsx
Normal file
198
src/design-system/base/card/card.tsx
Normal file
@@ -0,0 +1,198 @@
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Card 卡片组件
|
||||
*
|
||||
* Design System 中的卡片容器组件,提供统一的内容包装样式。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认卡片
|
||||
* <Card>
|
||||
* <p>卡片内容</p>
|
||||
* </Card>
|
||||
*
|
||||
* // 带边框的卡片
|
||||
* <Card variant="bordered" padding="lg">
|
||||
* <p>带边框的内容</p>
|
||||
* </Card>
|
||||
*
|
||||
* // 无内边距卡片
|
||||
* <Card padding="none">
|
||||
* <img src="image.jpg" alt="完全填充的图片" />
|
||||
* </Card>
|
||||
*
|
||||
* // 可点击的卡片
|
||||
* <Card clickable onClick={handleClick}>
|
||||
* <p>点击我</p>
|
||||
* </Card>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* 卡片变体样式
|
||||
*/
|
||||
const cardVariants = cva(
|
||||
// 基础样式
|
||||
"rounded-lg bg-white transition-all duration-250",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "shadow-xl",
|
||||
bordered: "border-2 border-gray-200 shadow-sm",
|
||||
elevated: "shadow-2xl",
|
||||
flat: "border border-gray-200 shadow-none",
|
||||
},
|
||||
padding: {
|
||||
none: "",
|
||||
xs: "p-3",
|
||||
sm: "p-4",
|
||||
md: "p-6",
|
||||
lg: "p-8",
|
||||
xl: "p-10",
|
||||
},
|
||||
clickable: {
|
||||
true: "cursor-pointer hover:shadow-primary/25 hover:-translate-y-0.5 active:translate-y-0",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
padding: "md",
|
||||
clickable: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type CardVariant = VariantProps<typeof cardVariants>["variant"];
|
||||
export type CardPadding = VariantProps<typeof cardVariants>["padding"];
|
||||
|
||||
export interface CardProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof cardVariants> {
|
||||
// 子元素
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Card 卡片组件
|
||||
*/
|
||||
export function Card({
|
||||
variant = "default",
|
||||
padding = "md",
|
||||
clickable = false,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: CardProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn(cardVariants({ variant, padding, clickable }), className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* CardSection - 卡片内容区块
|
||||
* 用于组织卡片内部的多个内容区块
|
||||
*/
|
||||
export interface CardSectionProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
children: React.ReactNode;
|
||||
noPadding?: boolean;
|
||||
}
|
||||
|
||||
export function CardSection({
|
||||
noPadding = false,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: CardSectionProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
!noPadding && "p-6",
|
||||
"first:rounded-t-2xl last:rounded-b-2xl",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* CardHeader - 卡片头部
|
||||
*/
|
||||
export interface CardHeaderProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function CardHeader({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: CardHeaderProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn("flex items-center justify-between p-6 border-b border-gray-200", className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* CardTitle - 卡片标题
|
||||
*/
|
||||
export interface CardTitleProps extends React.HTMLAttributes<HTMLHeadingElement> {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function CardTitle({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: CardTitleProps) {
|
||||
return (
|
||||
<h3
|
||||
className={cn("text-lg font-semibold text-gray-900", className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</h3>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* CardBody - 卡片主体
|
||||
*/
|
||||
export const CardBody = CardSection;
|
||||
|
||||
/**
|
||||
* CardFooter - 卡片底部
|
||||
*/
|
||||
export interface CardFooterProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function CardFooter({
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: CardFooterProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn("flex items-center justify-end gap-2 p-6 border-t border-gray-200", className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/design-system/base/card/index.ts
Normal file
1
src/design-system/base/card/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './card';
|
||||
170
src/design-system/base/checkbox/checkbox.tsx
Normal file
170
src/design-system/base/checkbox/checkbox.tsx
Normal file
@@ -0,0 +1,170 @@
|
||||
"use client";
|
||||
|
||||
import React, { forwardRef } from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Checkbox 复选框组件
|
||||
*
|
||||
* Design System 中的复选框组件,支持多种状态和尺寸。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认复选框
|
||||
* <Checkbox>同意条款</Checkbox>
|
||||
*
|
||||
* // 受控组件
|
||||
* <Checkbox checked={checked} onChange={handleChange}>
|
||||
* 同意条款
|
||||
* </Checkbox>
|
||||
*
|
||||
* // 错误状态
|
||||
* <Checkbox error>必选项</Checkbox>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* 复选框变体样式
|
||||
*/
|
||||
const checkboxVariants = cva(
|
||||
// 基础样式
|
||||
"peer h-4 w-4 shrink-0 rounded border-2 transition-all duration-250 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "border-gray-300 checked:bg-primary-500 checked:border-primary-500",
|
||||
success: "border-gray-300 checked:bg-success-500 checked:border-success-500",
|
||||
warning: "border-gray-300 checked:bg-warning-500 checked:border-warning-500",
|
||||
error: "border-gray-300 checked:bg-error-500 checked:border-error-500",
|
||||
},
|
||||
size: {
|
||||
sm: "h-3.5 w-3.5",
|
||||
md: "h-4 w-4",
|
||||
lg: "h-5 w-5",
|
||||
},
|
||||
error: {
|
||||
true: "border-error-500",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "md",
|
||||
error: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type CheckboxVariant = VariantProps<typeof checkboxVariants>["variant"];
|
||||
export type CheckboxSize = VariantProps<typeof checkboxVariants>["size"];
|
||||
|
||||
export interface CheckboxProps
|
||||
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "size">,
|
||||
VariantProps<typeof checkboxVariants> {
|
||||
// 标签文本
|
||||
label?: React.ReactNode;
|
||||
// 标签位置
|
||||
labelPosition?: "left" | "right";
|
||||
// 自定义复选框类名
|
||||
checkboxClassName?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checkbox 复选框组件
|
||||
*/
|
||||
export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
|
||||
(
|
||||
{
|
||||
variant = "default",
|
||||
size = "md",
|
||||
error = false,
|
||||
label,
|
||||
labelPosition = "right",
|
||||
className,
|
||||
checkboxClassName,
|
||||
disabled,
|
||||
...props
|
||||
},
|
||||
ref
|
||||
) => {
|
||||
const checkboxId = React.useId();
|
||||
|
||||
const renderCheckbox = () => (
|
||||
<input
|
||||
ref={ref}
|
||||
type="checkbox"
|
||||
id={checkboxId}
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
checkboxVariants({ variant, size, error }),
|
||||
checkboxClassName
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
|
||||
const renderLabel = () => {
|
||||
if (!label) return null;
|
||||
|
||||
return (
|
||||
<label
|
||||
htmlFor={checkboxId}
|
||||
className={cn(
|
||||
"text-base font-normal leading-none",
|
||||
disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer",
|
||||
labelPosition === "left" ? "mr-2" : "ml-2"
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</label>
|
||||
);
|
||||
};
|
||||
|
||||
if (!label) {
|
||||
return renderCheckbox();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("inline-flex items-center", className)}>
|
||||
{labelPosition === "left" && renderLabel()}
|
||||
{renderCheckbox()}
|
||||
{labelPosition === "right" && renderLabel()}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Checkbox.displayName = "Checkbox";
|
||||
|
||||
/**
|
||||
* CheckboxGroup - 复选框组
|
||||
*/
|
||||
export interface CheckboxGroupProps {
|
||||
children: React.ReactNode;
|
||||
label?: string;
|
||||
error?: string;
|
||||
required?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function CheckboxGroup({
|
||||
children,
|
||||
label,
|
||||
error,
|
||||
required,
|
||||
className,
|
||||
}: CheckboxGroupProps) {
|
||||
return (
|
||||
<div className={cn("space-y-2", className)}>
|
||||
{label && (
|
||||
<div className="text-base font-medium text-gray-900">
|
||||
{label}
|
||||
{required && <span className="text-error-500 ml-1">*</span>}
|
||||
</div>
|
||||
)}
|
||||
<div className="space-y-2">{children}</div>
|
||||
{error && <p className="text-sm text-error-500">{error}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/design-system/base/checkbox/index.ts
Normal file
1
src/design-system/base/checkbox/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './checkbox';
|
||||
1
src/design-system/base/input/index.ts
Normal file
1
src/design-system/base/input/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './input';
|
||||
151
src/design-system/base/input/input.tsx
Normal file
151
src/design-system/base/input/input.tsx
Normal file
@@ -0,0 +1,151 @@
|
||||
"use client";
|
||||
|
||||
import React, { forwardRef } from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Input 输入框组件
|
||||
*
|
||||
* Design System 中的输入框组件,支持多种样式变体和尺寸。
|
||||
* 完全可访问,支持焦点状态和错误状态。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认样式
|
||||
* <Input placeholder="请输入内容" />
|
||||
*
|
||||
* // 带边框样式
|
||||
* <Input variant="bordered" placeholder="带边框的输入框" />
|
||||
*
|
||||
* // 填充样式
|
||||
* <Input variant="filled" placeholder="填充背景的输入框" />
|
||||
*
|
||||
* // 错误状态
|
||||
* <Input variant="bordered" error placeholder="有错误的输入框" />
|
||||
*
|
||||
* // 禁用状态
|
||||
* <Input disabled placeholder="禁用的输入框" />
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* 输入框变体样式
|
||||
*/
|
||||
const inputVariants = cva(
|
||||
// 基础样式
|
||||
"flex w-full rounded-md border px-3 py-2 text-base transition-all duration-250 placeholder:text-gray-400 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "border-b-2 border-gray-300 bg-transparent rounded-t-md",
|
||||
bordered: "border-gray-300 bg-white",
|
||||
filled: "border-transparent bg-gray-100",
|
||||
search: "border-gray-200 bg-white pl-10 rounded-full",
|
||||
},
|
||||
size: {
|
||||
sm: "h-9 px-3 text-sm",
|
||||
md: "h-10 px-4 text-base",
|
||||
lg: "h-12 px-5 text-lg",
|
||||
},
|
||||
error: {
|
||||
true: "border-error-500 focus-visible:ring-error-500",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
compoundVariants: [
|
||||
// 填充变体的错误状态
|
||||
{
|
||||
variant: "filled",
|
||||
error: true,
|
||||
className: "bg-error-50",
|
||||
},
|
||||
],
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "md",
|
||||
error: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type InputVariant = VariantProps<typeof inputVariants>["variant"];
|
||||
export type InputSize = VariantProps<typeof inputVariants>["size"];
|
||||
|
||||
export interface InputProps
|
||||
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "size">,
|
||||
VariantProps<typeof inputVariants> {
|
||||
// 左侧图标(通常用于搜索框)
|
||||
leftIcon?: React.ReactNode;
|
||||
// 右侧图标(例如清除按钮)
|
||||
rightIcon?: React.ReactNode;
|
||||
// 容器类名(用于包裹图标和输入框)
|
||||
containerClassName?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Input 输入框组件
|
||||
*/
|
||||
export const Input = forwardRef<HTMLInputElement, InputProps>(
|
||||
(
|
||||
{
|
||||
variant = "default",
|
||||
size = "md",
|
||||
error = false,
|
||||
className,
|
||||
containerClassName,
|
||||
leftIcon,
|
||||
rightIcon,
|
||||
type = "text",
|
||||
...props
|
||||
},
|
||||
ref
|
||||
) => {
|
||||
// 如果有左侧图标,使用相对定位的容器
|
||||
if (leftIcon) {
|
||||
return (
|
||||
<div className={cn("relative", containerClassName)}>
|
||||
{/* 左侧图标 */}
|
||||
<div className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400 pointer-events-none">
|
||||
{leftIcon}
|
||||
</div>
|
||||
{/* 输入框 */}
|
||||
<input
|
||||
ref={ref}
|
||||
type={type}
|
||||
className={cn(
|
||||
inputVariants({ variant, size, error }),
|
||||
leftIcon && "pl-10"
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
{/* 右侧图标 */}
|
||||
{rightIcon && (
|
||||
<div className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400">
|
||||
{rightIcon}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// 普通输入框
|
||||
return (
|
||||
<div className={cn("relative", containerClassName)}>
|
||||
<input
|
||||
ref={ref}
|
||||
type={type}
|
||||
className={cn(inputVariants({ variant, size, error }), className)}
|
||||
{...props}
|
||||
/>
|
||||
{rightIcon && (
|
||||
<div className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400">
|
||||
{rightIcon}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Input.displayName = "Input";
|
||||
1
src/design-system/base/radio/index.ts
Normal file
1
src/design-system/base/radio/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './radio';
|
||||
220
src/design-system/base/radio/radio.tsx
Normal file
220
src/design-system/base/radio/radio.tsx
Normal file
@@ -0,0 +1,220 @@
|
||||
"use client";
|
||||
|
||||
import React, { forwardRef } from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Radio 单选按钮组件
|
||||
*
|
||||
* Design System 中的单选按钮组件,支持多种状态和尺寸。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认单选按钮
|
||||
* <Radio name="choice" value="1">选项 1</Radio>
|
||||
* <Radio name="choice" value="2">选项 2</Radio>
|
||||
*
|
||||
* // 受控组件
|
||||
* <Radio
|
||||
* name="choice"
|
||||
* value="1"
|
||||
* checked={value === "1"}
|
||||
* onChange={(e) => setValue(e.target.value)}
|
||||
* >
|
||||
* 选项 1
|
||||
* </Radio>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* 单选按钮变体样式
|
||||
*/
|
||||
const radioVariants = cva(
|
||||
// 基础样式
|
||||
"peer h-4 w-4 shrink-0 rounded-full border-2 transition-all duration-250 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 appearance-none cursor-pointer",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "border-gray-300 checked:border-primary-500",
|
||||
success: "border-gray-300 checked:border-success-500",
|
||||
warning: "border-gray-300 checked:border-warning-500",
|
||||
error: "border-gray-300 checked:border-error-500",
|
||||
},
|
||||
size: {
|
||||
sm: "h-3.5 w-3.5",
|
||||
md: "h-4 w-4",
|
||||
lg: "h-5 w-5",
|
||||
},
|
||||
error: {
|
||||
true: "border-error-500",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "md",
|
||||
error: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type RadioVariant = VariantProps<typeof radioVariants>["variant"];
|
||||
export type RadioSize = VariantProps<typeof radioVariants>["size"];
|
||||
|
||||
export interface RadioProps
|
||||
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "size">,
|
||||
VariantProps<typeof radioVariants> {
|
||||
// 标签文本
|
||||
label?: React.ReactNode;
|
||||
// 标签位置
|
||||
labelPosition?: "left" | "right";
|
||||
// 自定义单选按钮类名
|
||||
radioClassName?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Radio 单选按钮组件
|
||||
*/
|
||||
export const Radio = forwardRef<HTMLInputElement, RadioProps>(
|
||||
(
|
||||
{
|
||||
variant = "default",
|
||||
size = "md",
|
||||
error = false,
|
||||
label,
|
||||
labelPosition = "right",
|
||||
className,
|
||||
radioClassName,
|
||||
disabled,
|
||||
...props
|
||||
},
|
||||
ref
|
||||
) => {
|
||||
const radioId = React.useId();
|
||||
|
||||
const renderRadio = () => (
|
||||
<div className="relative">
|
||||
<input
|
||||
ref={ref}
|
||||
type="radio"
|
||||
id={radioId}
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
radioVariants({ variant, size, error }),
|
||||
"peer/radio",
|
||||
radioClassName
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
{/* 选中状态的圆点 */}
|
||||
<div
|
||||
className={cn(
|
||||
"absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 rounded-full pointer-events-none transition-all duration-250",
|
||||
"peer-checked/radio:bg-current",
|
||||
size === "sm" && "h-1.5 w-1.5",
|
||||
size === "md" && "h-2 w-2",
|
||||
size === "lg" && "h-2.5 w-2.5",
|
||||
variant === "default" && "text-primary-500",
|
||||
variant === "success" && "text-success-500",
|
||||
variant === "warning" && "text-warning-500",
|
||||
variant === "error" && "text-error-500"
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
const renderLabel = () => {
|
||||
if (!label) return null;
|
||||
|
||||
return (
|
||||
<label
|
||||
htmlFor={radioId}
|
||||
className={cn(
|
||||
"text-base font-normal leading-none",
|
||||
disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer",
|
||||
labelPosition === "left" ? "mr-2" : "ml-2"
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</label>
|
||||
);
|
||||
};
|
||||
|
||||
if (!label) {
|
||||
return renderRadio();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("inline-flex items-center", className)}>
|
||||
{labelPosition === "left" && renderLabel()}
|
||||
{renderRadio()}
|
||||
{labelPosition === "right" && renderLabel()}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Radio.displayName = "Radio";
|
||||
|
||||
/**
|
||||
* RadioGroup - 单选按钮组
|
||||
*/
|
||||
export interface RadioGroupProps {
|
||||
children: React.ReactNode;
|
||||
name: string;
|
||||
label?: string;
|
||||
error?: string;
|
||||
required?: boolean;
|
||||
value?: string;
|
||||
onChange?: (value: string) => void;
|
||||
className?: string;
|
||||
orientation?: "vertical" | "horizontal";
|
||||
}
|
||||
|
||||
export function RadioGroup({
|
||||
children,
|
||||
name,
|
||||
label,
|
||||
error,
|
||||
required,
|
||||
value,
|
||||
onChange,
|
||||
className,
|
||||
orientation = "vertical",
|
||||
}: RadioGroupProps) {
|
||||
// 为每个 Radio 注入 name 和 onChange
|
||||
const enhancedChildren = React.Children.map(children, (child) => {
|
||||
if (React.isValidElement(child)) {
|
||||
const childProps = child.props as { value?: string; onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void };
|
||||
return React.cloneElement(child as React.ReactElement<any>, {
|
||||
name,
|
||||
checked: value !== undefined ? childProps.value === value : undefined,
|
||||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
onChange?.(e.target.value);
|
||||
childProps.onChange?.(e);
|
||||
},
|
||||
});
|
||||
}
|
||||
return child;
|
||||
});
|
||||
|
||||
return (
|
||||
<div className={cn("space-y-2", className)}>
|
||||
{label && (
|
||||
<div className="text-base font-medium text-gray-900">
|
||||
{label}
|
||||
{required && <span className="text-error-500 ml-1">*</span>}
|
||||
</div>
|
||||
)}
|
||||
<div
|
||||
className={cn(
|
||||
orientation === "vertical" ? "space-y-2" : "flex gap-4"
|
||||
)}
|
||||
>
|
||||
{enhancedChildren}
|
||||
</div>
|
||||
{error && <p className="text-sm text-error-500">{error}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/design-system/base/select/index.ts
Normal file
1
src/design-system/base/select/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './select';
|
||||
112
src/design-system/base/select/select.tsx
Normal file
112
src/design-system/base/select/select.tsx
Normal file
@@ -0,0 +1,112 @@
|
||||
"use client";
|
||||
|
||||
import React, { forwardRef } from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Select 下拉选择框组件
|
||||
*
|
||||
* Design System 中的下拉选择框组件。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* <Select>
|
||||
* <option value="">请选择</option>
|
||||
* <option value="1">选项 1</option>
|
||||
* <option value="2">选项 2</option>
|
||||
* </Select>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Select 变体样式
|
||||
*/
|
||||
const selectVariants = cva(
|
||||
// 基础样式
|
||||
"flex w-full appearance-none items-center justify-between rounded-md border px-3 py-2 pr-8 text-base transition-all duration-250 placeholder:text-gray-400 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "border-b-2 border-gray-300 bg-transparent rounded-t-md",
|
||||
bordered: "border-gray-300 bg-white",
|
||||
filled: "border-transparent bg-gray-100",
|
||||
},
|
||||
size: {
|
||||
sm: "h-9 px-3 text-sm",
|
||||
md: "h-10 px-4 text-base",
|
||||
lg: "h-12 px-5 text-lg",
|
||||
},
|
||||
error: {
|
||||
true: "border-error-500 focus-visible:ring-error-500",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
compoundVariants: [
|
||||
{
|
||||
variant: "filled",
|
||||
error: true,
|
||||
className: "bg-error-50",
|
||||
},
|
||||
],
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "md",
|
||||
error: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type SelectVariant = VariantProps<typeof selectVariants>["variant"];
|
||||
export type SelectSize = VariantProps<typeof selectVariants>["size"];
|
||||
|
||||
export interface SelectProps
|
||||
extends Omit<React.SelectHTMLAttributes<HTMLSelectElement>, "size">,
|
||||
VariantProps<typeof selectVariants> {}
|
||||
|
||||
/**
|
||||
* Select 下拉选择框组件
|
||||
*/
|
||||
export const Select = forwardRef<HTMLSelectElement, SelectProps>(
|
||||
(
|
||||
{
|
||||
variant = "default",
|
||||
size = "md",
|
||||
error = false,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
},
|
||||
ref
|
||||
) => {
|
||||
return (
|
||||
<div className="relative">
|
||||
<select
|
||||
ref={ref}
|
||||
className={cn(selectVariants({ variant, size, error }), className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</select>
|
||||
{/* 下拉箭头图标 */}
|
||||
<div className="pointer-events-none absolute right-3 top-1/2 -translate-y-1/2 text-gray-400">
|
||||
<svg
|
||||
className="h-4 w-4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M19 9l-7 7-7-7"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Select.displayName = "Select";
|
||||
1
src/design-system/base/switch/index.ts
Normal file
1
src/design-system/base/switch/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './switch';
|
||||
182
src/design-system/base/switch/switch.tsx
Normal file
182
src/design-system/base/switch/switch.tsx
Normal file
@@ -0,0 +1,182 @@
|
||||
"use client";
|
||||
|
||||
import React, { forwardRef, useState } from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Switch 开关组件
|
||||
*
|
||||
* Design System 中的开关组件,用于二进制状态切换。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认开关
|
||||
* <Switch checked={checked} onChange={setChecked} />
|
||||
*
|
||||
* // 带标签
|
||||
* <Switch label="启用通知" checked={checked} onChange={setChecked} />
|
||||
*
|
||||
* // 不同尺寸
|
||||
* <Switch size="sm" checked={checked} onChange={setChecked} />
|
||||
* <Switch size="lg" checked={checked} onChange={setChecked} />
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* 开关变体样式
|
||||
*/
|
||||
const switchVariants = cva(
|
||||
// 基础样式
|
||||
"peer inline-flex shrink-0 cursor-pointer items-center rounded-full border-2 transition-all duration-250 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 appearance-none",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default:
|
||||
"border-gray-300 bg-gray-100 checked:border-primary-500 checked:bg-primary-500",
|
||||
success:
|
||||
"border-gray-300 bg-gray-100 checked:border-success-500 checked:bg-success-500",
|
||||
warning:
|
||||
"border-gray-300 bg-gray-100 checked:border-warning-500 checked:bg-warning-500",
|
||||
error:
|
||||
"border-gray-300 bg-gray-100 checked:border-error-500 checked:bg-error-500",
|
||||
},
|
||||
size: {
|
||||
sm: "h-5 w-9",
|
||||
md: "h-6 w-11",
|
||||
lg: "h-7 w-13",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "md",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type SwitchVariant = VariantProps<typeof switchVariants>["variant"];
|
||||
export type SwitchSize = VariantProps<typeof switchVariants>["size"];
|
||||
|
||||
export interface SwitchProps
|
||||
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "size">,
|
||||
VariantProps<typeof switchVariants> {
|
||||
// 标签文本
|
||||
label?: React.ReactNode;
|
||||
// 标签位置
|
||||
labelPosition?: "left" | "right";
|
||||
// 自定义开关类名
|
||||
switchClassName?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch 开关组件
|
||||
*/
|
||||
export const Switch = forwardRef<HTMLInputElement, SwitchProps>(
|
||||
(
|
||||
{
|
||||
variant = "default",
|
||||
size = "md",
|
||||
label,
|
||||
labelPosition = "right",
|
||||
className,
|
||||
switchClassName,
|
||||
disabled,
|
||||
checked,
|
||||
defaultChecked,
|
||||
onChange,
|
||||
...props
|
||||
},
|
||||
ref
|
||||
) => {
|
||||
const switchId = React.useId();
|
||||
const [internalChecked, setInternalChecked] = useState(
|
||||
checked ?? defaultChecked ?? false
|
||||
);
|
||||
|
||||
// 处理受控和非受控模式
|
||||
const isControlled = checked !== undefined;
|
||||
const isChecked = isControlled ? checked : internalChecked;
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
if (!isControlled) {
|
||||
setInternalChecked(e.target.checked);
|
||||
}
|
||||
onChange?.(e);
|
||||
};
|
||||
|
||||
// 确保 size 有默认值
|
||||
const actualSize = size ?? "md";
|
||||
|
||||
// 滑块大小
|
||||
const thumbSize = {
|
||||
sm: "h-3.5 w-3.5",
|
||||
md: "h-4 w-4",
|
||||
lg: "h-5 w-5",
|
||||
}[actualSize];
|
||||
|
||||
// 滑块位移
|
||||
const thumbTranslate = {
|
||||
sm: isChecked ? "translate-x-4" : "translate-x-0.5",
|
||||
md: isChecked ? "translate-x-5" : "translate-x-0.5",
|
||||
lg: isChecked ? "translate-x-6" : "translate-x-0.5",
|
||||
}[actualSize];
|
||||
|
||||
const renderSwitch = () => (
|
||||
<div className="relative inline-block">
|
||||
<input
|
||||
ref={ref}
|
||||
type="checkbox"
|
||||
id={switchId}
|
||||
disabled={disabled}
|
||||
checked={isChecked}
|
||||
onChange={handleChange}
|
||||
className={cn(
|
||||
switchVariants({ variant, size }),
|
||||
"peer/switch",
|
||||
switchClassName
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
{/* 滑块 */}
|
||||
<div
|
||||
className={cn(
|
||||
"pointer-events-none absolute top-1/2 -translate-y-1/2 rounded-full bg-white shadow-sm transition-transform duration-250",
|
||||
thumbSize,
|
||||
thumbTranslate
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
const renderLabel = () => {
|
||||
if (!label) return null;
|
||||
|
||||
return (
|
||||
<label
|
||||
htmlFor={switchId}
|
||||
className={cn(
|
||||
"text-base font-normal leading-none",
|
||||
disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer",
|
||||
labelPosition === "left" ? "mr-3" : "ml-3"
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</label>
|
||||
);
|
||||
};
|
||||
|
||||
if (!label) {
|
||||
return renderSwitch();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("inline-flex items-center", className)}>
|
||||
{labelPosition === "left" && renderLabel()}
|
||||
{renderSwitch()}
|
||||
{labelPosition === "right" && renderLabel()}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Switch.displayName = "Switch";
|
||||
1
src/design-system/base/textarea/index.ts
Normal file
1
src/design-system/base/textarea/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './textarea';
|
||||
104
src/design-system/base/textarea/textarea.tsx
Normal file
104
src/design-system/base/textarea/textarea.tsx
Normal file
@@ -0,0 +1,104 @@
|
||||
"use client";
|
||||
|
||||
import React, { forwardRef } from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Textarea 多行文本输入组件
|
||||
*
|
||||
* Design System 中的多行文本输入组件,支持多种样式变体。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认样式
|
||||
* <Textarea placeholder="请输入内容" rows={4} />
|
||||
*
|
||||
* // 带边框样式
|
||||
* <Textarea variant="bordered" placeholder="带边框的文本域" />
|
||||
*
|
||||
* // 填充样式
|
||||
* <Textarea variant="filled" placeholder="填充背景的文本域" />
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Textarea 变体样式
|
||||
*/
|
||||
const textareaVariants = cva(
|
||||
// 基础样式
|
||||
"flex w-full rounded-md border px-3 py-2 text-base transition-all duration-250 placeholder:text-gray-400 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 resize-none",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "border-b-2 border-gray-300 bg-transparent rounded-t-xl",
|
||||
bordered: "border-gray-300 bg-white",
|
||||
filled: "border-transparent bg-gray-100",
|
||||
},
|
||||
error: {
|
||||
true: "border-error-500 focus-visible:ring-error-500",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
compoundVariants: [
|
||||
{
|
||||
variant: "filled",
|
||||
error: true,
|
||||
className: "bg-error-50",
|
||||
},
|
||||
],
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
error: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type TextareaVariant = VariantProps<typeof textareaVariants>["variant"];
|
||||
|
||||
export interface TextareaProps
|
||||
extends React.TextareaHTMLAttributes<HTMLTextAreaElement>,
|
||||
VariantProps<typeof textareaVariants> {
|
||||
// 自动调整高度
|
||||
autoResize?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Textarea 多行文本输入组件
|
||||
*/
|
||||
export const Textarea = forwardRef<HTMLTextAreaElement, TextareaProps>(
|
||||
(
|
||||
{
|
||||
variant = "default",
|
||||
error = false,
|
||||
className,
|
||||
autoResize = false,
|
||||
onChange,
|
||||
rows = 3,
|
||||
...props
|
||||
},
|
||||
ref
|
||||
) => {
|
||||
// 自动调整高度的 change 处理
|
||||
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||
if (autoResize) {
|
||||
const target = e.target;
|
||||
target.style.height = "auto";
|
||||
target.style.height = `${target.scrollHeight}px`;
|
||||
}
|
||||
onChange?.(e);
|
||||
};
|
||||
|
||||
return (
|
||||
<textarea
|
||||
ref={ref}
|
||||
rows={rows}
|
||||
className={cn(textareaVariants({ variant, error }), className)}
|
||||
onChange={handleChange}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Textarea.displayName = "Textarea";
|
||||
160
src/design-system/data-display/badge/badge.tsx
Normal file
160
src/design-system/data-display/badge/badge.tsx
Normal file
@@ -0,0 +1,160 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Badge 徽章组件
|
||||
*
|
||||
* Design System 中的徽章组件,用于显示状态、标签等信息。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认徽章
|
||||
* <Badge>新</Badge>
|
||||
*
|
||||
* // 不同变体
|
||||
* <Badge variant="success">成功</Badge>
|
||||
* <Badge variant="warning">警告</Badge>
|
||||
* <Badge variant="error">错误</Badge>
|
||||
*
|
||||
* // 不同尺寸
|
||||
* <Badge size="sm">小</Badge>
|
||||
* <Badge size="lg">大</Badge>
|
||||
*
|
||||
* // 圆形徽章
|
||||
* <Badge variant="primary" dot />
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Badge 变体样式
|
||||
*/
|
||||
const badgeVariants = cva(
|
||||
// 基础样式
|
||||
"inline-flex items-center justify-center rounded-full font-medium transition-colors duration-250",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
default: "bg-gray-100 text-gray-800",
|
||||
primary: "bg-primary-100 text-primary-800",
|
||||
success: "bg-success-100 text-success-800",
|
||||
warning: "bg-warning-100 text-warning-800",
|
||||
error: "bg-error-100 text-error-800",
|
||||
info: "bg-info-100 text-info-800",
|
||||
},
|
||||
size: {
|
||||
sm: "px-2 py-0.5 text-xs",
|
||||
md: "px-2.5 py-1 text-sm",
|
||||
lg: "px-3 py-1.5 text-base",
|
||||
},
|
||||
dot: {
|
||||
true: "px-2 py-1",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "default",
|
||||
size: "md",
|
||||
dot: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type BadgeVariant = VariantProps<typeof badgeVariants>["variant"];
|
||||
export type BadgeSize = VariantProps<typeof badgeVariants>["size"];
|
||||
|
||||
export interface BadgeProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof badgeVariants> {
|
||||
// 子元素
|
||||
children?: React.ReactNode;
|
||||
// 是否为圆点样式(不显示文字)
|
||||
dot?: boolean;
|
||||
// 圆点颜色(仅当 dot=true 时有效)
|
||||
dotColor?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Badge 徽章组件
|
||||
*/
|
||||
export function Badge({
|
||||
variant = "default",
|
||||
size = "md",
|
||||
dot = false,
|
||||
dotColor,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: BadgeProps) {
|
||||
// 圆点颜色映射
|
||||
const dotColors = {
|
||||
default: "bg-gray-400",
|
||||
primary: "bg-primary-500",
|
||||
success: "bg-success-500",
|
||||
warning: "bg-warning-500",
|
||||
error: "bg-error-500",
|
||||
info: "bg-info-500",
|
||||
};
|
||||
|
||||
// 确保 variant 有默认值
|
||||
const actualVariant = variant ?? "default";
|
||||
|
||||
return (
|
||||
<div className={cn(badgeVariants({ variant: actualVariant, size, dot }), className)} {...props}>
|
||||
{dot && (
|
||||
<span
|
||||
className={cn(
|
||||
"h-2 w-2 rounded-full",
|
||||
dotColor || dotColors[actualVariant]
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
{!dot && children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* StatusBadge - 状态徽章
|
||||
*/
|
||||
export interface StatusBadgeProps extends Omit<BadgeProps, "variant" | "children"> {
|
||||
status: "online" | "offline" | "busy" | "away";
|
||||
label?: string;
|
||||
}
|
||||
|
||||
export function StatusBadge({ status, label, ...props }: StatusBadgeProps) {
|
||||
const statusConfig = {
|
||||
online: { variant: "success" as const, defaultLabel: "在线" },
|
||||
offline: { variant: "default" as const, defaultLabel: "离线" },
|
||||
busy: { variant: "error" as const, defaultLabel: "忙碌" },
|
||||
away: { variant: "warning" as const, defaultLabel: "离开" },
|
||||
};
|
||||
|
||||
const config = statusConfig[status];
|
||||
|
||||
return (
|
||||
<Badge variant={config.variant} {...props}>
|
||||
{label || config.defaultLabel}
|
||||
</Badge>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* CounterBadge - 计数徽章
|
||||
*/
|
||||
export interface CounterBadgeProps extends Omit<BadgeProps, "children"> {
|
||||
count: number;
|
||||
max?: number;
|
||||
}
|
||||
|
||||
export function CounterBadge({ count, max = 99, ...props }: CounterBadgeProps) {
|
||||
const displayCount = count > max ? `${max}+` : count;
|
||||
|
||||
return (
|
||||
<Badge variant="error" size="sm" {...props}>
|
||||
{displayCount}
|
||||
</Badge>
|
||||
);
|
||||
}
|
||||
1
src/design-system/data-display/badge/index.ts
Normal file
1
src/design-system/data-display/badge/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './badge';
|
||||
102
src/design-system/data-display/divider/divider.tsx
Normal file
102
src/design-system/data-display/divider/divider.tsx
Normal file
@@ -0,0 +1,102 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Divider 分隔线组件
|
||||
*
|
||||
* Design System 中的分隔线组件,用于分隔内容区域。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 水平分隔线
|
||||
* <Divider />
|
||||
*
|
||||
* // 带文字的分隔线
|
||||
* <Divider>或者</Divider>
|
||||
*
|
||||
* // 垂直分隔线
|
||||
* <Divider orientation="vertical" />
|
||||
*
|
||||
* // 不同样式
|
||||
* <Divider variant="dashed" />
|
||||
* <Divider variant="dotted" />
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Divider 变体样式
|
||||
*/
|
||||
const dividerVariants = cva(
|
||||
// 基础样式
|
||||
"border-gray-300",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
solid: "border-solid",
|
||||
dashed: "border-dashed",
|
||||
dotted: "border-dotted",
|
||||
},
|
||||
orientation: {
|
||||
horizontal: "w-full border-t",
|
||||
vertical: "h-full border-l",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "solid",
|
||||
orientation: "horizontal",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type DividerVariant = VariantProps<typeof dividerVariants>["variant"];
|
||||
export type DividerOrientation = VariantProps<typeof dividerVariants>["orientation"];
|
||||
|
||||
export interface DividerProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof dividerVariants> {
|
||||
// 子元素(用于带文字的分隔线)
|
||||
children?: React.ReactNode;
|
||||
// 文字位置(仅水平分隔线有效)
|
||||
labelPosition?: "center" | "left" | "right";
|
||||
}
|
||||
|
||||
/**
|
||||
* Divider 分隔线组件
|
||||
*/
|
||||
export function Divider({
|
||||
variant = "solid",
|
||||
orientation = "horizontal",
|
||||
labelPosition = "center",
|
||||
children,
|
||||
className,
|
||||
...props
|
||||
}: DividerProps) {
|
||||
// 带文字的水平分隔线
|
||||
if (children && orientation === "horizontal") {
|
||||
const labelAlignment = {
|
||||
left: "justify-start",
|
||||
center: "justify-center",
|
||||
right: "justify-end",
|
||||
}[labelPosition];
|
||||
|
||||
return (
|
||||
<div className={cn("flex items-center gap-4 w-full", className)} {...props}>
|
||||
<div className={cn("flex-1 border-t", `border-${variant}`)} />
|
||||
<span className="text-sm text-gray-500 whitespace-nowrap">{children}</span>
|
||||
<div className={cn("flex-1 border-t", `border-${variant}`)} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(dividerVariants({ variant, orientation }), className)}
|
||||
role="separator"
|
||||
aria-orientation={orientation as "horizontal" | "vertical"}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
1
src/design-system/data-display/divider/index.ts
Normal file
1
src/design-system/data-display/divider/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './divider';
|
||||
205
src/design-system/feedback/alert/alert.tsx
Normal file
205
src/design-system/feedback/alert/alert.tsx
Normal file
@@ -0,0 +1,205 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Alert 警告提示组件
|
||||
*
|
||||
* Design System 中的警告提示组件,用于显示重要信息。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认提示
|
||||
* <Alert>这是一条普通提示</Alert>
|
||||
*
|
||||
* // 成功提示
|
||||
* <Alert variant="success">操作成功!</Alert>
|
||||
*
|
||||
* // 错误提示(带标题)
|
||||
* <Alert variant="error" title="错误">
|
||||
* 发生了一些问题
|
||||
* </Alert>
|
||||
*
|
||||
* // 可关闭的提示
|
||||
* <Alert variant="warning" closable onClose={handleClose}>
|
||||
* 请注意此警告
|
||||
* </Alert>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Alert 变体样式
|
||||
*/
|
||||
const alertVariants = cva(
|
||||
// 基础样式
|
||||
"rounded-lg border-2 px-4 py-3 shadow-sm transition-all duration-250",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
info: "border-info-500 bg-info-50 text-info-900",
|
||||
success: "border-success-500 bg-success-50 text-success-900",
|
||||
warning: "border-warning-500 bg-warning-50 text-warning-900",
|
||||
error: "border-error-500 bg-error-50 text-error-900",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "info",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type AlertVariant = VariantProps<typeof alertVariants>["variant"];
|
||||
|
||||
export interface AlertProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof alertVariants> {
|
||||
// 标题
|
||||
title?: string;
|
||||
// 是否可关闭
|
||||
closable?: boolean;
|
||||
// 关闭回调
|
||||
onClose?: () => void;
|
||||
// 自定义图标
|
||||
icon?: React.ReactNode;
|
||||
}
|
||||
|
||||
// 默认图标
|
||||
const defaultIcons = {
|
||||
info: (
|
||||
<svg className="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
success: (
|
||||
<svg className="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
warning: (
|
||||
<svg className="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
error: (
|
||||
<svg className="h-5 w-5" fill="currentColor" viewBox="0 0 20 20">
|
||||
<path
|
||||
fillRule="evenodd"
|
||||
d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z"
|
||||
clipRule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
),
|
||||
};
|
||||
|
||||
/**
|
||||
* Alert 警告提示组件
|
||||
*/
|
||||
export function Alert({
|
||||
variant = "info",
|
||||
title,
|
||||
closable = false,
|
||||
onClose,
|
||||
icon,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: AlertProps) {
|
||||
const [visible, setVisible] = React.useState(true);
|
||||
|
||||
const handleClose = () => {
|
||||
setVisible(false);
|
||||
onClose?.();
|
||||
};
|
||||
|
||||
if (!visible) return null;
|
||||
|
||||
// 确保 variant 有默认值
|
||||
const actualVariant = variant ?? "info";
|
||||
|
||||
// 图标颜色
|
||||
const iconColors = {
|
||||
info: "text-info-500",
|
||||
success: "text-success-500",
|
||||
warning: "text-warning-500",
|
||||
error: "text-error-500",
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(alertVariants({ variant: actualVariant }), className)}
|
||||
role="alert"
|
||||
{...props}
|
||||
>
|
||||
<div className="flex items-start gap-3">
|
||||
{/* 图标 */}
|
||||
<div className={cn("shrink-0", iconColors[actualVariant])}>
|
||||
{icon || defaultIcons[actualVariant]}
|
||||
</div>
|
||||
|
||||
{/* 内容 */}
|
||||
<div className="flex-1 min-w-0">
|
||||
{title && (
|
||||
<h5 className="mb-1 font-semibold leading-tight">{title}</h5>
|
||||
)}
|
||||
<div className="text-sm leading-relaxed">{children}</div>
|
||||
</div>
|
||||
|
||||
{/* 关闭按钮 */}
|
||||
{closable && (
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="shrink-0 rounded-lg p-1 hover:bg-black/5 transition-colors"
|
||||
aria-label="Close"
|
||||
>
|
||||
<svg
|
||||
className="h-4 w-4"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M6 18L18 6M6 6l12 12"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 快捷组件
|
||||
*/
|
||||
export const InfoAlert = (props: Omit<AlertProps, "variant">) => (
|
||||
<Alert variant="info" {...props} />
|
||||
);
|
||||
|
||||
export const SuccessAlert = (props: Omit<AlertProps, "variant">) => (
|
||||
<Alert variant="success" {...props} />
|
||||
);
|
||||
|
||||
export const WarningAlert = (props: Omit<AlertProps, "variant">) => (
|
||||
<Alert variant="warning" {...props} />
|
||||
);
|
||||
|
||||
export const ErrorAlert = (props: Omit<AlertProps, "variant">) => (
|
||||
<Alert variant="error" {...props} />
|
||||
);
|
||||
1
src/design-system/feedback/alert/index.ts
Normal file
1
src/design-system/feedback/alert/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './alert';
|
||||
1
src/design-system/feedback/progress/index.ts
Normal file
1
src/design-system/feedback/progress/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './progress';
|
||||
215
src/design-system/feedback/progress/progress.tsx
Normal file
215
src/design-system/feedback/progress/progress.tsx
Normal file
@@ -0,0 +1,215 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Progress 进度条组件
|
||||
*
|
||||
* Design System 中的进度条组件,用于显示任务完成进度。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认进度条
|
||||
* <Progress value={60} />
|
||||
*
|
||||
* // 不同尺寸
|
||||
* <Progress value={60} size="sm" />
|
||||
* <Progress value={60} size="lg" />
|
||||
*
|
||||
* // 不同变体
|
||||
* <Progress variant="success" value={100} />
|
||||
* <Progress variant="warning" value={75} />
|
||||
* <Progress variant="error" value={30} />
|
||||
*
|
||||
* // 无标签
|
||||
* <Progress value={60} showLabel={false} />
|
||||
*
|
||||
* // 自定义颜色
|
||||
* <Progress value={60} color="#35786f" />
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Progress 变体样式
|
||||
*/
|
||||
const progressVariants = cva(
|
||||
// 基础样式
|
||||
"overflow-hidden rounded-full bg-gray-200 transition-all duration-250",
|
||||
{
|
||||
variants: {
|
||||
size: {
|
||||
sm: "h-1.5",
|
||||
md: "h-2",
|
||||
lg: "h-3",
|
||||
},
|
||||
variant: {
|
||||
default: "",
|
||||
success: "",
|
||||
warning: "",
|
||||
error: "",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
size: "md",
|
||||
variant: "default",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type ProgressSize = VariantProps<typeof progressVariants>["size"];
|
||||
export type ProgressVariant = VariantProps<typeof progressVariants>["variant"];
|
||||
|
||||
export interface ProgressProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof progressVariants> {
|
||||
// 进度值(0-100)
|
||||
value: number;
|
||||
// 是否显示百分比标签
|
||||
showLabel?: boolean;
|
||||
// 自定义标签
|
||||
label?: string;
|
||||
// 是否显示动画
|
||||
animated?: boolean;
|
||||
// 自定义颜色(覆盖 variant)
|
||||
color?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Progress 进度条组件
|
||||
*/
|
||||
export function Progress({
|
||||
value = 0,
|
||||
size = "md",
|
||||
variant = "default",
|
||||
showLabel = true,
|
||||
label,
|
||||
animated = true,
|
||||
color,
|
||||
className,
|
||||
...props
|
||||
}: ProgressProps) {
|
||||
// 确保值在 0-100 之间
|
||||
const clampedValue = Math.min(100, Math.max(0, value));
|
||||
|
||||
// 计算颜色
|
||||
const getColor = () => {
|
||||
if (color) return color;
|
||||
const colors = {
|
||||
default: "bg-primary-500",
|
||||
success: "bg-success-500",
|
||||
warning: "bg-warning-500",
|
||||
error: "bg-error-500",
|
||||
};
|
||||
const actualVariant = variant ?? "default";
|
||||
return colors[actualVariant];
|
||||
};
|
||||
|
||||
// 格式化标签
|
||||
const formatLabel = () => {
|
||||
if (label !== undefined) return label;
|
||||
return `${Math.round(clampedValue)}%`;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn("w-full", className)} {...props}>
|
||||
<div className="flex items-center justify-between mb-1">
|
||||
<div className="flex-1">
|
||||
<div
|
||||
className={cn(progressVariants({ size, variant }))}
|
||||
role="progressbar"
|
||||
aria-valuenow={clampedValue}
|
||||
aria-valuemin={0}
|
||||
aria-valuemax={100}
|
||||
>
|
||||
<div
|
||||
className={cn(
|
||||
"h-full rounded-full transition-all duration-500 ease-out",
|
||||
getColor(),
|
||||
animated && "animate-pulse"
|
||||
)}
|
||||
style={{ width: `${clampedValue}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{showLabel && (
|
||||
<div className="ml-3 text-sm font-medium text-gray-700 min-w-[3rem] text-right">
|
||||
{formatLabel()}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* CircularProgress - 环形进度条
|
||||
*/
|
||||
export interface CircularProgressProps extends React.SVGProps<SVGSVGElement> {
|
||||
value: number;
|
||||
size?: number;
|
||||
strokeWidth?: number;
|
||||
variant?: ProgressVariant;
|
||||
showLabel?: boolean;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
export function CircularProgress({
|
||||
value = 0,
|
||||
size = 120,
|
||||
strokeWidth = 8,
|
||||
variant = "default",
|
||||
showLabel = true,
|
||||
label,
|
||||
className,
|
||||
...props
|
||||
}: CircularProgressProps) {
|
||||
const clampedValue = Math.min(100, Math.max(0, value));
|
||||
const radius = (size - strokeWidth) / 2;
|
||||
const circumference = 2 * Math.PI * radius;
|
||||
const offset = circumference - (clampedValue / 100) * circumference;
|
||||
|
||||
const colors = {
|
||||
default: "#35786f",
|
||||
success: "#22c55e",
|
||||
warning: "#f59e0b",
|
||||
error: "#ef4444",
|
||||
};
|
||||
const strokeColor = colors[variant ?? "default"];
|
||||
|
||||
return (
|
||||
<div className={cn("inline-flex items-center justify-center", className)}>
|
||||
<svg width={size} height={size} {...props}>
|
||||
{/* 背景圆 */}
|
||||
<circle
|
||||
cx={size / 2}
|
||||
cy={size / 2}
|
||||
r={radius}
|
||||
fill="none"
|
||||
stroke="#e5e7eb"
|
||||
strokeWidth={strokeWidth}
|
||||
/>
|
||||
{/* 进度圆 */}
|
||||
<circle
|
||||
cx={size / 2}
|
||||
cy={size / 2}
|
||||
r={radius}
|
||||
fill="none"
|
||||
stroke={strokeColor}
|
||||
strokeWidth={strokeWidth}
|
||||
strokeDasharray={circumference}
|
||||
strokeDashoffset={offset}
|
||||
strokeLinecap="round"
|
||||
transform={`rotate(-90 ${size / 2} ${size / 2})`}
|
||||
className="transition-all duration-500 ease-out"
|
||||
/>
|
||||
</svg>
|
||||
{showLabel && (
|
||||
<div className="absolute text-base font-semibold text-gray-700">
|
||||
{label !== undefined ? label : `${Math.round(clampedValue)}%`}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/design-system/feedback/skeleton/index.ts
Normal file
1
src/design-system/feedback/skeleton/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './skeleton';
|
||||
192
src/design-system/feedback/skeleton/skeleton.tsx
Normal file
192
src/design-system/feedback/skeleton/skeleton.tsx
Normal file
@@ -0,0 +1,192 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Skeleton 骨架屏组件
|
||||
*
|
||||
* Design System 中的骨架屏组件,用于内容加载时的占位显示。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认骨架屏
|
||||
* <Skeleton className="h-4 w-32" />
|
||||
*
|
||||
* // 不同变体
|
||||
* <Skeleton variant="text" />
|
||||
* <Skeleton variant="circular" className="h-12 w-12" />
|
||||
* <Skeleton variant="rectangular" className="h-32 w-full" />
|
||||
*
|
||||
* // 自定义动画
|
||||
* <Skeleton animated={false} />
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Skeleton 变体样式
|
||||
*/
|
||||
const skeletonVariants = cva(
|
||||
// 基础样式
|
||||
"shrink-0 animate-pulse rounded",
|
||||
{
|
||||
variants: {
|
||||
variant: {
|
||||
text: "h-4 w-full",
|
||||
circular: "rounded-full",
|
||||
rectangular: "rounded-lg",
|
||||
},
|
||||
animated: {
|
||||
true: "animate-pulse",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
variant: "text",
|
||||
animated: true,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type SkeletonVariant = VariantProps<typeof skeletonVariants>["variant"];
|
||||
|
||||
export interface SkeletonProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof skeletonVariants> {}
|
||||
|
||||
/**
|
||||
* Skeleton 骨架屏组件
|
||||
*/
|
||||
export function Skeleton({
|
||||
variant = "text",
|
||||
animated = true,
|
||||
className,
|
||||
...props
|
||||
}: SkeletonProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"bg-gray-200",
|
||||
skeletonVariants({ variant, animated }),
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 预设的骨架屏组合
|
||||
*/
|
||||
|
||||
/**
|
||||
* 文本骨架屏(多行)
|
||||
*/
|
||||
export interface TextSkeletonProps {
|
||||
lines?: number;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function TextSkeleton({ lines = 3, className }: TextSkeletonProps) {
|
||||
return (
|
||||
<div className={cn("space-y-2", className)}>
|
||||
{Array.from({ length: lines }).map((_, i) => (
|
||||
<Skeleton
|
||||
key={i}
|
||||
variant="text"
|
||||
className={cn(i === lines - 1 && "w-3/4")}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 卡片骨架屏
|
||||
*/
|
||||
export interface CardSkeletonProps {
|
||||
showAvatar?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function CardSkeleton({
|
||||
showAvatar = false,
|
||||
className,
|
||||
}: CardSkeletonProps) {
|
||||
return (
|
||||
<div className={cn("p-6 space-y-4", className)}>
|
||||
{showAvatar && (
|
||||
<div className="flex items-center space-x-4">
|
||||
<Skeleton variant="circular" className="h-12 w-12" />
|
||||
<div className="space-y-2 flex-1">
|
||||
<Skeleton variant="text" className="w-1/3" />
|
||||
<Skeleton variant="text" className="w-1/4" />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<TextSkeleton lines={3} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表项骨架屏
|
||||
*/
|
||||
export interface ListItemSkeletonProps {
|
||||
showAvatar?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function ListItemSkeleton({
|
||||
showAvatar = true,
|
||||
className,
|
||||
}: ListItemSkeletonProps) {
|
||||
return (
|
||||
<div className={cn("flex items-center space-x-4 p-4", className)}>
|
||||
{showAvatar && <Skeleton variant="circular" className="h-10 w-10" />}
|
||||
<div className="space-y-2 flex-1">
|
||||
<Skeleton variant="text" className="w-3/4" />
|
||||
<Skeleton variant="text" className="w-1/2" />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 表格骨架屏
|
||||
*/
|
||||
export interface TableSkeletonProps {
|
||||
rows?: number;
|
||||
columns?: number;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function TableSkeleton({
|
||||
rows = 5,
|
||||
columns = 4,
|
||||
className,
|
||||
}: TableSkeletonProps) {
|
||||
return (
|
||||
<div className={cn("space-y-2", className)}>
|
||||
{/* 表头 */}
|
||||
<div className="flex space-x-4">
|
||||
{Array.from({ length: columns }).map((_, i) => (
|
||||
<Skeleton key={`header-${i}`} variant="text" className="flex-1" />
|
||||
))}
|
||||
</div>
|
||||
{/* 表体 */}
|
||||
{Array.from({ length: rows }).map((_, rowIndex) => (
|
||||
<div key={`row-${rowIndex}`} className="flex space-x-4">
|
||||
{Array.from({ length: columns }).map((_, colIndex) => (
|
||||
<Skeleton
|
||||
key={`cell-${rowIndex}-${colIndex}`}
|
||||
variant="text"
|
||||
className="flex-1"
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/design-system/feedback/toast/index.ts
Normal file
1
src/design-system/feedback/toast/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './toast';
|
||||
108
src/design-system/feedback/toast/toast.tsx
Normal file
108
src/design-system/feedback/toast/toast.tsx
Normal file
@@ -0,0 +1,108 @@
|
||||
"use client";
|
||||
|
||||
/**
|
||||
* Toast 组件
|
||||
*
|
||||
* 基于项目已安装的 sonner 库封装的 Toast 通知组件。
|
||||
* 提供类型安全的 API 和预设样式。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* import { toast } from '@/design-system/feedback/toast';
|
||||
*
|
||||
* // 基础用法
|
||||
* toast.success("操作成功!");
|
||||
* toast.error("发生错误");
|
||||
* toast.warning("请注意");
|
||||
* toast.info("提示信息");
|
||||
*
|
||||
* // 自定义选项
|
||||
* toast.success("操作成功!", {
|
||||
* description: "您的更改已保存",
|
||||
* duration: 5000,
|
||||
* });
|
||||
*
|
||||
* // Promise Toast
|
||||
* toast.promise(
|
||||
* fetchData(),
|
||||
* {
|
||||
* loading: "加载中...",
|
||||
* success: "加载成功",
|
||||
* error: "加载失败",
|
||||
* }
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
|
||||
import { toast as sonnerToast } from "sonner";
|
||||
|
||||
export type ToastProps = {
|
||||
description?: string;
|
||||
duration?: number;
|
||||
id?: string;
|
||||
onDismiss?: () => void;
|
||||
};
|
||||
|
||||
/**
|
||||
* Toast 通知组件
|
||||
*/
|
||||
export const toast = {
|
||||
success: (message: string, props?: ToastProps) => {
|
||||
return sonnerToast.success(message, {
|
||||
description: props?.description,
|
||||
duration: props?.duration,
|
||||
id: props?.id,
|
||||
onDismiss: props?.onDismiss,
|
||||
});
|
||||
},
|
||||
|
||||
error: (message: string, props?: ToastProps) => {
|
||||
return sonnerToast.error(message, {
|
||||
description: props?.description,
|
||||
duration: props?.duration,
|
||||
id: props?.id,
|
||||
onDismiss: props?.onDismiss,
|
||||
});
|
||||
},
|
||||
|
||||
warning: (message: string, props?: ToastProps) => {
|
||||
return sonnerToast.warning(message, {
|
||||
description: props?.description,
|
||||
duration: props?.duration,
|
||||
id: props?.id,
|
||||
onDismiss: props?.onDismiss,
|
||||
});
|
||||
},
|
||||
|
||||
info: (message: string, props?: ToastProps) => {
|
||||
return sonnerToast.info(message, {
|
||||
description: props?.description,
|
||||
duration: props?.duration,
|
||||
id: props?.id,
|
||||
onDismiss: props?.onDismiss,
|
||||
});
|
||||
},
|
||||
|
||||
promise: <T,>(
|
||||
promise: Promise<T>,
|
||||
{
|
||||
loading,
|
||||
success,
|
||||
error,
|
||||
}: {
|
||||
loading: string;
|
||||
success: string | ((data: T) => string);
|
||||
error: string | ((error: Error) => string);
|
||||
}
|
||||
) => {
|
||||
return sonnerToast.promise(promise, {
|
||||
loading,
|
||||
success,
|
||||
error,
|
||||
});
|
||||
},
|
||||
|
||||
dismiss: (id?: string) => {
|
||||
sonnerToast.dismiss(id);
|
||||
},
|
||||
};
|
||||
52
src/design-system/index.ts
Normal file
52
src/design-system/index.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Design System 统一导出
|
||||
*
|
||||
* 这是 Design System 的主入口,所有组件和工具都可以从这里导入。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 从主入口导入
|
||||
* import { Button, Input, Card } from '@/design-system';
|
||||
*
|
||||
* // 或者从子路径导入(更好的 tree-shaking)
|
||||
* import { Button } from '@/design-system/base/button';
|
||||
* import { Input } from '@/design-system/base/input';
|
||||
* ```
|
||||
*/
|
||||
|
||||
// 设计令牌
|
||||
export * from './tokens';
|
||||
|
||||
// 工具函数
|
||||
export * from './lib/utils';
|
||||
|
||||
// 基础组件
|
||||
export * from './base/button';
|
||||
export * from './base/input';
|
||||
export * from './base/textarea';
|
||||
export * from './base/card';
|
||||
export * from './base/checkbox';
|
||||
export * from './base/radio';
|
||||
export * from './base/switch';
|
||||
export * from './base/select';
|
||||
|
||||
// 反馈组件
|
||||
export * from './feedback/alert';
|
||||
export * from './feedback/progress';
|
||||
export * from './feedback/skeleton';
|
||||
export * from './feedback/toast';
|
||||
|
||||
// 覆盖组件
|
||||
export * from './overlay/modal';
|
||||
|
||||
// 数据展示组件
|
||||
export * from './data-display/badge';
|
||||
export * from './data-display/divider';
|
||||
|
||||
// 布局组件
|
||||
export * from './layout/container';
|
||||
export * from './layout/grid';
|
||||
export * from './layout/stack';
|
||||
|
||||
// 导航组件
|
||||
export * from './navigation/tabs';
|
||||
103
src/design-system/layout/container/container.tsx
Normal file
103
src/design-system/layout/container/container.tsx
Normal file
@@ -0,0 +1,103 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Container 容器组件
|
||||
*
|
||||
* Design System 中的容器组件,用于约束内容宽度并居中。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认容器
|
||||
* <Container>
|
||||
* <p>内容被居中并限制最大宽度</p>
|
||||
* </Container>
|
||||
*
|
||||
* // 不同尺寸
|
||||
* <Container size="sm">小容器</Container>
|
||||
* <Container size="lg">大容器</Container>
|
||||
*
|
||||
* // 全宽容器
|
||||
* <Container fullWidth>全宽容器</Container>
|
||||
*
|
||||
* // 带内边距
|
||||
* <Container padding="xl">带内边距的容器</Container>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Container 变体样式
|
||||
*/
|
||||
const containerVariants = cva(
|
||||
// 基础样式
|
||||
"mx-auto",
|
||||
{
|
||||
variants: {
|
||||
size: {
|
||||
xs: "max-w-xs",
|
||||
sm: "max-w-sm",
|
||||
md: "max-w-md",
|
||||
lg: "max-w-lg",
|
||||
xl: "max-w-xl",
|
||||
"2xl": "max-w-2xl",
|
||||
"3xl": "max-w-3xl",
|
||||
"4xl": "max-w-4xl",
|
||||
"5xl": "max-w-5xl",
|
||||
"6xl": "max-w-6xl",
|
||||
"7xl": "max-w-7xl",
|
||||
full: "max-w-full",
|
||||
},
|
||||
padding: {
|
||||
none: "",
|
||||
xs: "px-2",
|
||||
sm: "px-4",
|
||||
md: "px-6",
|
||||
lg: "px-8",
|
||||
xl: "px-10",
|
||||
},
|
||||
fullWidth: {
|
||||
true: "w-full",
|
||||
false: "",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
size: "7xl",
|
||||
padding: "md",
|
||||
fullWidth: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type ContainerSize = VariantProps<typeof containerVariants>["size"];
|
||||
export type ContainerPadding = VariantProps<typeof containerVariants>["padding"];
|
||||
|
||||
export interface ContainerProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof containerVariants> {
|
||||
// 子元素
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Container 容器组件
|
||||
*/
|
||||
export function Container({
|
||||
size = "7xl",
|
||||
padding = "md",
|
||||
fullWidth = false,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: ContainerProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn(containerVariants({ size, padding, fullWidth }), className)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/design-system/layout/container/index.ts
Normal file
1
src/design-system/layout/container/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './container';
|
||||
179
src/design-system/layout/grid/grid.tsx
Normal file
179
src/design-system/layout/grid/grid.tsx
Normal file
@@ -0,0 +1,179 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Grid 网格布局组件
|
||||
*
|
||||
* Design System 中的网格布局组件,基于 CSS Grid。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 默认网格(2列)
|
||||
* <Grid>
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* </Grid>
|
||||
*
|
||||
* // 自定义列数
|
||||
* <Grid cols={3}>
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* <div>项目 3</div>
|
||||
* </Grid>
|
||||
*
|
||||
* // 响应式网格
|
||||
* <Grid cols={{ sm: 1, md: 2, lg: 3 }}>
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* <div>项目 3</div>
|
||||
* </Grid>
|
||||
*
|
||||
* // 带间距的网格
|
||||
* <Grid gap={4}>
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* </Grid>
|
||||
* ```
|
||||
*/
|
||||
|
||||
export interface ResponsiveValue {
|
||||
sm?: number;
|
||||
md?: number;
|
||||
lg?: number;
|
||||
xl?: number;
|
||||
"2xl"?: number;
|
||||
}
|
||||
|
||||
export interface GridProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
// 列数
|
||||
cols?: number | ResponsiveValue;
|
||||
// 行间距
|
||||
rowGap?: number | string;
|
||||
// 列间距
|
||||
colGap?: number | string;
|
||||
// 间距(同时设置行列间距)
|
||||
gap?: number | string;
|
||||
// 子元素
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成网格类名
|
||||
*/
|
||||
function generateGridClass(cols?: number | ResponsiveValue): string {
|
||||
if (!cols) return "grid-cols-1 md:grid-cols-2";
|
||||
|
||||
if (typeof cols === "number") {
|
||||
return `grid-cols-${cols}`;
|
||||
}
|
||||
|
||||
// 响应式列数
|
||||
const classes = ["grid-cols-1"]; // 默认 1 列
|
||||
|
||||
if (cols.sm) classes.push(`sm:grid-cols-${cols.sm}`);
|
||||
if (cols.md) classes.push(`md:grid-cols-${cols.md}`);
|
||||
if (cols.lg) classes.push(`lg:grid-cols-${cols.lg}`);
|
||||
if (cols.xl) classes.push(`xl:grid-cols-${cols.xl}`);
|
||||
if (cols["2xl"]) classes.push(`"2xl":grid-cols-${cols["2xl"]}`);
|
||||
|
||||
// 如果没有指定 md,使用默认 2 列
|
||||
if (!cols.md && !cols.lg && !cols.xl && !cols["2xl"]) {
|
||||
classes.push("md:grid-cols-2");
|
||||
}
|
||||
|
||||
return classes.join(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Grid 网格布局组件
|
||||
*/
|
||||
export function Grid({
|
||||
cols,
|
||||
rowGap,
|
||||
colGap,
|
||||
gap,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: GridProps) {
|
||||
const gridClass = generateGridClass(cols);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
"grid",
|
||||
gridClass,
|
||||
gap && `gap-${gap}`,
|
||||
rowGap && `gap-y-${rowGap}`,
|
||||
colGap && `gap-x-${colGap}`,
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* GridItem - 网格项
|
||||
*/
|
||||
export interface GridItemProps extends React.HTMLAttributes<HTMLDivElement> {
|
||||
// 列跨度
|
||||
colSpan?: number | ResponsiveValue;
|
||||
// 行跨度
|
||||
rowSpan?: number;
|
||||
// 子元素
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成跨度类名
|
||||
*/
|
||||
function generateSpanClass(
|
||||
type: "col" | "row",
|
||||
span?: number | ResponsiveValue
|
||||
): string {
|
||||
if (!span) return "";
|
||||
|
||||
if (typeof span === "number") {
|
||||
return `${type === "col" ? "col" : "row"}-span-${span}`;
|
||||
}
|
||||
|
||||
// 响应式跨度
|
||||
const classes: string[] = [];
|
||||
|
||||
if (span.sm) classes.push(`sm:${type === "col" ? "col" : "row"}-span-${span.sm}`);
|
||||
if (span.md) classes.push(`md:${type === "col" ? "col" : "row"}-span-${span.md}`);
|
||||
if (span.lg) classes.push(`lg:${type === "col" ? "col" : "row"}-span-${span.lg}`);
|
||||
if (span.xl) classes.push(`xl:${type === "col" ? "col" : "row"}-span-${span.xl}`);
|
||||
if (span["2xl"]) classes.push(`"2xl":${type === "col" ? "col" : "row"}-span-${span["2xl"]}`);
|
||||
|
||||
return classes.join(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* GridItem 网格项组件
|
||||
*/
|
||||
export function GridItem({
|
||||
colSpan,
|
||||
rowSpan,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: GridItemProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
generateSpanClass("col", colSpan),
|
||||
generateSpanClass("row", rowSpan),
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/design-system/layout/grid/index.ts
Normal file
1
src/design-system/layout/grid/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './grid';
|
||||
1
src/design-system/layout/stack/index.ts
Normal file
1
src/design-system/layout/stack/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './stack';
|
||||
140
src/design-system/layout/stack/stack.tsx
Normal file
140
src/design-system/layout/stack/stack.tsx
Normal file
@@ -0,0 +1,140 @@
|
||||
"use client";
|
||||
|
||||
import React from "react";
|
||||
import { cva, type VariantProps } from "class-variance-authority";
|
||||
import { cn } from "@/design-system/lib/utils";
|
||||
|
||||
/**
|
||||
* Stack 堆叠布局组件
|
||||
*
|
||||
* Design System 中的堆叠布局组件,用于垂直或水平排列子元素。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* // 垂直堆叠
|
||||
* <Stack>
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* <div>项目 3</div>
|
||||
* </Stack>
|
||||
*
|
||||
* // 水平堆叠
|
||||
* <Stack direction="row">
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* <div>项目 3</div>
|
||||
* </Stack>
|
||||
*
|
||||
* // 自定义间距
|
||||
* <Stack gap={4}>
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* </Stack>
|
||||
*
|
||||
* // 居中对齐
|
||||
* <Stack align="center">
|
||||
* <div>项目 1</div>
|
||||
* <div>项目 2</div>
|
||||
* </Stack>
|
||||
* ```
|
||||
*/
|
||||
|
||||
/**
|
||||
* Stack 变体样式
|
||||
*/
|
||||
const stackVariants = cva(
|
||||
// 基础样式
|
||||
"flex",
|
||||
{
|
||||
variants: {
|
||||
direction: {
|
||||
column: "flex-col",
|
||||
row: "flex-row",
|
||||
},
|
||||
align: {
|
||||
start: "items-start",
|
||||
center: "items-center",
|
||||
end: "items-end",
|
||||
stretch: "items-stretch",
|
||||
},
|
||||
justify: {
|
||||
start: "justify-start",
|
||||
center: "justify-center",
|
||||
end: "justify-end",
|
||||
between: "justify-between",
|
||||
},
|
||||
wrap: {
|
||||
true: "flex-wrap",
|
||||
false: "flex-nowrap",
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
direction: "column",
|
||||
align: "start",
|
||||
justify: "start",
|
||||
wrap: false,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export type StackDirection = VariantProps<typeof stackVariants>["direction"];
|
||||
export type StackAlign = VariantProps<typeof stackVariants>["align"];
|
||||
export type StackJustify = VariantProps<typeof stackVariants>["justify"];
|
||||
|
||||
export interface StackProps
|
||||
extends React.HTMLAttributes<HTMLDivElement>,
|
||||
VariantProps<typeof stackVariants> {
|
||||
// 子元素
|
||||
children: React.ReactNode;
|
||||
// 间距(使用 Tailwind spacing)
|
||||
gap?: number | string;
|
||||
// 是否为内联布局
|
||||
inline?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stack 堆叠布局组件
|
||||
*/
|
||||
export function Stack({
|
||||
direction = "column",
|
||||
align = "start",
|
||||
justify = "start",
|
||||
wrap = false,
|
||||
gap,
|
||||
inline = false,
|
||||
className,
|
||||
children,
|
||||
...props
|
||||
}: StackProps) {
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
stackVariants({ direction, align, justify, wrap }),
|
||||
gap && (typeof gap === "number" ? `gap-${gap}` : `gap-[${gap}]`),
|
||||
inline && "inline-flex",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* VStack - 垂直堆叠组件(快捷方式)
|
||||
*/
|
||||
export interface VStackProps extends Omit<StackProps, "direction"> {}
|
||||
|
||||
export function VStack(props: VStackProps) {
|
||||
return <Stack direction="column" {...props} />;
|
||||
}
|
||||
|
||||
/**
|
||||
* HStack - 水平堆叠组件(快捷方式)
|
||||
*/
|
||||
export interface HStackProps extends Omit<StackProps, "direction"> {}
|
||||
|
||||
export function HStack(props: HStackProps) {
|
||||
return <Stack direction="row" {...props} />;
|
||||
}
|
||||
23
src/design-system/lib/utils.ts
Normal file
23
src/design-system/lib/utils.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { type ClassValue, clsx } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
/**
|
||||
* 合并 Tailwind CSS 类名的工具函数
|
||||
*
|
||||
* 使用 clsx 处理条件类名,然后使用 tailwind-merge 解决 Tailwind 类名冲突
|
||||
*
|
||||
* @param inputs - 类名(字符串、对象、数组等)
|
||||
* @returns 合并后的类名字符串
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* cn('px-4 py-2', isActive && 'bg-primary-500', 'text-white')
|
||||
* // => 'px-4 py-2 bg-primary-500 text-white'
|
||||
*
|
||||
* cn('px-4 px-6') // 自动解决冲突
|
||||
* // => 'px-6'
|
||||
* ```
|
||||
*/
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
1
src/design-system/navigation/tabs/index.ts
Normal file
1
src/design-system/navigation/tabs/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './tabs';
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user