This commit is contained in:
ryan
2026-07-13 15:17:45 +08:00
parent 08e61ce833
commit e0eb4e5975
334 changed files with 17212 additions and 12321 deletions
+1
View File
@@ -43,6 +43,7 @@
- 所有 HTTP 路由仅在 `internal/router/router.go` 中注册。
- 当 API Handler 发生变化时,更新 Swagger 文档(运行 `make swagger`)。
- 在完成代码开发后必须运行 `make code-check`, 并修复报错。
- 在完成代码开发后/git 提交前必须运行 `make prettier`进行格式化。
- 需要缓存或文件管理能力时,必须复用现有平台实现,禁止在业务包中自行创建缓存目录、直接管理缓存文件或重复封装存储后端。
- 文件摄取必须通过 `upload.Ingest`(`upload.PolicyCreate` / `PolicyDedupNewRecord` / `PolicyResolveExisting`);删除必须通过 `upload.Remove` 或 `upload.RemoveOwned`。禁止业务模块直接调用 `repository.CreateUpload` / `repository.SoftDeleteUpload`,禁止 `db.Create(&model.Upload{})` 旁路写 `w_uploads`。
- 禁止在 `init()` 中注册跨模块集成(任务 Handler、推送内置事件、域事件监听器、任务完成钩子)。统一通过 `internal/bootstrap` 在 `internal/cmd` 入口显式装配。
+2
View File
@@ -22,11 +22,13 @@ Modern frontend application for the Wavelet scaffold framework.
### Installation
1. Install dependencies:
```bash
pnpm install
```
2. Run the development server:
```bash
pnpm dev
```
+2
View File
@@ -22,11 +22,13 @@ Wavelet 脚手架系统的现代化前端应用。
### 安装
1. 安装依赖:
```bash
pnpm install
```
2. 运行开发服务器:
```bash
pnpm dev
```
+3 -3
View File
@@ -1,10 +1,10 @@
import { Suspense } from "react"
import { LoginPage } from "@/components/auth/login-page"
import { Suspense } from 'react';
import { LoginPage } from '@/components/auth/login-page';
export default function Page() {
return (
<Suspense>
<LoginPage />
</Suspense>
)
);
}
+3 -3
View File
@@ -1,10 +1,10 @@
import {Suspense} from "react"
import {RegisterPage} from "@/components/auth/register-page"
import { Suspense } from 'react';
import { RegisterPage } from '@/components/auth/register-page';
export default function Page() {
return (
<Suspense>
<RegisterPage />
</Suspense>
)
);
}
+7 -5
View File
@@ -1,15 +1,17 @@
import {LegalPageLayout} from "@/components/common/docs/legal-page-layout";
import {apiSections, DOCS_LAST_UPDATED} from "@/components/common/docs/api";
import { LegalPageLayout } from '@/components/common/docs/legal-page-layout';
import { apiSections, DOCS_LAST_UPDATED } from '@/components/common/docs/api';
export default function ApiDocPage() {
return (
<LegalPageLayout
title="API 接口文档"
title='API 接口文档'
lastUpdated={DOCS_LAST_UPDATED}
sections={apiSections}
description={
<p className="text-muted-foreground text-sm leading-relaxed">
Wavelet 提供简单、强大的 API 接口,支持多种编程语言和开发环境。通过标准化的 RESTful 接口,您可以轻松地与后端服务进行集成与二次开发。
<p className='text-muted-foreground text-sm leading-relaxed'>
Wavelet 提供简单、强大的 API
接口,支持多种编程语言和开发环境。通过标准化的 RESTful
接口,您可以轻松地与后端服务进行集成与二次开发。
</p>
}
/>
+7 -6
View File
@@ -1,16 +1,17 @@
import {LegalPageLayout} from "@/components/common/docs/legal-page-layout";
import {howToUseSections} from "@/components/common/docs/how-to-use";
import {DOCS_LAST_UPDATED} from "@/components/common/docs/api";
import { LegalPageLayout } from '@/components/common/docs/legal-page-layout';
import { howToUseSections } from '@/components/common/docs/how-to-use';
import { DOCS_LAST_UPDATED } from '@/components/common/docs/api';
export default function HowToUsePage() {
return (
<LegalPageLayout
title="平台使用指南"
title='平台使用指南'
lastUpdated={DOCS_LAST_UPDATED}
sections={howToUseSections}
description={
<p className="text-muted-foreground text-sm leading-relaxed">
欢迎使用 Wavelet 脚手架系统。本文档将引导您快速了解平台的核心组件、角色权限设计及二次开发流程,帮助您快速上手开发。
<p className='text-muted-foreground text-sm leading-relaxed'>
欢迎使用 Wavelet
脚手架系统。本文档将引导您快速了解平台的核心组件、角色权限设计及二次开发流程,帮助您快速上手开发。
</p>
}
/>
@@ -1,20 +1,20 @@
import { LegalPageLayout } from "@/components/common/docs/legal-page-layout"
import { privacySections } from "@/components/common/docs/privacy"
import { TERMS_LAST_UPDATED } from "@/components/common/docs/terms"
import { LegalPageLayout } from '@/components/common/docs/legal-page-layout';
import { privacySections } from '@/components/common/docs/privacy';
import { TERMS_LAST_UPDATED } from '@/components/common/docs/terms';
export default function PrivacyPolicyPage() {
return (
<LegalPageLayout
title="隐私政策 (Privacy)"
title='隐私政策 (Privacy)'
lastUpdated={TERMS_LAST_UPDATED}
sections={privacySections}
description={
<span>
我们非常重视您的隐私保护。本政策将向您透明地展示我们如何收集、使用、存储您的个人信息,
<br className="hidden md:block" />
<br className='hidden md:block' />
以及您享有的相关权利。
</span>
}
/>
)
);
}
@@ -1,17 +1,16 @@
import { LegalPageLayout } from "@/components/common/docs/legal-page-layout"
import { termsSections, TERMS_LAST_UPDATED } from "@/components/common/docs/terms"
import { LegalPageLayout } from '@/components/common/docs/legal-page-layout';
import {
TERMS_LAST_UPDATED,
termsSections,
} from '@/components/common/docs/terms';
export default function TermsOfServicePage() {
return (
<LegalPageLayout
title="服务协议 (Terms)"
title='服务协议 (Terms)'
lastUpdated={TERMS_LAST_UPDATED}
sections={termsSections}
description={
<span>
为了保障您的合法权益,请您仔细阅读以下条款。
</span>
}
description={<span>为了保障您的合法权益,请您仔细阅读以下条款。</span>}
/>
)
);
}
@@ -1,16 +1,22 @@
"use client"
'use client';
import * as React from "react"
import {useCallback, useEffect, useState} from "react"
import {toast} from "sonner"
import {HardDrive, RefreshCw, Trash2} from "lucide-react"
import * as React from 'react';
import { useCallback, useEffect, useState } from 'react';
import { toast } from 'sonner';
import { HardDrive, RefreshCw, Trash2 } from 'lucide-react';
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Button} from "@/components/ui/button"
import {Skeleton} from "@/components/ui/skeleton"
import {Label} from "@/components/ui/label"
import {Input} from "@/components/ui/input"
import {Switch} from "@/components/ui/switch"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Skeleton } from '@/components/ui/skeleton';
import { Label } from '@/components/ui/label';
import { Input } from '@/components/ui/input';
import { Switch } from '@/components/ui/switch';
import {
Dialog,
DialogContent,
@@ -18,183 +24,205 @@ import {
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import type {CacheStatus} from "@/lib/services/admin"
import services from "@/lib/services"
} from '@/components/ui/dialog';
import type { CacheStatus } from '@/lib/services/admin';
import services from '@/lib/services';
/**
* 格式化字节大小
*/
const formatBytes = (bytes: number) => {
if (bytes === 0) return "0 Bytes"
const k = 1024
const sizes = ["Bytes", "KB", "MB", "GB", "TB"]
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]
}
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
};
/**
* 格式化数字,每3位加逗号
*/
const formatNumber = (num: number | string) => {
if (num === undefined || num === null) return "0"
return num.toString().replace(/\B(?=(\d{3})+(?!\B))/g, ",")
}
if (num === undefined || num === null) return '0';
return num.toString().replace(/\B(?=(\d{3})+(?!\B))/g, ',');
};
interface CacheManagerProps {
refreshTrigger: number
refreshTrigger: number;
}
export function CacheManager({ refreshTrigger }: CacheManagerProps) {
const [cacheStatus, setCacheStatus] = useState<CacheStatus | null>(null)
const [loadingCache, setLoadingCache] = useState<boolean>(true)
const [savingConfig, setSavingConfig] = useState<boolean>(false)
const [clearingCache, setClearingCache] = useState<boolean>(false)
const [showClearConfirm, setShowClearConfirm] = useState<boolean>(false)
const [cacheStatus, setCacheStatus] = useState<CacheStatus | null>(null);
const [loadingCache, setLoadingCache] = useState<boolean>(true);
const [savingConfig, setSavingConfig] = useState<boolean>(false);
const [clearingCache, setClearingCache] = useState<boolean>(false);
const [showClearConfirm, setShowClearConfirm] = useState<boolean>(false);
// 策略配置表单状态
const [maxSizeMB, setMaxSizeMB] = useState<string>("100")
const [ttlMinutes, setTtlMinutes] = useState<string>("60")
const [lruEnabled, setLruEnabled] = useState<boolean>(true)
const [maxSizeMB, setMaxSizeMB] = useState<string>('100');
const [ttlMinutes, setTtlMinutes] = useState<string>('60');
const [lruEnabled, setLruEnabled] = useState<boolean>(true);
// 获取磁盘缓存状态
const fetchCacheStatus = useCallback(async (isSilent = false) => {
if (!isSilent) setLoadingCache(true)
if (!isSilent) setLoadingCache(true);
try {
const data = await services.adminCache.getCacheStatus()
setCacheStatus(data)
setMaxSizeMB(data.max_size_mb.toString())
setTtlMinutes(data.ttl_minutes.toString())
setLruEnabled(data.lru_enabled)
const data = await services.adminCache.getCacheStatus();
setCacheStatus(data);
setMaxSizeMB(data.max_size_mb.toString());
setTtlMinutes(data.ttl_minutes.toString());
setLruEnabled(data.lru_enabled);
} catch (err) {
toast.error("获取磁盘缓存状态失败", {
description: err instanceof Error ? err.message : "未知错误",
})
toast.error('获取磁盘缓存状态失败', {
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setLoadingCache(false)
setLoadingCache(false);
}
}, [])
}, []);
// 保存配置
const handleSaveConfig = async (e: React.FormEvent) => {
e.preventDefault()
const size = parseInt(maxSizeMB, 10)
const ttl = parseInt(ttlMinutes, 10)
e.preventDefault();
const size = parseInt(maxSizeMB, 10);
const ttl = parseInt(ttlMinutes, 10);
if (isNaN(size) || size < 1) {
toast.error("保存失败", { description: "最大容量限制必须是大于等于 1 的整数" })
return
toast.error('保存失败', {
description: '最大容量限制必须是大于等于 1 的整数',
});
return;
}
if (isNaN(ttl) || ttl < 0) {
toast.error("保存失败", { description: "默认过期时间必须是大于等于 0 的整数" })
return
toast.error('保存失败', {
description: '默认过期时间必须是大于等于 0 的整数',
});
return;
}
setSavingConfig(true)
setSavingConfig(true);
try {
await services.adminCache.updateCacheConfig({
max_size_mb: size,
ttl_minutes: ttl,
lru_enabled: lruEnabled,
})
toast.success("保存成功", { description: "磁盘缓存策略已热更新" })
await fetchCacheStatus(true)
});
toast.success('保存成功', { description: '磁盘缓存策略已热更新' });
await fetchCacheStatus(true);
} catch (err) {
toast.error("保存配置失败", {
description: err instanceof Error ? err.message : "未知错误",
})
toast.error('保存配置失败', {
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setSavingConfig(false)
}
setSavingConfig(false);
}
};
// 清空磁盘缓存数据
const handleClearCache = async () => {
setClearingCache(true)
setClearingCache(true);
try {
await services.adminCache.clearCache()
toast.success("清空成功", { description: "缓存数据已全部清除" })
setShowClearConfirm(false)
await fetchCacheStatus(true)
await services.adminCache.clearCache();
toast.success('清空成功', { description: '缓存数据已全部清除' });
setShowClearConfirm(false);
await fetchCacheStatus(true);
} catch (err) {
toast.error("清空缓存失败", {
description: err instanceof Error ? err.message : "未知错误",
})
toast.error('清空缓存失败', {
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setClearingCache(false)
}
setClearingCache(false);
}
};
// 初始化拉取
useEffect(() => {
fetchCacheStatus()
}, [fetchCacheStatus])
fetchCacheStatus();
}, [fetchCacheStatus]);
// 当外部刷新触发器改变时自动刷新
useEffect(() => {
fetchCacheStatus(true)
}, [refreshTrigger, fetchCacheStatus])
fetchCacheStatus(true);
}, [refreshTrigger, fetchCacheStatus]);
return (
<Card className="border-border/40 bg-card/50 backdrop-blur-sm shadow-sm">
<CardHeader className="pb-3 border-b border-dashed flex flex-col md:flex-row md:items-center md:justify-between gap-4">
<div className="space-y-0.5">
<div className="flex items-center gap-2">
<HardDrive className="size-4 text-primary animate-pulse" />
<CardTitle className="text-sm font-semibold">缓存管理</CardTitle>
<Card className='border-border/40 bg-card/50 backdrop-blur-sm shadow-sm'>
<CardHeader className='pb-3 border-b border-dashed flex flex-col md:flex-row md:items-center md:justify-between gap-4'>
<div className='space-y-0.5'>
<div className='flex items-center gap-2'>
<HardDrive className='size-4 text-primary animate-pulse' />
<CardTitle className='text-sm font-semibold'>缓存管理</CardTitle>
</div>
<CardDescription className="text-[11px]">管理和监控系统级磁盘缓存的资源占用、生命周期及淘汰策略</CardDescription>
<CardDescription className='text-[11px]'>
管理和监控系统级磁盘缓存的资源占用、生命周期及淘汰策略
</CardDescription>
</div>
<Button
size="sm"
variant="secondary"
className="h-8 gap-1.5 text-xs self-start md:self-auto"
size='sm'
variant='secondary'
className='h-8 gap-1.5 text-xs self-start md:self-auto'
onClick={() => fetchCacheStatus(true)}
disabled={loadingCache}
>
<RefreshCw className={`size-3 ${loadingCache ? "animate-spin" : ""}`} />
<RefreshCw
className={`size-3 ${loadingCache ? 'animate-spin' : ''}`}
/>
刷新状态
</Button>
</CardHeader>
<CardContent className="pt-4">
<CardContent className='pt-4'>
{loadingCache && !cacheStatus ? (
<div className="space-y-3 py-6">
<Skeleton className="h-6 w-full" />
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
<div className='space-y-3 py-6'>
<Skeleton className='h-6 w-full' />
<Skeleton className='h-10 w-full' />
<Skeleton className='h-10 w-full' />
</div>
) : cacheStatus ? (
<div className="grid grid-cols-1 lg:grid-cols-5 gap-6">
<div className='grid grid-cols-1 lg:grid-cols-5 gap-6'>
{/* 左边:状态区 (2/5 cols) */}
<div className="lg:col-span-2 space-y-4">
<h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">运行状态</h4>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className='lg:col-span-2 space-y-4'>
<h4 className='text-xs font-semibold text-muted-foreground uppercase tracking-wider'>
运行状态
</h4>
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
{/* 已占空间 */}
<div className="p-4 rounded-xl border border-border/40 bg-background/30 backdrop-blur-xs hover:border-primary/20 transition-all duration-300">
<p className="text-[10px] text-muted-foreground font-medium mb-1">已用空间</p>
<p className="text-xl font-bold tracking-tight text-foreground">
<div className='p-4 rounded-xl border border-border/40 bg-background/30 backdrop-blur-xs hover:border-primary/20 transition-all duration-300'>
<p className='text-[10px] text-muted-foreground font-medium mb-1'>
已用空间
</p>
<p className='text-xl font-bold tracking-tight text-foreground'>
{formatBytes(cacheStatus.total_size)}
</p>
</div>
{/* Key数量 */}
<div className="p-4 rounded-xl border border-border/40 bg-background/30 backdrop-blur-xs hover:border-primary/20 transition-all duration-300">
<p className="text-[10px] text-muted-foreground font-medium mb-1">缓存键数量</p>
<p className="text-xl font-bold tracking-tight text-foreground">
{formatNumber(cacheStatus.keys_count)} <span className="text-xs text-muted-foreground font-normal">个文件</span>
<div className='p-4 rounded-xl border border-border/40 bg-background/30 backdrop-blur-xs hover:border-primary/20 transition-all duration-300'>
<p className='text-[10px] text-muted-foreground font-medium mb-1'>
缓存键数量
</p>
<p className='text-xl font-bold tracking-tight text-foreground'>
{formatNumber(cacheStatus.keys_count)}{' '}
<span className='text-xs text-muted-foreground font-normal'>
个文件
</span>
</p>
</div>
</div>
{/* 存储路径 */}
<div className="p-4 rounded-xl border border-border/40 bg-background/30 backdrop-blur-xs hover:border-primary/20 transition-all duration-300">
<p className="text-[10px] text-muted-foreground font-medium mb-1.5">缓存基准目录</p>
<code className="text-xs font-mono bg-muted/60 px-2 py-1 rounded-md block truncate" title={cacheStatus.base_path}>
<div className='p-4 rounded-xl border border-border/40 bg-background/30 backdrop-blur-xs hover:border-primary/20 transition-all duration-300'>
<p className='text-[10px] text-muted-foreground font-medium mb-1.5'>
缓存基准目录
</p>
<code
className='text-xs font-mono bg-muted/60 px-2 py-1 rounded-md block truncate'
title={cacheStatus.base_path}
>
{cacheStatus.base_path}
</code>
</div>
<Button
variant="secondary"
size="sm"
className="h-8 text-xs font-medium w-full"
variant='secondary'
size='sm'
className='h-8 text-xs font-medium w-full'
onClick={() => setShowClearConfirm(true)}
>
立即清空缓存
@@ -202,111 +230,125 @@ export function CacheManager({ refreshTrigger }: CacheManagerProps) {
</div>
{/* 右边:配置区 (3/5 cols) */}
<div className="lg:col-span-3 border-t lg:border-t-0 lg:border-l border-border/40 pt-6 lg:pt-0 lg:pl-6 space-y-4">
<h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">策略配置</h4>
<form onSubmit={handleSaveConfig} className="space-y-4">
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-1.5">
<Label htmlFor="maxSizeMB" className="text-xs font-medium">最大容量限制 (MB)</Label>
<div className='lg:col-span-3 border-t lg:border-t-0 lg:border-l border-border/40 pt-6 lg:pt-0 lg:pl-6 space-y-4'>
<h4 className='text-xs font-semibold text-muted-foreground uppercase tracking-wider'>
策略配置
</h4>
<form onSubmit={handleSaveConfig} className='space-y-4'>
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
<div className='space-y-1.5'>
<Label htmlFor='maxSizeMB' className='text-xs font-medium'>
最大容量限制 (MB)
</Label>
<Input
id="maxSizeMB"
type="number"
min="1"
id='maxSizeMB'
type='number'
min='1'
value={maxSizeMB}
onChange={(e) => setMaxSizeMB(e.target.value)}
className="h-8 text-xs bg-background/50 border-border/40"
placeholder="例如 100"
className='h-8 text-xs bg-background/50 border-border/40'
placeholder='例如 100'
required
/>
<p className="text-[9px] text-muted-foreground">当总大小超出该值时,自动触发淘汰算法</p>
<p className='text-[9px] text-muted-foreground'>
当总大小超出该值时,自动触发淘汰算法
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="ttlMinutes" className="text-xs font-medium">生存时间限制 (分钟)</Label>
<div className='space-y-1.5'>
<Label htmlFor='ttlMinutes' className='text-xs font-medium'>
生存时间限制 (分钟)
</Label>
<Input
id="ttlMinutes"
type="number"
min="0"
id='ttlMinutes'
type='number'
min='0'
value={ttlMinutes}
onChange={(e) => setTtlMinutes(e.target.value)}
className="h-8 text-xs bg-background/50 border-border/40"
placeholder="例如 60,0 表示永不过期"
className='h-8 text-xs bg-background/50 border-border/40'
placeholder='例如 60,0 表示永不过期'
required
/>
<p className="text-[9px] text-muted-foreground">缓存项的最长存活时间,超期后将失效被清理</p>
<p className='text-[9px] text-muted-foreground'>
缓存项的最长存活时间,超期后将失效被清理
</p>
</div>
</div>
<div className="flex items-start justify-between p-4 rounded-xl border border-border/40 bg-background/20">
<div className="space-y-1 pr-4">
<Label htmlFor="lruEnabled" className="text-xs font-semibold block cursor-pointer">
<div className='flex items-start justify-between p-4 rounded-xl border border-border/40 bg-background/20'>
<div className='space-y-1 pr-4'>
<Label
htmlFor='lruEnabled'
className='text-xs font-semibold block cursor-pointer'
>
启用 LRU 淘汰机制
</Label>
<span className="text-[10px] text-muted-foreground block">
<span className='text-[10px] text-muted-foreground block'>
在到达最大容量限制时,自动移除最久未被访问的缓存项。关闭该功能仅清理过期项。
</span>
</div>
<Switch
id="lruEnabled"
id='lruEnabled'
checked={lruEnabled}
onCheckedChange={setLruEnabled}
/>
</div>
<div className="flex justify-end pt-2">
<div className='flex justify-end pt-2'>
<Button
type="submit"
type='submit'
disabled={savingConfig}
className="h-8 text-xs px-4"
className='h-8 text-xs px-4'
>
{savingConfig ? "正在保存..." : "保存设置"}
{savingConfig ? '正在保存...' : '保存设置'}
</Button>
</div>
</form>
</div>
</div>
) : (
<div className="flex flex-col items-center justify-center py-10 text-muted-foreground">
<HardDrive className="size-8 opacity-45 mb-2" />
<span className="text-xs">未加载到缓存状态信息</span>
<div className='flex flex-col items-center justify-center py-10 text-muted-foreground'>
<HardDrive className='size-8 opacity-45 mb-2' />
<span className='text-xs'>未加载到缓存状态信息</span>
</div>
)}
</CardContent>
{/* 清除缓存确认对话框 */}
<Dialog open={showClearConfirm} onOpenChange={setShowClearConfirm}>
<DialogContent className="sm:max-w-md">
<DialogContent className='sm:max-w-md'>
<DialogHeader>
<DialogTitle className="text-sm font-semibold flex items-center gap-2 text-destructive">
<Trash2 className="size-4" />
<DialogTitle className='text-sm font-semibold flex items-center gap-2 text-destructive'>
<Trash2 className='size-4' />
确认清空所有缓存?
</DialogTitle>
<DialogDescription className="text-xs text-muted-foreground pt-1">
该操作将彻底清空磁盘目录下的全部缓存文件(包含临时解压、处理后的图片及各种块文件),重置键数量统计为 0。此操作不可撤销,且可能导致用户拉取资源时出现一过性的响应变慢。
<DialogDescription className='text-xs text-muted-foreground pt-1'>
该操作将彻底清空磁盘目录下的全部缓存文件(包含临时解压、处理后的图片及各种块文件),重置键数量统计为
0。此操作不可撤销,且可能导致用户拉取资源时出现一过性的响应变慢。
</DialogDescription>
</DialogHeader>
<DialogFooter className="flex gap-2 justify-end mt-4">
<DialogFooter className='flex gap-2 justify-end mt-4'>
<Button
variant="outline"
size="sm"
className="h-8 text-xs"
variant='outline'
size='sm'
className='h-8 text-xs'
onClick={() => setShowClearConfirm(false)}
disabled={clearingCache}
>
取消
</Button>
<Button
variant="destructive"
size="sm"
className="h-8 text-xs"
variant='destructive'
size='sm'
className='h-8 text-xs'
onClick={handleClearCache}
disabled={clearingCache}
>
{clearingCache ? "清理中..." : "确认清空"}
{clearingCache ? '清理中...' : '确认清空'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</Card>
)
);
}
@@ -1,182 +1,210 @@
"use client"
'use client';
import * as React from "react"
import {useRef, useState} from "react"
import {useTheme} from "next-themes"
import CodeMirror from "@uiw/react-codemirror"
import {sql} from "@codemirror/lang-sql"
import {toast} from "sonner"
import {ArrowLeft, Play, RefreshCw, Terminal, Trash2,} from "lucide-react"
import * as React from 'react';
import { useRef, useState } from 'react';
import { useTheme } from 'next-themes';
import CodeMirror from '@uiw/react-codemirror';
import { sql } from '@codemirror/lang-sql';
import { toast } from 'sonner';
import { ArrowLeft, Play, RefreshCw, Terminal, Trash2 } from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import type {ExecuteSQLResponse} from "@/lib/services/db-manage"
import {DbManageService} from "@/lib/services/db-manage"
import { Button } from '@/components/ui/button';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import type { ExecuteSQLResponse } from '@/lib/services/db-manage';
import { DbManageService } from '@/lib/services/db-manage';
/**
* 格式化表格单元格内容
*/
const formatCellValue = (val: unknown): string => {
if (val === null || val === undefined) return "-"
if (typeof val === "boolean") return val ? "true" : "false"
if (typeof val === "object") {
if (val === null || val === undefined) return '-';
if (typeof val === 'boolean') return val ? 'true' : 'false';
if (typeof val === 'object') {
try {
return JSON.stringify(val)
return JSON.stringify(val);
} catch {
return "[Object]"
return '[Object]';
}
}
return String(val)
}
return String(val);
};
interface SQLConsoleProps {
dbType?: string // "postgres" | "sqlite"
onClose: () => void
dbType?: string; // "postgres" | "sqlite"
onClose: () => void;
}
export function SQLConsole({ dbType, onClose }: SQLConsoleProps) {
// SQL 控制台状态
const [sqlQuery, setSqlQuery] = useState<string>("")
const [executingSQL, setExecutingSQL] = useState<boolean>(false)
const [sqlResult, setSqlResult] = useState<ExecuteSQLResponse | null>(null)
const [sqlError, setSqlError] = useState<string | null>(null)
const [sqlQuery, setSqlQuery] = useState<string>('');
const [executingSQL, setExecutingSQL] = useState<boolean>(false);
const [sqlResult, setSqlResult] = useState<ExecuteSQLResponse | null>(null);
const [sqlError, setSqlError] = useState<string | null>(null);
// 拖拽与主题状态
const containerRef = useRef<HTMLDivElement>(null)
const [editorHeight, setEditorHeight] = useState<number>(240)
const { resolvedTheme } = useTheme()
const cmTheme = resolvedTheme === "dark" ? "dark" : "light"
const containerRef = useRef<HTMLDivElement>(null);
const [editorHeight, setEditorHeight] = useState<number>(240);
const { resolvedTheme } = useTheme();
const cmTheme = resolvedTheme === 'dark' ? 'dark' : 'light';
const handleMouseDown = (e: React.MouseEvent) => {
e.preventDefault()
const startY = e.clientY
const startHeight = editorHeight
e.preventDefault();
const startY = e.clientY;
const startHeight = editorHeight;
const handleMouseMove = (moveEvent: MouseEvent) => {
if (!containerRef.current) return
const deltaY = moveEvent.clientY - startY
const containerHeight = containerRef.current.getBoundingClientRect().height
const newHeight = startHeight + deltaY
if (!containerRef.current) return;
const deltaY = moveEvent.clientY - startY;
const containerHeight =
containerRef.current.getBoundingClientRect().height;
const newHeight = startHeight + deltaY;
// Limit editor height between 80px and containerHeight - 80px
if (newHeight > 80 && newHeight < containerHeight - 80) {
setEditorHeight(newHeight)
}
setEditorHeight(newHeight);
}
};
const handleMouseUp = () => {
document.removeEventListener("mousemove", handleMouseMove)
document.removeEventListener("mouseup", handleMouseUp)
}
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
};
document.addEventListener("mousemove", handleMouseMove)
document.addEventListener("mouseup", handleMouseUp)
}
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
};
// 执行 SQL 查询
const handleExecuteSQL = async () => {
if (!sqlQuery.trim()) return
setExecutingSQL(true)
setSqlResult(null)
setSqlError(null)
if (!sqlQuery.trim()) return;
setExecutingSQL(true);
setSqlResult(null);
setSqlError(null);
try {
const result = await DbManageService.executeSQL(sqlQuery)
setSqlResult(result)
toast.success("SQL 执行成功")
const result = await DbManageService.executeSQL(sqlQuery);
setSqlResult(result);
toast.success('SQL 执行成功');
} catch (err) {
setSqlError(err instanceof Error ? err.message : "未知执行错误")
toast.error("SQL 执行失败")
setSqlError(err instanceof Error ? err.message : '未知执行错误');
toast.error('SQL 执行失败');
} finally {
setExecutingSQL(false)
}
setExecutingSQL(false);
}
};
// Preset SQL 选择器
const handlePresetSQLChange = (query: string) => {
setSqlQuery(query)
}
setSqlQuery(query);
};
const handleClose = () => {
setSqlResult(null)
setSqlError(null)
onClose()
}
setSqlResult(null);
setSqlError(null);
onClose();
};
return (
<div className="py-6 px-1 space-y-6 w-full">
<div className='py-6 px-1 space-y-6 w-full'>
{/* 顶部控制与标题 */}
<div className="flex items-center justify-between pb-2">
<div className="flex items-center gap-3">
<div className='flex items-center justify-between pb-2'>
<div className='flex items-center gap-3'>
<Button
variant="outline"
size="icon"
className="h-8 w-8"
variant='outline'
size='icon'
className='h-8 w-8'
onClick={handleClose}
>
<ArrowLeft className="size-4" />
<ArrowLeft className='size-4' />
</Button>
<div className="flex items-center gap-2">
<Terminal className="size-5 text-primary" />
<div className='flex items-center gap-2'>
<Terminal className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">SQL 查询终端</h1>
<h1 className='text-2xl font-semibold tracking-tight'>
SQL 查询终端
</h1>
</div>
</div>
</div>
<div className="text-xs text-muted-foreground font-mono">
{dbType === "postgres" ? "PostgreSQL Connected" : "SQLite Connected"}
<div className='text-xs text-muted-foreground font-mono'>
{dbType === 'postgres' ? 'PostgreSQL Connected' : 'SQLite Connected'}
</div>
</div>
{/* 类似于 VS Code 的单个整体编辑器+结果区域,固定高度,有分水岭拖拽调整大小 */}
<div
ref={containerRef}
className="w-full border border-border/40 bg-card/60 backdrop-blur-md rounded-lg overflow-hidden flex flex-col shadow-sm h-[calc(100vh-140px)] min-h-[500px]"
className='w-full border border-border/40 bg-card/60 backdrop-blur-md rounded-lg overflow-hidden flex flex-col shadow-sm h-[calc(100vh-140px)] min-h-[500px]'
>
{/* 顶部编辑工具栏 */}
<div className="flex items-center justify-between px-4 py-2 border-b bg-muted/40 shrink-0 gap-4 flex-wrap">
<div className="flex items-center gap-2">
<Terminal className="size-4 text-primary" />
<span className="text-xs font-semibold">SQL 编辑器</span>
<div className='flex items-center justify-between px-4 py-2 border-b bg-muted/40 shrink-0 gap-4 flex-wrap'>
<div className='flex items-center gap-2'>
<Terminal className='size-4 text-primary' />
<span className='text-xs font-semibold'>SQL 编辑器</span>
</div>
<div className="flex items-center gap-3 flex-wrap">
<div className='flex items-center gap-3 flex-wrap'>
{/* 快速模板 */}
<div className="flex items-center gap-1.5">
<span className="text-[11px] text-muted-foreground">快速模板:</span>
<div className='flex items-center gap-1.5'>
<span className='text-[11px] text-muted-foreground'>
快速模板:
</span>
<Select onValueChange={handlePresetSQLChange}>
<SelectTrigger className="h-7 w-[180px] text-[11px] bg-background">
<SelectValue placeholder="选择预设 SQL" />
<SelectTrigger className='h-7 w-[180px] text-[11px] bg-background'>
<SelectValue placeholder='选择预设 SQL' />
</SelectTrigger>
<SelectContent>
<SelectItem value="SELECT * FROM users LIMIT 10;">查询用户 (SELECT users)</SelectItem>
<SelectItem value="SELECT * FROM uploads LIMIT 10;">查询文件 (SELECT uploads)</SelectItem>
<SelectItem value="SELECT * FROM task_executions ORDER BY created_at DESC LIMIT 10;">查询任务流水 (SELECT task_executions)</SelectItem>
<SelectItem value="SELECT sqlite_version();">SQLite 版本 (SQLite only)</SelectItem>
<SelectItem value="SELECT version();">PostgreSQL 版本 (Postgres only)</SelectItem>
<SelectItem value='SELECT * FROM users LIMIT 10;'>
查询用户 (SELECT users)
</SelectItem>
<SelectItem value='SELECT * FROM uploads LIMIT 10;'>
查询文件 (SELECT uploads)
</SelectItem>
<SelectItem value='SELECT * FROM task_executions ORDER BY created_at DESC LIMIT 10;'>
查询任务流水 (SELECT task_executions)
</SelectItem>
<SelectItem value='SELECT sqlite_version();'>
SQLite 版本 (SQLite only)
</SelectItem>
<SelectItem value='SELECT version();'>
PostgreSQL 版本 (Postgres only)
</SelectItem>
</SelectContent>
</Select>
</div>
{/* 控制按钮 */}
<div className="flex items-center gap-1.5">
<div className='flex items-center gap-1.5'>
<Button
variant="ghost"
size="icon"
className="h-7 w-7 text-muted-foreground hover:text-foreground"
onClick={() => setSqlQuery("")}
title="清空编辑器"
variant='ghost'
size='icon'
className='h-7 w-7 text-muted-foreground hover:text-foreground'
onClick={() => setSqlQuery('')}
title='清空编辑器'
>
<Trash2 className="size-3.5" />
<Trash2 className='size-3.5' />
</Button>
<Button
size="sm"
className="h-7 px-3 gap-1 text-[11px]"
size='sm'
className='h-7 px-3 gap-1 text-[11px]'
onClick={handleExecuteSQL}
disabled={executingSQL || !sqlQuery.trim()}
>
<Play className="size-3" />
{executingSQL ? "运行中..." : "运行"}
<Play className='size-3' />
{executingSQL ? '运行中...' : '运行'}
</Button>
</div>
</div>
@@ -185,15 +213,15 @@ export function SQLConsole({ dbType, onClose }: SQLConsoleProps) {
{/* 上半区:编辑器输入框 */}
<div
style={{ height: editorHeight }}
className="w-full overflow-hidden relative min-h-[100px] bg-background"
className='w-full overflow-hidden relative min-h-[100px] bg-background'
>
<CodeMirror
value={sqlQuery}
height="100%"
height='100%'
extensions={[sql()]}
theme={cmTheme}
onChange={(value) => setSqlQuery(value)}
className="h-full text-xs font-mono"
className='h-full text-xs font-mono'
basicSetup={{
lineNumbers: true,
foldGutter: true,
@@ -207,59 +235,78 @@ export function SQLConsole({ dbType, onClose }: SQLConsoleProps) {
{/* 拖动分割线 */}
<div
onMouseDown={handleMouseDown}
className="h-1.5 bg-border/60 hover:bg-primary/50 cursor-row-resize transition-colors flex items-center justify-center shrink-0 select-none z-10"
title="拖动调整大小"
className='h-1.5 bg-border/60 hover:bg-primary/50 cursor-row-resize transition-colors flex items-center justify-center shrink-0 select-none z-10'
title='拖动调整大小'
>
<div className="w-8 h-1 rounded bg-muted-foreground/30" />
<div className='w-8 h-1 rounded bg-muted-foreground/30' />
</div>
{/* 下半区:查看结果 (可滑动查看) */}
<div className="flex-1 min-h-[100px] flex flex-col overflow-hidden bg-muted/10">
<div className='flex-1 min-h-[100px] flex flex-col overflow-hidden bg-muted/10'>
{/* 结果栏工具提示 */}
<div className="flex items-center justify-between px-4 py-1.5 border-b bg-muted/20 shrink-0 text-[11px] text-muted-foreground font-mono">
<span className="font-semibold">执行输出</span>
<div className='flex items-center justify-between px-4 py-1.5 border-b bg-muted/20 shrink-0 text-[11px] text-muted-foreground font-mono'>
<span className='font-semibold'>执行输出</span>
{sqlResult && (
<span>
类型: {sqlResult.type.toUpperCase()} | 耗时: {sqlResult.execution_time_ms} ms
类型: {sqlResult.type.toUpperCase()} | 耗时:{' '}
{sqlResult.execution_time_ms} ms
</span>
)}
</div>
{/* 滚动结果内容 */}
<div className="flex-1 overflow-auto p-4 min-h-0">
<div className='flex-1 overflow-auto p-4 min-h-0'>
{executingSQL && (
<div className="flex flex-col items-center justify-center h-full py-10 space-y-2">
<RefreshCw className="size-5 text-primary animate-spin" />
<span className="text-xs text-muted-foreground">正在在数据库执行查询,请稍候...</span>
<div className='flex flex-col items-center justify-center h-full py-10 space-y-2'>
<RefreshCw className='size-5 text-primary animate-spin' />
<span className='text-xs text-muted-foreground'>
正在在数据库执行查询,请稍候...
</span>
</div>
)}
{sqlError && (
<div className="bg-destructive/10 border border-destructive/20 text-destructive font-mono text-xs p-4 rounded-lg overflow-auto h-full max-h-[300px]">
<p className="font-semibold mb-1">SQL 执行报错 (Error):</p>
<pre className="whitespace-pre-wrap">{sqlError}</pre>
<div className='bg-destructive/10 border border-destructive/20 text-destructive font-mono text-xs p-4 rounded-lg overflow-auto h-full max-h-[300px]'>
<p className='font-semibold mb-1'>SQL 执行报错 (Error):</p>
<pre className='whitespace-pre-wrap'>{sqlError}</pre>
</div>
)}
{sqlResult && (
<div className="h-full flex flex-col">
{sqlResult.type === "select" && sqlResult.columns && sqlResult.columns.length > 0 && (
<div className="border rounded-md overflow-auto max-w-full max-h-full bg-background relative flex-1 min-h-0">
<Table className="text-xs">
<TableHeader className="bg-muted/40 font-semibold sticky top-0 z-10">
<div className='h-full flex flex-col'>
{sqlResult.type === 'select' &&
sqlResult.columns &&
sqlResult.columns.length > 0 && (
<div className='border rounded-md overflow-auto max-w-full max-h-full bg-background relative flex-1 min-h-0'>
<Table className='text-xs'>
<TableHeader className='bg-muted/40 font-semibold sticky top-0 z-10'>
<TableRow>
{sqlResult.columns.map((col) => (
<TableHead key={col} className="font-semibold text-foreground py-2">{col}</TableHead>
<TableHead
key={col}
className='font-semibold text-foreground py-2'
>
{col}
</TableHead>
))}
</TableRow>
</TableHeader>
<TableBody>
{sqlResult.results && sqlResult.results.length > 0 ? (
sqlResult.results.map((row, rIndex) => (
<TableRow key={rIndex} className="hover:bg-muted/10">
<TableRow
key={rIndex}
className='hover:bg-muted/10'
>
{sqlResult.columns!.map((col) => (
<TableCell key={col} className="font-mono py-1.5">
<span className="truncate max-w-[240px] block" title={formatCellValue(row[col])}>
<TableCell
key={col}
className='font-mono py-1.5'
>
<span
className='truncate max-w-[240px] block'
title={formatCellValue(row[col])}
>
{formatCellValue(row[col])}
</span>
</TableCell>
@@ -268,7 +315,10 @@ export function SQLConsole({ dbType, onClose }: SQLConsoleProps) {
))
) : (
<TableRow>
<TableCell colSpan={sqlResult.columns.length} className="text-center py-10 text-muted-foreground">
<TableCell
colSpan={sqlResult.columns.length}
className='text-center py-10 text-muted-foreground'
>
查询结果为空
</TableCell>
</TableRow>
@@ -278,11 +328,14 @@ export function SQLConsole({ dbType, onClose }: SQLConsoleProps) {
</div>
)}
{sqlResult.type === "exec" && (
<div className="bg-primary/5 border border-primary/10 text-primary-foreground font-mono text-xs p-6 rounded-lg text-center my-auto">
<p className="text-foreground text-sm font-semibold">SQL 执行成功</p>
<p className="text-muted-foreground mt-2">
受影响行数: {sqlResult.affected_rows} 行,耗时 {sqlResult.execution_time_ms} 毫秒。
{sqlResult.type === 'exec' && (
<div className='bg-primary/5 border border-primary/10 text-primary-foreground font-mono text-xs p-6 rounded-lg text-center my-auto'>
<p className='text-foreground text-sm font-semibold'>
SQL 执行成功
</p>
<p className='text-muted-foreground mt-2'>
受影响行数: {sqlResult.affected_rows} 行,耗时{' '}
{sqlResult.execution_time_ms} 毫秒。
</p>
</div>
)}
@@ -290,14 +343,16 @@ export function SQLConsole({ dbType, onClose }: SQLConsoleProps) {
)}
{!executingSQL && !sqlResult && !sqlError && (
<div className="flex flex-col items-center justify-center h-full py-10 text-muted-foreground opacity-60">
<Terminal className="size-8 mb-2" />
<span className="text-xs">编辑器就绪,请在上方编写 SQL 并运行查看结果</span>
<div className='flex flex-col items-center justify-center h-full py-10 text-muted-foreground opacity-60'>
<Terminal className='size-8 mb-2' />
<span className='text-xs'>
编辑器就绪,请在上方编写 SQL 并运行查看结果
</span>
</div>
)}
</div>
</div>
</div>
</div>
)
);
}
@@ -1,157 +1,195 @@
"use client"
'use client';
import * as React from "react"
import {useCallback, useEffect, useState} from "react"
import {toast} from "sonner"
import {Layers, RefreshCw} from "lucide-react"
import * as React from 'react';
import { useCallback, useEffect, useState } from 'react';
import { toast } from 'sonner';
import { Layers, RefreshCw } from 'lucide-react';
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Skeleton} from "@/components/ui/skeleton"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import {Button} from "@/components/ui/button"
import type {TableDataResponse} from "@/lib/services/db-manage"
import {DbManageService} from "@/lib/services/db-manage"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Skeleton } from '@/components/ui/skeleton';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { Button } from '@/components/ui/button';
import type { TableDataResponse } from '@/lib/services/db-manage';
import { DbManageService } from '@/lib/services/db-manage';
/**
* 格式化数字,每3位加逗号
*/
const formatNumber = (num: number | string) => {
if (num === undefined || num === null) return "0"
return num.toString().replace(/\B(?=(\d{3})+(?!\B))/g, ",")
}
if (num === undefined || num === null) return '0';
return num.toString().replace(/\B(?=(\d{3})+(?!\B))/g, ',');
};
/**
* 格式化表格单元格内容
*/
const formatCellValue = (val: unknown): string => {
if (val === null || val === undefined) return "-"
if (typeof val === "boolean") return val ? "true" : "false"
if (typeof val === "object") {
if (val === null || val === undefined) return '-';
if (typeof val === 'boolean') return val ? 'true' : 'false';
if (typeof val === 'object') {
try {
return JSON.stringify(val)
return JSON.stringify(val);
} catch {
return "[Object]"
return '[Object]';
}
}
return String(val)
}
return String(val);
};
interface TableBrowserProps {
tables: string[]
loadingTables: boolean
refreshTrigger: number
tables: string[];
loadingTables: boolean;
refreshTrigger: number;
}
export function TableBrowser({ tables, loadingTables, refreshTrigger }: TableBrowserProps) {
const [selectedTable, setSelectedTable] = useState<string>("")
const [tableData, setTableData] = useState<TableDataResponse | null>(null)
const [page, setPage] = useState<number>(1)
const [loadingData, setLoadingData] = useState<boolean>(false)
const pageSize = 10
export function TableBrowser({
tables,
loadingTables,
refreshTrigger,
}: TableBrowserProps) {
const [selectedTable, setSelectedTable] = useState<string>('');
const [tableData, setTableData] = useState<TableDataResponse | null>(null);
const [page, setPage] = useState<number>(1);
const [loadingData, setLoadingData] = useState<boolean>(false);
const pageSize = 10;
// 获取具体表数据
const fetchTableData = useCallback(async (tableName: string, targetPage: number, size: number) => {
if (!tableName) return
setLoadingData(true)
const fetchTableData = useCallback(
async (tableName: string, targetPage: number, size: number) => {
if (!tableName) return;
setLoadingData(true);
try {
const data = await DbManageService.getTableData({
table: tableName,
page: targetPage,
pageSize: size,
})
setTableData(data)
});
setTableData(data);
} catch (err) {
toast.error(`获取数据表 ${tableName} 数据失败`, {
description: err instanceof Error ? err.message : "未知错误",
})
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setLoadingData(false)
setLoadingData(false);
}
}, [])
},
[],
);
// 默认选择第一张表
useEffect(() => {
if (tables.length > 0 && !selectedTable) {
setSelectedTable(tables[0])
setSelectedTable(tables[0]);
}
}, [tables, selectedTable])
}, [tables, selectedTable]);
// 当选中的表、页码或外部刷新触发器改变时获取数据
useEffect(() => {
if (selectedTable) {
fetchTableData(selectedTable, page, pageSize)
fetchTableData(selectedTable, page, pageSize);
}
}, [selectedTable, page, pageSize, refreshTrigger, fetchTableData])
}, [selectedTable, page, pageSize, refreshTrigger, fetchTableData]);
// 处理页码改变
const handlePageChange = (newPage: number) => {
setPage(newPage)
}
setPage(newPage);
};
// 处理表切换
const handleTableChange = (value: string) => {
setSelectedTable(value)
setPage(1) // 切换表重置为第一页
}
setSelectedTable(value);
setPage(1); // 切换表重置为第一页
};
return (
<Card className="border-border/40 bg-card/50 backdrop-blur-sm shadow-sm">
<CardHeader className="pb-3 border-b border-dashed flex flex-col md:flex-row md:items-center md:justify-between gap-4">
<div className="space-y-0.5">
<CardTitle className="text-sm font-semibold">数据表浏览器</CardTitle>
<CardDescription className="text-[11px]">浏览数据库中的物理数据表详情及内容</CardDescription>
<Card className='border-border/40 bg-card/50 backdrop-blur-sm shadow-sm'>
<CardHeader className='pb-3 border-b border-dashed flex flex-col md:flex-row md:items-center md:justify-between gap-4'>
<div className='space-y-0.5'>
<CardTitle className='text-sm font-semibold'>数据表浏览器</CardTitle>
<CardDescription className='text-[11px]'>
浏览数据库中的物理数据表详情及内容
</CardDescription>
</div>
<div className="flex items-center gap-2">
<div className='flex items-center gap-2'>
{loadingTables ? (
<Skeleton className="h-8 w-48" />
<Skeleton className='h-8 w-48' />
) : (
<Select value={selectedTable} onValueChange={handleTableChange}>
<SelectTrigger className="h-8 w-[200px] text-xs bg-background border-border/40">
<SelectValue placeholder="选择数据表" />
<SelectTrigger className='h-8 w-[200px] text-xs bg-background border-border/40'>
<SelectValue placeholder='选择数据表' />
</SelectTrigger>
<SelectContent className="max-h-[300px]">
<SelectContent className='max-h-[300px]'>
{tables.map((t) => (
<SelectItem key={t} value={t} className="text-xs font-mono">{t}</SelectItem>
<SelectItem key={t} value={t} className='text-xs font-mono'>
{t}
</SelectItem>
))}
</SelectContent>
</Select>
)}
</div>
</CardHeader>
<CardContent className="pt-4">
<CardContent className='pt-4'>
{loadingData && !tableData ? (
<div className="space-y-3 py-6">
<Skeleton className="h-6 w-full" />
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
<div className='space-y-3 py-6'>
<Skeleton className='h-6 w-full' />
<Skeleton className='h-10 w-full' />
<Skeleton className='h-10 w-full' />
<Skeleton className='h-10 w-full' />
</div>
) : tableData && tableData.columns.length > 0 ? (
<div className="space-y-4">
<div className='space-y-4'>
{/* 数据表格区域 */}
<div className="border rounded-md overflow-x-auto max-w-full bg-background/50 relative">
<div className='border rounded-md overflow-x-auto max-w-full bg-background/50 relative'>
{loadingData && (
<div className="absolute inset-0 bg-background/40 backdrop-blur-[1px] flex items-center justify-center z-10">
<RefreshCw className="size-5 text-primary animate-spin" />
<div className='absolute inset-0 bg-background/40 backdrop-blur-[1px] flex items-center justify-center z-10'>
<RefreshCw className='size-5 text-primary animate-spin' />
</div>
)}
<Table className="text-xs">
<TableHeader className="bg-muted/40 font-semibold sticky top-0">
<Table className='text-xs'>
<TableHeader className='bg-muted/40 font-semibold sticky top-0'>
<TableRow>
{tableData.columns.map((col) => (
<TableHead key={col} className="font-semibold text-foreground py-2.5">{col}</TableHead>
<TableHead
key={col}
className='font-semibold text-foreground py-2.5'
>
{col}
</TableHead>
))}
</TableRow>
</TableHeader>
<TableBody>
{tableData.results && tableData.results.length > 0 ? (
tableData.results.map((row, rIndex) => (
<TableRow key={rIndex} className="hover:bg-muted/10">
<TableRow key={rIndex} className='hover:bg-muted/10'>
{tableData.columns.map((col) => (
<TableCell key={col} className="font-mono py-2">
<span className="truncate max-w-[200px] block" title={formatCellValue(row[col])}>
<TableCell key={col} className='font-mono py-2'>
<span
className='truncate max-w-[200px] block'
title={formatCellValue(row[col])}
>
{formatCellValue(row[col])}
</span>
</TableCell>
@@ -160,7 +198,10 @@ export function TableBrowser({ tables, loadingTables, refreshTrigger }: TableBro
))
) : (
<TableRow>
<TableCell colSpan={tableData.columns.length} className="text-center py-10 text-muted-foreground">
<TableCell
colSpan={tableData.columns.length}
className='text-center py-10 text-muted-foreground'
>
表中无记录数据
</TableCell>
</TableRow>
@@ -170,29 +211,36 @@ export function TableBrowser({ tables, loadingTables, refreshTrigger }: TableBro
</div>
{/* 分页控制 */}
<div className="flex items-center justify-between text-xs text-muted-foreground flex-wrap gap-2 pt-2 border-t border-dashed">
<div className='flex items-center justify-between text-xs text-muted-foreground flex-wrap gap-2 pt-2 border-t border-dashed'>
<div>
总行数: <span className="font-mono text-foreground font-semibold">{formatNumber(tableData.total)}</span> 条记录
总行数:{' '}
<span className='font-mono text-foreground font-semibold'>
{formatNumber(tableData.total)}
</span>{' '}
条记录
</div>
<div className="flex items-center gap-2">
<div className='flex items-center gap-2'>
<Button
variant="outline"
size="sm"
className="h-7 px-2 text-[11px]"
variant='outline'
size='sm'
className='h-7 px-2 text-[11px]'
onClick={() => handlePageChange(page - 1)}
disabled={page <= 1 || loadingData}
>
上一页
</Button>
<span className="text-xs px-2 font-mono">
第 {page} / {Math.max(1, Math.ceil(tableData.total / pageSize))} 页
<span className='text-xs px-2 font-mono'>
第 {page} /{' '}
{Math.max(1, Math.ceil(tableData.total / pageSize))} 页
</span>
<Button
variant="outline"
size="sm"
className="h-7 px-2 text-[11px]"
variant='outline'
size='sm'
className='h-7 px-2 text-[11px]'
onClick={() => handlePageChange(page + 1)}
disabled={page >= Math.ceil(tableData.total / pageSize) || loadingData}
disabled={
page >= Math.ceil(tableData.total / pageSize) || loadingData
}
>
下一页
</Button>
@@ -200,12 +248,12 @@ export function TableBrowser({ tables, loadingTables, refreshTrigger }: TableBro
</div>
</div>
) : (
<div className="flex flex-col items-center justify-center py-10 text-muted-foreground">
<Layers className="size-8 opacity-45 mb-2" />
<span className="text-xs">未选择数据表或表结构无法加载</span>
<div className='flex flex-col items-center justify-center py-10 text-muted-foreground'>
<Layers className='size-8 opacity-45 mb-2' />
<span className='text-xs'>未选择数据表或表结构无法加载</span>
</div>
)}
</CardContent>
</Card>
)
);
}
+191 -153
View File
@@ -1,9 +1,9 @@
"use client"
'use client';
import dynamic from "next/dynamic"
import * as React from "react"
import {useCallback, useEffect, useState} from "react"
import {toast} from "sonner"
import dynamic from 'next/dynamic';
import * as React from 'react';
import { useCallback, useEffect, useState } from 'react';
import { toast } from 'sonner';
import {
Activity,
Cpu,
@@ -15,134 +15,142 @@ import {
RefreshCw,
Server,
Terminal,
} from "lucide-react"
} from 'lucide-react';
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Button} from "@/components/ui/button"
import {Skeleton} from "@/components/ui/skeleton"
import services from "@/lib/services"
import type {DBOverview} from "@/lib/services/db-manage"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Skeleton } from '@/components/ui/skeleton';
import services from '@/lib/services';
import type { DBOverview } from '@/lib/services/db-manage';
const sectionFallback = (
<div className="h-48 animate-pulse rounded-lg border border-border/40 bg-muted/20" />
)
<div className='h-48 animate-pulse rounded-lg border border-border/40 bg-muted/20' />
);
const TableBrowser = dynamic(
() => import("./components/table-browser").then((mod) => mod.TableBrowser),
() => import('./components/table-browser').then((mod) => mod.TableBrowser),
{ loading: () => sectionFallback },
)
);
const CacheManager = dynamic(
() => import("./components/cache-manager").then((mod) => mod.CacheManager),
() => import('./components/cache-manager').then((mod) => mod.CacheManager),
{ loading: () => sectionFallback },
)
);
const SQLConsole = dynamic(
() => import("./components/sql-console").then((mod) => mod.SQLConsole),
() => import('./components/sql-console').then((mod) => mod.SQLConsole),
{ ssr: false },
)
);
/**
* 格式化数字,每3位加逗号
*/
const formatNumber = (num: number | string) => {
if (num === undefined || num === null) return "0"
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
}
if (num === undefined || num === null) return '0';
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
export function DatabasePageClient() {
// 核心状态
const [overview, setOverview] = useState<DBOverview | null>(null)
const [tables, setTables] = useState<string[]>([])
const [loadingOverview, setLoadingOverview] = useState<boolean>(true)
const [loadingTables, setLoadingTables] = useState<boolean>(true)
const [exporting, setExporting] = useState<boolean>(false)
const [overview, setOverview] = useState<DBOverview | null>(null);
const [tables, setTables] = useState<string[]>([]);
const [loadingOverview, setLoadingOverview] = useState<boolean>(true);
const [loadingTables, setLoadingTables] = useState<boolean>(true);
const [exporting, setExporting] = useState<boolean>(false);
// 协调子组件刷新与视图切换状态
const [showConsole, setShowConsole] = useState<boolean>(false)
const [refreshTrigger, setRefreshTrigger] = useState<number>(0)
const [showConsole, setShowConsole] = useState<boolean>(false);
const [refreshTrigger, setRefreshTrigger] = useState<number>(0);
// 1. 获取运行概览
const fetchOverview = useCallback(async (isSilent = false) => {
if (!isSilent) setLoadingOverview(true)
if (!isSilent) setLoadingOverview(true);
try {
const data = await services.dbManage.getOverview()
setOverview(data)
const data = await services.dbManage.getOverview();
setOverview(data);
} catch (err) {
toast.error("获取数据库概览失败", {
description: err instanceof Error ? err.message : "未知错误",
})
toast.error('获取数据库概览失败', {
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setLoadingOverview(false)
setLoadingOverview(false);
}
}, [])
}, []);
// 2. 获取表列表
const fetchTables = useCallback(async () => {
setLoadingTables(true)
setLoadingTables(true);
try {
const data = await services.dbManage.listTables()
setTables(data)
const data = await services.dbManage.listTables();
setTables(data);
} catch (err) {
toast.error("获取数据库数据表列表失败", {
description: err instanceof Error ? err.message : "未知错误",
})
toast.error('获取数据库数据表列表失败', {
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setLoadingTables(false)
setLoadingTables(false);
}
}, [])
}, []);
// 3. 协调刷新
const handleRefreshAll = () => {
fetchOverview()
fetchTables()
setRefreshTrigger((prev) => prev + 1)
}
fetchOverview();
fetchTables();
setRefreshTrigger((prev) => prev + 1);
};
// 4. 导出数据库备份
const handleExport = async () => {
setExporting(true)
setExporting(true);
try {
const { blob, filename } = await services.dbManage.exportDatabase()
const url = URL.createObjectURL(blob)
const a = document.createElement("a")
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
a.remove()
URL.revokeObjectURL(url)
toast.success("数据库导出成功", { description: `已下载归档文件: ${filename}` })
const { blob, filename } = await services.dbManage.exportDatabase();
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
toast.success('数据库导出成功', {
description: `已下载归档文件: ${filename}`,
});
} catch (err) {
toast.error("数据库导出失败", {
description: err instanceof Error ? err.message : "导出异常",
})
toast.error('数据库导出失败', {
description: err instanceof Error ? err.message : '导出异常',
});
} finally {
setExporting(false)
}
setExporting(false);
}
};
// 初始化加载
useEffect(() => {
fetchOverview()
fetchTables()
}, [fetchOverview, fetchTables])
fetchOverview();
fetchTables();
}, [fetchOverview, fetchTables]);
// 渲染概览卡片骨架
const renderOverviewSkeleton = () => (
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4">
<div className='grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4'>
{Array.from({ length: 6 }).map((_, i) => (
<Card key={i} className="border-border/40 bg-card/50">
<CardHeader className="pb-2">
<Skeleton className="h-4 w-16" />
<Card key={i} className='border-border/40 bg-card/50'>
<CardHeader className='pb-2'>
<Skeleton className='h-4 w-16' />
</CardHeader>
<CardContent>
<Skeleton className="h-6 w-24" />
<Skeleton className='h-6 w-24' />
</CardContent>
</Card>
))}
</div>
)
);
// 渲染 SQL 查询控台全屏视图
if (showConsole) {
@@ -151,109 +159,135 @@ export function DatabasePageClient() {
dbType={overview?.type}
onClose={() => setShowConsole(false)}
/>
)
);
}
// 正常页面排版布局
return (
<div className="py-6 px-1 space-y-6 w-full">
<div className='py-6 px-1 space-y-6 w-full'>
{/* 顶部控制与标题 */}
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between pb-4 gap-4">
<div className="flex items-center gap-2">
<Database className="size-5 text-primary" />
<div className='flex flex-col sm:flex-row sm:items-center sm:justify-between pb-4 gap-4'>
<div className='flex items-center gap-2'>
<Database className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">数据管理</h1>
<h1 className='text-2xl font-semibold tracking-tight'>数据管理</h1>
</div>
</div>
<Button
size="sm"
variant="secondary"
className="h-8 gap-1.5 text-xs self-start sm:self-auto"
size='sm'
variant='secondary'
className='h-8 gap-1.5 text-xs self-start sm:self-auto'
onClick={handleRefreshAll}
disabled={loadingOverview}
>
<RefreshCw className={`size-3 ${loadingOverview ? "animate-spin" : ""}`} />
<RefreshCw
className={`size-3 ${loadingOverview ? 'animate-spin' : ''}`}
/>
刷新数据
</Button>
</div>
{/* 1. 概览指标卡片组 */}
{loadingOverview ? (
renderOverviewSkeleton()
) : (
overview && (
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4">
{loadingOverview
? renderOverviewSkeleton()
: overview && (
<div className='grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4'>
{/* 卡片1: 数据库类型 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardDescription className="text-[10px] font-medium">数据库类型</CardDescription>
<Server className="size-3.5 text-muted-foreground" />
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardDescription className='text-[10px] font-medium'>
数据库类型
</CardDescription>
<Server className='size-3.5 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-sm font-bold uppercase">{overview.type}</div>
<div className='text-sm font-bold uppercase'>
{overview.type}
</div>
</CardContent>
</Card>
{/* 卡片2: 数据库版本 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300 col-span-1 md:col-span-2 lg:col-span-1">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardDescription className="text-[10px] font-medium">版本信息</CardDescription>
<Cpu className="size-3.5 text-muted-foreground" />
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300 col-span-1 md:col-span-2 lg:col-span-1'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardDescription className='text-[10px] font-medium'>
版本信息
</CardDescription>
<Cpu className='size-3.5 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-xs font-semibold truncate" title={overview.version}>
{overview.version.split(" ").slice(0, 2).join(" ")}
<div
className='text-xs font-semibold truncate'
title={overview.version}
>
{overview.version.split(' ').slice(0, 2).join(' ')}
</div>
</CardContent>
</Card>
{/* 卡片3: 数据库名称 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardDescription className="text-[10px] font-medium">名称/路径</CardDescription>
<Database className="size-3.5 text-muted-foreground" />
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardDescription className='text-[10px] font-medium'>
名称/路径
</CardDescription>
<Database className='size-3.5 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-xs font-semibold truncate" title={overview.name}>
{overview.name.substring(overview.name.lastIndexOf("/") + 1)}
<div
className='text-xs font-semibold truncate'
title={overview.name}
>
{overview.name.substring(
overview.name.lastIndexOf('/') + 1,
)}
</div>
</CardContent>
</Card>
{/* 卡片4: 数据库大小 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardDescription className="text-[10px] font-medium">数据库大小</CardDescription>
<HardDrive className="size-3.5 text-muted-foreground" />
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardDescription className='text-[10px] font-medium'>
数据库大小
</CardDescription>
<HardDrive className='size-3.5 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-sm font-bold">{overview.size}</div>
<div className='text-sm font-bold'>{overview.size}</div>
</CardContent>
</Card>
{/* 卡片5: 表数量 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardDescription className="text-[10px] font-medium">物理数据表</CardDescription>
<Layers className="size-3.5 text-muted-foreground" />
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardDescription className='text-[10px] font-medium'>
物理数据表
</CardDescription>
<Layers className='size-3.5 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-sm font-bold">{formatNumber(overview.table_count)}</div>
<div className='text-sm font-bold'>
{formatNumber(overview.table_count)}
</div>
</CardContent>
</Card>
{/* 卡片6: 连接数 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardDescription className="text-[10px] font-medium">活跃连接数</CardDescription>
<Activity className="size-3.5 text-muted-foreground" />
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-sm hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardDescription className='text-[10px] font-medium'>
活跃连接数
</CardDescription>
<Activity className='size-3.5 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-sm font-bold">{formatNumber(overview.connections)}</div>
<div className='text-sm font-bold'>
{formatNumber(overview.connections)}
</div>
</CardContent>
</Card>
</div>
)
)}
{/* 2. 数据表浏览器区块 */}
@@ -264,60 +298,64 @@ export function DatabasePageClient() {
/>
{/* 3. 缓存管理区块 */}
<CacheManager
refreshTrigger={refreshTrigger}
/>
<CacheManager refreshTrigger={refreshTrigger} />
{/* 4. 底部功能卡片区 */}
<Card className="border-border/40 bg-card/50 backdrop-blur-sm shadow-sm">
<CardHeader className="pb-3 border-b border-dashed">
<CardTitle className="text-sm font-semibold">功能区</CardTitle>
<CardDescription className="text-[11px]">数据库导出及自定义高级 SQL 执行终端</CardDescription>
<Card className='border-border/40 bg-card/50 backdrop-blur-sm shadow-sm'>
<CardHeader className='pb-3 border-b border-dashed'>
<CardTitle className='text-sm font-semibold'>功能区</CardTitle>
<CardDescription className='text-[11px]'>
数据库导出及自定义高级 SQL 执行终端
</CardDescription>
</CardHeader>
<CardContent className="pt-4">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<CardContent className='pt-4'>
<div className='grid grid-cols-1 md:grid-cols-2 gap-4'>
{/* 功能一: 数据库导出 */}
<div className="flex items-center justify-between p-4 border border-dashed rounded-lg hover:bg-muted/20 transition-colors duration-150">
<div className="space-y-1 pr-4">
<p className="text-xs font-semibold flex items-center gap-1.5">
<Download className="size-4 text-primary" />
<div className='flex items-center justify-between p-4 border border-dashed rounded-lg hover:bg-muted/20 transition-colors duration-150'>
<div className='space-y-1 pr-4'>
<p className='text-xs font-semibold flex items-center gap-1.5'>
<Download className='size-4 text-primary' />
数据库备份导出
</p>
<p className="text-[10px] text-muted-foreground">
直接导出并下载物理数据库镜像文件(SQLite 导出 .db,PostgreSQL 导出为打包的 .sql 文本文件)
<p className='text-[10px] text-muted-foreground'>
直接导出并下载物理数据库镜像文件(SQLite 导出 .db,PostgreSQL
导出为打包的 .sql 文本文件)
</p>
</div>
<Button
size="sm"
variant="outline"
className="h-8 text-xs gap-1"
size='sm'
variant='outline'
className='h-8 text-xs gap-1'
onClick={handleExport}
disabled={exporting}
>
<RefreshCw className={`size-3 ${exporting ? "animate-spin" : ""}`} />
{exporting ? "正在准备..." : "开始导出"}
<RefreshCw
className={`size-3 ${exporting ? 'animate-spin' : ''}`}
/>
{exporting ? '正在准备...' : '开始导出'}
</Button>
</div>
{/* 功能二: SQL 查询终端 */}
<div className="flex items-center justify-between p-4 border border-dashed rounded-lg hover:bg-muted/20 transition-colors duration-150">
<div className="space-y-1 pr-4">
<p className="text-xs font-semibold flex items-center gap-1.5">
<Terminal className="size-4 text-primary" />
<div className='flex items-center justify-between p-4 border border-dashed rounded-lg hover:bg-muted/20 transition-colors duration-150'>
<div className='space-y-1 pr-4'>
<p className='text-xs font-semibold flex items-center gap-1.5'>
<Terminal className='size-4 text-primary' />
SQL 查询控台
</p>
<p className="text-[10px] text-muted-foreground">
打开在线 SQL 控制终端,执行原生的 SQL 进行数据筛选、更新、调试或性能优化
<p className='text-[10px] text-muted-foreground'>
打开在线 SQL 控制终端,执行原生的 SQL
进行数据筛选、更新、调试或性能优化
</p>
</div>
<Button
size="sm"
className="h-8 text-xs gap-1 bg-primary text-primary-foreground hover:bg-primary/95"
size='sm'
className='h-8 text-xs gap-1 bg-primary text-primary-foreground hover:bg-primary/95'
onClick={() => {
setShowConsole(true)
setShowConsole(true);
}}
>
<FileText className="size-3.5" />
<FileText className='size-3.5' />
进入控台
</Button>
</div>
@@ -325,5 +363,5 @@ export function DatabasePageClient() {
</CardContent>
</Card>
</div>
)
);
}
+2 -2
View File
@@ -1,5 +1,5 @@
import {DatabasePageClient} from "./page-client"
import { DatabasePageClient } from './page-client';
export default function DatabasePage() {
return <DatabasePageClient />
return <DatabasePageClient />;
}
+131 -37
View File
@@ -1,82 +1,176 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
"use client"
'use client';
import * as React from "react"
import {Code} from "lucide-react"
import * as React from 'react';
import { Code } from 'lucide-react';
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
// 引入模块化拆分后的 Tab 子组件
import {DashboardTab} from "./tabs/dashboard-tab"
import {TableTab} from "./tabs/table-tab"
import {ControlsTab} from "./tabs/controls-tab"
import { DashboardTab } from './tabs/dashboard-tab';
import { TableTab } from './tabs/table-tab';
import { ControlsTab } from './tabs/controls-tab';
export default function HeaderDemoPage() {
const [activeTab, setActiveTab] = React.useState("dashboard")
const [activeTab, setActiveTab] = React.useState('dashboard');
return (
<div className="py-6 px-1 space-y-6">
<div className='py-6 px-1 space-y-6'>
{/* 1. 标准页面标题 */}
<div className="flex items-center gap-2">
<Code className="size-5 text-primary" />
<div className='flex items-center gap-2'>
<Code className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">开发规范示例</h1>
<h1 className='text-2xl font-semibold tracking-tight'>
开发规范示例
</h1>
</div>
</div>
{/* 说明区域 */}
<Card className="border-dashed shadow-none">
<CardHeader className="pb-3">
<div className="flex items-center gap-2">
<Code className="size-4 text-primary" />
<CardTitle className="text-base font-semibold">规范说明</CardTitle>
<Card className='border-dashed shadow-none'>
<CardHeader className='pb-3'>
<div className='flex items-center gap-2'>
<Code className='size-4 text-primary' />
<CardTitle className='text-base font-semibold'>规范说明</CardTitle>
</div>
<CardDescription>
此页面展示了后台 Sidebar 关联页面的标准标题栏布局,供开发参考。
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<div className="space-y-2">
<h3 className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">核心规范细节与编写原则:</h3>
<ul className="list-disc pl-4 text-xs text-muted-foreground space-y-1.5">
<li><strong>结构专注</strong>:页面标题区必须保持绝对干净。请不要在标题下方放置任何描述段落或小字副标题,让页面在视线进入时保持极简与信息纯粹。</li>
<li><strong>容器与上边距对齐</strong>:使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">flex items-center gap-2</code> 作为标题行基础容器(不含下边框),外层统一使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">py-6 px-1</code> (或 <code className="bg-muted px-1 py-0.5 rounded font-mono">py-6</code>) 进行上边距的视觉对齐,确保切换菜单时顶部高度感一致。</li>
<li><strong>图标呈现</strong>:直接将 Lucide 图标组件嵌套于标题容器中,应用 <code className="bg-muted px-1 py-0.5 rounded font-mono">size-5 text-primary</code> 样式。禁止为图标包裹任何背景色小卡片、圆角或装饰性边框。</li>
<li><strong>文本样式标准</strong>:文本使用且仅使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">{'h1 className="text-2xl font-semibold tracking-tight"'}</code>,禁用字重加粗(如 <code className="bg-muted px-1 py-0.5 rounded font-mono">font-bold</code>)或任何渐变色,保证全站字体一致性。</li>
<li><strong>Tabs 模块化拆分</strong>:凡是带有多个 Tab 页切换的页面,**禁止**将所有 Tab 的渲染代码堆积在同一个物理页面主文件内。每个 Tab 页对应的 Content 必须单独拆分为独立组件文件(如 <code className="bg-muted px-1 py-0.5 rounded font-mono">tabs/events-tab.tsx</code> 或就近放在 <code className="bg-muted px-1 py-0.5 rounded font-mono">components/</code> 下),主页面文件只负责维护当前 Tab 的激活状态及外层骨架布局。</li>
<li><strong>复杂度驱动的区块拆分</strong>:不仅是 Tabs 切换,当一个页面代码行数超过 600 行时,必须将高密度区块拆分为子组件。路由专属、不复用的子组件应放在对应路由的特征目录中(如 <code className="bg-muted px-1 py-0.5 rounded font-mono">app/(main)/admin/database/components/</code>),只有真正跨页面复用的组件才进入全局 <code className="bg-muted px-1 py-0.5 rounded font-mono">components/common/</code>。典型标杆案例参考“数据管理” (<code className="bg-muted px-1 py-0.5 rounded font-mono">/admin/database</code>) 的 <code className="bg-muted px-1 py-0.5 rounded font-mono">table-browser.tsx</code>、<code className="bg-muted px-1 py-0.5 rounded font-mono">cache-manager.tsx</code> 与 <code className="bg-muted px-1 py-0.5 rounded font-mono">sql-console.tsx</code>。</li>
<CardContent className='space-y-4'>
<div className='space-y-2'>
<h3 className='text-xs font-semibold uppercase tracking-wider text-muted-foreground'>
核心规范细节与编写原则:
</h3>
<ul className='list-disc pl-4 text-xs text-muted-foreground space-y-1.5'>
<li>
<strong>结构专注</strong>
:页面标题区必须保持绝对干净。请不要在标题下方放置任何描述段落或小字副标题,让页面在视线进入时保持极简与信息纯粹。
</li>
<li>
<strong>容器与上边距对齐</strong>:使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
flex items-center gap-2
</code>{' '}
作为标题行基础容器(不含下边框),外层统一使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
py-6 px-1
</code>{' '}
(或{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
py-6
</code>
) 进行上边距的视觉对齐,确保切换菜单时顶部高度感一致。
</li>
<li>
<strong>图标呈现</strong>:直接将 Lucide
图标组件嵌套于标题容器中,应用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
size-5 text-primary
</code>{' '}
样式。禁止为图标包裹任何背景色小卡片、圆角或装饰性边框。
</li>
<li>
<strong>文本样式标准</strong>:文本使用且仅使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
{'h1 className="text-2xl font-semibold tracking-tight"'}
</code>
,禁用字重加粗(如{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
font-bold
</code>
)或任何渐变色,保证全站字体一致性。
</li>
<li>
<strong>Tabs 模块化拆分</strong>:凡是带有多个 Tab
页切换的页面,**禁止**将所有 Tab
的渲染代码堆积在同一个物理页面主文件内。每个 Tab 页对应的
Content 必须单独拆分为独立组件文件(如{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
tabs/events-tab.tsx
</code>{' '}
或就近放在{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
components/
</code>{' '}
下),主页面文件只负责维护当前 Tab 的激活状态及外层骨架布局。
</li>
<li>
<strong>复杂度驱动的区块拆分</strong>:不仅是 Tabs
切换,当一个页面代码行数超过 600
行时,必须将高密度区块拆分为子组件。路由专属、不复用的子组件应放在对应路由的特征目录中(如{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
app/(main)/admin/database/components/
</code>
),只有真正跨页面复用的组件才进入全局{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
components/common/
</code>
。典型标杆案例参考“数据管理” (
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
/admin/database
</code>
) 的{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
table-browser.tsx
</code>
、
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
cache-manager.tsx
</code>{' '}
与{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
sql-console.tsx
</code>
。
</li>
</ul>
</div>
</CardContent>
</Card>
{/* Tabs 切换 */}
<Tabs value={activeTab} onValueChange={setActiveTab} className="w-full">
<TabsList variant="line" className="w-fit inline-flex gap-8 mb-6">
<TabsTrigger value="dashboard" className="px-0 pb-2 text-xs font-semibold">
<Tabs value={activeTab} onValueChange={setActiveTab} className='w-full'>
<TabsList variant='line' className='w-fit inline-flex gap-8 mb-6'>
<TabsTrigger
value='dashboard'
className='px-0 pb-2 text-xs font-semibold'
>
仪表盘与指标卡片
</TabsTrigger>
<TabsTrigger value="table" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='table'
className='px-0 pb-2 text-xs font-semibold'
>
数据表格规范
</TabsTrigger>
<TabsTrigger value="controls" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='controls'
className='px-0 pb-2 text-xs font-semibold'
>
输入控件与表单
</TabsTrigger>
</TabsList>
<TabsContent value="dashboard" className="focus-visible:outline-none">
<TabsContent value='dashboard' className='focus-visible:outline-none'>
<DashboardTab />
</TabsContent>
<TabsContent value="table" className="focus-visible:outline-none">
<TabsContent value='table' className='focus-visible:outline-none'>
<TableTab />
</TabsContent>
<TabsContent value="controls" className="focus-visible:outline-none">
<TabsContent value='controls' className='focus-visible:outline-none'>
<ControlsTab />
</TabsContent>
</Tabs>
</div>
)
);
}
@@ -1,100 +1,148 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
"use client"
'use client';
import * as React from "react"
import {Code} from "lucide-react"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Label} from "@/components/ui/label"
import {Input} from "@/components/ui/input"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select"
import {Checkbox} from "@/components/ui/checkbox"
import {Button} from "@/components/ui/button"
import * as React from 'react';
import { Code } from 'lucide-react';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Label } from '@/components/ui/label';
import { Input } from '@/components/ui/input';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Checkbox } from '@/components/ui/checkbox';
import { Button } from '@/components/ui/button';
export function ControlsTab() {
const [formData, setFormData] = React.useState({
name: "",
category: "database",
name: '',
category: 'database',
allowNotify: true,
channel: "all",
})
channel: 'all',
});
return (
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className='grid grid-cols-1 lg:grid-cols-2 gap-6'>
{/* 控件展示 */}
<Card className="border-dashed shadow-none">
<CardHeader className="pb-3">
<CardTitle className="text-sm font-semibold">表单与输入框交互演示</CardTitle>
<CardDescription>包含按钮变体、下拉选择、多选框及基础输入框的标准样式</CardDescription>
<Card className='border-dashed shadow-none'>
<CardHeader className='pb-3'>
<CardTitle className='text-sm font-semibold'>
表单与输入框交互演示
</CardTitle>
<CardDescription>
包含按钮变体、下拉选择、多选框及基础输入框的标准样式
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<CardContent className='space-y-4'>
{/* 输入框 */}
<div className="space-y-1.5">
<Label htmlFor="task-name" className="text-xs font-medium text-foreground">任务名称</Label>
<div className='space-y-1.5'>
<Label
htmlFor='task-name'
className='text-xs font-medium text-foreground'
>
任务名称
</Label>
<Input
id="task-name"
id='task-name'
value={formData.name}
onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
placeholder="输入要创建的任务描述..."
className="h-8 text-xs shadow-none bg-background focus-visible:ring-1"
onChange={(e) =>
setFormData((prev) => ({ ...prev, name: e.target.value }))
}
placeholder='输入要创建的任务描述...'
className='h-8 text-xs shadow-none bg-background focus-visible:ring-1'
/>
<p className="text-[10px] text-muted-foreground">任务名称将用于后台执行记录的可读性显示</p>
<p className='text-[10px] text-muted-foreground'>
任务名称将用于后台执行记录的可读性显示
</p>
</div>
{/* 下拉框 */}
<div className="space-y-1.5">
<Label className="text-xs font-medium text-foreground">任务分类</Label>
<div className='space-y-1.5'>
<Label className='text-xs font-medium text-foreground'>
任务分类
</Label>
<Select
value={formData.category}
onValueChange={(val) => setFormData(prev => ({ ...prev, category: val }))}
onValueChange={(val) =>
setFormData((prev) => ({ ...prev, category: val }))
}
>
<SelectTrigger className="h-8 text-xs shadow-none bg-background focus:ring-1">
<SelectTrigger className='h-8 text-xs shadow-none bg-background focus:ring-1'>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="database">数据库运维</SelectItem>
<SelectItem value="sync">数据批量同步</SelectItem>
<SelectItem value="cleanup">过期磁盘清理</SelectItem>
<SelectItem value='database'>数据库运维</SelectItem>
<SelectItem value='sync'>数据批量同步</SelectItem>
<SelectItem value='cleanup'>过期磁盘清理</SelectItem>
</SelectContent>
</Select>
</div>
{/* 多选框 Checkbox */}
<div className="flex items-start gap-2.5 p-3 bg-muted/30 border border-dashed rounded-lg">
<div className='flex items-start gap-2.5 p-3 bg-muted/30 border border-dashed rounded-lg'>
<Checkbox
id="allow-notify"
id='allow-notify'
checked={formData.allowNotify}
onCheckedChange={(checked) => setFormData(prev => ({ ...prev, allowNotify: checked === true }))}
className="mt-0.5"
onCheckedChange={(checked) =>
setFormData((prev) => ({
...prev,
allowNotify: checked === true,
}))
}
className='mt-0.5'
/>
<div className="grid gap-1">
<div className='grid gap-1'>
<label
htmlFor="allow-notify"
className="text-xs font-medium text-foreground cursor-pointer select-none leading-none"
htmlFor='allow-notify'
className='text-xs font-medium text-foreground cursor-pointer select-none leading-none'
>
允许触发异步通知推送
</label>
<p className="text-[10px] text-muted-foreground leading-normal">
<p className='text-[10px] text-muted-foreground leading-normal'>
勾选后,在当前后台任务顺利执行结束时,会关联派发多渠道通知推送。
</p>
</div>
</div>
{/* 按钮变体 */}
<div className="pt-2">
<div className="text-[10px] font-semibold text-muted-foreground uppercase tracking-wider mb-2">按钮样式对齐:</div>
<div className="flex flex-wrap items-center gap-2">
<Button size="sm" className="h-8 text-xs shadow-none">
<div className='pt-2'>
<div className='text-[10px] font-semibold text-muted-foreground uppercase tracking-wider mb-2'>
按钮样式对齐:
</div>
<div className='flex flex-wrap items-center gap-2'>
<Button size='sm' className='h-8 text-xs shadow-none'>
主要操作 (Primary)
</Button>
<Button variant="secondary" size="sm" className="h-8 text-xs shadow-none">
<Button
variant='secondary'
size='sm'
className='h-8 text-xs shadow-none'
>
次要操作 (Secondary)
</Button>
<Button variant="outline" size="sm" className="h-8 border-dashed text-xs shadow-none">
<Button
variant='outline'
size='sm'
className='h-8 border-dashed text-xs shadow-none'
>
辅助线框 (Outline)
</Button>
<Button variant="ghost" size="sm" className="h-8 text-xs hover:bg-muted">
<Button
variant='ghost'
size='sm'
className='h-8 text-xs hover:bg-muted'
>
幽灵按钮 (Ghost)
</Button>
</div>
@@ -103,38 +151,84 @@ export function ControlsTab() {
</Card>
{/* 控件设计规范说明 */}
<Card className="border-dashed shadow-none bg-muted/20">
<CardHeader className="pb-3">
<CardTitle className="text-sm font-semibold flex items-center gap-1.5">
<Code className="size-4 text-primary" />
<Card className='border-dashed shadow-none bg-muted/20'>
<CardHeader className='pb-3'>
<CardTitle className='text-sm font-semibold flex items-center gap-1.5'>
<Code className='size-4 text-primary' />
表单设计规范
</CardTitle>
</CardHeader>
<CardContent className="space-y-4 text-xs text-muted-foreground">
<div className="space-y-2 leading-relaxed">
<p className="font-semibold text-foreground text-[11px]">1. 统一控件高度与投影:</p>
<CardContent className='space-y-4 text-xs text-muted-foreground'>
<div className='space-y-2 leading-relaxed'>
<p className='font-semibold text-foreground text-[11px]'>
1. 统一控件高度与投影:
</p>
<p>
输入框 (<code className="bg-muted px-1 py-0.5 rounded font-mono">Input</code>) 与下拉选择器 (<code className="bg-muted px-1 py-0.5 rounded font-mono">SelectTrigger</code>) 的高度应统一限制为小号高度 <code className="bg-muted px-1 py-0.5 rounded font-mono">h-8</code>,字体大小使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">text-xs</code>,并使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">shadow-none</code> 去除原生的重叠阴影。
输入框 (
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
Input
</code>
) 与下拉选择器 (
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
SelectTrigger
</code>
) 的高度应统一限制为小号高度{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
h-8
</code>
,字体大小使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
text-xs
</code>
,并使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
shadow-none
</code>{' '}
去除原生的重叠阴影。
</p>
<p className="font-semibold text-foreground text-[11px] pt-1">2. Checkbox 与文本的对齐:</p>
<p className='font-semibold text-foreground text-[11px] pt-1'>
2. Checkbox 与文本的对齐:
</p>
<p>
Checkbox 应在左侧垂直偏上对齐,使用容器 <code className="bg-muted px-1 py-0.5 rounded font-mono">flex items-start gap-2.5</code> 排列。Checkbox 组件本身添加 <code className="bg-muted px-1 py-0.5 rounded font-mono">mt-0.5</code>,从而在多行描述文本的情况下依然保持良好的顶对齐,避免出现居中对齐导致的凌乱感。
Checkbox 应在左侧垂直偏上对齐,使用容器{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
flex items-start gap-2.5
</code>{' '}
排列。Checkbox 组件本身添加{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
mt-0.5
</code>
,从而在多行描述文本的情况下依然保持良好的顶对齐,避免出现居中对齐导致的凌乱感。
</p>
<p className="font-semibold text-foreground text-[11px] pt-1">3. 按钮使用策略:</p>
<p className='font-semibold text-foreground text-[11px] pt-1'>
3. 按钮使用策略:
</p>
<p>
普通主表单及新增/保存按钮,一律使用默认 Primary 按钮或次要 <code className="bg-muted px-1 py-0.5 rounded font-mono">{'variant="secondary"'}</code> 按钮,小号高度统一采用 <code className="bg-muted px-1 py-0.5 rounded font-mono">h-8 text-xs</code>。线框按钮必须带有 <code className="bg-muted px-1 py-0.5 rounded font-mono">border-dashed shadow-none</code> 类。
普通主表单及新增/保存按钮,一律使用默认 Primary 按钮或次要{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
{'variant="secondary"'}
</code>{' '}
按钮,小号高度统一采用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
h-8 text-xs
</code>
。线框按钮必须带有{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
border-dashed shadow-none
</code>{' '}
类。
</p>
</div>
<div className="bg-background/80 p-3 rounded border border-border/40 font-mono text-[10px] space-y-2 overflow-x-auto">
<div className='bg-background/80 p-3 rounded border border-border/40 font-mono text-[10px] space-y-2 overflow-x-auto'>
<div>{`// 1. Label 与 Input 对齐`}</div>
<div>{`<div className="space-y-1.5">
<Label htmlFor="id" className="text-xs font-medium text-foreground">标题</Label>
<Input id="id" className="h-8 text-xs shadow-none" />
</div>`}</div>
<div className="pt-2">{`// 2. Checkbox 顶对齐`}</div>
<div className='pt-2'>{`// 2. Checkbox 顶对齐`}</div>
<div>{`<div className="flex items-start gap-2.5">
<Checkbox id="c" className="mt-0.5" />
<div className="grid gap-1">
@@ -146,5 +240,5 @@ export function ControlsTab() {
</CardContent>
</Card>
</div>
)
);
}
@@ -1,61 +1,81 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
"use client"
'use client';
import * as React from "react"
import {Code, Cpu, Database, HardDrive} from "lucide-react"
import {Card, CardContent, CardHeader, CardTitle} from "@/components/ui/card"
import {Progress} from "@/components/ui/progress"
import * as React from 'react';
import { Code, Cpu, Database, HardDrive } from 'lucide-react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Progress } from '@/components/ui/progress';
export function DashboardTab() {
return (
<div className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div className='space-y-6'>
<div className='grid grid-cols-1 md:grid-cols-3 gap-4'>
{/* CPU */}
<Card className="border-dashed shadow-none">
<CardHeader className="flex flex-row items-center justify-between pb-2">
<span className="text-xs font-medium text-muted-foreground">容器 CPU 使用率</span>
<Cpu className="size-4 text-primary" />
<Card className='border-dashed shadow-none'>
<CardHeader className='flex flex-row items-center justify-between pb-2'>
<span className='text-xs font-medium text-muted-foreground'>
容器 CPU 使用率
</span>
<Cpu className='size-4 text-primary' />
</CardHeader>
<CardContent className="space-y-2">
<div className="text-2xl font-semibold tracking-tight">42.8 %</div>
<Progress value={42.8} className="h-1.5" />
<p className="text-[10px] text-muted-foreground flex items-center gap-1">
<span className="size-1.5 rounded-full bg-emerald-500 inline-block animate-pulse" />
<CardContent className='space-y-2'>
<div className='text-2xl font-semibold tracking-tight'>42.8 %</div>
<Progress value={42.8} className='h-1.5' />
<p className='text-[10px] text-muted-foreground flex items-center gap-1'>
<span className='size-1.5 rounded-full bg-emerald-500 inline-block animate-pulse' />
容器运行正常,负载平稳
</p>
</CardContent>
</Card>
{/* Storage */}
<Card className="border-dashed shadow-none">
<CardHeader className="flex flex-row items-center justify-between pb-2">
<span className="text-xs font-medium text-muted-foreground">存储已用容量</span>
<HardDrive className="size-4 text-primary" />
<Card className='border-dashed shadow-none'>
<CardHeader className='flex flex-row items-center justify-between pb-2'>
<span className='text-xs font-medium text-muted-foreground'>
存储已用容量
</span>
<HardDrive className='size-4 text-primary' />
</CardHeader>
<CardContent className="space-y-2">
<div className="text-2xl font-semibold tracking-tight">72.4 GB <span className="text-xs text-muted-foreground">/ 100 GB</span></div>
<Progress value={72.4} className="h-1.5" />
<p className="text-[10px] text-muted-foreground">
<CardContent className='space-y-2'>
<div className='text-2xl font-semibold tracking-tight'>
72.4 GB{' '}
<span className='text-xs text-muted-foreground'>/ 100 GB</span>
</div>
<Progress value={72.4} className='h-1.5' />
<p className='text-[10px] text-muted-foreground'>
数据已同步远端 S3 存储桶,支持自动过期
</p>
</CardContent>
</Card>
{/* DB Overview */}
<Card className="border-dashed shadow-none">
<CardHeader className="flex flex-row items-center justify-between pb-2">
<span className="text-xs font-medium text-muted-foreground">物理数据库</span>
<Database className="size-4 text-primary" />
<Card className='border-dashed shadow-none'>
<CardHeader className='flex flex-row items-center justify-between pb-2'>
<span className='text-xs font-medium text-muted-foreground'>
物理数据库
</span>
<Database className='size-4 text-primary' />
</CardHeader>
<CardContent className="space-y-2">
<div className="text-2xl font-semibold tracking-tight">PostgreSQL <span className="text-xs font-normal text-muted-foreground">(v16.2)</span></div>
<div className="flex items-center gap-3 text-xs text-muted-foreground">
<div>活跃连接: <span className="font-semibold text-foreground">12</span></div>
<div>总表数量: <span className="font-semibold text-foreground">34</span></div>
<CardContent className='space-y-2'>
<div className='text-2xl font-semibold tracking-tight'>
PostgreSQL{' '}
<span className='text-xs font-normal text-muted-foreground'>
(v16.2)
</span>
</div>
<p className="text-[10px] text-muted-foreground">
<div className='flex items-center gap-3 text-xs text-muted-foreground'>
<div>
活跃连接:{' '}
<span className='font-semibold text-foreground'>12</span>
</div>
<div>
总表数量:{' '}
<span className='font-semibold text-foreground'>34</span>
</div>
</div>
<p className='text-[10px] text-muted-foreground'>
链接池自动维护中,最大连接限制为 100
</p>
</CardContent>
@@ -63,22 +83,52 @@ export function DashboardTab() {
</div>
{/* 指标卡片规范与代码 */}
<Card className="border-dashed shadow-none bg-muted/20">
<CardHeader className="pb-3">
<CardTitle className="text-sm font-semibold flex items-center gap-1.5">
<Code className="size-4 text-primary" />
<Card className='border-dashed shadow-none bg-muted/20'>
<CardHeader className='pb-3'>
<CardTitle className='text-sm font-semibold flex items-center gap-1.5'>
<Code className='size-4 text-primary' />
指标卡片 & 仪表盘设计规范
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<ul className="list-disc pl-4 text-xs text-muted-foreground space-y-1.5">
<li>指标卡片统一采用 <code className="bg-muted px-1 py-0.5 rounded font-mono">border-dashed shadow-none</code> 作为边框和投影。</li>
<li>头部使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">flex flex-row items-center justify-between pb-2</code> 布局,左侧为标题小字,右侧为直接呈现的 Lucide 图标。</li>
<li>数值强调使用且仅使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">text-2xl font-semibold tracking-tight</code> 展示。</li>
<li>进度条使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">h-1.5</code> 细条高度,不喧宾夺主。</li>
<li>底部的次要说明信息必须统一使用 <code className="bg-muted px-1 py-0.5 rounded font-mono">text-[10px] text-muted-foreground</code> 等辅助类。</li>
<CardContent className='space-y-4'>
<ul className='list-disc pl-4 text-xs text-muted-foreground space-y-1.5'>
<li>
指标卡片统一采用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
border-dashed shadow-none
</code>{' '}
作为边框和投影。
</li>
<li>
头部使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
flex flex-row items-center justify-between pb-2
</code>{' '}
布局,左侧为标题小字,右侧为直接呈现的 Lucide 图标。
</li>
<li>
数值强调使用且仅使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
text-2xl font-semibold tracking-tight
</code>{' '}
展示。
</li>
<li>
进度条使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
h-1.5
</code>{' '}
细条高度,不喧宾夺主。
</li>
<li>
底部的次要说明信息必须统一使用{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
text-[10px] text-muted-foreground
</code>{' '}
等辅助类。
</li>
</ul>
<pre className="text-[11px] font-mono text-muted-foreground overflow-x-auto p-3 bg-background rounded border border-border/40 leading-relaxed">
<pre className='text-[11px] font-mono text-muted-foreground overflow-x-auto p-3 bg-background rounded border border-border/40 leading-relaxed'>
{`<Card className="border-dashed shadow-none">
<CardHeader className="flex flex-row items-center justify-between pb-2">
<span className="text-xs font-medium text-muted-foreground">指标标题</span>
@@ -94,5 +144,5 @@ export function DashboardTab() {
</CardContent>
</Card>
</div>
)
);
}
+185 -67
View File
@@ -1,92 +1,171 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
"use client"
'use client';
import * as React from "react"
import {Code, Edit, ListFilter, Plus, Search, Trash2} from "lucide-react"
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import {Badge} from "@/components/ui/badge"
import {Card, CardContent, CardHeader, CardTitle} from "@/components/ui/card"
import * as React from 'react';
import { Code, Edit, ListFilter, Plus, Search, Trash2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
export function TableTab() {
const mockTableData = [
{ id: "TX-1002", name: "用户信息批量同步导出", type: "数据同步", status: "success", duration: "1.2s", time: "2026-06-15 14:22" },
{ id: "TX-1003", name: "系统数据库每周自动备份归档", type: "数据库运维", status: "running", duration: "在执行中", time: "2026-06-15 15:00" },
{ id: "TX-1004", name: "远端S3过期缓存对象自动清理任务", type: "磁盘清理", status: "failed", duration: "0.4s", time: "2026-06-15 10:05" },
]
{
id: 'TX-1002',
name: '用户信息批量同步导出',
type: '数据同步',
status: 'success',
duration: '1.2s',
time: '2026-06-15 14:22',
},
{
id: 'TX-1003',
name: '系统数据库每周自动备份归档',
type: '数据库运维',
status: 'running',
duration: '在执行中',
time: '2026-06-15 15:00',
},
{
id: 'TX-1004',
name: '远端S3过期缓存对象自动清理任务',
type: '磁盘清理',
status: 'failed',
duration: '0.4s',
time: '2026-06-15 10:05',
},
];
return (
<div className="space-y-6">
<div className='space-y-6'>
{/* 表格控制工具栏 */}
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
<div className="flex items-center gap-2">
<div className="relative w-64">
<Search className="absolute left-2.5 top-2.5 size-3 text-muted-foreground" />
<Input placeholder="输入任务名称搜索..." className="h-8 pl-8 text-xs w-full shadow-none border-dashed bg-background" />
<div className='flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3'>
<div className='flex items-center gap-2'>
<div className='relative w-64'>
<Search className='absolute left-2.5 top-2.5 size-3 text-muted-foreground' />
<Input
placeholder='输入任务名称搜索...'
className='h-8 pl-8 text-xs w-full shadow-none border-dashed bg-background'
/>
</div>
<Button variant="outline" size="sm" className="h-8 border-dashed text-xs shadow-none">
<ListFilter className="size-3 mr-1" />
<Button
variant='outline'
size='sm'
className='h-8 border-dashed text-xs shadow-none'
>
<ListFilter className='size-3 mr-1' />
过滤
</Button>
</div>
<Button size="sm" className="h-8 text-xs shadow-none">
<Plus className="size-3.5 mr-1" />
<Button size='sm' className='h-8 text-xs shadow-none'>
<Plus className='size-3.5 mr-1' />
新建任务
</Button>
</div>
{/* 数据表格本身 */}
<div className="border border-dashed shadow-none rounded-lg overflow-hidden bg-background">
<Table className="w-full caption-bottom text-sm min-w-full">
<TableHeader className="bg-muted/40">
<TableRow className="border-dashed hover:bg-transparent">
<TableHead className="w-[100px] text-xs font-semibold">任务编号</TableHead>
<TableHead className="text-xs font-semibold">执行任务名称</TableHead>
<TableHead className="w-[120px] text-xs font-semibold">任务类型</TableHead>
<TableHead className="w-[100px] text-xs font-semibold">状态</TableHead>
<TableHead className="w-[100px] text-xs font-semibold">执行耗时</TableHead>
<TableHead className="w-[150px] text-xs font-semibold">触发时间</TableHead>
<TableHead className="w-[80px] text-xs font-semibold text-right">操作</TableHead>
<div className='border border-dashed shadow-none rounded-lg overflow-hidden bg-background'>
<Table className='w-full caption-bottom text-sm min-w-full'>
<TableHeader className='bg-muted/40'>
<TableRow className='border-dashed hover:bg-transparent'>
<TableHead className='w-[100px] text-xs font-semibold'>
任务编号
</TableHead>
<TableHead className='text-xs font-semibold'>
执行任务名称
</TableHead>
<TableHead className='w-[120px] text-xs font-semibold'>
任务类型
</TableHead>
<TableHead className='w-[100px] text-xs font-semibold'>
状态
</TableHead>
<TableHead className='w-[100px] text-xs font-semibold'>
执行耗时
</TableHead>
<TableHead className='w-[150px] text-xs font-semibold'>
触发时间
</TableHead>
<TableHead className='w-[80px] text-xs font-semibold text-right'>
操作
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{mockTableData.map((row) => (
<TableRow key={row.id} className="border-dashed hover:bg-muted/10 transition-colors">
<TableCell className="font-mono text-xs font-semibold">{row.id}</TableCell>
<TableCell className="text-xs font-medium text-foreground">{row.name}</TableCell>
<TableCell className="text-xs text-muted-foreground">{row.type}</TableCell>
<TableRow
key={row.id}
className='border-dashed hover:bg-muted/10 transition-colors'
>
<TableCell className='font-mono text-xs font-semibold'>
{row.id}
</TableCell>
<TableCell className='text-xs font-medium text-foreground'>
{row.name}
</TableCell>
<TableCell className='text-xs text-muted-foreground'>
{row.type}
</TableCell>
<TableCell>
{row.status === "success" && (
<Badge variant="outline" className="text-[10px] bg-emerald-500/10 border-emerald-500/20 text-emerald-600 rounded-full py-0 px-2 font-medium">
<span className="size-1 bg-emerald-500 rounded-full mr-1.5 shrink-0" />
{row.status === 'success' && (
<Badge
variant='outline'
className='text-[10px] bg-emerald-500/10 border-emerald-500/20 text-emerald-600 rounded-full py-0 px-2 font-medium'
>
<span className='size-1 bg-emerald-500 rounded-full mr-1.5 shrink-0' />
成功
</Badge>
)}
{row.status === "running" && (
<Badge variant="outline" className="text-[10px] bg-blue-500/10 border-blue-500/20 text-blue-600 rounded-full py-0 px-2 font-medium">
<span className="size-1 bg-blue-500 rounded-full mr-1.5 shrink-0 animate-pulse" />
{row.status === 'running' && (
<Badge
variant='outline'
className='text-[10px] bg-blue-500/10 border-blue-500/20 text-blue-600 rounded-full py-0 px-2 font-medium'
>
<span className='size-1 bg-blue-500 rounded-full mr-1.5 shrink-0 animate-pulse' />
运行中
</Badge>
)}
{row.status === "failed" && (
<Badge variant="outline" className="text-[10px] bg-destructive/10 border-destructive/20 text-destructive rounded-full py-0 px-2 font-medium">
<span className="size-1 bg-destructive rounded-full mr-1.5 shrink-0" />
{row.status === 'failed' && (
<Badge
variant='outline'
className='text-[10px] bg-destructive/10 border-destructive/20 text-destructive rounded-full py-0 px-2 font-medium'
>
<span className='size-1 bg-destructive rounded-full mr-1.5 shrink-0' />
失败
</Badge>
)}
</TableCell>
<TableCell className="text-xs text-muted-foreground font-mono">{row.duration}</TableCell>
<TableCell className="text-xs text-muted-foreground">{row.time}</TableCell>
<TableCell className="text-right">
<div className="flex items-center justify-end gap-1.5">
<Button variant="ghost" size="icon" className="h-6 w-6 rounded hover:bg-muted text-muted-foreground">
<Edit className="size-3" />
<TableCell className='text-xs text-muted-foreground font-mono'>
{row.duration}
</TableCell>
<TableCell className='text-xs text-muted-foreground'>
{row.time}
</TableCell>
<TableCell className='text-right'>
<div className='flex items-center justify-end gap-1.5'>
<Button
variant='ghost'
size='icon'
className='h-6 w-6 rounded hover:bg-muted text-muted-foreground'
>
<Edit className='size-3' />
</Button>
<Button variant="ghost" size="icon" className="h-6 w-6 rounded hover:bg-destructive/10 text-destructive">
<Trash2 className="size-3" />
<Button
variant='ghost'
size='icon'
className='h-6 w-6 rounded hover:bg-destructive/10 text-destructive'
>
<Trash2 className='size-3' />
</Button>
</div>
</TableCell>
@@ -97,22 +176,61 @@ export function TableTab() {
</div>
{/* 表格规范指引 */}
<Card className="border-dashed shadow-none bg-muted/20">
<CardHeader className="pb-3">
<CardTitle className="text-sm font-semibold flex items-center gap-1.5">
<Code className="size-4 text-primary" />
<Card className='border-dashed shadow-none bg-muted/20'>
<CardHeader className='pb-3'>
<CardTitle className='text-sm font-semibold flex items-center gap-1.5'>
<Code className='size-4 text-primary' />
表格设计规范与代码模板
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<ul className="list-disc pl-4 text-xs text-muted-foreground space-y-1.5">
<li>表格容器必须统一使用带有虚线边框的容器,即类名:<code className="bg-muted px-1 py-0.5 rounded font-mono">border border-dashed shadow-none rounded-lg overflow-hidden bg-background</code>。</li>
<li>表头背景设为 <code className="bg-muted px-1 py-0.5 rounded font-mono">bg-muted/40</code>。表头表项字号统一为小字粗体 <code className="bg-muted px-1 py-0.5 rounded font-mono">text-xs font-semibold</code>。</li>
<li>单元格行与列之间使用虚线分割(<code className="bg-muted px-1 py-0.5 rounded font-mono">border-dashed</code>)。表格行悬浮交互采用温和过渡色 <code className="bg-muted px-1 py-0.5 rounded font-mono">hover:bg-muted/10 transition-colors</code>。</li>
<li>编码、主键或唯一 ID 类字段一律采用等宽字体呈现:<code className="bg-muted px-1 py-0.5 rounded font-mono">font-mono text-xs</code>。</li>
<li>状态标签(Badge)尽量圆角化(<code className="bg-muted px-1 py-0.5 rounded font-mono">rounded-full py-0 px-2</code>),并以浅背景配以各自对应的图标圆点,以实现现代、高对比度的视觉质感。</li>
<CardContent className='space-y-4'>
<ul className='list-disc pl-4 text-xs text-muted-foreground space-y-1.5'>
<li>
表格容器必须统一使用带有虚线边框的容器,即类名:
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
border border-dashed shadow-none rounded-lg overflow-hidden
bg-background
</code>
。
</li>
<li>
表头背景设为{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
bg-muted/40
</code>
。表头表项字号统一为小字粗体{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
text-xs font-semibold
</code>
。
</li>
<li>
单元格行与列之间使用虚线分割(
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
border-dashed
</code>
)。表格行悬浮交互采用温和过渡色{' '}
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
hover:bg-muted/10 transition-colors
</code>
。
</li>
<li>
编码、主键或唯一 ID 类字段一律采用等宽字体呈现:
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
font-mono text-xs
</code>
。
</li>
<li>
状态标签(Badge)尽量圆角化(
<code className='bg-muted px-1 py-0.5 rounded font-mono'>
rounded-full py-0 px-2
</code>
),并以浅背景配以各自对应的图标圆点,以实现现代、高对比度的视觉质感。
</li>
</ul>
<pre className="text-[11px] font-mono text-muted-foreground overflow-x-auto p-3 bg-background rounded border border-border/40 leading-relaxed">
<pre className='text-[11px] font-mono text-muted-foreground overflow-x-auto p-3 bg-background rounded border border-border/40 leading-relaxed'>
{`<div className="border border-dashed shadow-none rounded-lg overflow-hidden bg-background">
<Table>
<TableHeader className="bg-muted/40">
@@ -138,5 +256,5 @@ export function TableTab() {
</CardContent>
</Card>
</div>
)
);
}
@@ -1,8 +1,8 @@
"use client"
'use client';
import * as React from "react"
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"
import {AnimatePresence, motion} from "motion/react"
import * as React from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { AnimatePresence, motion } from 'motion/react';
import {
Download,
Eye,
@@ -16,14 +16,21 @@ import {
Trash2,
Upload,
X,
} from "lucide-react"
import {toast} from "sonner"
} from 'lucide-react';
import { toast } from 'sonner';
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Badge} from "@/components/ui/badge"
import {Checkbox} from "@/components/ui/checkbox"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import { Checkbox } from '@/components/ui/checkbox';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import {
AlertDialog,
AlertDialogAction,
@@ -33,207 +40,236 @@ import {
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import {Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle} from "@/components/ui/sheet"
import {FileImagePreview} from "@/components/common/file-image-preview"
import services, {formatFileSize} from "@/lib/services"
import type {Upload as UploadRecord} from "@/lib/services/upload/types"
} from '@/components/ui/alert-dialog';
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
} from '@/components/ui/sheet';
import { FileImagePreview } from '@/components/common/file-image-preview';
import services, { formatFileSize } from '@/lib/services';
import type { Upload as UploadRecord } from '@/lib/services/upload/types';
/* ─── 工具函数 ─────────────────────────────────────────── */
function getFileIcon(mimeType: string, className = "size-8") {
if (mimeType.startsWith("image/")) return <FileImage className={`${className} text-blue-400`} />
if (mimeType.startsWith("video/")) return <FileVideo className={`${className} text-purple-400`} />
if (mimeType.startsWith("audio/")) return <FileAudio className={`${className} text-green-400`} />
if (mimeType.includes("zip") || mimeType.includes("tar") || mimeType.includes("gzip"))
return <FileArchive className={`${className} text-amber-400`} />
return <FileText className={`${className} text-slate-400`} />
function getFileIcon(mimeType: string, className = 'size-8') {
if (mimeType.startsWith('image/'))
return <FileImage className={`${className} text-blue-400`} />;
if (mimeType.startsWith('video/'))
return <FileVideo className={`${className} text-purple-400`} />;
if (mimeType.startsWith('audio/'))
return <FileAudio className={`${className} text-green-400`} />;
if (
mimeType.includes('zip') ||
mimeType.includes('tar') ||
mimeType.includes('gzip')
)
return <FileArchive className={`${className} text-amber-400`} />;
return <FileText className={`${className} text-slate-400`} />;
}
function formatDate(dateStr: string) {
return new Date(dateStr).toLocaleString("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
})
return new Date(dateStr).toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
});
}
export function FileList() {
const queryClient = useQueryClient()
const [keyword, setKeyword] = React.useState("")
const [debouncedKeyword, setDebouncedKeyword] = React.useState("")
const [selectedIds, setSelectedIds] = React.useState<Set<string>>(new Set())
const [deleteTarget, setDeleteTarget] = React.useState<UploadRecord | null>(null)
const [detailTarget, setDetailTarget] = React.useState<UploadRecord | null>(null)
const [page, setPage] = React.useState(1)
const pageSize = 15 // 表格视图下 15 条更紧凑
const queryClient = useQueryClient();
const [keyword, setKeyword] = React.useState('');
const [debouncedKeyword, setDebouncedKeyword] = React.useState('');
const [selectedIds, setSelectedIds] = React.useState<Set<string>>(new Set());
const [deleteTarget, setDeleteTarget] = React.useState<UploadRecord | null>(
null,
);
const [detailTarget, setDetailTarget] = React.useState<UploadRecord | null>(
null,
);
const [page, setPage] = React.useState(1);
const pageSize = 15; // 表格视图下 15 条更紧凑
// 搜索防抖
React.useEffect(() => {
const timer = setTimeout(() => {
setDebouncedKeyword(keyword)
setPage(1)
}, 400)
return () => clearTimeout(timer)
}, [keyword])
setDebouncedKeyword(keyword);
setPage(1);
}, 400);
return () => clearTimeout(timer);
}, [keyword]);
// 文件列表查询
const listQuery = useQuery({
queryKey: ["files", "all", page, pageSize, debouncedKeyword],
queryFn: () => services.adminUpload.listUploads(page, pageSize, debouncedKeyword || undefined),
})
queryKey: ['files', 'all', page, pageSize, debouncedKeyword],
queryFn: () =>
services.adminUpload.listUploads(
page,
pageSize,
debouncedKeyword || undefined,
),
});
const storageDriverQuery = useQuery({
queryKey: ["admin", "storage-config", "driver"],
queryKey: ['admin', 'storage-config', 'driver'],
queryFn: async () => {
const record = await services.adminSystemConfig.getSystemConfig("storage_config")
const cfg = JSON.parse(record.value) as {driver?: string}
return cfg.driver ?? "local"
const record =
await services.adminSystemConfig.getSystemConfig('storage_config');
const cfg = JSON.parse(record.value) as { driver?: string };
return cfg.driver ?? 'local';
},
})
});
const files = listQuery.data?.items ?? []
const total = listQuery.data?.total ?? 0
const totalPages = Math.ceil(total / pageSize)
const files = listQuery.data?.items ?? [];
const total = listQuery.data?.total ?? 0;
const totalPages = Math.ceil(total / pageSize);
const isAllSelected = files.length > 0 && files.every((f) => selectedIds.has(f.id))
const isSomeSelected = files.length > 0 && files.some((f) => selectedIds.has(f.id)) && !isAllSelected
const isAllSelected =
files.length > 0 && files.every((f) => selectedIds.has(f.id));
const isSomeSelected =
files.length > 0 &&
files.some((f) => selectedIds.has(f.id)) &&
!isAllSelected;
const handleSelectAll = () => {
if (isAllSelected) {
setSelectedIds((prev) => {
const next = new Set(prev)
files.forEach((f) => next.delete(f.id))
return next
})
const next = new Set(prev);
files.forEach((f) => next.delete(f.id));
return next;
});
} else {
setSelectedIds((prev) => {
const next = new Set(prev)
files.forEach((f) => next.add(f.id))
return next
})
}
const next = new Set(prev);
files.forEach((f) => next.add(f.id));
return next;
});
}
};
// 删除单文件
const deleteMutation = useMutation({
mutationFn: (id: string) => services.adminUpload.deleteFile(id),
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: ["files", "my"] })
void queryClient.invalidateQueries({ queryKey: ["files", "stats"] })
toast.success("文件已删除")
setDeleteTarget(null)
void queryClient.invalidateQueries({ queryKey: ['files', 'my'] });
void queryClient.invalidateQueries({ queryKey: ['files', 'stats'] });
toast.success('文件已删除');
setDeleteTarget(null);
},
onError: (err: Error) => toast.error(err.message || "删除失败"),
})
onError: (err: Error) => toast.error(err.message || '删除失败'),
});
// 批量 ZIP 下载
const batchDownloadMutation = useMutation({
mutationFn: (ids: string[]) => services.adminUpload.batchDownload(ids),
onSuccess: (blob) => {
const url = URL.createObjectURL(blob)
const a = document.createElement("a")
a.href = url
a.download = "batch_download.zip"
a.click()
URL.revokeObjectURL(url)
toast.success("批量下载已开始")
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'batch_download.zip';
a.click();
URL.revokeObjectURL(url);
toast.success('批量下载已开始');
},
onError: () => toast.error("批量下载失败"),
})
onError: () => toast.error('批量下载失败'),
});
const toggleSelect = (id: string) => {
setSelectedIds((prev) => {
const next = new Set(prev)
const next = new Set(prev);
if (next.has(id)) {
next.delete(id)
next.delete(id);
} else {
next.add(id)
}
return next
})
next.add(id);
}
return next;
});
};
const clearSelection = () => setSelectedIds(new Set())
const clearSelection = () => setSelectedIds(new Set());
const selectAll = () => setSelectedIds(new Set(files.map((f) => f.id)))
const selectAll = () => setSelectedIds(new Set(files.map((f) => f.id)));
const handleDownload = (file: UploadRecord) => {
const url = services.adminUpload.getDownloadUrl(file.id)
const a = document.createElement("a")
a.href = url
a.download = file.file_name
a.click()
}
const url = services.adminUpload.getDownloadUrl(file.id);
const a = document.createElement('a');
a.href = url;
a.download = file.file_name;
a.click();
};
return (
<div className="space-y-6">
<div className='space-y-6'>
{/* 搜索栏 */}
<div className="flex items-center gap-3">
<div className="relative flex-1 max-w-sm">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 size-3.5 text-muted-foreground" />
<div className='flex items-center gap-3'>
<div className='relative flex-1 max-w-sm'>
<Search className='absolute left-3 top-1/2 -translate-y-1/2 size-3.5 text-muted-foreground' />
<Input
placeholder="搜索文件名..."
className="pl-8 h-8 text-xs border-dashed rounded-lg focus-visible:ring-0"
placeholder='搜索文件名...'
className='pl-8 h-8 text-xs border-dashed rounded-lg focus-visible:ring-0'
value={keyword}
onChange={(e) => setKeyword(e.target.value)}
/>
{keyword && (
<button
className="absolute right-2.5 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
onClick={() => setKeyword("")}
className='absolute right-2.5 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground'
onClick={() => setKeyword('')}
>
<X className="size-3" />
<X className='size-3' />
</button>
)}
</div>
{files.length > 0 && (
<Button
size="sm"
variant="ghost"
className="text-xs h-8 border border-dashed text-muted-foreground"
onClick={selectedIds.size === files.length ? clearSelection : selectAll}
size='sm'
variant='ghost'
className='text-xs h-8 border border-dashed text-muted-foreground'
onClick={
selectedIds.size === files.length ? clearSelection : selectAll
}
>
{selectedIds.size === files.length ? "取消全选" : "全选本页"}
{selectedIds.size === files.length ? '取消全选' : '全选本页'}
</Button>
)}
{/* 打包下载等批量操作 */}
<div className="flex items-center gap-2 ml-auto shrink-0">
<div className='flex items-center gap-2 ml-auto shrink-0'>
<AnimatePresence>
{selectedIds.size > 0 && (
<motion.div
initial={{ opacity: 0, scale: 0.9 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.9 }}
className="flex items-center gap-2"
className='flex items-center gap-2'
>
<Badge variant="secondary" className="text-xs px-2.5">
<Badge variant='secondary' className='text-xs px-2.5'>
已选 {selectedIds.size} 个
</Badge>
<Button
size="sm"
variant="outline"
className="border-dashed text-xs h-8"
size='sm'
variant='outline'
className='border-dashed text-xs h-8'
onClick={() => batchDownloadMutation.mutate([...selectedIds])}
disabled={batchDownloadMutation.isPending}
>
{batchDownloadMutation.isPending ? (
<Loader2 className="size-3.5 mr-1 animate-spin" />
<Loader2 className='size-3.5 mr-1 animate-spin' />
) : (
<FileArchive className="size-3.5 mr-1" />
<FileArchive className='size-3.5 mr-1' />
)}
打包下载
</Button>
<Button
size="sm"
variant="ghost"
className="text-xs h-8 px-2"
size='sm'
variant='ghost'
className='text-xs h-8 px-2'
onClick={clearSelection}
>
<X className="size-3.5" />
<X className='size-3.5' />
</Button>
</motion.div>
)}
@@ -241,131 +277,152 @@ export function FileList() {
</div>
{total > 0 && (
<span className="text-xs text-muted-foreground shrink-0">共 {total} 个文件</span>
<span className='text-xs text-muted-foreground shrink-0'>
共 {total} 个文件
</span>
)}
</div>
{/* 文件列表 Table */}
{listQuery.isPending ? (
<div className="flex items-center justify-center py-20">
<Loader2 className="size-6 animate-spin text-sky-500" />
<div className='flex items-center justify-center py-20'>
<Loader2 className='size-6 animate-spin text-sky-500' />
</div>
) : files.length === 0 ? (
<div className="flex flex-col items-center justify-center py-20 gap-4 text-muted-foreground">
<Upload className="size-12 text-muted-foreground/30" />
<p className="text-sm">
{debouncedKeyword ? "没有匹配的文件" : "您还没有上传任何文件"}
<div className='flex flex-col items-center justify-center py-20 gap-4 text-muted-foreground'>
<Upload className='size-12 text-muted-foreground/30' />
<p className='text-sm'>
{debouncedKeyword ? '没有匹配的文件' : '您还没有上传任何文件'}
</p>
</div>
) : (
<div className="border border-dashed rounded-xl bg-card overflow-hidden">
<div className='border border-dashed rounded-xl bg-card overflow-hidden'>
<Table>
<TableHeader>
<TableRow className="hover:bg-transparent border-dashed">
<TableHead className="w-[50px] pl-4 py-3">
<TableRow className='hover:bg-transparent border-dashed'>
<TableHead className='w-[50px] pl-4 py-3'>
<Checkbox
checked={isAllSelected || (isSomeSelected ? "indeterminate" : false)}
checked={
isAllSelected ||
(isSomeSelected ? 'indeterminate' : false)
}
onCheckedChange={handleSelectAll}
/>
</TableHead>
<TableHead className="w-[80px] py-3">预览</TableHead>
<TableHead className="w-[180px] py-3">ID</TableHead>
<TableHead className="py-3">文件名</TableHead>
<TableHead className="max-w-[200px] truncate py-3">路径</TableHead>
<TableHead className="w-[100px] py-3">业务类别</TableHead>
<TableHead className="w-[125px] py-3">MIME类型</TableHead>
<TableHead className="w-[100px] py-3">大小</TableHead>
<TableHead className="w-[150px] py-3">上传时间</TableHead>
<TableHead className="w-[120px] text-right pr-4 py-3">操作</TableHead>
<TableHead className='w-[80px] py-3'>预览</TableHead>
<TableHead className='w-[180px] py-3'>ID</TableHead>
<TableHead className='py-3'>文件名</TableHead>
<TableHead className='max-w-[200px] truncate py-3'>
路径
</TableHead>
<TableHead className='w-[100px] py-3'>业务类别</TableHead>
<TableHead className='w-[125px] py-3'>MIME类型</TableHead>
<TableHead className='w-[100px] py-3'>大小</TableHead>
<TableHead className='w-[150px] py-3'>上传时间</TableHead>
<TableHead className='w-[120px] text-right pr-4 py-3'>
操作
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{files.map((file) => {
const isSelected = selectedIds.has(file.id)
const isSelected = selectedIds.has(file.id);
return (
<TableRow
key={file.id}
className={`border-dashed hover:bg-muted/30 transition-colors ${
isSelected ? "bg-sky-500/5 hover:bg-sky-500/10" : ""
isSelected ? 'bg-sky-500/5 hover:bg-sky-500/10' : ''
}`}
>
<TableCell className="pl-4 py-3">
<TableCell className='pl-4 py-3'>
<Checkbox
checked={isSelected}
onCheckedChange={() => toggleSelect(file.id)}
/>
</TableCell>
<TableCell className="py-3">
<div className="flex items-center justify-center size-9 rounded-lg bg-muted/40 overflow-hidden border">
{file.mime_type.startsWith("image/") ? (
<TableCell className='py-3'>
<div className='flex items-center justify-center size-9 rounded-lg bg-muted/40 overflow-hidden border'>
{file.mime_type.startsWith('image/') ? (
<FileImagePreview
fileId={file.id}
alt={file.file_name}
quality="low"
variant="compact"
className="size-full object-cover"
quality='low'
variant='compact'
className='size-full object-cover'
/>
) : (
getFileIcon(file.mime_type, "size-4.5")
getFileIcon(file.mime_type, 'size-4.5')
)}
</div>
</TableCell>
<TableCell className="py-3 font-mono text-[11px] text-muted-foreground select-all">
<TableCell className='py-3 font-mono text-[11px] text-muted-foreground select-all'>
{file.id}
</TableCell>
<TableCell className="py-3 font-medium max-w-[180px] truncate text-xs" title={file.file_name}>
<TableCell
className='py-3 font-medium max-w-[180px] truncate text-xs'
title={file.file_name}
>
{file.file_name}
</TableCell>
<TableCell className="py-3 font-mono text-[11px] max-w-[200px] truncate text-muted-foreground select-all" title={file.file_path}>
<TableCell
className='py-3 font-mono text-[11px] max-w-[200px] truncate text-muted-foreground select-all'
title={file.file_path}
>
{file.file_path}
</TableCell>
<TableCell className="py-3">
<Badge variant="secondary" className="text-[10px] py-0 px-1.5 font-normal rounded-md">
<TableCell className='py-3'>
<Badge
variant='secondary'
className='text-[10px] py-0 px-1.5 font-normal rounded-md'
>
{file.type}
</Badge>
</TableCell>
<TableCell className="py-3 text-[11px] text-muted-foreground truncate max-w-[125px]" title={file.mime_type}>
<TableCell
className='py-3 text-[11px] text-muted-foreground truncate max-w-[125px]'
title={file.mime_type}
>
{file.mime_type}
</TableCell>
<TableCell className="py-3 text-xs">
<TableCell className='py-3 text-xs'>
{formatFileSize(file.file_size)}
</TableCell>
<TableCell className="py-3 text-xs text-muted-foreground">
<TableCell className='py-3 text-xs text-muted-foreground'>
{formatDate(file.created_at)}
</TableCell>
<TableCell className="py-3 text-right pr-4">
<div className="flex items-center justify-end gap-0.5">
<TableCell className='py-3 text-right pr-4'>
<div className='flex items-center justify-end gap-0.5'>
<Button
size="icon"
variant="ghost"
className="h-7 w-7 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground"
title="查看详情"
size='icon'
variant='ghost'
className='h-7 w-7 rounded-md hover:bg-muted text-muted-foreground hover:text-foreground'
title='查看详情'
onClick={() => setDetailTarget(file)}
>
<Eye className="size-3.5" />
<Eye className='size-3.5' />
</Button>
<Button
size="icon"
variant="ghost"
className="h-7 w-7 rounded-md hover:bg-muted"
title="下载"
size='icon'
variant='ghost'
className='h-7 w-7 rounded-md hover:bg-muted'
title='下载'
onClick={() => handleDownload(file)}
>
<Download className="size-3.5" />
<Download className='size-3.5' />
</Button>
<Button
size="icon"
variant="ghost"
className="h-7 w-7 rounded-md hover:bg-destructive/10 text-muted-foreground hover:text-destructive"
title="删除"
size='icon'
variant='ghost'
className='h-7 w-7 rounded-md hover:bg-destructive/10 text-muted-foreground hover:text-destructive'
title='删除'
onClick={() => setDeleteTarget(file)}
>
<Trash2 className="size-3.5" />
<Trash2 className='size-3.5' />
</Button>
</div>
</TableCell>
</TableRow>
)
);
})}
</TableBody>
</Table>
@@ -374,23 +431,23 @@ export function FileList() {
{/* 分页 */}
{totalPages > 1 && (
<div className="flex items-center justify-center gap-2 pt-2">
<div className='flex items-center justify-center gap-2 pt-2'>
<Button
size="sm"
variant="outline"
className="border-dashed text-xs h-7 px-3"
size='sm'
variant='outline'
className='border-dashed text-xs h-7 px-3'
disabled={page <= 1}
onClick={() => setPage((p) => p - 1)}
>
上一页
</Button>
<span className="text-xs text-muted-foreground">
<span className='text-xs text-muted-foreground'>
{page} / {totalPages}
</span>
<Button
size="sm"
variant="outline"
className="border-dashed text-xs h-7 px-3"
size='sm'
variant='outline'
className='border-dashed text-xs h-7 px-3'
disabled={page >= totalPages}
onClick={() => setPage((p) => p + 1)}
>
@@ -400,29 +457,34 @@ export function FileList() {
)}
{/* 详情查看 Sheet */}
<Sheet open={!!detailTarget} onOpenChange={(open) => !open && setDetailTarget(null)}>
<SheetContent className="w-full sm:max-w-md overflow-y-auto p-6">
<SheetHeader className="border-b pb-4 px-0">
<Sheet
open={!!detailTarget}
onOpenChange={(open) => !open && setDetailTarget(null)}
>
<SheetContent className='w-full sm:max-w-md overflow-y-auto p-6'>
<SheetHeader className='border-b pb-4 px-0'>
<SheetTitle>文件详情</SheetTitle>
<SheetDescription>查看文件的完整元数据与配置属性。</SheetDescription>
<SheetDescription>
查看文件的完整元数据与配置属性。
</SheetDescription>
</SheetHeader>
{detailTarget && (
<div className="space-y-6 py-4">
<div className='space-y-6 py-4'>
{/* 大图/格式预览 */}
<div className="flex items-center justify-center h-48 rounded-xl bg-muted/30 border border-dashed overflow-hidden p-2">
{detailTarget.mime_type.startsWith("image/") ? (
<div className='flex items-center justify-center h-48 rounded-xl bg-muted/30 border border-dashed overflow-hidden p-2'>
{detailTarget.mime_type.startsWith('image/') ? (
<FileImagePreview
fileId={detailTarget.id}
alt={detailTarget.file_name}
quality="low"
className="max-h-full max-w-full object-contain rounded-lg shadow-sm"
fallbackClassName="min-h-32 w-full rounded-lg"
quality='low'
className='max-h-full max-w-full object-contain rounded-lg shadow-sm'
fallbackClassName='min-h-32 w-full rounded-lg'
/>
) : (
<div className="flex flex-col items-center gap-3">
{getFileIcon(detailTarget.mime_type, "size-14")}
<span className="text-xs font-medium text-muted-foreground uppercase">
<div className='flex flex-col items-center gap-3'>
{getFileIcon(detailTarget.mime_type, 'size-14')}
<span className='text-xs font-medium text-muted-foreground uppercase'>
{detailTarget.extension} 文件
</span>
</div>
@@ -430,72 +492,119 @@ export function FileList() {
</div>
{/* 元数据列表 */}
<div className="space-y-4 text-xs">
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">文件 ID</span>
<span className="col-span-2 font-mono break-all select-all text-foreground/90">{detailTarget.id}</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">文件名</span>
<span className="col-span-2 break-all font-medium text-foreground/90">{detailTarget.file_name}</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">存储路径</span>
<span className="col-span-2 font-mono break-all select-all text-foreground/90">{detailTarget.file_path}</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">大小</span>
<span className="col-span-2 text-foreground/90">
{formatFileSize(detailTarget.file_size)} ({detailTarget.file_size.toLocaleString()} 字节)
<div className='space-y-4 text-xs'>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
文件 ID
</span>
<span className='col-span-2 font-mono break-all select-all text-foreground/90'>
{detailTarget.id}
</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">MIME 类型</span>
<span className="col-span-2 font-mono text-foreground/90">{detailTarget.mime_type}</span>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
文件名
</span>
<span className='col-span-2 break-all font-medium text-foreground/90'>
{detailTarget.file_name}
</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">文件后缀</span>
<span className="col-span-2 font-mono text-foreground/90">{detailTarget.extension}</span>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
存储路径
</span>
<span className='col-span-2 font-mono break-all select-all text-foreground/90'>
{detailTarget.file_path}
</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">Hash (SHA-256)</span>
<span className="col-span-2 font-mono break-all select-all text-foreground/90">{detailTarget.hash}</span>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
大小
</span>
<span className='col-span-2 text-foreground/90'>
{formatFileSize(detailTarget.file_size)} (
{detailTarget.file_size.toLocaleString()} 字节)
</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">业务类型</span>
<span className="col-span-2">
<Badge variant="secondary" className="text-[10px] py-0 px-2 rounded-md font-normal">
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
MIME 类型
</span>
<span className='col-span-2 font-mono text-foreground/90'>
{detailTarget.mime_type}
</span>
</div>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
文件后缀
</span>
<span className='col-span-2 font-mono text-foreground/90'>
{detailTarget.extension}
</span>
</div>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
Hash (SHA-256)
</span>
<span className='col-span-2 font-mono break-all select-all text-foreground/90'>
{detailTarget.hash}
</span>
</div>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
业务类型
</span>
<span className='col-span-2'>
<Badge
variant='secondary'
className='text-[10px] py-0 px-2 rounded-md font-normal'
>
{detailTarget.type}
</Badge>
</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">存储驱动</span>
<span className="col-span-2 font-mono text-foreground/90">{storageDriverQuery.data ?? "local"}</span>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
存储驱动
</span>
<span className='col-span-2 font-mono text-foreground/90'>
{storageDriverQuery.data ?? 'local'}
</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">上传者 ID</span>
<span className="col-span-2 font-mono text-foreground/90">{detailTarget.user_id}</span>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
上传者 ID
</span>
<span className='col-span-2 font-mono text-foreground/90'>
{detailTarget.user_id}
</span>
</div>
<div className="grid grid-cols-3 gap-2 border-b border-dashed pb-2">
<span className="text-muted-foreground font-medium">上传时间</span>
<span className="col-span-2 text-foreground/90">{formatDate(detailTarget.created_at)}</span>
<div className='grid grid-cols-3 gap-2 border-b border-dashed pb-2'>
<span className='text-muted-foreground font-medium'>
上传时间
</span>
<span className='col-span-2 text-foreground/90'>
{formatDate(detailTarget.created_at)}
</span>
</div>
{detailTarget.metadata && Object.keys(detailTarget.metadata).length > 0 && (
<div className="space-y-1.5 pt-2">
<span className="text-muted-foreground font-medium block">额外元数据</span>
<pre className="p-3 bg-muted/40 border rounded-lg text-[10px] font-mono overflow-x-auto text-foreground/90 max-h-40 overflow-y-auto">
{detailTarget.metadata &&
Object.keys(detailTarget.metadata).length > 0 && (
<div className='space-y-1.5 pt-2'>
<span className='text-muted-foreground font-medium block'>
额外元数据
</span>
<pre className='p-3 bg-muted/40 border rounded-lg text-[10px] font-mono overflow-x-auto text-foreground/90 max-h-40 overflow-y-auto'>
{JSON.stringify(detailTarget.metadata, null, 2)}
</pre>
</div>
@@ -507,29 +616,40 @@ export function FileList() {
</Sheet>
{/* 删除确认 Dialog */}
<AlertDialog open={!!deleteTarget} onOpenChange={(open) => !open && setDeleteTarget(null)}>
<AlertDialog
open={!!deleteTarget}
onOpenChange={(open) => !open && setDeleteTarget(null)}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>确认删除文件</AlertDialogTitle>
<AlertDialogDescription>
确定要删除文件{" "}
<span className="font-semibold text-foreground">「{deleteTarget?.file_name}」</span>{" "}
确定要删除文件{' '}
<span className='font-semibold text-foreground'>
「{deleteTarget?.file_name}」
</span>{' '}
吗?此操作不可撤销。
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={deleteMutation.isPending}>取消</AlertDialogCancel>
<AlertDialogCancel disabled={deleteMutation.isPending}>
取消
</AlertDialogCancel>
<AlertDialogAction
onClick={() => deleteTarget && deleteMutation.mutate(deleteTarget.id)}
onClick={() =>
deleteTarget && deleteMutation.mutate(deleteTarget.id)
}
disabled={deleteMutation.isPending}
className="bg-destructive hover:bg-destructive/90 text-destructive-foreground"
className='bg-destructive hover:bg-destructive/90 text-destructive-foreground'
>
{deleteMutation.isPending && <Loader2 className="mr-1.5 size-3.5 animate-spin" />}
{deleteMutation.isPending && (
<Loader2 className='mr-1.5 size-3.5 animate-spin' />
)}
确认删除
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)
);
}
@@ -1,12 +1,35 @@
"use client"
'use client';
import * as React from "react"
import {useQuery} from "@tanstack/react-query"
import {Database, Files, HardDrive, Info, Loader2, TrendingUp, Upload,} from "lucide-react"
import {Area, AreaChart, CartesianGrid, Cell, Pie, PieChart, XAxis, YAxis,} from "recharts"
import * as React from 'react';
import { useQuery } from '@tanstack/react-query';
import {
Database,
Files,
HardDrive,
Info,
Loader2,
TrendingUp,
Upload,
} from 'lucide-react';
import {
Area,
AreaChart,
CartesianGrid,
Cell,
Pie,
PieChart,
XAxis,
YAxis,
} from 'recharts';
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import {
ChartConfig,
ChartContainer,
@@ -14,265 +37,322 @@ import {
ChartLegendContent,
ChartTooltip,
ChartTooltipContent,
} from "@/components/ui/chart"
import services, {formatFileSize} from "@/lib/services"
} from '@/components/ui/chart';
import services, { formatFileSize } from '@/lib/services';
const categoryMap: Record<string, string> = {
"图片": "images",
"视频": "videos",
"音频": "audio",
"文档": "documents",
"压缩包": "archives",
"其他": "others",
}
图片: 'images',
视频: 'videos',
音频: 'audio',
文档: 'documents',
压缩包: 'archives',
其他: 'others',
};
const statsChartConfig = {
images: {
label: "图片",
color: "hsl(var(--chart-1))",
label: '图片',
color: 'hsl(var(--chart-1))',
},
videos: {
label: "视频",
color: "hsl(var(--chart-2))",
label: '视频',
color: 'hsl(var(--chart-2))',
},
audio: {
label: "音频",
color: "hsl(var(--chart-3))",
label: '音频',
color: 'hsl(var(--chart-3))',
},
documents: {
label: "文档",
color: "hsl(var(--chart-4))",
label: '文档',
color: 'hsl(var(--chart-4))',
},
archives: {
label: "压缩包",
color: "hsl(var(--chart-5))",
label: '压缩包',
color: 'hsl(var(--chart-5))',
},
others: {
label: "其他",
color: "hsl(var(--chart-6))",
label: '其他',
color: 'hsl(var(--chart-6))',
},
count: {
label: "新增文件数",
color: "hsl(var(--primary))",
label: '新增文件数',
color: 'hsl(var(--primary))',
},
size: {
label: "新增大小",
color: "hsl(var(--chart-2))",
label: '新增大小',
color: 'hsl(var(--chart-2))',
},
} satisfies ChartConfig
} satisfies ChartConfig;
export function FileStats() {
const [trendMetric, setTrendMetric] = React.useState<"count" | "size">("count")
const [trendMetric, setTrendMetric] = React.useState<'count' | 'size'>(
'count',
);
const statsQuery = useQuery({
queryKey: ["files", "stats"],
queryKey: ['files', 'stats'],
queryFn: () => services.adminUpload.getFileStats(),
staleTime: 0,
refetchOnMount: "always",
})
refetchOnMount: 'always',
});
const stats = statsQuery.data
const stats = statsQuery.data;
const trendData = React.useMemo(() => {
if (!stats?.trend) return []
if (!stats?.trend) return [];
return stats.trend.map((item) => ({
...item,
formattedDate: item.date.substring(5), // YYYY-MM-DD -> MM-DD
value: trendMetric === "count" ? item.count : item.size,
}))
}, [stats?.trend, trendMetric])
value: trendMetric === 'count' ? item.count : item.size,
}));
}, [stats?.trend, trendMetric]);
const categoryCountData = React.useMemo(() => {
if (!stats?.categories) return []
if (!stats?.categories) return [];
return stats.categories
.filter((c) => c.count > 0)
.map((c) => {
const key = categoryMap[c.name] || "others"
const key = categoryMap[c.name] || 'others';
return {
name: key,
value: c.count,
fill: `var(--color-${key})`,
}
})
}, [stats?.categories])
};
});
}, [stats?.categories]);
const categorySizeData = React.useMemo(() => {
if (!stats?.categories) return []
if (!stats?.categories) return [];
return stats.categories
.filter((c) => c.size > 0)
.map((c) => {
const key = categoryMap[c.name] || "others"
const key = categoryMap[c.name] || 'others';
return {
name: key,
value: c.size,
fill: `var(--color-${key})`,
}
})
}, [stats?.categories])
};
});
}, [stats?.categories]);
const maxStats = React.useMemo(() => {
if (!stats?.categories || stats.categories.length === 0) {
return { maxCountName: "无", maxCount: 0, maxSizeName: "无", maxSize: 0 }
return { maxCountName: '无', maxCount: 0, maxSizeName: '无', maxSize: 0 };
}
let maxCountName = "无"
let maxCount = 0
let maxSizeName = "无"
let maxSize = 0
let maxCountName = '无';
let maxCount = 0;
let maxSizeName = '无';
let maxSize = 0;
stats.categories.forEach((cat) => {
if (cat.count > maxCount) {
maxCount = cat.count
maxCountName = cat.name
maxCount = cat.count;
maxCountName = cat.name;
}
if (cat.size > maxSize) {
maxSize = cat.size
maxSizeName = cat.name
maxSize = cat.size;
maxSizeName = cat.name;
}
})
});
return { maxCountName, maxCount, maxSizeName, maxSize }
}, [stats?.categories])
return { maxCountName, maxCount, maxSizeName, maxSize };
}, [stats?.categories]);
const trendSummary = React.useMemo(() => {
if (!stats?.trend) return { count: 0, size: 0 }
if (!stats?.trend) return { count: 0, size: 0 };
return stats.trend.reduce(
(acc, curr) => ({
count: acc.count + curr.count,
size: acc.size + curr.size,
}),
{ count: 0, size: 0 }
)
}, [stats?.trend])
{ count: 0, size: 0 },
);
}, [stats?.trend]);
if (statsQuery.isLoading) {
return (
<div className="flex items-center justify-center py-32">
<Loader2 className="size-8 animate-spin text-sky-500" />
<div className='flex items-center justify-center py-32'>
<Loader2 className='size-8 animate-spin text-sky-500' />
</div>
)
);
}
if (!stats || stats.total_count === 0) {
return (
<div className="flex flex-col items-center justify-center py-24 border border-dashed rounded-xl bg-card text-muted-foreground gap-4">
<Upload className="size-14 text-muted-foreground/25" />
<div className="text-center space-y-1">
<p className="font-medium">暂无上传数据</p>
<p className="text-xs text-muted-foreground">上传文件后即可在此查看容量与文件分布分析。</p>
<div className='flex flex-col items-center justify-center py-24 border border-dashed rounded-xl bg-card text-muted-foreground gap-4'>
<Upload className='size-14 text-muted-foreground/25' />
<div className='text-center space-y-1'>
<p className='font-medium'>暂无上传数据</p>
<p className='text-xs text-muted-foreground'>
上传文件后即可在此查看容量与文件分布分析。
</p>
</div>
</div>
)
);
}
return (
<div className="space-y-6">
<div className='space-y-6'>
{/* 四个指标卡片 */}
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
<Card className="bg-card/25 border-border/40 hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-xs font-semibold tracking-tight text-muted-foreground">总文件数</CardTitle>
<Files className="size-4 text-sky-500" />
<div className='grid gap-4 sm:grid-cols-2 lg:grid-cols-4'>
<Card className='bg-card/25 border-border/40 hover:shadow-md transition-shadow'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardTitle className='text-xs font-semibold tracking-tight text-muted-foreground'>
总文件数
</CardTitle>
<Files className='size-4 text-sky-500' />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold font-mono text-sky-500">
{stats.total_count.toLocaleString()}{" "}
<span className="text-xs font-normal text-muted-foreground">个</span>
<div className='text-2xl font-bold font-mono text-sky-500'>
{stats.total_count.toLocaleString()}{' '}
<span className='text-xs font-normal text-muted-foreground'>
个
</span>
</div>
<p className="text-[10px] text-muted-foreground mt-1">您上传的可用文件数量汇总</p>
<p className='text-[10px] text-muted-foreground mt-1'>
您上传的可用文件数量汇总
</p>
</CardContent>
</Card>
<Card className="bg-card/25 border-border/40 hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-xs font-semibold tracking-tight text-muted-foreground">占用存储容量</CardTitle>
<Database className="size-4 text-emerald-500" />
<Card className='bg-card/25 border-border/40 hover:shadow-md transition-shadow'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardTitle className='text-xs font-semibold tracking-tight text-muted-foreground'>
占用存储容量
</CardTitle>
<Database className='size-4 text-emerald-500' />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold font-mono text-emerald-500">{formatFileSize(stats.total_size)}</div>
<p className="text-[10px] text-muted-foreground mt-1">文件实际在磁盘/S3中占用的总空间</p>
</CardContent>
</Card>
<Card className="bg-card/25 border-border/40 hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-xs font-semibold tracking-tight text-muted-foreground">近 7 天新增文件</CardTitle>
<TrendingUp className="size-4 text-indigo-500" />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold font-mono text-indigo-500">
+{trendSummary.count.toLocaleString()}{" "}
<span className="text-xs font-normal text-muted-foreground">个</span>
<div className='text-2xl font-bold font-mono text-emerald-500'>
{formatFileSize(stats.total_size)}
</div>
<p className="text-[10px] text-muted-foreground mt-1">最近 7 天上传成功的文件总数</p>
<p className='text-[10px] text-muted-foreground mt-1'>
文件实际在磁盘/S3中占用的总空间
</p>
</CardContent>
</Card>
<Card className="bg-card/25 border-border/40 hover:shadow-md transition-shadow">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-xs font-semibold tracking-tight text-muted-foreground">近 7 天新增大小</CardTitle>
<HardDrive className="size-4 text-amber-500" />
<Card className='bg-card/25 border-border/40 hover:shadow-md transition-shadow'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardTitle className='text-xs font-semibold tracking-tight text-muted-foreground'>
近 7 天新增文件
</CardTitle>
<TrendingUp className='size-4 text-indigo-500' />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold font-mono text-amber-500">+{formatFileSize(trendSummary.size)}</div>
<p className="text-[10px] text-muted-foreground mt-1">最近 7 天上传所消耗的存储带宽</p>
<div className='text-2xl font-bold font-mono text-indigo-500'>
+{trendSummary.count.toLocaleString()}{' '}
<span className='text-xs font-normal text-muted-foreground'>
个
</span>
</div>
<p className='text-[10px] text-muted-foreground mt-1'>
最近 7 天上传成功的文件总数
</p>
</CardContent>
</Card>
<Card className='bg-card/25 border-border/40 hover:shadow-md transition-shadow'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardTitle className='text-xs font-semibold tracking-tight text-muted-foreground'>
近 7 天新增大小
</CardTitle>
<HardDrive className='size-4 text-amber-500' />
</CardHeader>
<CardContent>
<div className='text-2xl font-bold font-mono text-amber-500'>
+{formatFileSize(trendSummary.size)}
</div>
<p className='text-[10px] text-muted-foreground mt-1'>
最近 7 天上传所消耗的存储带宽
</p>
</CardContent>
</Card>
</div>
{/* 7 天新增趋势折线图 */}
<Card className="bg-card/20 border-border/40">
<CardHeader className="flex flex-row items-center justify-between">
<Card className='bg-card/20 border-border/40'>
<CardHeader className='flex flex-row items-center justify-between'>
<div>
<CardTitle className="text-sm font-bold">最近 7 天新增趋势</CardTitle>
<CardDescription className="text-xs">按日统计近一周期内的上传波动</CardDescription>
<CardTitle className='text-sm font-bold'>
最近 7 天新增趋势
</CardTitle>
<CardDescription className='text-xs'>
按日统计近一周期内的上传波动
</CardDescription>
</div>
<div className="flex items-center gap-1 border rounded-lg p-0.5 bg-muted/30">
<div className='flex items-center gap-1 border rounded-lg p-0.5 bg-muted/30'>
<Button
size="sm"
variant={trendMetric === "count" ? "secondary" : "ghost"}
className="h-7 text-xs px-2.5 rounded-md"
onClick={() => setTrendMetric("count")}
size='sm'
variant={trendMetric === 'count' ? 'secondary' : 'ghost'}
className='h-7 text-xs px-2.5 rounded-md'
onClick={() => setTrendMetric('count')}
>
新增数量 (个)
</Button>
<Button
size="sm"
variant={trendMetric === "size" ? "secondary" : "ghost"}
className="h-7 text-xs px-2.5 rounded-md"
onClick={() => setTrendMetric("size")}
size='sm'
variant={trendMetric === 'size' ? 'secondary' : 'ghost'}
className='h-7 text-xs px-2.5 rounded-md'
onClick={() => setTrendMetric('size')}
>
新增大小 (MB)
</Button>
</div>
</CardHeader>
<CardContent className="pl-2 pr-4 pt-2">
<div className="h-[280px] w-full">
<ChartContainer config={statsChartConfig} className="w-full h-full">
<AreaChart data={trendData} margin={{ top: 10, right: 10, left: 10, bottom: 0 }}>
<CardContent className='pl-2 pr-4 pt-2'>
<div className='h-[280px] w-full'>
<ChartContainer config={statsChartConfig} className='w-full h-full'>
<AreaChart
data={trendData}
margin={{ top: 10, right: 10, left: 10, bottom: 0 }}
>
<defs>
<linearGradient id="colorCount" x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor="var(--color-count)" stopOpacity={0.3} />
<stop offset="95%" stopColor="var(--color-count)" stopOpacity={0.01} />
<linearGradient id='colorCount' x1='0' y1='0' x2='0' y2='1'>
<stop
offset='5%'
stopColor='var(--color-count)'
stopOpacity={0.3}
/>
<stop
offset='95%'
stopColor='var(--color-count)'
stopOpacity={0.01}
/>
</linearGradient>
<linearGradient id="colorSize" x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor="var(--color-size)" stopOpacity={0.3} />
<stop offset="95%" stopColor="var(--color-size)" stopOpacity={0.01} />
<linearGradient id='colorSize' x1='0' y1='0' x2='0' y2='1'>
<stop
offset='5%'
stopColor='var(--color-size)'
stopOpacity={0.3}
/>
<stop
offset='95%'
stopColor='var(--color-size)'
stopOpacity={0.01}
/>
</linearGradient>
</defs>
<CartesianGrid strokeDasharray="3 3" vertical={false} stroke="hsl(var(--border)/40)" />
<CartesianGrid
strokeDasharray='3 3'
vertical={false}
stroke='hsl(var(--border)/40)'
/>
<XAxis
dataKey="formattedDate"
dataKey='formattedDate'
tickLine={false}
axisLine={false}
tickMargin={8}
style={{ fontSize: 11, fill: "hsl(var(--muted-foreground))" }}
style={{ fontSize: 11, fill: 'hsl(var(--muted-foreground))' }}
/>
<YAxis
tickLine={false}
axisLine={false}
tickMargin={8}
style={{ fontSize: 11, fill: "hsl(var(--muted-foreground))" }}
tickFormatter={(v) => (trendMetric === "size" ? formatFileSize(v) : v.toString())}
style={{ fontSize: 11, fill: 'hsl(var(--muted-foreground))' }}
tickFormatter={(v) =>
trendMetric === 'size' ? formatFileSize(v) : v.toString()
}
/>
<ChartTooltip
cursor={false}
@@ -280,37 +360,52 @@ export function FileStats() {
<ChartTooltipContent
hideLabel
formatter={(value) => {
const label = trendMetric === "count" ? "新增文件数" : "新增大小"
const label =
trendMetric === 'count' ? '新增文件数' : '新增大小';
const formattedValue =
trendMetric === "size" ? formatFileSize(Number(value)) : `${value} 个`
trendMetric === 'size'
? formatFileSize(Number(value))
: `${value} 个`;
return (
<>
<div className="flex items-center gap-1.5">
<div className='flex items-center gap-1.5'>
<span
className="size-2 rounded-full animate-pulse"
className='size-2 rounded-full animate-pulse'
style={{
backgroundColor:
trendMetric === "count" ? "var(--color-count)" : "var(--color-size)",
trendMetric === 'count'
? 'var(--color-count)'
: 'var(--color-size)',
}}
/>
<span className="text-muted-foreground">{label}</span>
<span className='text-muted-foreground'>
{label}
</span>
</div>
<span className="text-foreground font-mono font-medium tabular-nums ml-auto">
<span className='text-foreground font-mono font-medium tabular-nums ml-auto'>
{formattedValue}
</span>
</>
)
);
}}
/>
}
/>
<Area
type="monotone"
dataKey="value"
stroke={trendMetric === "count" ? "var(--color-count)" : "var(--color-size)"}
type='monotone'
dataKey='value'
stroke={
trendMetric === 'count'
? 'var(--color-count)'
: 'var(--color-size)'
}
strokeWidth={2.5}
fillOpacity={1}
fill={trendMetric === "count" ? "url(#colorCount)" : "url(#colorSize)"}
fill={
trendMetric === 'count'
? 'url(#colorCount)'
: 'url(#colorSize)'
}
/>
</AreaChart>
</ChartContainer>
@@ -319,30 +414,37 @@ export function FileStats() {
</Card>
{/* 两个饼图分布 */}
<div className="grid gap-6 md:grid-cols-2">
<div className='grid gap-6 md:grid-cols-2'>
{/* 饼图 1: 格式数量分布 */}
<Card className="bg-card/20 border-border/40 flex flex-col">
<Card className='bg-card/20 border-border/40 flex flex-col'>
<CardHeader>
<CardTitle className="text-sm font-bold">文件格式数量分布</CardTitle>
<CardDescription className="text-xs">各种格式类型的文件个数占比</CardDescription>
<CardTitle className='text-sm font-bold'>
文件格式数量分布
</CardTitle>
<CardDescription className='text-xs'>
各种格式类型的文件个数占比
</CardDescription>
</CardHeader>
<CardContent className="flex-1 flex flex-col justify-between gap-4">
<CardContent className='flex-1 flex flex-col justify-between gap-4'>
{categoryCountData.length === 0 ? (
<div className="h-[240px] flex items-center justify-center text-xs text-muted-foreground">
<div className='h-[240px] flex items-center justify-center text-xs text-muted-foreground'>
暂无分类数据
</div>
) : (
<div className="h-[240px] w-full">
<ChartContainer config={statsChartConfig} className="w-full h-full">
<div className='h-[240px] w-full'>
<ChartContainer
config={statsChartConfig}
className='w-full h-full'
>
<PieChart>
<Pie
data={categoryCountData}
cx="50%"
cy="50%"
cx='50%'
cy='50%'
innerRadius={60}
outerRadius={80}
paddingAngle={3}
dataKey="value"
dataKey='value'
>
{categoryCountData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.fill} />
@@ -354,66 +456,88 @@ export function FileStats() {
<ChartTooltipContent
hideLabel
formatter={(value, name) => {
const configObj = statsChartConfig[name as keyof typeof statsChartConfig]
const label = configObj?.label || name
const color = configObj?.color || "hsl(var(--muted))"
const configObj =
statsChartConfig[
name as keyof typeof statsChartConfig
];
const label = configObj?.label || name;
const color =
configObj?.color || 'hsl(var(--muted))';
return (
<>
<div className="flex items-center gap-1.5">
<span className="size-2 rounded-full" style={{ backgroundColor: color }} />
<span className="text-muted-foreground">{label}</span>
<div className='flex items-center gap-1.5'>
<span
className='size-2 rounded-full'
style={{ backgroundColor: color }}
/>
<span className='text-muted-foreground'>
{label}
</span>
</div>
<span className="text-foreground font-mono font-medium tabular-nums ml-auto">
<span className='text-foreground font-mono font-medium tabular-nums ml-auto'>
{value} 个
</span>
</>
)
);
}}
/>
}
/>
<ChartLegend
content={<ChartLegendContent nameKey="name" />}
className="flex-wrap justify-center gap-x-4 gap-y-1 text-[11px] pt-4"
content={<ChartLegendContent nameKey='name' />}
className='flex-wrap justify-center gap-x-4 gap-y-1 text-[11px] pt-4'
/>
</PieChart>
</ChartContainer>
</div>
)}
<div className="bg-sky-500/5 border border-sky-500/10 rounded-lg p-3 flex items-start gap-2.5 text-xs text-sky-600 dark:text-sky-400">
<Info className="size-4 shrink-0 mt-0.5" />
<p className="leading-normal">
文件类型中数量最多的是{" "}
<strong className="font-bold">「{maxStats.maxCountName}」</strong>,累计上传了{" "}
<strong className="font-mono font-bold">{maxStats.maxCount}</strong> 个文件。
<div className='bg-sky-500/5 border border-sky-500/10 rounded-lg p-3 flex items-start gap-2.5 text-xs text-sky-600 dark:text-sky-400'>
<Info className='size-4 shrink-0 mt-0.5' />
<p className='leading-normal'>
文件类型中数量最多的是{' '}
<strong className='font-bold'>
「{maxStats.maxCountName}」
</strong>
,累计上传了{' '}
<strong className='font-mono font-bold'>
{maxStats.maxCount}
</strong>{' '}
个文件。
</p>
</div>
</CardContent>
</Card>
{/* 饼图 2: 格式容量分布 */}
<Card className="bg-card/20 border-border/40 flex flex-col">
<Card className='bg-card/20 border-border/40 flex flex-col'>
<CardHeader>
<CardTitle className="text-sm font-bold">文件占用容量分布</CardTitle>
<CardDescription className="text-xs">各种格式类型的文件大小占比</CardDescription>
<CardTitle className='text-sm font-bold'>
文件占用容量分布
</CardTitle>
<CardDescription className='text-xs'>
各种格式类型的文件大小占比
</CardDescription>
</CardHeader>
<CardContent className="flex-1 flex flex-col justify-between gap-4">
<CardContent className='flex-1 flex flex-col justify-between gap-4'>
{categorySizeData.length === 0 ? (
<div className="h-[240px] flex items-center justify-center text-xs text-muted-foreground">
<div className='h-[240px] flex items-center justify-center text-xs text-muted-foreground'>
暂无容量数据
</div>
) : (
<div className="h-[240px] w-full">
<ChartContainer config={statsChartConfig} className="w-full h-full">
<div className='h-[240px] w-full'>
<ChartContainer
config={statsChartConfig}
className='w-full h-full'
>
<PieChart>
<Pie
data={categorySizeData}
cx="50%"
cy="50%"
cx='50%'
cy='50%'
innerRadius={60}
outerRadius={80}
paddingAngle={3}
dataKey="value"
dataKey='value'
>
{categorySizeData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.fill} />
@@ -425,38 +549,52 @@ export function FileStats() {
<ChartTooltipContent
hideLabel
formatter={(value, name) => {
const configObj = statsChartConfig[name as keyof typeof statsChartConfig]
const label = configObj?.label || name
const color = configObj?.color || "hsl(var(--muted))"
const configObj =
statsChartConfig[
name as keyof typeof statsChartConfig
];
const label = configObj?.label || name;
const color =
configObj?.color || 'hsl(var(--muted))';
return (
<>
<div className="flex items-center gap-1.5">
<span className="size-2 rounded-full" style={{ backgroundColor: color }} />
<span className="text-muted-foreground">{label}</span>
<div className='flex items-center gap-1.5'>
<span
className='size-2 rounded-full'
style={{ backgroundColor: color }}
/>
<span className='text-muted-foreground'>
{label}
</span>
</div>
<span className="text-foreground font-mono font-medium tabular-nums ml-auto">
<span className='text-foreground font-mono font-medium tabular-nums ml-auto'>
{formatFileSize(Number(value))}
</span>
</>
)
);
}}
/>
}
/>
<ChartLegend
content={<ChartLegendContent nameKey="name" />}
className="flex-wrap justify-center gap-x-4 gap-y-1 text-[11px] pt-4"
content={<ChartLegendContent nameKey='name' />}
className='flex-wrap justify-center gap-x-4 gap-y-1 text-[11px] pt-4'
/>
</PieChart>
</ChartContainer>
</div>
)}
<div className="bg-emerald-500/5 border border-emerald-500/10 rounded-lg p-3 flex items-start gap-2.5 text-xs text-emerald-600 dark:text-emerald-400">
<Info className="size-4 shrink-0 mt-0.5" />
<p className="leading-normal">
文件类型中占用存储最大的是{" "}
<strong className="font-bold">「{maxStats.maxSizeName}」</strong>,共消耗了{" "}
<strong className="font-mono font-bold">{formatFileSize(maxStats.maxSize)}</strong>{" "}
<div className='bg-emerald-500/5 border border-emerald-500/10 rounded-lg p-3 flex items-start gap-2.5 text-xs text-emerald-600 dark:text-emerald-400'>
<Info className='size-4 shrink-0 mt-0.5' />
<p className='leading-normal'>
文件类型中占用存储最大的是{' '}
<strong className='font-bold'>
「{maxStats.maxSizeName}」
</strong>
,共消耗了{' '}
<strong className='font-mono font-bold'>
{formatFileSize(maxStats.maxSize)}
</strong>{' '}
存储容量。
</p>
</div>
@@ -464,5 +602,5 @@ export function FileStats() {
</Card>
</div>
</div>
)
);
}
@@ -1,104 +1,130 @@
"use client"
'use client';
import * as React from "react"
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"
import {Database, Loader2, Play, Save} from "lucide-react"
import {toast} from "sonner"
import * as React from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Database, Loader2, Play, Save } from 'lucide-react';
import { toast } from 'sonner';
import {Badge} from "@/components/ui/badge"
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle} from "@/components/ui/card"
import {Field, FieldDescription, FieldGroup, FieldLabel} from "@/components/ui/field"
import {Input} from "@/components/ui/input"
import {Progress} from "@/components/ui/progress"
import {Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select"
import {Switch} from "@/components/ui/switch"
import services from "@/lib/services"
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import {
Field,
FieldDescription,
FieldGroup,
FieldLabel,
} from '@/components/ui/field';
import { Input } from '@/components/ui/input';
import { Progress } from '@/components/ui/progress';
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Switch } from '@/components/ui/switch';
import services from '@/lib/services';
import type {
ObjectStorageConfig,
StorageConfig,
StorageDriver,
TaskExecution,
TaskExecutionStatus
} from "@/lib/services/admin/types"
TaskExecutionStatus,
} from '@/lib/services/admin/types';
const storageConfigKey = "storage_config"
const storageMigrationTaskType = "storage_migration"
const storageConfigKey = 'storage_config';
const storageMigrationTaskType = 'storage_migration';
const driverLabels: Record<StorageDriver, string> = {
local: "本地文件系统",
s3: "AWS S3",
r2: "Cloudflare R2",
minio: "MinIO",
oss: "阿里云 OSS",
webdav: "WebDAV",
}
local: '本地文件系统',
s3: 'AWS S3',
r2: 'Cloudflare R2',
minio: 'MinIO',
oss: '阿里云 OSS',
webdav: 'WebDAV',
};
const emptyObjectConfig: ObjectStorageConfig = {
endpoint: "",
region: "",
bucket: "",
access_key_id: "",
secret_access_key: "",
endpoint: '',
region: '',
bucket: '',
access_key_id: '',
secret_access_key: '',
path_style: false,
key_prefix: "",
cdn_url: "",
}
key_prefix: '',
cdn_url: '',
};
function normalizeConfig(config: StorageConfig): StorageConfig {
const s3 = {...emptyObjectConfig, ...config.s3}
const r2 = {...emptyObjectConfig, ...config.r2}
const minio = {...emptyObjectConfig, ...config.minio}
const oss = {...emptyObjectConfig, ...config.oss}
const webdav = {...config.webdav}
const s3 = { ...emptyObjectConfig, ...config.s3 };
const r2 = { ...emptyObjectConfig, ...config.r2 };
const minio = { ...emptyObjectConfig, ...config.minio };
const oss = { ...emptyObjectConfig, ...config.oss };
const webdav = { ...config.webdav };
return {
...config,
local: {root: config.local?.root || "."},
s3: {...s3, region: s3.region || "us-east-1"},
r2: {...r2, region: r2.region || "auto"},
minio: {...minio, region: minio.region || "us-east-1", path_style: config.minio?.path_style ?? true},
local: { root: config.local?.root || '.' },
s3: { ...s3, region: s3.region || 'us-east-1' },
r2: { ...r2, region: r2.region || 'auto' },
minio: {
...minio,
region: minio.region || 'us-east-1',
path_style: config.minio?.path_style ?? true,
},
oss,
webdav: {
endpoint: webdav.endpoint || "",
username: webdav.username || "",
password: webdav.password || "",
base_path: webdav.base_path || "",
endpoint: webdav.endpoint || '',
username: webdav.username || '',
password: webdav.password || '',
base_path: webdav.base_path || '',
},
}
};
}
type StorageMigrationView = {
state: "idle" | TaskExecutionStatus
id?: string
task_id?: string
source_driver?: StorageDriver
target_driver?: StorageDriver
error?: string
}
state: 'idle' | TaskExecutionStatus;
id?: string;
task_id?: string;
source_driver?: StorageDriver;
target_driver?: StorageDriver;
error?: string;
};
type StorageMigrationPayload = {
target: StorageConfig
}
target: StorageConfig;
};
function parseMigrationPayload(payload: string): StorageConfig | undefined {
try {
const parsed = JSON.parse(payload) as Partial<StorageMigrationPayload>
return parsed.target ? normalizeConfig(parsed.target) : undefined
const parsed = JSON.parse(payload) as Partial<StorageMigrationPayload>;
return parsed.target ? normalizeConfig(parsed.target) : undefined;
} catch {
return undefined
return undefined;
}
}
function driverLabel(driver?: StorageDriver): string {
return driver ? driverLabels[driver] : "未知"
return driver ? driverLabels[driver] : '未知';
}
function latestMigration(executions: TaskExecution[], current?: StorageConfig): StorageMigrationView {
const execution = executions[0]
if (!execution || execution.status === "succeeded") {
return {state: "idle"}
function latestMigration(
executions: TaskExecution[],
current?: StorageConfig,
): StorageMigrationView {
const execution = executions[0];
if (!execution || execution.status === 'succeeded') {
return { state: 'idle' };
}
const target = parseMigrationPayload(execution.payload)
const target = parseMigrationPayload(execution.payload);
return {
state: execution.status,
id: execution.id,
@@ -106,227 +132,339 @@ function latestMigration(executions: TaskExecution[], current?: StorageConfig):
source_driver: current?.driver,
target_driver: target?.driver,
error: execution.error_message,
}
};
}
export function StorageConfigTab() {
const queryClient = useQueryClient()
const [config, setConfig] = React.useState<StorageConfig | null>(null)
const queryClient = useQueryClient();
const [config, setConfig] = React.useState<StorageConfig | null>(null);
const query = useQuery({
queryKey: ["admin", "storage-config"],
queryKey: ['admin', 'storage-config'],
queryFn: async () => {
const [configRecord, executions] = await Promise.all([
services.adminSystemConfig.getSystemConfig(storageConfigKey),
services.adminTask.listTaskExecutions({task_type: storageMigrationTaskType, page: 1, page_size: 1}),
])
const current = normalizeConfig(JSON.parse(configRecord.value) as StorageConfig)
const migration = latestMigration(executions.items, current)
services.adminTask.listTaskExecutions({
task_type: storageMigrationTaskType,
page: 1,
page_size: 1,
}),
]);
const current = normalizeConfig(
JSON.parse(configRecord.value) as StorageConfig,
);
const migration = latestMigration(executions.items, current);
return {
config: current,
migration,
}
};
},
refetchInterval: (result) => {
const state = result.state.data?.migration.state
return state === "pending" || state === "running" ? 2000 : false
const state = result.state.data?.migration.state;
return state === 'pending' || state === 'running' ? 2000 : false;
},
})
});
React.useEffect(() => {
if (query.data?.config) {
setConfig((current) => {
const normalized = normalizeConfig(query.data.config)
const normalized = normalizeConfig(query.data.config);
if (current) {
return {
...normalized,
driver: current.driver,
};
}
return normalized;
});
}
return normalized
})
}
}, [query.data?.config])
}, [query.data?.config]);
const saveMutation = useMutation({
mutationFn: async (value: StorageConfig) => {
await services.adminSystemConfig.updateSystemConfig(storageConfigKey, {
value: JSON.stringify(value),
})
});
},
onSuccess: () => {
toast.success("存储配置已保存")
void queryClient.invalidateQueries({queryKey: ["admin", "storage-config"]})
toast.success('存储配置已保存');
void queryClient.invalidateQueries({
queryKey: ['admin', 'storage-config'],
});
},
onError: (error: Error) => {
toast.error(error.message || "保存存储配置失败")
void queryClient.invalidateQueries({queryKey: ["admin", "storage-config"]})
toast.error(error.message || '保存存储配置失败');
void queryClient.invalidateQueries({
queryKey: ['admin', 'storage-config'],
});
},
})
});
const migrateMutation = useMutation({
mutationFn: async (value: StorageConfig) => {
const payload: StorageMigrationPayload = {target: value}
const payload: StorageMigrationPayload = { target: value };
return services.adminTask.dispatchTask({
task_type: storageMigrationTaskType,
payload: JSON.stringify(payload),
})
});
},
onSuccess: () => {
toast.success("存储迁移任务已下发")
void queryClient.invalidateQueries({queryKey: ["admin", "storage-config"]})
toast.success('存储迁移任务已下发');
void queryClient.invalidateQueries({
queryKey: ['admin', 'storage-config'],
});
},
onError: (error: Error) => {
toast.error(error.message || "下发存储迁移任务失败")
void queryClient.invalidateQueries({queryKey: ["admin", "storage-config"]})
toast.error(error.message || '下发存储迁移任务失败');
void queryClient.invalidateQueries({
queryKey: ['admin', 'storage-config'],
});
},
})
});
const runMutation = useMutation({
mutationFn: (executionID: string) => services.adminTask.retryTaskExecution(executionID),
mutationFn: (executionID: string) =>
services.adminTask.retryTaskExecution(executionID),
onSuccess: () => {
toast.success("存储迁移任务已重新下发")
void queryClient.invalidateQueries({queryKey: ["admin", "storage-config"]})
toast.success('存储迁移任务已重新下发');
void queryClient.invalidateQueries({
queryKey: ['admin', 'storage-config'],
});
},
onError: (error: Error) => toast.error(error.message || "运行存储迁移失败"),
})
onError: (error: Error) => toast.error(error.message || '运行存储迁移失败'),
});
if (query.isPending || !config) {
return <div className="flex justify-center py-20"><Loader2 className="animate-spin" /></div>
return (
<div className='flex justify-center py-20'>
<Loader2 className='animate-spin' />
</div>
);
}
const migration = query.data?.migration
const isReadOnly = migration ? migration.state !== "idle" : false
const migration = query.data?.migration;
const isReadOnly = migration ? migration.state !== 'idle' : false;
const isFormDisabled =
(migration ? (migration.state === "pending" || migration.state === "running") : false) ||
(migration
? migration.state === 'pending' || migration.state === 'running'
: false) ||
saveMutation.isPending ||
migrateMutation.isPending
migrateMutation.isPending;
const updateObject = (driver: "s3" | "r2" | "minio" | "oss", patch: Partial<ObjectStorageConfig>) => {
setConfig((current) => current ? {
const updateObject = (
driver: 's3' | 'r2' | 'minio' | 'oss',
patch: Partial<ObjectStorageConfig>,
) => {
setConfig((current) =>
current
? {
...current,
[driver]: { ...current[driver], ...patch },
} : current)
}
: current,
);
};
return (
<div className="flex w-full flex-col gap-6">
<div className='flex w-full flex-col gap-6'>
{isReadOnly && migration && (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<CardTitle className='flex items-center gap-2'>
存储维护模式
<Badge variant={migration.state === "failed" ? "destructive" : "secondary"}>
<Badge
variant={
migration.state === 'failed' ? 'destructive' : 'secondary'
}
>
{migration.state}
</Badge>
</CardTitle>
<CardDescription>
{driverLabel(migration.source_driver)} → {driverLabel(migration.target_driver)}。迁移期间文件只允许读取,禁止上传、删除和清理。
{driverLabel(migration.source_driver)} →{' '}
{driverLabel(migration.target_driver)}
。迁移期间文件只允许读取,禁止上传、删除和清理。
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-3">
<Progress value={migration.state === "succeeded" ? 100 : undefined} />
<p className="text-sm text-muted-foreground">
迁移进度请查看任务执行日志:{migration.task_id || "尚未下发"}
<CardContent className='flex flex-col gap-3'>
<Progress
value={migration.state === 'succeeded' ? 100 : undefined}
/>
<p className='text-sm text-muted-foreground'>
迁移进度请查看任务执行日志:{migration.task_id || '尚未下发'}
</p>
{migration.error && <p className="text-sm text-destructive">{migration.error}</p>}
{migration.error && (
<p className='text-sm text-destructive'>{migration.error}</p>
)}
</CardContent>
<CardFooter>
<Button
variant="outline"
disabled={migration.state !== "failed" || !migration.id || runMutation.isPending}
variant='outline'
disabled={
migration.state !== 'failed' ||
!migration.id ||
runMutation.isPending
}
onClick={() => migration.id && runMutation.mutate(migration.id)}
>
{runMutation.isPending ? <Loader2 data-icon="inline-start" className="animate-spin" /> : <Play data-icon="inline-start" />}
{runMutation.isPending ? (
<Loader2 data-icon='inline-start' className='animate-spin' />
) : (
<Play data-icon='inline-start' />
)}
重试迁移
</Button>
</CardFooter>
</Card>
)}
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<div className="flex items-center gap-2">
<div className="rounded-lg bg-indigo-500/10 p-1.5 text-indigo-500">
<Database className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<div className='flex items-center gap-2'>
<div className='rounded-lg bg-indigo-500/10 p-1.5 text-indigo-500'>
<Database className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">文件存储</CardTitle>
<CardDescription className="text-xs">
<CardTitle className='text-base font-semibold'>
文件存储
</CardTitle>
<CardDescription className='text-xs'>
默认使用本地存储。配置系统文件的存储媒介,切换存储类型且已有文件时,系统会自动进入维护模式并迁移文件。
</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="pt-6">
<FieldGroup className="space-y-6">
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
<Field className="flex flex-col gap-1.5 md:col-span-2">
<FieldLabel className="text-xs font-semibold">存储类型</FieldLabel>
<CardContent className='pt-6'>
<FieldGroup className='space-y-6'>
<div className='grid grid-cols-1 gap-4 md:grid-cols-2'>
<Field className='flex flex-col gap-1.5 md:col-span-2'>
<FieldLabel className='text-xs font-semibold'>
存储类型
</FieldLabel>
<Select
value={config.driver}
disabled={isFormDisabled}
onValueChange={(value) => setConfig({...config, driver: value as StorageDriver})}
onValueChange={(value) =>
setConfig({ ...config, driver: value as StorageDriver })
}
>
<SelectTrigger className="w-full border-dashed bg-card text-xs">
<SelectTrigger className='w-full border-dashed bg-card text-xs'>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectGroup>
{(Object.keys(driverLabels) as StorageDriver[]).map((driver) => (
<SelectItem key={driver} value={driver}>{driverLabels[driver]}</SelectItem>
))}
{(Object.keys(driverLabels) as StorageDriver[]).map(
(driver) => (
<SelectItem key={driver} value={driver}>
{driverLabels[driver]}
</SelectItem>
),
)}
</SelectGroup>
</SelectContent>
</Select>
</Field>
{config.driver === "local" && (
<div className="md:col-span-2">
{config.driver === 'local' && (
<div className='md:col-span-2'>
<TextField
label="根目录"
label='根目录'
value={config.local.root}
placeholder="."
onChange={(root) => setConfig({...config, local: {root}})}
placeholder='.'
onChange={(root) =>
setConfig({ ...config, local: { root } })
}
/>
</div>
)}
{(config.driver === "s3" || config.driver === "r2" || config.driver === "minio" || config.driver === "oss") && (
{(config.driver === 's3' ||
config.driver === 'r2' ||
config.driver === 'minio' ||
config.driver === 'oss') && (
<ObjectFields
driver={config.driver as "s3" | "r2" | "minio" | "oss"}
value={config[config.driver as "s3" | "r2" | "minio" | "oss"]}
onChange={(patch) => updateObject(config.driver as "s3" | "r2" | "minio" | "oss", patch)}
driver={config.driver as 's3' | 'r2' | 'minio' | 'oss'}
value={config[config.driver as 's3' | 'r2' | 'minio' | 'oss']}
onChange={(patch) =>
updateObject(
config.driver as 's3' | 'r2' | 'minio' | 'oss',
patch,
)
}
/>
)}
{config.driver === "webdav" && (
{config.driver === 'webdav' && (
<>
<div className="md:col-span-2">
<TextField label="服务地址" value={config.webdav.endpoint} placeholder="https://dav.example.com" onChange={(endpoint) => setConfig({...config, webdav: {...config.webdav, endpoint}})} />
<div className='md:col-span-2'>
<TextField
label='服务地址'
value={config.webdav.endpoint}
placeholder='https://dav.example.com'
onChange={(endpoint) =>
setConfig({
...config,
webdav: { ...config.webdav, endpoint },
})
}
/>
</div>
<TextField label="用户名" value={config.webdav.username} onChange={(username) => setConfig({...config, webdav: {...config.webdav, username}})} />
<TextField label="密码" type="password" value={config.webdav.password} onChange={(password) => setConfig({...config, webdav: {...config.webdav, password}})} />
<div className="md:col-span-2">
<TextField label="基础路径" value={config.webdav.base_path} placeholder="wavelet" onChange={(base_path) => setConfig({...config, webdav: {...config.webdav, base_path}})} />
<TextField
label='用户名'
value={config.webdav.username}
onChange={(username) =>
setConfig({
...config,
webdav: { ...config.webdav, username },
})
}
/>
<TextField
label='密码'
type='password'
value={config.webdav.password}
onChange={(password) =>
setConfig({
...config,
webdav: { ...config.webdav, password },
})
}
/>
<div className='md:col-span-2'>
<TextField
label='基础路径'
value={config.webdav.base_path}
placeholder='wavelet'
onChange={(base_path) =>
setConfig({
...config,
webdav: { ...config.webdav, base_path },
})
}
/>
</div>
</>
)}
</div>
</FieldGroup>
</CardContent>
<CardFooter className="justify-end gap-2 flex-col sm:flex-row items-end sm:items-center border-t border-dashed pt-4 mt-6">
<CardFooter className='justify-end gap-2 flex-col sm:flex-row items-end sm:items-center border-t border-dashed pt-4 mt-6'>
{config.driver !== query.data?.config.driver && (
<span className="text-xs text-amber-500 mr-auto text-left max-w-md">
⚠️ 您已切换存储类型。点击「保存配置」将立即切换活动存储引擎,并同步更新已有文件的存储驱动标记。仅在需要复制物理文件时才使用「开始迁移」。
<span className='text-xs text-amber-500 mr-auto text-left max-w-md'>
⚠️
您已切换存储类型。点击「保存配置」将立即切换活动存储引擎,并同步更新已有文件的存储驱动标记。仅在需要复制物理文件时才使用「开始迁移」。
</span>
)}
<div className="flex gap-2">
<div className='flex gap-2'>
<Button
variant="outline"
variant='outline'
disabled={isFormDisabled}
onClick={() => saveMutation.mutate(config)}
className="border-dashed"
className='border-dashed'
>
{saveMutation.isPending ? <Loader2 data-icon="inline-start" className="animate-spin" /> : <Save data-icon="inline-start" />}
{saveMutation.isPending ? (
<Loader2 data-icon='inline-start' className='animate-spin' />
) : (
<Save data-icon='inline-start' />
)}
保存配置
</Button>
{config.driver !== query.data?.config.driver && (
@@ -334,7 +472,11 @@ export function StorageConfigTab() {
disabled={isFormDisabled}
onClick={() => migrateMutation.mutate(config)}
>
{migrateMutation.isPending ? <Loader2 data-icon="inline-start" className="animate-spin" /> : <Play data-icon="inline-start" />}
{migrateMutation.isPending ? (
<Loader2 data-icon='inline-start' className='animate-spin' />
) : (
<Play data-icon='inline-start' />
)}
开始迁移
</Button>
)}
@@ -342,7 +484,7 @@ export function StorageConfigTab() {
</CardFooter>
</Card>
</div>
)
);
}
function ObjectFields({
@@ -350,41 +492,87 @@ function ObjectFields({
value,
onChange,
}: {
driver: "s3" | "r2" | "minio" | "oss"
value: ObjectStorageConfig
onChange: (patch: Partial<ObjectStorageConfig>) => void
driver: 's3' | 'r2' | 'minio' | 'oss';
value: ObjectStorageConfig;
onChange: (patch: Partial<ObjectStorageConfig>) => void;
}) {
return (
<>
{driver === "r2" && (
<div className="md:col-span-2">
<TextField label="Account ID" value={value.account_id || ""} onChange={(account_id) => onChange({account_id})} />
{driver === 'r2' && (
<div className='md:col-span-2'>
<TextField
label='Account ID'
value={value.account_id || ''}
onChange={(account_id) => onChange({ account_id })}
/>
</div>
)}
{driver !== "s3" && (
<div className="md:col-span-2">
<TextField label="Endpoint" value={value.endpoint} placeholder="https://..." onChange={(endpoint) => onChange({endpoint})} />
{driver !== 's3' && (
<div className='md:col-span-2'>
<TextField
label='Endpoint'
value={value.endpoint}
placeholder='https://...'
onChange={(endpoint) => onChange({ endpoint })}
/>
</div>
)}
<TextField label="Region" value={value.region} onChange={(region) => onChange({region})} />
<TextField label="Bucket" value={value.bucket} onChange={(bucket) => onChange({bucket})} />
<TextField label="Access Key ID" value={value.access_key_id} onChange={(access_key_id) => onChange({access_key_id})} />
<TextField label="Secret Access Key" type="password" value={value.secret_access_key} onChange={(secret_access_key) => onChange({secret_access_key})} />
<TextField label="对象前缀" value={value.key_prefix} placeholder="uploads" onChange={(key_prefix) => onChange({key_prefix})} />
<TextField label="CDN 地址" value={value.cdn_url} placeholder="https://cdn.example.com" onChange={(cdn_url) => onChange({cdn_url})} />
{(driver === "s3" || driver === "minio") && (
<div className="md:col-span-2">
<Field orientation="horizontal" className="flex items-center justify-between border border-dashed rounded-lg p-4 bg-muted/30">
<div className="flex flex-col gap-0.5">
<FieldLabel className="text-xs font-semibold cursor-pointer">Path Style</FieldLabel>
<FieldDescription className="text-[11px] leading-relaxed text-muted-foreground">MinIO 等自托管 S3 通常需要开启。</FieldDescription>
<TextField
label='Region'
value={value.region}
onChange={(region) => onChange({ region })}
/>
<TextField
label='Bucket'
value={value.bucket}
onChange={(bucket) => onChange({ bucket })}
/>
<TextField
label='Access Key ID'
value={value.access_key_id}
onChange={(access_key_id) => onChange({ access_key_id })}
/>
<TextField
label='Secret Access Key'
type='password'
value={value.secret_access_key}
onChange={(secret_access_key) => onChange({ secret_access_key })}
/>
<TextField
label='对象前缀'
value={value.key_prefix}
placeholder='uploads'
onChange={(key_prefix) => onChange({ key_prefix })}
/>
<TextField
label='CDN 地址'
value={value.cdn_url}
placeholder='https://cdn.example.com'
onChange={(cdn_url) => onChange({ cdn_url })}
/>
{(driver === 's3' || driver === 'minio') && (
<div className='md:col-span-2'>
<Field
orientation='horizontal'
className='flex items-center justify-between border border-dashed rounded-lg p-4 bg-muted/30'
>
<div className='flex flex-col gap-0.5'>
<FieldLabel className='text-xs font-semibold cursor-pointer'>
Path Style
</FieldLabel>
<FieldDescription className='text-[11px] leading-relaxed text-muted-foreground'>
MinIO 等自托管 S3 通常需要开启。
</FieldDescription>
</div>
<Switch checked={value.path_style} onCheckedChange={(path_style) => onChange({path_style})} />
<Switch
checked={value.path_style}
onCheckedChange={(path_style) => onChange({ path_style })}
/>
</Field>
</div>
)}
</>
)
);
}
function TextField({
@@ -392,24 +580,24 @@ function TextField({
value,
onChange,
placeholder,
type = "text",
type = 'text',
}: {
label: string
value: string
onChange: (value: string) => void
placeholder?: string
type?: React.HTMLInputTypeAttribute
label: string;
value: string;
onChange: (value: string) => void;
placeholder?: string;
type?: React.HTMLInputTypeAttribute;
}) {
return (
<Field className="flex flex-col gap-1.5">
<FieldLabel className="text-xs font-semibold">{label}</FieldLabel>
<Field className='flex flex-col gap-1.5'>
<FieldLabel className='text-xs font-semibold'>{label}</FieldLabel>
<Input
type={type}
value={value}
placeholder={placeholder}
onChange={(event) => onChange(event.target.value)}
className="border-dashed bg-card text-xs"
className='border-dashed bg-card text-xs'
/>
</Field>
)
);
}
+32 -26
View File
@@ -1,59 +1,65 @@
"use client"
'use client';
import * as React from "react"
import {motion} from "motion/react"
import {FolderOpen} from "lucide-react"
import * as React from 'react';
import { motion } from 'motion/react';
import { FolderOpen } from 'lucide-react';
import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs"
import {FileStats} from "./components/file-stats"
import {FileList} from "./components/file-list"
import {StorageConfigTab} from "./components/storage-config-tab"
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { FileStats } from './components/file-stats';
import { FileList } from './components/file-list';
import { StorageConfigTab } from './components/storage-config-tab';
export default function FilesPage() {
const [activeTab, setActiveTab] = React.useState("stats")
const [activeTab, setActiveTab] = React.useState('stats');
return (
<motion.div
initial={{ opacity: 0, y: 15 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.35, ease: "easeOut" }}
className="flex w-full flex-col gap-6 py-6"
transition={{ duration: 0.35, ease: 'easeOut' }}
className='flex w-full flex-col gap-6 py-6'
>
{/* 顶部标题区 */}
<div className="flex items-center gap-2">
<FolderOpen className="size-5 text-primary" />
<div className='flex items-center gap-2'>
<FolderOpen className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">存储管理</h1>
<h1 className='text-2xl font-semibold tracking-tight'>存储管理</h1>
</div>
</div>
<Tabs value={activeTab} onValueChange={setActiveTab} className="w-full">
<TabsList variant="line" className="w-fit inline-flex gap-8 mb-6">
<TabsTrigger value="stats" className="px-0 pb-2 text-xs font-semibold">
<Tabs value={activeTab} onValueChange={setActiveTab} className='w-full'>
<TabsList variant='line' className='w-fit inline-flex gap-8 mb-6'>
<TabsTrigger
value='stats'
className='px-0 pb-2 text-xs font-semibold'
>
文件存储信息
</TabsTrigger>
<TabsTrigger value="list" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger value='list' className='px-0 pb-2 text-xs font-semibold'>
文件列表
</TabsTrigger>
<TabsTrigger value="storage" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='storage'
className='px-0 pb-2 text-xs font-semibold'
>
存储配置
</TabsTrigger>
</TabsList>
{/* ──────── TAB 1: 统计看板 ──────── */}
<TabsContent value="stats" className="outline-hidden">
{activeTab === "stats" ? <FileStats /> : null}
<TabsContent value='stats' className='outline-hidden'>
{activeTab === 'stats' ? <FileStats /> : null}
</TabsContent>
{/* ──────── TAB 2: 文件列表 ──────── */}
<TabsContent value="list" className="outline-hidden">
{activeTab === "list" ? <FileList /> : null}
<TabsContent value='list' className='outline-hidden'>
{activeTab === 'list' ? <FileList /> : null}
</TabsContent>
<TabsContent value="storage" className="outline-hidden">
{activeTab === "storage" ? <StorageConfigTab /> : null}
<TabsContent value='storage' className='outline-hidden'>
{activeTab === 'storage' ? <StorageConfigTab /> : null}
</TabsContent>
</Tabs>
</motion.div>
)
);
}
+6 -8
View File
@@ -1,18 +1,16 @@
"use client"
'use client';
import {AdminUsersProvider} from "@/contexts/admin-users-context"
import {RequireAdminAuth} from "@/components/auth/require-auth"
import { AdminUsersProvider } from '@/contexts/admin-users-context';
import { RequireAdminAuth } from '@/components/auth/require-auth';
export default function AdminLayout({
children,
}: {
children: React.ReactNode
children: React.ReactNode;
}) {
return (
<RequireAdminAuth>
<AdminUsersProvider>
{children}
</AdminUsersProvider>
<AdminUsersProvider>{children}</AdminUsersProvider>
</RequireAdminAuth>
)
);
}
@@ -14,209 +14,282 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
"use client"
'use client';
import {useCallback, useEffect, useState} from "react"
import {BarChart3, Globe, RefreshCw, TrendingUp, Users, XCircle} from "lucide-react"
import {Area, AreaChart, CartesianGrid, XAxis, YAxis} from "recharts"
import { useCallback, useEffect, useState } from 'react';
import {
BarChart3,
Globe,
RefreshCw,
TrendingUp,
Users,
XCircle,
} from 'lucide-react';
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from 'recharts';
import services from "@/lib/services"
import {ErrorInline} from "@/components/layout/error"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {EmptyStateWithBorder} from "@/components/layout/empty"
import {Badge} from "@/components/ui/badge"
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent} from "@/components/ui/chart"
import {Spinner} from "@/components/ui/spinner"
import services from '@/lib/services';
import { ErrorInline } from '@/components/layout/error';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { EmptyStateWithBorder } from '@/components/layout/empty';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from '@/components/ui/chart';
import { Spinner } from '@/components/ui/spinner';
interface TrendData {
date: string
count: number
date: string;
count: number;
}
interface BrowserData {
browser: string
count: number
browser: string;
count: number;
}
interface TopUserData {
user_id: string
username: string
nickname: string
count: number
user_id: string;
username: string;
nickname: string;
count: number;
}
const chartConfig = {
count: {
label: "请求量",
color: "hsl(var(--primary))",
label: '请求量',
color: 'hsl(var(--primary))',
},
} satisfies ChartConfig
} satisfies ChartConfig;
// Format YYYY-MM-DD to MM/DD
function formatDateLabel(dateStr: string) {
if (!dateStr || dateStr.length < 10) return dateStr
const parts = dateStr.split("-")
if (!dateStr || dateStr.length < 10) return dateStr;
const parts = dateStr.split('-');
if (parts.length === 3) {
return `${parts[1]}/${parts[2]}`
return `${parts[1]}/${parts[2]}`;
}
return dateStr
return dateStr;
}
export function AccessAnalytics() {
const [loading, setLoading] = useState(true)
const [error, setError] = useState<Error | null>(null)
const [clickhouseDisabled, setClickhouseDisabled] = useState(false)
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const [clickhouseDisabled, setClickhouseDisabled] = useState(false);
const [trend, setTrend] = useState<TrendData[]>([])
const [browsers, setBrowsers] = useState<BrowserData[]>([])
const [topUsers, setTopUsers] = useState<TopUserData[]>([])
const [trend, setTrend] = useState<TrendData[]>([]);
const [browsers, setBrowsers] = useState<BrowserData[]>([]);
const [topUsers, setTopUsers] = useState<TopUserData[]>([]);
const fetchAnalytics = useCallback(async () => {
try {
setLoading(true)
setError(null)
const data = await services.adminLog.getLogsAnalytics()
setLoading(true);
setError(null);
const data = await services.adminLog.getLogsAnalytics();
// Formats the date labels for the X-axis representation
const formattedTrend = (data.trend || []).map(item => ({
const formattedTrend = (data.trend || []).map((item) => ({
...item,
formattedDate: formatDateLabel(item.date)
}))
formattedDate: formatDateLabel(item.date),
}));
setTrend(formattedTrend)
setBrowsers(data.browsers || [])
setTopUsers(data.top_users || [])
setClickhouseDisabled(false)
setTrend(formattedTrend);
setBrowsers(data.browsers || []);
setTopUsers(data.top_users || []);
setClickhouseDisabled(false);
} catch (err) {
const errorInstance = err instanceof Error ? err : new Error("获取数据统计失败")
const errMsg = errorInstance.message || ""
if (errMsg.includes("ClickHouse") || errMsg.includes("未启用")) {
setClickhouseDisabled(true)
const errorInstance =
err instanceof Error ? err : new Error('获取数据统计失败');
const errMsg = errorInstance.message || '';
if (errMsg.includes('ClickHouse') || errMsg.includes('未启用')) {
setClickhouseDisabled(true);
} else {
setError(errorInstance)
setError(errorInstance);
}
} finally {
setLoading(false)
setLoading(false);
}
}, [])
}, []);
useEffect(() => {
fetchAnalytics()
}, [fetchAnalytics])
fetchAnalytics();
}, [fetchAnalytics]);
const totalBrowserRequests = browsers.reduce((sum, item) => sum + item.count, 0)
const totalTrendRequests = trend.reduce((sum, item) => sum + item.count, 0)
const totalBrowserRequests = browsers.reduce(
(sum, item) => sum + item.count,
0,
);
const totalTrendRequests = trend.reduce((sum, item) => sum + item.count, 0);
if (clickhouseDisabled) {
return (
<div className="flex flex-col items-center justify-center p-8 border border-dashed rounded-lg bg-card text-center my-6 min-h-[300px]">
<XCircle className="size-10 text-muted-foreground mb-3" />
<h3 className="text-base font-semibold">ClickHouse 未启用</h3>
<p className="text-sm text-muted-foreground mt-1 max-w-[400px] mb-4">
当前系统配置未启用 ClickHouse 存储,系统不会收集用户访问日志。如需使用此功能,请在后端 `config.yaml` 配置文件中配置并启用 ClickHouse。
<div className='flex flex-col items-center justify-center p-8 border border-dashed rounded-lg bg-card text-center my-6 min-h-[300px]'>
<XCircle className='size-10 text-muted-foreground mb-3' />
<h3 className='text-base font-semibold'>ClickHouse 未启用</h3>
<p className='text-sm text-muted-foreground mt-1 max-w-[400px] mb-4'>
当前系统配置未启用 ClickHouse
存储,系统不会收集用户访问日志。如需使用此功能,请在后端 `config.yaml`
配置文件中配置并启用 ClickHouse。
</p>
</div>
)
);
}
if (error) {
return <ErrorInline error={error} onRetry={fetchAnalytics} />
return <ErrorInline error={error} onRetry={fetchAnalytics} />;
}
if (loading) {
return <LoadingStateWithBorder icon={BarChart3} description="加载访问统计指标中..." />
return (
<LoadingStateWithBorder
icon={BarChart3}
description='加载访问统计指标中...'
/>
);
}
return (
<div className="space-y-6">
<div className='space-y-6'>
{/* Overview Cards */}
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
<Card className="bg-card/25 border-border/40">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-semibold tracking-tight">近 7 天总请求数</CardTitle>
<TrendingUp className="size-4 text-muted-foreground" />
<div className='grid gap-4 sm:grid-cols-2 lg:grid-cols-3'>
<Card className='bg-card/25 border-border/40'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardTitle className='text-sm font-semibold tracking-tight'>
近 7 天总请求数
</CardTitle>
<TrendingUp className='size-4 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold font-mono">{totalTrendRequests.toLocaleString()}</div>
<p className="text-[10px] text-muted-foreground mt-1">系统记录的所有成功认证的访问总频次</p>
<div className='text-2xl font-bold font-mono'>
{totalTrendRequests.toLocaleString()}
</div>
<p className='text-[10px] text-muted-foreground mt-1'>
系统记录的所有成功认证的访问总频次
</p>
</CardContent>
</Card>
<Card className="bg-card/25 border-border/40">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-semibold tracking-tight">活跃终端分类数</CardTitle>
<Globe className="size-4 text-muted-foreground" />
<Card className='bg-card/25 border-border/40'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardTitle className='text-sm font-semibold tracking-tight'>
活跃终端分类数
</CardTitle>
<Globe className='size-4 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold font-mono">{browsers.length}</div>
<p className="text-[10px] text-muted-foreground mt-1">在一周内发起请求的浏览器代理大类统计</p>
<div className='text-2xl font-bold font-mono'>
{browsers.length}
</div>
<p className='text-[10px] text-muted-foreground mt-1'>
在一周内发起请求的浏览器代理大类统计
</p>
</CardContent>
</Card>
<Card className="bg-card/25 border-border/40 sm:col-span-2 lg:col-span-1">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<CardTitle className="text-sm font-semibold tracking-tight">活跃独立用户数</CardTitle>
<Users className="size-4 text-muted-foreground" />
<Card className='bg-card/25 border-border/40 sm:col-span-2 lg:col-span-1'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<CardTitle className='text-sm font-semibold tracking-tight'>
活跃独立用户数
</CardTitle>
<Users className='size-4 text-muted-foreground' />
</CardHeader>
<CardContent>
<div className="text-2xl font-bold font-mono">{topUsers.length}</div>
<p className="text-[10px] text-muted-foreground mt-1">一周内累计发起高频请求的注册账户总量</p>
<div className='text-2xl font-bold font-mono'>
{topUsers.length}
</div>
<p className='text-[10px] text-muted-foreground mt-1'>
一周内累计发起高频请求的注册账户总量
</p>
</CardContent>
</Card>
</div>
{/* Access Trend Chart */}
<Card className="bg-card/20 border-border/40">
<CardHeader className="flex flex-row items-center justify-between">
<Card className='bg-card/20 border-border/40'>
<CardHeader className='flex flex-row items-center justify-between'>
<div>
<CardTitle className="text-base font-bold">一周访问量趋势</CardTitle>
<CardDescription className="text-xs">
<CardTitle className='text-base font-bold'>
一周访问量趋势
</CardTitle>
<CardDescription className='text-xs'>
展现系统最近 7 天内的每日 API 请求曲线
</CardDescription>
</div>
<Button variant="ghost" size="icon" className="size-8" onClick={fetchAnalytics} disabled={loading}>
{loading ? <Spinner className="size-4" /> : <RefreshCw className="size-4 text-muted-foreground" />}
<Button
variant='ghost'
size='icon'
className='size-8'
onClick={fetchAnalytics}
disabled={loading}
>
{loading ? (
<Spinner className='size-4' />
) : (
<RefreshCw className='size-4 text-muted-foreground' />
)}
</Button>
</CardHeader>
<CardContent className="pl-2 pr-4 pt-2">
<CardContent className='pl-2 pr-4 pt-2'>
{trend.length === 0 ? (
<EmptyStateWithBorder icon={TrendingUp} description="暂无趋势数据" />
<EmptyStateWithBorder
icon={TrendingUp}
description='暂无趋势数据'
/>
) : (
<div className="h-[280px] w-full">
<ChartContainer config={chartConfig} className="w-full h-full aspect-auto">
<div className='h-[280px] w-full'>
<ChartContainer
config={chartConfig}
className='w-full h-full aspect-auto'
>
<AreaChart
data={trend}
margin={{ top: 10, right: 10, left: -20, bottom: 0 }}
>
<defs>
<linearGradient id="colorCount" x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor="var(--color-count)" stopOpacity={0.3} />
<stop offset="95%" stopColor="var(--color-count)" stopOpacity={0.01} />
<linearGradient id='colorCount' x1='0' y1='0' x2='0' y2='1'>
<stop
offset='5%'
stopColor='var(--color-count)'
stopOpacity={0.3}
/>
<stop
offset='95%'
stopColor='var(--color-count)'
stopOpacity={0.01}
/>
</linearGradient>
</defs>
<CartesianGrid strokeDasharray="3 3" vertical={false} />
<CartesianGrid strokeDasharray='3 3' vertical={false} />
<XAxis
dataKey="formattedDate"
dataKey='formattedDate'
tickLine={false}
axisLine={false}
dy={10}
className="font-mono"
className='font-mono'
/>
<YAxis
tickLine={false}
axisLine={false}
dx={-10}
className="font-mono"
className='font-mono'
/>
<ChartTooltip content={<ChartTooltipContent />} />
<Area
type="monotone"
dataKey="count"
stroke="var(--color-count)"
type='monotone'
dataKey='count'
stroke='var(--color-count)'
strokeWidth={2}
fillOpacity={1}
fill="url(#colorCount)"
name="请求次数"
fill='url(#colorCount)'
name='请求次数'
/>
</AreaChart>
</ChartContainer>
@@ -226,46 +299,56 @@ export function AccessAnalytics() {
</Card>
{/* Two Columns for Ranking Statistics */}
<div className="grid gap-6 md:grid-cols-2">
<div className='grid gap-6 md:grid-cols-2'>
{/* Browser Rankings */}
<Card className="bg-card/20 border-border/40 flex flex-col h-full">
<Card className='bg-card/20 border-border/40 flex flex-col h-full'>
<CardHeader>
<CardTitle className="text-base font-bold flex items-center gap-1.5">
<Globe className="size-4.5 text-muted-foreground" />
<CardTitle className='text-base font-bold flex items-center gap-1.5'>
<Globe className='size-4.5 text-muted-foreground' />
使用的浏览器排行
</CardTitle>
<CardDescription className="text-xs">
<CardDescription className='text-xs'>
基于请求头 User-Agent 智能分类的一周占比统计
</CardDescription>
</CardHeader>
<CardContent className="flex-1">
<CardContent className='flex-1'>
{browsers.length === 0 ? (
<EmptyStateWithBorder icon={Globe} description="暂无浏览器排行数据" />
<EmptyStateWithBorder
icon={Globe}
description='暂无浏览器排行数据'
/>
) : (
<div className="space-y-4">
<div className='space-y-4'>
{browsers.map((item, index) => {
const percent = totalBrowserRequests > 0 ? (item.count / totalBrowserRequests) * 100 : 0
const percent =
totalBrowserRequests > 0
? (item.count / totalBrowserRequests) * 100
: 0;
return (
<div key={item.browser} className="space-y-1.5">
<div className="flex items-center justify-between text-xs font-medium">
<span className="flex items-center gap-2">
<Badge variant="outline" className="px-1.5 py-0 h-5 font-mono text-[10px] select-none">
<div key={item.browser} className='space-y-1.5'>
<div className='flex items-center justify-between text-xs font-medium'>
<span className='flex items-center gap-2'>
<Badge
variant='outline'
className='px-1.5 py-0 h-5 font-mono text-[10px] select-none'
>
{index + 1}
</Badge>
<span className="font-semibold">{item.browser}</span>
<span className='font-semibold'>{item.browser}</span>
</span>
<span className="text-muted-foreground font-mono text-xs">
{item.count.toLocaleString()} 次 ({percent.toFixed(1)}%)
<span className='text-muted-foreground font-mono text-xs'>
{item.count.toLocaleString()} 次 ({percent.toFixed(1)}
%)
</span>
</div>
<div className="h-2 w-full rounded-full bg-muted overflow-hidden">
<div className='h-2 w-full rounded-full bg-muted overflow-hidden'>
<div
className="h-full bg-primary rounded-full transition-all duration-500"
className='h-full bg-primary rounded-full transition-all duration-500'
style={{ width: `${percent}%` }}
/>
</div>
</div>
)
);
})}
</div>
)}
@@ -273,39 +356,50 @@ export function AccessAnalytics() {
</Card>
{/* Top Users */}
<Card className="bg-card/20 border-border/40 flex flex-col h-full">
<Card className='bg-card/20 border-border/40 flex flex-col h-full'>
<CardHeader>
<CardTitle className="text-base font-bold flex items-center gap-1.5">
<Users className="size-4.5 text-muted-foreground" />
<CardTitle className='text-base font-bold flex items-center gap-1.5'>
<Users className='size-4.5 text-muted-foreground' />
最活跃的用户排行 (Top 10)
</CardTitle>
<CardDescription className="text-xs">
<CardDescription className='text-xs'>
统计最近一周发起接口访问请求数量最多的账户
</CardDescription>
</CardHeader>
<CardContent className="flex-1">
<CardContent className='flex-1'>
{topUsers.length === 0 ? (
<EmptyStateWithBorder icon={Users} description="暂无活跃用户数据" />
<EmptyStateWithBorder
icon={Users}
description='暂无活跃用户数据'
/>
) : (
<div className="divide-y divide-border/40">
<div className='divide-y divide-border/40'>
{topUsers.map((user) => (
<div key={user.user_id} className="flex items-center justify-between py-2.5 first:pt-0 last:pb-0">
<div className="flex items-center gap-3">
<div className="flex size-8 shrink-0 items-center justify-center rounded-full bg-primary/10 text-primary font-bold text-xs select-none">
{(user.username || "U").slice(0, 1).toUpperCase()}
<div
key={user.user_id}
className='flex items-center justify-between py-2.5 first:pt-0 last:pb-0'
>
<div className='flex items-center gap-3'>
<div className='flex size-8 shrink-0 items-center justify-center rounded-full bg-primary/10 text-primary font-bold text-xs select-none'>
{(user.username || 'U').slice(0, 1).toUpperCase()}
</div>
<div className="truncate max-w-[180px] sm:max-w-xs">
<div className="text-xs font-bold leading-tight truncate">
{user.username || "未知"}
<div className='truncate max-w-[180px] sm:max-w-xs'>
<div className='text-xs font-bold leading-tight truncate'>
{user.username || '未知'}
</div>
<div className="text-[10px] text-muted-foreground leading-normal truncate">
{user.nickname ? `(${user.nickname})` : "(无昵称)"} | ID: {user.user_id}
<div className='text-[10px] text-muted-foreground leading-normal truncate'>
{user.nickname ? `(${user.nickname})` : '(无昵称)'} |
ID: {user.user_id}
</div>
</div>
</div>
<div className="text-right shrink-0">
<div className="text-xs font-bold font-mono">{user.count.toLocaleString()}</div>
<div className="text-[9px] text-muted-foreground uppercase font-bold tracking-wider">次请求</div>
<div className='text-right shrink-0'>
<div className='text-xs font-bold font-mono'>
{user.count.toLocaleString()}
</div>
<div className='text-[9px] text-muted-foreground uppercase font-bold tracking-wider'>
次请求
</div>
</div>
</div>
))}
@@ -315,5 +409,5 @@ export function AccessAnalytics() {
</Card>
</div>
</div>
)
);
}
@@ -14,96 +14,118 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
"use client"
'use client';
import {useMemo, useState} from "react"
import {useQuery} from "@tanstack/react-query"
import {toast} from "sonner"
import {format} from "date-fns"
import {Activity, ChevronLeft, ChevronRight, Copy, Eye, RotateCcw, Search, XCircle} from "lucide-react"
import { useMemo, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { toast } from 'sonner';
import { format } from 'date-fns';
import {
Activity,
ChevronLeft,
ChevronRight,
Copy,
Eye,
RotateCcw,
Search,
XCircle,
} from 'lucide-react';
import services from "@/lib/services"
import {ErrorInline} from "@/components/layout/error"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {EmptyStateWithBorder} from "@/components/layout/empty"
import {Badge} from "@/components/ui/badge"
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle} from "@/components/ui/sheet"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import services from '@/lib/services';
import { ErrorInline } from '@/components/layout/error';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { EmptyStateWithBorder } from '@/components/layout/empty';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
} from '@/components/ui/sheet';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
interface AccessLog {
id: string
user_id: string
username: string
nickname: string
path: string
method: string
ip: string
user_agent: string
headers: string
status: number
latency: number
created_at: string
id: string;
user_id: string;
username: string;
nickname: string;
path: string;
method: string;
ip: string;
user_agent: string;
headers: string;
status: number;
latency: number;
created_at: string;
}
const PAGE_SIZE = 15
const PAGE_SIZE = 15;
function formatDateTime(value?: string | null) {
if (!value) return "-"
const date = new Date(value)
if (Number.isNaN(date.getTime())) return value
return format(date, "yyyy-MM-dd HH:mm:ss")
if (!value) return '-';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return format(date, 'yyyy-MM-dd HH:mm:ss');
}
function formatLatency(ms: number) {
if (ms < 1000) return `${ms}ms`
return `${(ms / 1000).toFixed(2)}s`
if (ms < 1000) return `${ms}ms`;
return `${(ms / 1000).toFixed(2)}s`;
}
function statusVariant(status: number) {
if (status >= 500) return "destructive"
if (status >= 400) return "destructive"
if (status >= 300) return "outline"
return "secondary"
if (status >= 500) return 'destructive';
if (status >= 400) return 'destructive';
if (status >= 300) return 'outline';
return 'secondary';
}
function isClickhouseDisabledError(error: Error) {
const errMsg = error.message || ""
return errMsg.includes("ClickHouse") || errMsg.includes("未启用")
const errMsg = error.message || '';
return errMsg.includes('ClickHouse') || errMsg.includes('未启用');
}
export function AccessLogs() {
const [page, setPage] = useState(1)
const [page, setPage] = useState(1);
// 搜索过滤条件
const [usernameFilter, setUsernameFilter] = useState("")
const [pathFilter, setPathFilter] = useState("")
const [startTimeFilter, setStartTimeFilter] = useState("")
const [endTimeFilter, setEndTimeFilter] = useState("")
const [usernameFilter, setUsernameFilter] = useState('');
const [pathFilter, setPathFilter] = useState('');
const [startTimeFilter, setStartTimeFilter] = useState('');
const [endTimeFilter, setEndTimeFilter] = useState('');
// 实际提交的搜索条件
const [searchParams, setSearchParams] = useState({
username: "",
path: "",
start_time: "",
end_time: "",
})
username: '',
path: '',
start_time: '',
end_time: '',
});
const [selectedLog, setSelectedLog] = useState<AccessLog | null>(null)
const [detailOpen, setDetailOpen] = useState(false)
const [selectedLog, setSelectedLog] = useState<AccessLog | null>(null);
const [detailOpen, setDetailOpen] = useState(false);
const logsQuery = useQuery({
queryKey: ["admin", "access-logs", page, searchParams],
queryKey: ['admin', 'access-logs', page, searchParams],
queryFn: async () => {
let startISO = ""
let startISO = '';
if (searchParams.start_time) {
startISO = new Date(searchParams.start_time).toISOString()
startISO = new Date(searchParams.start_time).toISOString();
}
let endISO = ""
let endISO = '';
if (searchParams.end_time) {
endISO = new Date(searchParams.end_time).toISOString()
endISO = new Date(searchParams.end_time).toISOString();
}
return services.adminLog.getAccessLogs({
@@ -113,131 +135,157 @@ export function AccessLogs() {
path: searchParams.path || undefined,
start_time: startISO || undefined,
end_time: endISO || undefined,
})
});
},
retry: false,
})
});
const logs = logsQuery.data?.list ?? []
const total = logsQuery.data?.total ?? 0
const loading = logsQuery.isPending || logsQuery.isFetching
const clickhouseDisabled = logsQuery.isError && isClickhouseDisabledError(logsQuery.error)
const error = logsQuery.isError && !clickhouseDisabled ? logsQuery.error : null
const logs = logsQuery.data?.list ?? [];
const total = logsQuery.data?.total ?? 0;
const loading = logsQuery.isPending || logsQuery.isFetching;
const clickhouseDisabled =
logsQuery.isError && isClickhouseDisabledError(logsQuery.error);
const error =
logsQuery.isError && !clickhouseDisabled ? logsQuery.error : null;
const totalPages = useMemo(() => Math.max(1, Math.ceil(total / PAGE_SIZE)), [total])
const totalPages = useMemo(
() => Math.max(1, Math.ceil(total / PAGE_SIZE)),
[total],
);
const handleSearch = (e: React.FormEvent) => {
e.preventDefault()
setPage(1)
e.preventDefault();
setPage(1);
setSearchParams({
username: usernameFilter.trim(),
path: pathFilter.trim(),
start_time: startTimeFilter,
end_time: endTimeFilter,
})
}
});
};
const handleReset = () => {
setUsernameFilter("")
setPathFilter("")
setStartTimeFilter("")
setEndTimeFilter("")
setPage(1)
setUsernameFilter('');
setPathFilter('');
setStartTimeFilter('');
setEndTimeFilter('');
setPage(1);
setSearchParams({
username: "",
path: "",
start_time: "",
end_time: "",
})
}
username: '',
path: '',
start_time: '',
end_time: '',
});
};
const handlePageChange = (newPage: number) => {
setPage(newPage)
}
setPage(newPage);
};
const copyToClipboard = (text: string, subject: string) => {
navigator.clipboard.writeText(text)
toast.success(`${subject}已复制到剪贴板`)
}
navigator.clipboard.writeText(text);
toast.success(`${subject}已复制到剪贴板`);
};
// Prettify Headers JSON string
const getPrettyHeaders = (headersRaw?: string) => {
if (!headersRaw) return "暂无头部数据"
if (!headersRaw) return '暂无头部数据';
try {
const parsed = JSON.parse(headersRaw)
return JSON.stringify(parsed, null, 2)
const parsed = JSON.parse(headersRaw);
return JSON.stringify(parsed, null, 2);
} catch {
return headersRaw
}
return headersRaw;
}
};
if (clickhouseDisabled) {
return (
<div className="flex flex-col items-center justify-center p-8 border border-dashed rounded-lg bg-card text-center my-6 min-h-[300px]">
<XCircle className="size-10 text-muted-foreground mb-3" />
<h3 className="text-base font-semibold">ClickHouse 未启用</h3>
<p className="text-sm text-muted-foreground mt-1 max-w-[400px] mb-4">
当前系统配置未启用 ClickHouse 存储,系统不会收集用户访问日志。如需使用此功能,请在后端 `config.yaml` 配置文件中配置并启用 ClickHouse。
<div className='flex flex-col items-center justify-center p-8 border border-dashed rounded-lg bg-card text-center my-6 min-h-[300px]'>
<XCircle className='size-10 text-muted-foreground mb-3' />
<h3 className='text-base font-semibold'>ClickHouse 未启用</h3>
<p className='text-sm text-muted-foreground mt-1 max-w-[400px] mb-4'>
当前系统配置未启用 ClickHouse
存储,系统不会收集用户访问日志。如需使用此功能,请在后端 `config.yaml`
配置文件中配置并启用 ClickHouse。
</p>
</div>
)
);
}
if (error) {
return <ErrorInline error={error} onRetry={() => void logsQuery.refetch()} />
return (
<ErrorInline error={error} onRetry={() => void logsQuery.refetch()} />
);
}
return (
<div className="space-y-4">
<div className='space-y-4'>
{/* Filters */}
<form onSubmit={handleSearch} className="grid gap-3 p-4 border rounded-lg bg-card/30 md:grid-cols-5 items-end">
<div className="grid gap-1.5">
<Label htmlFor="search-user" className="text-xs">用户名</Label>
<form
onSubmit={handleSearch}
className='grid gap-3 p-4 border rounded-lg bg-card/30 md:grid-cols-5 items-end'
>
<div className='grid gap-1.5'>
<Label htmlFor='search-user' className='text-xs'>
用户名
</Label>
<Input
id="search-user"
placeholder="输入用户名搜索..."
id='search-user'
placeholder='输入用户名搜索...'
value={usernameFilter}
onChange={(e) => setUsernameFilter(e.target.value)}
className="h-8 text-xs"
className='h-8 text-xs'
/>
</div>
<div className="grid gap-1.5">
<Label htmlFor="search-path" className="text-xs">接口路径</Label>
<div className='grid gap-1.5'>
<Label htmlFor='search-path' className='text-xs'>
接口路径
</Label>
<Input
id="search-path"
placeholder="模糊匹配路径..."
id='search-path'
placeholder='模糊匹配路径...'
value={pathFilter}
onChange={(e) => setPathFilter(e.target.value)}
className="h-8 text-xs"
className='h-8 text-xs'
/>
</div>
<div className="grid gap-1.5">
<Label htmlFor="search-start" className="text-xs">起始时间</Label>
<div className='grid gap-1.5'>
<Label htmlFor='search-start' className='text-xs'>
起始时间
</Label>
<Input
id="search-start"
type="datetime-local"
id='search-start'
type='datetime-local'
value={startTimeFilter}
onChange={(e) => setStartTimeFilter(e.target.value)}
className="h-8 text-xs"
className='h-8 text-xs'
/>
</div>
<div className="grid gap-1.5">
<Label htmlFor="search-end" className="text-xs">结束时间</Label>
<div className='grid gap-1.5'>
<Label htmlFor='search-end' className='text-xs'>
结束时间
</Label>
<Input
id="search-end"
type="datetime-local"
id='search-end'
type='datetime-local'
value={endTimeFilter}
onChange={(e) => setEndTimeFilter(e.target.value)}
className="h-8 text-xs"
className='h-8 text-xs'
/>
</div>
<div className="flex gap-2">
<Button type="submit" size="sm" className="h-8 flex-1">
<Search className="size-3.5 mr-1" />
<div className='flex gap-2'>
<Button type='submit' size='sm' className='h-8 flex-1'>
<Search className='size-3.5 mr-1' />
搜索
</Button>
<Button type="button" variant="outline" size="sm" onClick={handleReset} className="h-8 flex-1">
<RotateCcw className="size-3.5 mr-1" />
<Button
type='button'
variant='outline'
size='sm'
onClick={handleReset}
className='h-8 flex-1'
>
<RotateCcw className='size-3.5 mr-1' />
重置
</Button>
</div>
@@ -245,44 +293,57 @@ export function AccessLogs() {
{/* Loading Table */}
{loading && logs.length === 0 ? (
<LoadingStateWithBorder icon={Activity} description="加载访问日志中..." />
<LoadingStateWithBorder
icon={Activity}
description='加载访问日志中...'
/>
) : logs.length === 0 ? (
<EmptyStateWithBorder icon={Activity} description="未查询到任何访问日志数据" />
<EmptyStateWithBorder
icon={Activity}
description='未查询到任何访问日志数据'
/>
) : (
<div className="rounded-lg border bg-card overflow-hidden">
<Table className="min-w-[900px]">
<div className='rounded-lg border bg-card overflow-hidden'>
<Table className='min-w-[900px]'>
<TableHeader>
<TableRow className="hover:bg-transparent">
<TableHead className="w-[100px]">请求方法</TableHead>
<TableHead className="min-w-[200px]">路径</TableHead>
<TableHead className="w-[150px]">用户</TableHead>
<TableHead className="w-[120px]">IP</TableHead>
<TableHead className="w-[90px]">状态</TableHead>
<TableHead className="w-[100px]">耗时</TableHead>
<TableHead className="w-[170px]">请求时间</TableHead>
<TableHead className="w-[80px] text-center">详情</TableHead>
<TableRow className='hover:bg-transparent'>
<TableHead className='w-[100px]'>请求方法</TableHead>
<TableHead className='min-w-[200px]'>路径</TableHead>
<TableHead className='w-[150px]'>用户</TableHead>
<TableHead className='w-[120px]'>IP</TableHead>
<TableHead className='w-[90px]'>状态</TableHead>
<TableHead className='w-[100px]'>耗时</TableHead>
<TableHead className='w-[170px]'>请求时间</TableHead>
<TableHead className='w-[80px] text-center'>详情</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{logs.map((log) => (
<TableRow key={log.id} className="hover:bg-muted/30">
<TableRow key={log.id} className='hover:bg-muted/30'>
<TableCell>
<Badge variant="outline" className="font-mono font-semibold uppercase text-xs">
<Badge
variant='outline'
className='font-mono font-semibold uppercase text-xs'
>
{log.method}
</Badge>
</TableCell>
<TableCell className="font-mono text-xs break-all max-w-[280px]">
<TableCell className='font-mono text-xs break-all max-w-[280px]'>
{log.path}
</TableCell>
<TableCell>
<div className="flex flex-col">
<span className="text-xs font-medium">{log.username || "未知用户"}</span>
<div className='flex flex-col'>
<span className='text-xs font-medium'>
{log.username || '未知用户'}
</span>
{log.nickname && (
<span className="text-[10px] text-muted-foreground">({log.nickname})</span>
<span className='text-[10px] text-muted-foreground'>
({log.nickname})
</span>
)}
</div>
</TableCell>
<TableCell className="font-mono text-xs text-muted-foreground">
<TableCell className='font-mono text-xs text-muted-foreground'>
{log.ip}
</TableCell>
<TableCell>
@@ -290,23 +351,23 @@ export function AccessLogs() {
{log.status}
</Badge>
</TableCell>
<TableCell className="font-mono text-xs text-muted-foreground">
<TableCell className='font-mono text-xs text-muted-foreground'>
{formatLatency(log.latency)}
</TableCell>
<TableCell className="font-mono text-[11px] text-muted-foreground">
<TableCell className='font-mono text-[11px] text-muted-foreground'>
{formatDateTime(log.created_at)}
</TableCell>
<TableCell className="text-center">
<TableCell className='text-center'>
<Button
variant="ghost"
size="icon"
className="size-7 hover:bg-muted"
variant='ghost'
size='icon'
className='size-7 hover:bg-muted'
onClick={() => {
setSelectedLog(log)
setDetailOpen(true)
setSelectedLog(log);
setDetailOpen(true);
}}
>
<Eye className="size-3.5" />
<Eye className='size-3.5' />
</Button>
</TableCell>
</TableRow>
@@ -318,30 +379,30 @@ export function AccessLogs() {
{/* Pagination */}
{logs.length > 0 && (
<div className="flex items-center justify-between py-1">
<div className="text-xs text-muted-foreground">
<div className='flex items-center justify-between py-1'>
<div className='text-xs text-muted-foreground'>
共 {total} 条记录,当前第 {page}/{totalPages} 页
</div>
<div className="flex items-center gap-2">
<div className='flex items-center gap-2'>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
onClick={() => handlePageChange(Math.max(1, page - 1))}
disabled={page <= 1 || loading}
className="h-8 text-xs"
className='h-8 text-xs'
>
<ChevronLeft className="size-3.5 mr-1" />
<ChevronLeft className='size-3.5 mr-1' />
上一页
</Button>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
onClick={() => handlePageChange(Math.min(totalPages, page + 1))}
disabled={page >= totalPages || loading}
className="h-8 text-xs"
className='h-8 text-xs'
>
下一页
<ChevronRight className="size-3.5 ml-1" />
<ChevronRight className='size-3.5 ml-1' />
</Button>
</div>
</div>
@@ -349,106 +410,144 @@ export function AccessLogs() {
{/* Detail Drawer */}
<Sheet open={detailOpen} onOpenChange={setDetailOpen}>
<SheetContent className="w-full p-0 sm:max-w-[640px] flex flex-col h-full bg-background border-l">
<SheetHeader className="border-b px-5 py-4 shrink-0">
<SheetTitle className="text-lg font-bold">访问日志详情</SheetTitle>
<SheetDescription className="text-xs">
<SheetContent className='w-full p-0 sm:max-w-[640px] flex flex-col h-full bg-background border-l'>
<SheetHeader className='border-b px-5 py-4 shrink-0'>
<SheetTitle className='text-lg font-bold'>访问日志详情</SheetTitle>
<SheetDescription className='text-xs'>
访问 ID: {selectedLog?.id}
</SheetDescription>
</SheetHeader>
{selectedLog ? (
<div className="flex-1 overflow-y-auto px-5 py-4 space-y-5">
<div className='flex-1 overflow-y-auto px-5 py-4 space-y-5'>
{/* Summary Details */}
<div className="grid grid-cols-2 gap-3 sm:grid-cols-3">
<div className="rounded-lg border p-3 bg-muted/10">
<div className="text-[10px] text-muted-foreground uppercase font-bold tracking-wider">请求方法</div>
<div className="mt-1 font-mono text-sm font-bold uppercase">{selectedLog.method}</div>
<div className='grid grid-cols-2 gap-3 sm:grid-cols-3'>
<div className='rounded-lg border p-3 bg-muted/10'>
<div className='text-[10px] text-muted-foreground uppercase font-bold tracking-wider'>
请求方法
</div>
<div className="rounded-lg border p-3 bg-muted/10">
<div className="text-[10px] text-muted-foreground uppercase font-bold tracking-wider">响应状态</div>
<div className="mt-1">
<div className='mt-1 font-mono text-sm font-bold uppercase'>
{selectedLog.method}
</div>
</div>
<div className='rounded-lg border p-3 bg-muted/10'>
<div className='text-[10px] text-muted-foreground uppercase font-bold tracking-wider'>
响应状态
</div>
<div className='mt-1'>
<Badge variant={statusVariant(selectedLog.status)}>
{selectedLog.status}
</Badge>
</div>
</div>
<div className="rounded-lg border p-3 bg-muted/10">
<div className="text-[10px] text-muted-foreground uppercase font-bold tracking-wider">耗时</div>
<div className="mt-1 font-mono text-sm">{formatLatency(selectedLog.latency)}</div>
<div className='rounded-lg border p-3 bg-muted/10'>
<div className='text-[10px] text-muted-foreground uppercase font-bold tracking-wider'>
耗时
</div>
<div className="rounded-lg border p-3 bg-muted/10 col-span-2 sm:col-span-1">
<div className="text-[10px] text-muted-foreground uppercase font-bold tracking-wider">IP 地址</div>
<div className="mt-1 font-mono text-sm">{selectedLog.ip}</div>
<div className='mt-1 font-mono text-sm'>
{formatLatency(selectedLog.latency)}
</div>
<div className="rounded-lg border p-3 bg-muted/10 col-span-2">
<div className="text-[10px] text-muted-foreground uppercase font-bold tracking-wider">用户</div>
<div className="mt-1 text-sm font-medium">
{selectedLog.username ? `${selectedLog.username} (${selectedLog.nickname || '无昵称'})` : "未知/游客"}
<span className="block font-mono text-[10px] text-muted-foreground mt-0.5">ID: {selectedLog.user_id}</span>
</div>
<div className='rounded-lg border p-3 bg-muted/10 col-span-2 sm:col-span-1'>
<div className='text-[10px] text-muted-foreground uppercase font-bold tracking-wider'>
IP 地址
</div>
<div className='mt-1 font-mono text-sm'>{selectedLog.ip}</div>
</div>
<div className='rounded-lg border p-3 bg-muted/10 col-span-2'>
<div className='text-[10px] text-muted-foreground uppercase font-bold tracking-wider'>
用户
</div>
<div className='mt-1 text-sm font-medium'>
{selectedLog.username
? `${selectedLog.username} (${selectedLog.nickname || '无昵称'})`
: '未知/游客'}
<span className='block font-mono text-[10px] text-muted-foreground mt-0.5'>
ID: {selectedLog.user_id}
</span>
</div>
</div>
</div>
{/* Path */}
<div className="grid gap-2">
<Label className="text-xs font-bold text-muted-foreground">请求路径</Label>
<div className="rounded-md border bg-muted/30 px-3 py-2 font-mono text-xs break-all">
<div className='grid gap-2'>
<Label className='text-xs font-bold text-muted-foreground'>
请求路径
</Label>
<div className='rounded-md border bg-muted/30 px-3 py-2 font-mono text-xs break-all'>
{selectedLog.path}
</div>
</div>
{/* Request Time */}
<div className="grid gap-2">
<Label className="text-xs font-bold text-muted-foreground">请求时间</Label>
<div className="font-mono text-xs">{formatDateTime(selectedLog.created_at)}</div>
<div className='grid gap-2'>
<Label className='text-xs font-bold text-muted-foreground'>
请求时间
</Label>
<div className='font-mono text-xs'>
{formatDateTime(selectedLog.created_at)}
</div>
</div>
{/* User Agent */}
<div className="grid gap-2">
<div className="flex items-center justify-between">
<Label className="text-xs font-bold text-muted-foreground">User-Agent</Label>
<div className='grid gap-2'>
<div className='flex items-center justify-between'>
<Label className='text-xs font-bold text-muted-foreground'>
User-Agent
</Label>
<Button
variant="ghost"
size="icon"
className="size-6 text-muted-foreground hover:text-foreground"
onClick={() => copyToClipboard(selectedLog.user_agent, "User-Agent")}
title="复制 User-Agent"
variant='ghost'
size='icon'
className='size-6 text-muted-foreground hover:text-foreground'
onClick={() =>
copyToClipboard(selectedLog.user_agent, 'User-Agent')
}
title='复制 User-Agent'
>
<Copy className="size-3" />
<Copy className='size-3' />
</Button>
</div>
<div className="rounded-md border bg-muted/20 px-3 py-2 text-xs font-mono break-all max-h-24 overflow-y-auto leading-relaxed">
<div className='rounded-md border bg-muted/20 px-3 py-2 text-xs font-mono break-all max-h-24 overflow-y-auto leading-relaxed'>
{selectedLog.user_agent}
</div>
</div>
{/* Headers */}
<div className="grid gap-2 flex-1">
<div className="flex items-center justify-between">
<Label className="text-xs font-bold text-muted-foreground">Request Headers</Label>
<div className='grid gap-2 flex-1'>
<div className='flex items-center justify-between'>
<Label className='text-xs font-bold text-muted-foreground'>
Request Headers
</Label>
<Button
variant="ghost"
size="icon"
className="size-6 text-muted-foreground hover:text-foreground"
onClick={() => copyToClipboard(getPrettyHeaders(selectedLog.headers), "Headers")}
title="复制 Headers"
variant='ghost'
size='icon'
className='size-6 text-muted-foreground hover:text-foreground'
onClick={() =>
copyToClipboard(
getPrettyHeaders(selectedLog.headers),
'Headers',
)
}
title='复制 Headers'
>
<Copy className="size-3" />
<Copy className='size-3' />
</Button>
</div>
<pre className="overflow-auto rounded-md border bg-[#0d1117] p-3 text-xs leading-relaxed text-gray-300 font-mono max-h-[300px]">
<pre className='overflow-auto rounded-md border bg-[#0d1117] p-3 text-xs leading-relaxed text-gray-300 font-mono max-h-[300px]'>
{getPrettyHeaders(selectedLog.headers)}
</pre>
</div>
</div>
) : (
<div className="flex-grow flex items-center justify-center">
<EmptyStateWithBorder icon={Activity} description="未选中日志明细" />
<div className='flex-grow flex items-center justify-center'>
<EmptyStateWithBorder
icon={Activity}
description='未选中日志明细'
/>
</div>
)}
</SheetContent>
</Sheet>
</div>
)
);
}
@@ -14,92 +14,103 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
"use client"
'use client';
import {memo, useCallback, useEffect, useRef, useState} from "react"
import {useVirtualizer} from "@tanstack/react-virtual"
import {toast} from "sonner"
import {ArrowDown, ChevronUp, Loader2} from "lucide-react"
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { useVirtualizer } from '@tanstack/react-virtual';
import { toast } from 'sonner';
import { ArrowDown, ChevronUp, Loader2 } from 'lucide-react';
import services from "@/lib/services"
import {ErrorInline} from "@/components/layout/error"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {Button} from "@/components/ui/button"
import services from '@/lib/services';
import { ErrorInline } from '@/components/layout/error';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { Button } from '@/components/ui/button';
interface LogEntry {
index: number
data: string
index: number;
data: string;
}
// Distance (px) from bottom to treat as "at bottom"
const BOTTOM_THRESHOLD = 40
const LOG_LINE_ESTIMATE_PX = 20
const LOG_VIRTUAL_OVERSCAN = 24
const BOTTOM_THRESHOLD = 40;
const LOG_LINE_ESTIMATE_PX = 20;
const LOG_VIRTUAL_OVERSCAN = 24;
const LogLine = memo(function LogLine({ data }: { data: string }) {
const level = parseLogLevel(data)
const color = level === "error"
? "text-red-400"
: level === "warn"
? "text-yellow-400"
: level === "debug"
? "text-gray-500"
: "text-gray-300"
const level = parseLogLevel(data);
const color =
level === 'error'
? 'text-red-400'
: level === 'warn'
? 'text-yellow-400'
: level === 'debug'
? 'text-gray-500'
: 'text-gray-300';
return (
<div className={`${color} whitespace-pre-wrap break-all px-2 py-0.5 rounded hover:bg-white/5`}>
<div
className={`${color} whitespace-pre-wrap break-all px-2 py-0.5 rounded hover:bg-white/5`}
>
{data}
</div>
)
})
);
});
function getApiBaseUrl(): string {
if (typeof window !== "undefined") {
if (typeof window !== 'undefined') {
// If NEXT_PUBLIC_WAVELET_BACKEND_URL is set, use it. Otherwise, use origin.
const base = process.env.NEXT_PUBLIC_WAVELET_BACKEND_URL || ""
if (base.startsWith("http")) return base
const base = process.env.NEXT_PUBLIC_WAVELET_BACKEND_URL || '';
if (base.startsWith('http')) return base;
// Relative URL fallback
const proto = window.location.protocol
const host = window.location.host
return `${proto}//${host}${base}`
const proto = window.location.protocol;
const host = window.location.host;
return `${proto}//${host}${base}`;
}
return process.env.NEXT_PUBLIC_WAVELET_BACKEND_URL || ""
return process.env.NEXT_PUBLIC_WAVELET_BACKEND_URL || '';
}
function buildWsUrl(): string {
const base = getApiBaseUrl()
const wsBase = base.replace(/^http/, "ws")
return `${wsBase}/api/v1/admin/logs/ws`
const base = getApiBaseUrl();
const wsBase = base.replace(/^http/, 'ws');
return `${wsBase}/api/v1/admin/logs/ws`;
}
function parseLogLevel(line: string): "debug" | "info" | "warn" | "error" | "unknown" {
const lower = line.toLowerCase()
if (lower.includes("\"level\":\"error\"") || lower.includes("level=error")) return "error"
if (lower.includes("\"level\":\"warn\"") || lower.includes("level=warn")) return "warn"
if (lower.includes("\"level\":\"debug\"") || lower.includes("level=debug")) return "debug"
if (lower.includes("\"level\":\"info\"") || lower.includes("level=info")) return "info"
return "unknown"
function parseLogLevel(
line: string,
): 'debug' | 'info' | 'warn' | 'error' | 'unknown' {
const lower = line.toLowerCase();
if (lower.includes('"level":"error"') || lower.includes('level=error'))
return 'error';
if (lower.includes('"level":"warn"') || lower.includes('level=warn'))
return 'warn';
if (lower.includes('"level":"debug"') || lower.includes('level=debug'))
return 'debug';
if (lower.includes('"level":"info"') || lower.includes('level=info'))
return 'info';
return 'unknown';
}
export function AppLogs() {
const [loading, setLoading] = useState(true)
const [error, setError] = useState<Error | null>(null)
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const [logs, setLogs] = useState<LogEntry[]>([])
const [hasMore, setHasMore] = useState(false)
const [nextCursor, setNextCursor] = useState(0)
const [loadingMore, setLoadingMore] = useState(false)
const [logs, setLogs] = useState<LogEntry[]>([]);
const [hasMore, setHasMore] = useState(false);
const [nextCursor, setNextCursor] = useState(0);
const [loadingMore, setLoadingMore] = useState(false);
// autoScroll = true → new logs auto-scroll to bottom
const [autoScroll, setAutoScroll] = useState(true)
const [autoScroll, setAutoScroll] = useState(true);
const containerRef = useRef<HTMLDivElement>(null)
const wsRef = useRef<WebSocket | null>(null)
const autoScrollRef = useRef(autoScroll)
const isUserScrolling = useRef(false)
const containerRef = useRef<HTMLDivElement>(null);
const wsRef = useRef<WebSocket | null>(null);
const autoScrollRef = useRef(autoScroll);
const isUserScrolling = useRef(false);
useEffect(() => { autoScrollRef.current = autoScroll }, [autoScroll])
useEffect(() => {
autoScrollRef.current = autoScroll;
}, [autoScroll]);
const rowVirtualizer = useVirtualizer({
count: logs.length,
@@ -107,200 +118,222 @@ export function AppLogs() {
estimateSize: () => LOG_LINE_ESTIMATE_PX,
overscan: LOG_VIRTUAL_OVERSCAN,
measureElement: (element) => element.getBoundingClientRect().height,
})
});
// ---- Scroll detection ------------------------------------------------
const handleScroll = useCallback(() => {
const el = containerRef.current
if (!el) return
const el = containerRef.current;
if (!el) return;
if (!isUserScrolling.current) return
if (!isUserScrolling.current) return;
const atBottom = el.scrollHeight - el.scrollTop - el.clientHeight < BOTTOM_THRESHOLD
const atBottom =
el.scrollHeight - el.scrollTop - el.clientHeight < BOTTOM_THRESHOLD;
if (atBottom && !autoScrollRef.current) {
setAutoScroll(true)
setAutoScroll(true);
} else if (!atBottom && autoScrollRef.current) {
setAutoScroll(false)
setAutoScroll(false);
}
}, [])
}, []);
const handleWheel = useCallback(() => { isUserScrolling.current = true }, [])
const handleTouchStart = useCallback(() => { isUserScrolling.current = true }, [])
const handleWheel = useCallback(() => {
isUserScrolling.current = true;
}, []);
const handleTouchStart = useCallback(() => {
isUserScrolling.current = true;
}, []);
useEffect(() => {
const el = containerRef.current
if (!el) return
const el = containerRef.current;
if (!el) return;
let timer: ReturnType<typeof setTimeout>
let timer: ReturnType<typeof setTimeout>;
const onScrollEnd = () => {
clearTimeout(timer)
timer = setTimeout(() => { isUserScrolling.current = false }, 150)
}
el.addEventListener("scroll", onScrollEnd, { passive: true })
clearTimeout(timer);
timer = setTimeout(() => {
isUserScrolling.current = false;
}, 150);
};
el.addEventListener('scroll', onScrollEnd, { passive: true });
return () => {
el.removeEventListener("scroll", onScrollEnd)
clearTimeout(timer)
}
}, [])
el.removeEventListener('scroll', onScrollEnd);
clearTimeout(timer);
};
}, []);
// ---- Auto-scroll to bottom when new logs arrive ----------------------
useEffect(() => {
if (!autoScroll || !containerRef.current) return
isUserScrolling.current = false
const el = containerRef.current
if (!autoScroll || !containerRef.current) return;
isUserScrolling.current = false;
const el = containerRef.current;
requestAnimationFrame(() => {
el.scrollTop = el.scrollHeight
})
}, [logs, autoScroll])
el.scrollTop = el.scrollHeight;
});
}, [logs, autoScroll]);
// ---- Data fetching ---------------------------------------------------
const fetchLogs = useCallback(async (cursor: number = 0) => {
try {
return await services.adminLog.getLogs(cursor)
return await services.adminLog.getLogs(cursor);
} catch (err) {
throw err instanceof Error ? err : new Error("获取日志失败")
throw err instanceof Error ? err : new Error('获取日志失败');
}
}, [])
}, []);
const loadHistory = useCallback(async (cursor: number = 0) => {
const isInitial = cursor === 0
const loadHistory = useCallback(
async (cursor: number = 0) => {
const isInitial = cursor === 0;
if (isInitial) {
setLoading(true)
setError(null)
setLoading(true);
setError(null);
} else {
setLoadingMore(true)
setLoadingMore(true);
}
try {
const data = await fetchLogs(cursor)
const data = await fetchLogs(cursor);
if (isInitial) {
setLogs(data.lines || [])
setLogs(data.lines || []);
} else {
const el = containerRef.current
const previousScrollHeight = el?.scrollHeight ?? 0
setLogs(prev => [...(data.lines || []), ...prev])
const el = containerRef.current;
const previousScrollHeight = el?.scrollHeight ?? 0;
setLogs((prev) => [...(data.lines || []), ...prev]);
requestAnimationFrame(() => {
if (!el) return
isUserScrolling.current = false
el.scrollTop = el.scrollHeight - previousScrollHeight
})
if (!el) return;
isUserScrolling.current = false;
el.scrollTop = el.scrollHeight - previousScrollHeight;
});
}
setHasMore(data.has_more)
setNextCursor(data.next_cursor)
setHasMore(data.has_more);
setNextCursor(data.next_cursor);
} catch (err) {
if (isInitial) {
setError(err instanceof Error ? err : new Error("获取日志失败"))
setError(err instanceof Error ? err : new Error('获取日志失败'));
} else {
toast.error("加载更早日志失败")
toast.error('加载更早日志失败');
}
} finally {
if (isInitial) setLoading(false)
else setLoadingMore(false)
if (isInitial) setLoading(false);
else setLoadingMore(false);
}
}, [fetchLogs])
},
[fetchLogs],
);
// ---- WebSocket -------------------------------------------------------
const connectWs = useCallback(() => {
if (wsRef.current) wsRef.current.close()
if (wsRef.current) wsRef.current.close();
const ws = new WebSocket(buildWsUrl())
wsRef.current = ws
const ws = new WebSocket(buildWsUrl());
wsRef.current = ws;
ws.onmessage = (event) => {
try {
const msg = JSON.parse(event.data)
if (msg.type === "log" && msg.data) {
const entry: LogEntry = msg.data
setLogs(prev => {
const next = [...prev, entry]
return next.length > 2000 ? next.slice(-2000) : next
})
const msg = JSON.parse(event.data);
if (msg.type === 'log' && msg.data) {
const entry: LogEntry = msg.data;
setLogs((prev) => {
const next = [...prev, entry];
return next.length > 2000 ? next.slice(-2000) : next;
});
}
} catch { /* ignore */ }
} catch {
/* ignore */
}
};
ws.onclose = () => { wsRef.current = null }
}, [])
ws.onclose = () => {
wsRef.current = null;
};
}, []);
// ---- Initialize ------------------------------------------------------
useEffect(() => {
loadHistory(0).then(() => connectWs())
loadHistory(0).then(() => connectWs());
return () => {
wsRef.current?.close()
wsRef.current = null
}
wsRef.current?.close();
wsRef.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
}, []);
// ---- Actions ---------------------------------------------------------
const scrollToBottom = useCallback(() => {
setAutoScroll(true)
setAutoScroll(true);
requestAnimationFrame(() => {
if (containerRef.current) {
containerRef.current.scrollTop = containerRef.current.scrollHeight
containerRef.current.scrollTop = containerRef.current.scrollHeight;
}
})
}, [])
});
}, []);
const handleLoadMore = useCallback(() => {
if (nextCursor > 0) loadHistory(nextCursor)
}, [nextCursor, loadHistory])
if (nextCursor > 0) loadHistory(nextCursor);
}, [nextCursor, loadHistory]);
// ---- Render ----------------------------------------------------------
if (loading) return <LoadingStateWithBorder />
if (error) return <ErrorInline error={error} onRetry={() => loadHistory(0)} />
if (loading) return <LoadingStateWithBorder />;
if (error)
return <ErrorInline error={error} onRetry={() => loadHistory(0)} />;
return (
<div className="flex flex-col h-full relative">
<div className='flex flex-col h-full relative'>
{/* Log viewer — fixed height, scrollable */}
<div
ref={containerRef}
onScroll={handleScroll}
onWheel={handleWheel}
onTouchStart={handleTouchStart}
className="h-[calc(100vh-270px)] overflow-y-auto overflow-x-hidden rounded-lg border border-border/40 bg-[#0d1117] font-mono text-[13px] leading-5 relative"
className='h-[calc(100vh-270px)] overflow-y-auto overflow-x-hidden rounded-lg border border-border/40 bg-[#0d1117] font-mono text-[13px] leading-5 relative'
>
{/* Load older logs */}
{hasMore && (
<div className="sticky top-0 z-10 flex justify-center py-1.5 bg-[#0d1117]/90 backdrop-blur-sm">
<div className='sticky top-0 z-10 flex justify-center py-1.5 bg-[#0d1117]/90 backdrop-blur-sm'>
<Button
variant="ghost"
size="sm"
variant='ghost'
size='sm'
onClick={handleLoadMore}
disabled={loadingMore}
className="text-muted-foreground hover:text-foreground h-7 text-xs"
className='text-muted-foreground hover:text-foreground h-7 text-xs'
>
{loadingMore
? <><Loader2 className="size-3 mr-1.5 animate-spin" />加载中...</>
: <><ChevronUp className="size-3 mr-1.5" />加载更早日志</>
}
{loadingMore ? (
<>
<Loader2 className='size-3 mr-1.5 animate-spin' />
加载中...
</>
) : (
<>
<ChevronUp className='size-3 mr-1.5' />
加载更早日志
</>
)}
</Button>
</div>
)}
{logs.length === 0 ? (
<div className="px-4 py-8 text-center text-gray-500">暂无日志</div>
<div className='px-4 py-8 text-center text-gray-500'>暂无日志</div>
) : (
<div
className="relative px-3 py-3"
className='relative px-3 py-3'
style={{ height: `${rowVirtualizer.getTotalSize()}px` }}
>
{rowVirtualizer.getVirtualItems().map((virtualRow) => {
const entry = logs[virtualRow.index]
if (!entry) return null
const entry = logs[virtualRow.index];
if (!entry) return null;
return (
<div
key={entry.index}
ref={rowVirtualizer.measureElement}
data-index={virtualRow.index}
className="absolute top-0 left-0 w-full"
className='absolute top-0 left-0 w-full'
style={{ transform: `translateY(${virtualRow.start}px)` }}
>
<LogLine data={entry.data} />
</div>
)
);
})}
</div>
)}
@@ -308,18 +341,18 @@ export function AppLogs() {
{/* Floating "back to latest" button */}
{!autoScroll && (
<div className="absolute bottom-6 left-1/2 -translate-x-1/2 z-20">
<div className='absolute bottom-6 left-1/2 -translate-x-1/2 z-20'>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
onClick={scrollToBottom}
className="shadow-lg bg-background/80 backdrop-blur-sm border-border/50"
className='shadow-lg bg-background/80 backdrop-blur-sm border-border/50'
>
<ArrowDown className="size-3.5 mr-1.5" />
<ArrowDown className='size-3.5 mr-1.5' />
回到最新
</Button>
</div>
)}
</div>
)
);
}
+38 -28
View File
@@ -1,66 +1,76 @@
"use client"
'use client';
import dynamic from "next/dynamic"
import {Activity, BarChart3, Terminal} from "lucide-react"
import dynamic from 'next/dynamic';
import { Activity, BarChart3, Terminal } from 'lucide-react';
import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs"
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
const tabFallback = (
<div className="h-64 animate-pulse rounded-lg border border-border/40 bg-muted/20" />
)
<div className='h-64 animate-pulse rounded-lg border border-border/40 bg-muted/20' />
);
const AccessAnalytics = dynamic(
() => import("./components/access-analytics").then((mod) => mod.AccessAnalytics),
() =>
import('./components/access-analytics').then((mod) => mod.AccessAnalytics),
{ loading: () => tabFallback },
)
);
const AccessLogs = dynamic(
() => import("./components/access-logs").then((mod) => mod.AccessLogs),
() => import('./components/access-logs').then((mod) => mod.AccessLogs),
{ loading: () => tabFallback },
)
);
const AppLogs = dynamic(
() => import("./components/app-logs").then((mod) => mod.AppLogs),
() => import('./components/app-logs').then((mod) => mod.AppLogs),
{ loading: () => tabFallback },
)
);
export function LogsPageClient() {
return (
<div className="flex flex-col h-full space-y-6 py-6">
<div className='flex flex-col h-full space-y-6 py-6'>
{/* Header */}
<div className="flex items-center gap-2">
<Terminal className="size-5 text-primary" />
<div className='flex items-center gap-2'>
<Terminal className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">系统日志</h1>
<h1 className='text-2xl font-semibold tracking-tight'>系统日志</h1>
</div>
</div>
{/* Tabs Layout */}
<Tabs defaultValue="analytics" className="w-full">
<TabsList variant="line" className="w-fit inline-flex gap-8 mb-6">
<TabsTrigger value="analytics" className="px-0 pb-2 text-xs font-semibold flex items-center gap-1.5">
<BarChart3 className="size-3.5" />
<Tabs defaultValue='analytics' className='w-full'>
<TabsList variant='line' className='w-fit inline-flex gap-8 mb-6'>
<TabsTrigger
value='analytics'
className='px-0 pb-2 text-xs font-semibold flex items-center gap-1.5'
>
<BarChart3 className='size-3.5' />
访问分析
</TabsTrigger>
<TabsTrigger value="access" className="px-0 pb-2 text-xs font-semibold flex items-center gap-1.5">
<Activity className="size-3.5" />
<TabsTrigger
value='access'
className='px-0 pb-2 text-xs font-semibold flex items-center gap-1.5'
>
<Activity className='size-3.5' />
用户访问日志
</TabsTrigger>
<TabsTrigger value="app" className="px-0 pb-2 text-xs font-semibold flex items-center gap-1.5">
<Terminal className="size-3.5" />
<TabsTrigger
value='app'
className='px-0 pb-2 text-xs font-semibold flex items-center gap-1.5'
>
<Terminal className='size-3.5' />
应用运行日志
</TabsTrigger>
</TabsList>
<TabsContent value="analytics" className="mt-0 outline-none flex-1">
<TabsContent value='analytics' className='mt-0 outline-none flex-1'>
<AccessAnalytics />
</TabsContent>
<TabsContent value="access" className="mt-0 outline-none flex-1">
<TabsContent value='access' className='mt-0 outline-none flex-1'>
<AccessLogs />
</TabsContent>
<TabsContent value="app" className="mt-0 outline-none flex-1">
<TabsContent value='app' className='mt-0 outline-none flex-1'>
<AppLogs />
</TabsContent>
</Tabs>
</div>
)
);
}
+2 -2
View File
@@ -14,8 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import {LogsPageClient} from "./page-client"
import { LogsPageClient } from './page-client';
export default function LogsPage() {
return <LogsPageClient />
return <LogsPageClient />;
}
File diff suppressed because it is too large Load Diff
@@ -1,21 +1,40 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
"use client"
'use client';
import * as React from "react"
import {useQuery} from "@tanstack/react-query"
import {ChevronLeft, ChevronRight, History, RefreshCw, Search,} from "lucide-react"
import * as React from 'react';
import { useQuery } from '@tanstack/react-query';
import {
ChevronLeft,
ChevronRight,
History,
RefreshCw,
Search,
} from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Badge} from "@/components/ui/badge"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow,} from "@/components/ui/table"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue,} from "@/components/ui/select"
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import {ErrorInline} from "@/components/layout/error"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {cn} from "@/lib/utils"
import { ErrorInline } from '@/components/layout/error';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { cn } from '@/lib/utils';
import {
Dialog,
@@ -24,156 +43,205 @@ import {
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
} from '@/components/ui/dialog';
import {PushService} from "@/lib/services/push"
import type {PushHistory} from "@/lib/services/push"
import type { PushHistory } from '@/lib/services/push';
import { PushService } from '@/lib/services/push';
function getLevelBadgeVariant(level: string): "outline" | "secondary" | "destructive" | "default" {
function getLevelBadgeVariant(
level: string,
): 'outline' | 'secondary' | 'destructive' | 'default' {
switch (level) {
case "CRITICAL":
return "destructive"
case "IMPORTANT":
return "default"
case 'CRITICAL':
return 'destructive';
case 'IMPORTANT':
return 'default';
default:
return "outline"
return 'outline';
}
}
export function HistoriesTab() {
const [historyPage, setHistoryPage] = React.useState(1)
const [historySearch, setHistorySearch] = React.useState("")
const [historyStatus, setHistoryStatus] = React.useState("all")
const [historyPage, setHistoryPage] = React.useState(1);
const [historySearch, setHistorySearch] = React.useState('');
const [historyStatus, setHistoryStatus] = React.useState('all');
const [detailOpen, setDetailOpen] = React.useState(false)
const [selectedHistory, setSelectedHistory] = React.useState<PushHistory | null>(null)
const [detailOpen, setDetailOpen] = React.useState(false);
const [selectedHistory, setSelectedHistory] =
React.useState<PushHistory | null>(null);
const handleRowClick = (hist: PushHistory) => {
setSelectedHistory(hist)
setDetailOpen(true)
}
setSelectedHistory(hist);
setDetailOpen(true);
};
const historiesQuery = useQuery({
queryKey: ["admin", "push-histories", historyPage, historySearch, historyStatus],
queryKey: [
'admin',
'push-histories',
historyPage,
historySearch,
historyStatus,
],
queryFn: () =>
PushService.listHistories({
page: historyPage,
page_size: 10,
event_key: historySearch || undefined,
status: historyStatus === "all" ? undefined : historyStatus,
status: historyStatus === 'all' ? undefined : historyStatus,
}),
})
});
return (
<div className="pt-4 space-y-4">
<div className="flex flex-col sm:flex-row gap-2">
<div className="relative flex-1">
<Search className="absolute left-2.5 top-2.5 size-4 text-muted-foreground" />
<div className='pt-4 space-y-4'>
<div className='flex flex-col sm:flex-row gap-2'>
<div className='relative flex-1'>
<Search className='absolute left-2.5 top-2.5 size-4 text-muted-foreground' />
<Input
type="text"
placeholder="输入事件键过滤..."
className="pl-8 text-xs h-9"
type='text'
placeholder='输入事件键过滤...'
className='pl-8 text-xs h-9'
value={historySearch}
onChange={e => {
setHistorySearch(e.target.value)
setHistoryPage(1)
onChange={(e) => {
setHistorySearch(e.target.value);
setHistoryPage(1);
}}
/>
</div>
<div className="w-[150px]">
<div className='w-[150px]'>
<Select
value={historyStatus}
onValueChange={val => {
setHistoryStatus(val)
setHistoryPage(1)
onValueChange={(val) => {
setHistoryStatus(val);
setHistoryPage(1);
}}
>
<SelectTrigger className="text-xs h-9">
<SelectValue placeholder="发送状态" />
<SelectTrigger className='text-xs h-9'>
<SelectValue placeholder='发送状态' />
</SelectTrigger>
<SelectContent>
<SelectItem value="all" className="text-xs">全部状态</SelectItem>
<SelectItem value="success" className="text-xs">发送成功</SelectItem>
<SelectItem value="failed" className="text-xs">发送失败</SelectItem>
<SelectItem value='all' className='text-xs'>
全部状态
</SelectItem>
<SelectItem value='success' className='text-xs'>
发送成功
</SelectItem>
<SelectItem value='failed' className='text-xs'>
发送失败
</SelectItem>
</SelectContent>
</Select>
</div>
<Button
variant="outline"
size="icon"
className="h-9 w-9 shrink-0"
variant='outline'
size='icon'
className='h-9 w-9 shrink-0'
onClick={() => historiesQuery.refetch()}
>
<RefreshCw className={cn("size-3.5", historiesQuery.isFetching && "animate-spin")} />
<RefreshCw
className={cn(
'size-3.5',
historiesQuery.isFetching && 'animate-spin',
)}
/>
</Button>
</div>
<div className="border rounded-lg overflow-hidden">
<div className='border rounded-lg overflow-hidden'>
<Table>
<TableHeader>
<TableRow className="bg-muted/30">
<TableHead className="text-xs font-semibold">事件</TableHead>
<TableHead className="text-xs font-semibold">渠道</TableHead>
<TableHead className="text-xs font-semibold">目标</TableHead>
<TableHead className="text-xs font-semibold">标题</TableHead>
<TableHead className="text-xs font-semibold">等级</TableHead>
<TableHead className="text-xs font-semibold">状态</TableHead>
<TableHead className="text-xs font-semibold">时间</TableHead>
<TableRow className='bg-muted/30'>
<TableHead className='text-xs font-semibold'>事件</TableHead>
<TableHead className='text-xs font-semibold'>渠道</TableHead>
<TableHead className='text-xs font-semibold'>目标</TableHead>
<TableHead className='text-xs font-semibold'>标题</TableHead>
<TableHead className='text-xs font-semibold'>等级</TableHead>
<TableHead className='text-xs font-semibold'>状态</TableHead>
<TableHead className='text-xs font-semibold'>时间</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{historiesQuery.isLoading ? (
<TableRow>
<TableCell colSpan={7} className="h-32">
<LoadingStateWithBorder icon={History} description="加载推送历史记录中..." className="border-0 shadow-none" />
<TableCell colSpan={7} className='h-32'>
<LoadingStateWithBorder
icon={History}
description='加载推送历史记录中...'
className='border-0 shadow-none'
/>
</TableCell>
</TableRow>
) : historiesQuery.isError ? (
<TableRow>
<TableCell colSpan={7} className="h-32">
<ErrorInline error={historiesQuery.error} onRetry={() => historiesQuery.refetch()} className="justify-center" />
<TableCell colSpan={7} className='h-32'>
<ErrorInline
error={historiesQuery.error}
onRetry={() => historiesQuery.refetch()}
className='justify-center'
/>
</TableCell>
</TableRow>
) : (historiesQuery.data?.results ?? []).length === 0 ? (
<TableRow>
<TableCell colSpan={7} className="h-32 text-center text-xs text-muted-foreground">
<TableCell
colSpan={7}
className='h-32 text-center text-xs text-muted-foreground'
>
无推送历史数据
</TableCell>
</TableRow>
) : (
(historiesQuery.data?.results ?? []).map(hist => (
(historiesQuery.data?.results ?? []).map((hist) => (
<TableRow
key={hist.id}
className="hover:bg-muted/10 cursor-pointer transition-colors"
className='hover:bg-muted/10 cursor-pointer transition-colors'
onClick={() => handleRowClick(hist)}
>
<TableCell className="text-xs font-mono font-medium">{hist.event_key}</TableCell>
<TableCell className="text-xs uppercase font-semibold text-muted-foreground">
{hist.channel === "email" ? "邮件" : hist.channel}
<TableCell className='text-xs font-mono font-medium'>
{hist.event_key}
</TableCell>
<TableCell className="text-xs font-mono text-muted-foreground max-w-[120px] truncate" title={hist.target}>
<TableCell className='text-xs uppercase font-semibold text-muted-foreground'>
{hist.channel === 'email' ? '邮件' : hist.channel}
</TableCell>
<TableCell
className='text-xs font-mono text-muted-foreground max-w-[120px] truncate'
title={hist.target}
>
{hist.target}
</TableCell>
<TableCell className="text-xs max-w-[180px] truncate" title={hist.content}>{hist.title}</TableCell>
<TableCell className="text-xs">
<Badge variant={getLevelBadgeVariant(hist.level)} className="text-[10px] font-semibold">
<TableCell
className='text-xs max-w-[180px] truncate'
title={hist.content}
>
{hist.title}
</TableCell>
<TableCell className='text-xs'>
<Badge
variant={getLevelBadgeVariant(hist.level)}
className='text-[10px] font-semibold'
>
{hist.level}
</Badge>
</TableCell>
<TableCell className="text-xs">
<TableCell className='text-xs'>
<Badge
variant={hist.status === "success" ? "secondary" : "destructive"}
className="text-[10px] font-semibold"
variant={
hist.status === 'success' ? 'secondary' : 'destructive'
}
className='text-[10px] font-semibold'
>
{hist.status === "success" ? "成功" : "失败"}
{hist.status === 'success' ? '成功' : '失败'}
</Badge>
{hist.status !== "success" && hist.error_msg && (
<div className="text-[10px] text-muted-foreground font-mono truncate max-w-[180px] mt-1" title={hist.error_msg}>
{hist.status !== 'success' && hist.error_msg && (
<div
className='text-[10px] text-muted-foreground font-mono truncate max-w-[180px] mt-1'
title={hist.error_msg}
>
{hist.error_msg}
</div>
)}
</TableCell>
<TableCell className="text-[11px] text-muted-foreground whitespace-nowrap">
<TableCell className='text-[11px] text-muted-foreground whitespace-nowrap'>
{new Date(hist.created_at).toLocaleString()}
</TableCell>
</TableRow>
@@ -184,27 +252,27 @@ export function HistoriesTab() {
</div>
{historiesQuery.data && historiesQuery.data.total > 0 && (
<div className="flex justify-between items-center text-xs text-muted-foreground">
<div className='flex justify-between items-center text-xs text-muted-foreground'>
<span>共 {historiesQuery.data.total} 条历史记录</span>
<div className="flex items-center gap-1.5">
<div className='flex items-center gap-1.5'>
<Button
variant="outline"
size="icon"
variant='outline'
size='icon'
disabled={historyPage === 1}
onClick={() => setHistoryPage(p => Math.max(1, p - 1))}
className="h-8 w-8"
onClick={() => setHistoryPage((p) => Math.max(1, p - 1))}
className='h-8 w-8'
>
<ChevronLeft className="size-4" />
<ChevronLeft className='size-4' />
</Button>
<span className="text-xs font-medium px-2">{historyPage}</span>
<span className='text-xs font-medium px-2'>{historyPage}</span>
<Button
variant="outline"
size="icon"
variant='outline'
size='icon'
disabled={historyPage * 10 >= historiesQuery.data.total}
onClick={() => setHistoryPage(p => p + 1)}
className="h-8 w-8"
onClick={() => setHistoryPage((p) => p + 1)}
className='h-8 w-8'
>
<ChevronRight className="size-4" />
<ChevronRight className='size-4' />
</Button>
</div>
</div>
@@ -212,10 +280,10 @@ export function HistoriesTab() {
{/* ==================== 对话框:推送详情 ==================== */}
<Dialog open={detailOpen} onOpenChange={setDetailOpen}>
<DialogContent className="sm:max-w-[550px] max-h-[85vh] overflow-y-auto">
<DialogContent className='sm:max-w-[550px] max-h-[85vh] overflow-y-auto'>
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<History className="size-5 text-primary" />
<DialogTitle className='flex items-center gap-2'>
<History className='size-5 text-primary' />
推送通知详情
</DialogTitle>
<DialogDescription>
@@ -224,76 +292,109 @@ export function HistoriesTab() {
</DialogHeader>
{selectedHistory && (
<div className="space-y-4 py-4 text-xs">
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">事件标识</span>
<div className="font-mono bg-muted/40 p-2 rounded border">{selectedHistory.event_key}</div>
<div className='space-y-4 py-4 text-xs'>
<div className='grid grid-cols-2 gap-4'>
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
事件标识
</span>
<div className='font-mono bg-muted/40 p-2 rounded border'>
{selectedHistory.event_key}
</div>
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">发送渠道</span>
<div className="bg-muted/40 p-2 rounded border uppercase font-medium">
{selectedHistory.channel === "email" ? "邮件推送" : selectedHistory.channel}
</div>
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
发送渠道
</span>
<div className='bg-muted/40 p-2 rounded border uppercase font-medium'>
{selectedHistory.channel === 'email'
? '邮件推送'
: selectedHistory.channel}
</div>
</div>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">推送目标</span>
<div className="font-mono bg-muted/40 p-2 rounded border truncate" title={selectedHistory.target}>
<div className='grid grid-cols-2 gap-4'>
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
推送目标
</span>
<div
className='font-mono bg-muted/40 p-2 rounded border truncate'
title={selectedHistory.target}
>
{selectedHistory.target}
</div>
</div>
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">发送时间</span>
<div className="bg-muted/40 p-2 rounded border">
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
发送时间
</span>
<div className='bg-muted/40 p-2 rounded border'>
{new Date(selectedHistory.created_at).toLocaleString()}
</div>
</div>
</div>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">通知等级</span>
<div className='grid grid-cols-2 gap-4'>
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
通知等级
</span>
<div>
<Badge variant={getLevelBadgeVariant(selectedHistory.level)} className="text-[10px] font-semibold py-0.5 px-2">
<Badge
variant={getLevelBadgeVariant(selectedHistory.level)}
className='text-[10px] font-semibold py-0.5 px-2'
>
{selectedHistory.level}
</Badge>
</div>
</div>
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">发送状态</span>
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
发送状态
</span>
<div>
<Badge
variant={selectedHistory.status === "success" ? "secondary" : "destructive"}
className="text-[10px] font-semibold py-0.5 px-2"
variant={
selectedHistory.status === 'success'
? 'secondary'
: 'destructive'
}
className='text-[10px] font-semibold py-0.5 px-2'
>
{selectedHistory.status === "success" ? "成功" : "失败"}
{selectedHistory.status === 'success' ? '成功' : '失败'}
</Badge>
</div>
</div>
</div>
{selectedHistory.status === "failed" && selectedHistory.error_msg && (
<div className="space-y-1.5">
<span className="font-semibold text-destructive block">失败原因</span>
<div className="font-mono text-destructive bg-destructive/10 p-2.5 rounded border border-destructive/20 whitespace-pre-wrap break-all">
{selectedHistory.status === 'failed' &&
selectedHistory.error_msg && (
<div className='space-y-1.5'>
<span className='font-semibold text-destructive block'>
失败原因
</span>
<div className='font-mono text-destructive bg-destructive/10 p-2.5 rounded border border-destructive/20 whitespace-pre-wrap break-all'>
{selectedHistory.error_msg}
</div>
</div>
)}
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">通知标题</span>
<div className="bg-muted/30 p-2.5 rounded border font-medium text-[13px]">
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
通知标题
</span>
<div className='bg-muted/30 p-2.5 rounded border font-medium text-[13px]'>
{selectedHistory.title}
</div>
</div>
<div className="space-y-1.5">
<span className="font-semibold text-muted-foreground block">通知内容</span>
<div className="bg-muted/30 p-3 rounded border whitespace-pre-wrap break-all leading-relaxed font-sans text-xs">
<div className='space-y-1.5'>
<span className='font-semibold text-muted-foreground block'>
通知内容
</span>
<div className='bg-muted/30 p-3 rounded border whitespace-pre-wrap break-all leading-relaxed font-sans text-xs'>
{selectedHistory.content}
</div>
</div>
@@ -301,12 +402,17 @@ export function HistoriesTab() {
)}
<DialogFooter>
<Button variant="outline" size="sm" onClick={() => setDetailOpen(false)} className="h-9 text-xs">
<Button
variant='outline'
size='sm'
onClick={() => setDetailOpen(false)}
className='h-9 text-xs'
>
关闭
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
);
}
File diff suppressed because it is too large Load Diff
+37 -26
View File
@@ -1,65 +1,76 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
"use client"
'use client';
import * as React from "react"
import {motion} from "motion/react"
import {Bell, History, Layers, Settings} from "lucide-react"
import * as React from 'react';
import { motion } from 'motion/react';
import { Bell, History, Layers, Settings } from 'lucide-react';
import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs"
import {EventsTab} from "./components/events-tab"
import {HistoriesTab} from "./components/histories-tab"
import {SettingsTab} from "./components/settings-tab"
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { EventsTab } from './components/events-tab';
import { HistoriesTab } from './components/histories-tab';
import { SettingsTab } from './components/settings-tab';
export default function PushAdminPage() {
const [activeTab, setActiveTab] = React.useState("events")
const [activeTab, setActiveTab] = React.useState('events');
return (
<motion.div
initial={{ opacity: 0, y: 15 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.3, ease: "easeOut" }}
className="w-full py-6 space-y-6"
transition={{ duration: 0.3, ease: 'easeOut' }}
className='w-full py-6 space-y-6'
>
<div className="flex items-center gap-2">
<Bell className="size-5 text-primary" />
<div className='flex items-center gap-2'>
<Bell className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">通知推送管理</h1>
<h1 className='text-2xl font-semibold tracking-tight'>
通知推送管理
</h1>
</div>
</div>
<Tabs value={activeTab} onValueChange={setActiveTab} className="w-full">
<TabsList variant="line" className="w-fit inline-flex gap-8">
<TabsTrigger value="events" className="px-0 pb-2 text-xs font-semibold">
<Layers className="size-3.5 mr-1" />
<Tabs value={activeTab} onValueChange={setActiveTab} className='w-full'>
<TabsList variant='line' className='w-fit inline-flex gap-8'>
<TabsTrigger
value='events'
className='px-0 pb-2 text-xs font-semibold'
>
<Layers className='size-3.5 mr-1' />
事件管理
</TabsTrigger>
<TabsTrigger value="histories" className="px-0 pb-2 text-xs font-semibold">
<History className="size-3.5 mr-1" />
<TabsTrigger
value='histories'
className='px-0 pb-2 text-xs font-semibold'
>
<History className='size-3.5 mr-1' />
通知历史
</TabsTrigger>
<TabsTrigger value="settings" className="px-0 pb-2 text-xs font-semibold">
<Settings className="size-3.5 mr-1" />
<TabsTrigger
value='settings'
className='px-0 pb-2 text-xs font-semibold'
>
<Settings className='size-3.5 mr-1' />
通道管理与设置
</TabsTrigger>
</TabsList>
{/* ==================== 1. 事件管理 TAB ==================== */}
<TabsContent value="events" className="focus-visible:outline-none">
<TabsContent value='events' className='focus-visible:outline-none'>
<EventsTab />
</TabsContent>
{/* ==================== 2. 通知历史 TAB ==================== */}
<TabsContent value="histories" className="focus-visible:outline-none">
<TabsContent value='histories' className='focus-visible:outline-none'>
<HistoriesTab />
</TabsContent>
{/* ==================== 3. 通道管理与设置 TAB ==================== */}
<TabsContent value="settings" className="focus-visible:outline-none">
<TabsContent value='settings' className='focus-visible:outline-none'>
<SettingsTab />
</TabsContent>
</Tabs>
</motion.div>
)
);
}
@@ -1,206 +1,242 @@
"use client"
'use client';
import * as React from "react"
import {useCallback, useEffect, useRef, useState} from "react"
import {toast} from "sonner"
import {Activity, Cpu, Database, HardDrive, Layers, RefreshCw} from "lucide-react"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Button} from "@/components/ui/button"
import {Switch} from "@/components/ui/switch"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue,} from "@/components/ui/select"
import {Badge} from "@/components/ui/badge"
import {Progress} from "@/components/ui/progress"
import {Skeleton} from "@/components/ui/skeleton"
import type {SystemStatus} from "@/lib/services/admin"
import services from "@/lib/services"
import * as React from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { toast } from 'sonner';
import {
Activity,
Cpu,
Database,
HardDrive,
Layers,
RefreshCw,
} from 'lucide-react';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Switch } from '@/components/ui/switch';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Badge } from '@/components/ui/badge';
import { Progress } from '@/components/ui/progress';
import { Skeleton } from '@/components/ui/skeleton';
import type { SystemStatus } from '@/lib/services/admin';
import services from '@/lib/services';
/**
* 格式化数字,每3位加逗号
*/
const formatNumber = (num: number | string) => {
if (num === undefined || num === null) return "0"
return num.toString().replace(/\B(?=(\d{3})+(?!\B))/g, ",")
}
if (num === undefined || num === null) return '0';
return num.toString().replace(/\B(?=(\d{3})+(?!\B))/g, ',');
};
/**
* 运行时系统状态展示与管理组件
*/
export function SystemStatusManager() {
const [status, setStatus] = useState<SystemStatus | null>(null)
const [loading, setLoading] = useState(true)
const [wavelet, setWavelet] = useState(false)
const [autoRefresh, setAutoRefresh] = useState(true)
const [intervalTime, setIntervalTime] = useState("5000") // 默认5秒
const [status, setStatus] = useState<SystemStatus | null>(null);
const [loading, setLoading] = useState(true);
const [wavelet, setWavelet] = useState(false);
const [autoRefresh, setAutoRefresh] = useState(true);
const [intervalTime, setIntervalTime] = useState('5000'); // 默认5秒
const intervalRef = useRef<NodeJS.Timeout | null>(null)
const prevStatusRef = useRef<SystemStatus | null>(null)
const [changedFields, setChangedFields] = useState<Record<string, boolean>>({})
const intervalRef = useRef<NodeJS.Timeout | null>(null);
const prevStatusRef = useRef<SystemStatus | null>(null);
const [changedFields, setChangedFields] = useState<Record<string, boolean>>(
{},
);
// 获取状态数据
const fetchStatus = useCallback(async (isSilent = false) => {
if (!isSilent) setWavelet(true)
if (!isSilent) setWavelet(true);
try {
const data = await services.adminStatus.getSystemStatus()
const data = await services.adminStatus.getSystemStatus();
// 检测变化的字段用于微动画效果
if (prevStatusRef.current) {
const changes: Record<string, boolean> = {}
const changes: Record<string, boolean> = {};
Object.keys(data).forEach((key) => {
const k = key as keyof SystemStatus
const k = key as keyof SystemStatus;
if (prevStatusRef.current && prevStatusRef.current[k] !== data[k]) {
changes[k] = true
changes[k] = true;
}
})
setChangedFields(changes)
});
setChangedFields(changes);
// 1秒后清除变化状态
setTimeout(() => {
setChangedFields({})
}, 1000)
setChangedFields({});
}, 1000);
}
setStatus(data)
prevStatusRef.current = data
setStatus(data);
prevStatusRef.current = data;
} catch (err) {
toast.error("获取系统状态失败", {
description: err instanceof Error ? err.message : "请求时发生未知错误"
})
toast.error('获取系统状态失败', {
description: err instanceof Error ? err.message : '请求时发生未知错误',
});
} finally {
setLoading(false)
setWavelet(false)
setLoading(false);
setWavelet(false);
}
}, [])
}, []);
// 轮询逻辑
useEffect(() => {
// 首次加载
fetchStatus()
}, [fetchStatus])
fetchStatus();
}, [fetchStatus]);
useEffect(() => {
if (intervalRef.current) {
clearInterval(intervalRef.current)
clearInterval(intervalRef.current);
}
if (autoRefresh) {
const time = parseInt(intervalTime, 10)
const time = parseInt(intervalTime, 10);
intervalRef.current = setInterval(() => {
fetchStatus(true)
}, time)
fetchStatus(true);
}, time);
}
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current)
clearInterval(intervalRef.current);
}
}
}, [autoRefresh, intervalTime, fetchStatus])
};
}, [autoRefresh, intervalTime, fetchStatus]);
// 计算内存分配百分比 (Alloc / Sys)
const getMemoryUsagePercent = () => {
if (!status) return 0
if (!status) return 0;
const parseSizeToBytes = (sizeStr: string): number => {
const parts = sizeStr.split(" ")
if (parts.length < 2) return 0
const value = parseFloat(parts[0])
const unit = parts[1].toLowerCase()
const parts = sizeStr.split(' ');
if (parts.length < 2) return 0;
const value = parseFloat(parts[0]);
const unit = parts[1].toLowerCase();
if (unit.startsWith("gib")) return value * 1024 * 1024 * 1024
if (unit.startsWith("mib")) return value * 1024 * 1024
if (unit.startsWith("kib")) return value * 1024
return value
}
if (unit.startsWith('gib')) return value * 1024 * 1024 * 1024;
if (unit.startsWith('mib')) return value * 1024 * 1024;
if (unit.startsWith('kib')) return value * 1024;
return value;
};
const alloc = parseSizeToBytes(status.alloc)
const sys = parseSizeToBytes(status.sys)
if (sys === 0) return 0
return Math.min(Math.round((alloc / sys) * 100), 100)
}
const alloc = parseSizeToBytes(status.alloc);
const sys = parseSizeToBytes(status.sys);
if (sys === 0) return 0;
return Math.min(Math.round((alloc / sys) * 100), 100);
};
// 闪烁动画类
const getPulseClass = (field: string) => {
return changedFields[field] ? "animate-pulse text-primary font-bold transition-all duration-300 scale-105 inline-block" : "transition-all duration-300"
}
return changedFields[field]
? 'animate-pulse text-primary font-bold transition-all duration-300 scale-105 inline-block'
: 'transition-all duration-300';
};
const RenderMetricRow = ({ label, value, field }: { label: string; value: string | number; field?: string }) => (
<div className="flex items-center justify-between py-2 border-b border-dashed border-border/40 last:border-b-0 hover:bg-muted/30 px-2 rounded-sm transition-colors duration-150">
<span className="text-xs font-medium text-muted-foreground">{label}</span>
<span className={`text-xs font-mono font-medium ${field ? getPulseClass(field) : ""}`}>
{typeof value === "number" ? formatNumber(value) : value}
const RenderMetricRow = ({
label,
value,
field,
}: {
label: string;
value: string | number;
field?: string;
}) => (
<div className='flex items-center justify-between py-2 border-b border-dashed border-border/40 last:border-b-0 hover:bg-muted/30 px-2 rounded-sm transition-colors duration-150'>
<span className='text-xs font-medium text-muted-foreground'>{label}</span>
<span
className={`text-xs font-mono font-medium ${field ? getPulseClass(field) : ''}`}
>
{typeof value === 'number' ? formatNumber(value) : value}
</span>
</div>
)
);
if (loading) {
return (
<div className="space-y-6 p-1">
<div className="flex items-center justify-between border-b border-border/40 pb-4">
<div className="space-y-1">
<Skeleton className="h-6 w-32" />
<Skeleton className="h-4 w-48" />
<div className='space-y-6 p-1'>
<div className='flex items-center justify-between border-b border-border/40 pb-4'>
<div className='space-y-1'>
<Skeleton className='h-6 w-32' />
<Skeleton className='h-4 w-48' />
</div>
<div className="flex items-center gap-3">
<Skeleton className="h-8 w-24" />
<Skeleton className="h-8 w-16" />
<Skeleton className="h-8 w-8" />
<div className='flex items-center gap-3'>
<Skeleton className='h-8 w-24' />
<Skeleton className='h-8 w-16' />
<Skeleton className='h-8 w-8' />
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<Skeleton className="h-48 w-full" />
<Skeleton className="h-48 w-full" />
<Skeleton className="h-48 w-full" />
<Skeleton className="h-48 w-full" />
<Skeleton className="h-48 w-full" />
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6'>
<Skeleton className='h-48 w-full' />
<Skeleton className='h-48 w-full' />
<Skeleton className='h-48 w-full' />
<Skeleton className='h-48 w-full' />
<Skeleton className='h-48 w-full' />
</div>
</div>
)
);
}
return (
<div className="py-6 px-1 space-y-6">
<div className='py-6 px-1 space-y-6'>
{/* 顶部控制栏 */}
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between pb-4 gap-4">
<div className="flex items-center gap-2">
<Activity className="size-5 text-primary" />
<div className='flex flex-col sm:flex-row sm:items-center sm:justify-between pb-4 gap-4'>
<div className='flex items-center gap-2'>
<Activity className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">系统状态</h1>
<h1 className='text-2xl font-semibold tracking-tight'>系统状态</h1>
</div>
</div>
<div className="flex items-center flex-wrap gap-4 bg-muted/30 p-1.5 rounded-lg border border-border/40 backdrop-blur-sm">
<div className="flex items-center gap-2 px-1">
<div className='flex items-center flex-wrap gap-4 bg-muted/30 p-1.5 rounded-lg border border-border/40 backdrop-blur-sm'>
<div className='flex items-center gap-2 px-1'>
<Switch
id="auto-refresh"
id='auto-refresh'
checked={autoRefresh}
onCheckedChange={setAutoRefresh}
/>
<label htmlFor="auto-refresh" className="text-xs font-medium cursor-pointer select-none">
<label
htmlFor='auto-refresh'
className='text-xs font-medium cursor-pointer select-none'
>
自动刷新
</label>
</div>
{autoRefresh && (
<Select value={intervalTime} onValueChange={setIntervalTime}>
<SelectTrigger className="h-7 w-20 text-[11px] bg-background border-border/40">
<SelectTrigger className='h-7 w-20 text-[11px] bg-background border-border/40'>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="1000">1s</SelectItem>
<SelectItem value="2000">2s</SelectItem>
<SelectItem value="5000">5s</SelectItem>
<SelectItem value="10000">10s</SelectItem>
<SelectItem value='1000'>1s</SelectItem>
<SelectItem value='2000'>2s</SelectItem>
<SelectItem value='5000'>5s</SelectItem>
<SelectItem value='10000'>10s</SelectItem>
</SelectContent>
</Select>
)}
<Button
size="sm"
variant="secondary"
className="h-7 text-[11px] gap-1.5"
size='sm'
variant='secondary'
className='h-7 text-[11px] gap-1.5'
onClick={() => fetchStatus()}
disabled={wavelet}
>
<RefreshCw className={`size-3 ${wavelet ? "animate-spin" : ""}`} />
<RefreshCw className={`size-3 ${wavelet ? 'animate-spin' : ''}`} />
刷新
</Button>
</div>
@@ -209,130 +245,277 @@ export function SystemStatusManager() {
{status && (
<>
{/* 主网格排版 */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6'>
{/* 1. 服务概览 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<div className="space-y-0.5">
<CardTitle className="text-sm font-semibold">服务概览</CardTitle>
<CardDescription className="text-[10px]">服务的基础生命指标</CardDescription>
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<div className='space-y-0.5'>
<CardTitle className='text-sm font-semibold'>
服务概览
</CardTitle>
<CardDescription className='text-[10px]'>
服务的基础生命指标
</CardDescription>
</div>
<Badge variant="secondary" className="p-1 rounded-full bg-primary/10 text-primary border-none">
<Activity className="size-4" />
<Badge
variant='secondary'
className='p-1 rounded-full bg-primary/10 text-primary border-none'
>
<Activity className='size-4' />
</Badge>
</CardHeader>
<CardContent className="space-y-1">
<RenderMetricRow label="服务运行时间" value={status.uptime} field="uptime" />
<RenderMetricRow label="当前 Goroutines 数量" value={status.num_goroutine} field="num_goroutine" />
<RenderMetricRow label="Heap 对象数量" value={status.heap_objects} field="heap_objects" />
<CardContent className='space-y-1'>
<RenderMetricRow
label='服务运行时间'
value={status.uptime}
field='uptime'
/>
<RenderMetricRow
label='当前 Goroutines 数量'
value={status.num_goroutine}
field='num_goroutine'
/>
<RenderMetricRow
label='Heap 对象数量'
value={status.heap_objects}
field='heap_objects'
/>
</CardContent>
</Card>
{/* 2. 内存统计 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<div className="space-y-0.5">
<CardTitle className="text-sm font-semibold">内存统计</CardTitle>
<CardDescription className="text-[10px]">主内存消耗概览</CardDescription>
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<div className='space-y-0.5'>
<CardTitle className='text-sm font-semibold'>
内存统计
</CardTitle>
<CardDescription className='text-[10px]'>
主内存消耗概览
</CardDescription>
</div>
<Badge variant="secondary" className="p-1 rounded-full bg-primary/10 text-primary border-none">
<Cpu className="size-4" />
<Badge
variant='secondary'
className='p-1 rounded-full bg-primary/10 text-primary border-none'
>
<Cpu className='size-4' />
</Badge>
</CardHeader>
<CardContent className="space-y-3">
<div className="space-y-1">
<RenderMetricRow label="当前内存使用量" value={status.alloc} field="alloc" />
<RenderMetricRow label="所有已分配的内存" value={status.total_alloc} field="total_alloc" />
<RenderMetricRow label="内存占用量" value={status.sys} field="sys" />
<RenderMetricRow label="下次 GC 内存回收量" value={status.next_gc} field="next_gc" />
<CardContent className='space-y-3'>
<div className='space-y-1'>
<RenderMetricRow
label='当前内存使用量'
value={status.alloc}
field='alloc'
/>
<RenderMetricRow
label='所有已分配的内存'
value={status.total_alloc}
field='total_alloc'
/>
<RenderMetricRow
label='内存占用量'
value={status.sys}
field='sys'
/>
<RenderMetricRow
label='下次 GC 内存回收量'
value={status.next_gc}
field='next_gc'
/>
</div>
{/* 物理内存水位比例 */}
<div className="space-y-1 px-1 pt-1">
<div className="flex justify-between text-[10px] text-muted-foreground">
<div className='space-y-1 px-1 pt-1'>
<div className='flex justify-between text-[10px] text-muted-foreground'>
<span>当前使用率 (Alloc / Sys)</span>
<span className="font-mono">{getMemoryUsagePercent()}%</span>
<span className='font-mono'>
{getMemoryUsagePercent()}%
</span>
</div>
<Progress value={getMemoryUsagePercent()} className="h-1.5" />
<Progress value={getMemoryUsagePercent()} className='h-1.5' />
</div>
</CardContent>
</Card>
{/* 3. 堆/栈详情 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<div className="space-y-0.5">
<CardTitle className="text-sm font-semibold">堆/栈详情</CardTitle>
<CardDescription className="text-[10px]">运行时堆栈内存空间细节</CardDescription>
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<div className='space-y-0.5'>
<CardTitle className='text-sm font-semibold'>
堆/栈详情
</CardTitle>
<CardDescription className='text-[10px]'>
运行时堆栈内存空间细节
</CardDescription>
</div>
<Badge variant="secondary" className="p-1 rounded-full bg-primary/10 text-primary border-none">
<Database className="size-4" />
<Badge
variant='secondary'
className='p-1 rounded-full bg-primary/10 text-primary border-none'
>
<Database className='size-4' />
</Badge>
</CardHeader>
<CardContent className="space-y-1">
<RenderMetricRow label="当前 Heap 内存使用量" value={status.heap_alloc} field="heap_alloc" />
<RenderMetricRow label="Heap 内存占用量" value={status.heap_sys} field="heap_sys" />
<RenderMetricRow label="Heap 内存空闲量" value={status.heap_idle} field="heap_idle" />
<RenderMetricRow label="正在使用的 Heap 内存" value={status.heap_inuse} field="heap_inuse" />
<RenderMetricRow label="已释放的 Heap 内存" value={status.heap_released} field="heap_released" />
<RenderMetricRow label="启动 Stack 使用量" value={status.stack_inuse} field="stack_inuse" />
<RenderMetricRow label="已分配的 Stack 内存" value={status.stack_sys} field="stack_sys" />
<CardContent className='space-y-1'>
<RenderMetricRow
label='当前 Heap 内存使用量'
value={status.heap_alloc}
field='heap_alloc'
/>
<RenderMetricRow
label='Heap 内存占用量'
value={status.heap_sys}
field='heap_sys'
/>
<RenderMetricRow
label='Heap 内存空闲量'
value={status.heap_idle}
field='heap_idle'
/>
<RenderMetricRow
label='正在使用的 Heap 内存'
value={status.heap_inuse}
field='heap_inuse'
/>
<RenderMetricRow
label='已释放的 Heap 内存'
value={status.heap_released}
field='heap_released'
/>
<RenderMetricRow
label='启动 Stack 使用量'
value={status.stack_inuse}
field='stack_inuse'
/>
<RenderMetricRow
label='已分配的 Stack 内存'
value={status.stack_sys}
field='stack_sys'
/>
</CardContent>
</Card>
{/* 4. 底层组件与结构体 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<div className="space-y-0.5">
<CardTitle className="text-sm font-semibold">底层及结构体内存</CardTitle>
<CardDescription className="text-[10px]">运行时底层管理结构体开销</CardDescription>
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<div className='space-y-0.5'>
<CardTitle className='text-sm font-semibold'>
底层及结构体内存
</CardTitle>
<CardDescription className='text-[10px]'>
运行时底层管理结构体开销
</CardDescription>
</div>
<Badge variant="secondary" className="p-1 rounded-full bg-primary/10 text-primary border-none">
<HardDrive className="size-4" />
<Badge
variant='secondary'
className='p-1 rounded-full bg-primary/10 text-primary border-none'
>
<HardDrive className='size-4' />
</Badge>
</CardHeader>
<CardContent className="space-y-1">
<RenderMetricRow label="MSpan 结构内存使用量" value={status.mspan_inuse} field="mspan_inuse" />
<RenderMetricRow label="已分配的 MSpan 结构内存" value={status.mspan_sys} field="mspan_sys" />
<RenderMetricRow label="MCache 结构内存使用量" value={status.mcache_inuse} field="mcache_inuse" />
<RenderMetricRow label="已分配的 MCache 结构内存" value={status.mcache_sys} field="mcache_sys" />
<RenderMetricRow label="已分配的剖析哈希表内存" value={status.buck_hash_sys} field="buck_hash_sys" />
<RenderMetricRow label="已分配的 GC 元数据内存" value={status.gc_sys} field="gc_sys" />
<RenderMetricRow label="其它已分配的系统内存" value={status.other_sys} field="other_sys" />
<CardContent className='space-y-1'>
<RenderMetricRow
label='MSpan 结构内存使用量'
value={status.mspan_inuse}
field='mspan_inuse'
/>
<RenderMetricRow
label='已分配的 MSpan 结构内存'
value={status.mspan_sys}
field='mspan_sys'
/>
<RenderMetricRow
label='MCache 结构内存使用量'
value={status.mcache_inuse}
field='mcache_inuse'
/>
<RenderMetricRow
label='已分配的 MCache 结构内存'
value={status.mcache_sys}
field='mcache_sys'
/>
<RenderMetricRow
label='已分配的剖析哈希表内存'
value={status.buck_hash_sys}
field='buck_hash_sys'
/>
<RenderMetricRow
label='已分配的 GC 元数据内存'
value={status.gc_sys}
field='gc_sys'
/>
<RenderMetricRow
label='其它已分配的系统内存'
value={status.other_sys}
field='other_sys'
/>
</CardContent>
</Card>
{/* 5. 垃圾回收与分配计数 */}
<Card className="shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300 md:col-span-2">
<CardHeader className="flex flex-row items-center justify-between pb-2 space-y-0">
<div className="space-y-0.5">
<CardTitle className="text-sm font-semibold">垃圾回收与分配计数</CardTitle>
<CardDescription className="text-[10px]">GC 历史数据与分配频次</CardDescription>
<Card className='shadow-sm border-border/40 bg-card/50 backdrop-blur-md hover:border-primary/20 transition-all duration-300 md:col-span-2'>
<CardHeader className='flex flex-row items-center justify-between pb-2 space-y-0'>
<div className='space-y-0.5'>
<CardTitle className='text-sm font-semibold'>
垃圾回收与分配计数
</CardTitle>
<CardDescription className='text-[10px]'>
GC 历史数据与分配频次
</CardDescription>
</div>
<Badge variant="secondary" className="p-1 rounded-full bg-primary/10 text-primary border-none">
<Layers className="size-4" />
<Badge
variant='secondary'
className='p-1 rounded-full bg-primary/10 text-primary border-none'
>
<Layers className='size-4' />
</Badge>
</CardHeader>
<CardContent>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-x-6">
<div className='grid grid-cols-1 sm:grid-cols-2 gap-x-6'>
<div>
<RenderMetricRow label="GC 执行次数" value={status.num_gc} field="num_gc" />
<RenderMetricRow label="距离上次 GC 时间" value={status.last_gc_time} field="last_gc_time" />
<RenderMetricRow label="GC 暂停时间总量" value={status.pause_total_ns} field="pause_total_ns" />
<RenderMetricRow label="上次 GC 暂停时间" value={status.last_pause} field="last_pause" />
<RenderMetricRow
label='GC 执行次数'
value={status.num_gc}
field='num_gc'
/>
<RenderMetricRow
label='距离上次 GC 时间'
value={status.last_gc_time}
field='last_gc_time'
/>
<RenderMetricRow
label='GC 暂停时间总量'
value={status.pause_total_ns}
field='pause_total_ns'
/>
<RenderMetricRow
label='上次 GC 暂停时间'
value={status.last_pause}
field='last_pause'
/>
</div>
<div>
<RenderMetricRow label="内存分配次数" value={status.mallocs} field="mallocs" />
<RenderMetricRow label="内存释放次数" value={status.frees} field="frees" />
<RenderMetricRow label="指针查找次数" value={status.lookups} field="lookups" />
<RenderMetricRow
label='内存分配次数'
value={status.mallocs}
field='mallocs'
/>
<RenderMetricRow
label='内存释放次数'
value={status.frees}
field='frees'
/>
<RenderMetricRow
label='指针查找次数'
value={status.lookups}
field='lookups'
/>
</div>
</div>
</CardContent>
</Card>
</div>
</>
)}
</div>
)
);
}
@@ -1,155 +1,204 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
"use client"
'use client';
import dynamic from "next/dynamic"
import {useEffect, useMemo} from "react"
import {useQuery} from "@tanstack/react-query"
import {Loader2, Settings} from "lucide-react"
import {useRouter, useSearchParams} from "next/navigation"
import {motion} from "motion/react"
import dynamic from 'next/dynamic';
import { useEffect, useMemo } from 'react';
import { useQuery } from '@tanstack/react-query';
import { Loader2, Settings } from 'lucide-react';
import { useRouter, useSearchParams } from 'next/navigation';
import { motion } from 'motion/react';
import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs"
import {useAuth} from "@/components/providers/auth-provider"
import services from "@/lib/services"
import type {SystemConfig} from "@/lib/services/admin"
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { useAuth } from '@/components/providers/auth-provider';
import services from '@/lib/services';
import type { SystemConfig } from '@/lib/services/admin';
const tabFallback = (
<div className="h-64 animate-pulse rounded-lg border border-border/40 bg-muted/20" />
)
<div className='h-64 animate-pulse rounded-lg border border-border/40 bg-muted/20' />
);
const SecurityTab = dynamic(
() => import("@/components/common/settings/security-tab").then((mod) => mod.SecurityTab),
() =>
import('@/components/common/settings/security-tab').then(
(mod) => mod.SecurityTab,
),
{ loading: () => tabFallback },
)
);
const OperationTab = dynamic(
() => import("@/components/common/settings/operation-tab").then((mod) => mod.OperationTab),
() =>
import('@/components/common/settings/operation-tab').then(
(mod) => mod.OperationTab,
),
{ loading: () => tabFallback },
)
);
const SystemTab = dynamic(
() => import("@/components/common/settings/system-tab").then((mod) => mod.SystemTab),
() =>
import('@/components/common/settings/system-tab').then(
(mod) => mod.SystemTab,
),
{ loading: () => tabFallback },
)
);
const OtherTab = dynamic(
() => import("@/components/common/settings/other-tab").then((mod) => mod.OtherTab),
() =>
import('@/components/common/settings/other-tab').then(
(mod) => mod.OtherTab,
),
{ loading: () => tabFallback },
)
);
const InfoTab = dynamic(
() => import("@/components/common/settings/info-tab").then((mod) => mod.InfoTab),
() =>
import('@/components/common/settings/info-tab').then((mod) => mod.InfoTab),
{ loading: () => tabFallback },
)
);
const SystemStatusManager = dynamic(
() => import("./components/system-status").then((mod) => mod.SystemStatusManager),
() =>
import('./components/system-status').then((mod) => mod.SystemStatusManager),
{ loading: () => tabFallback },
)
);
function systemConfigMap(configs: SystemConfig[]) {
return configs.reduce<Record<string, SystemConfig>>((accumulator, config) => {
accumulator[config.key] = config
return accumulator
}, {})
accumulator[config.key] = config;
return accumulator;
}, {});
}
export function AdminSettingsPageClient() {
const { user, loading } = useAuth()
const router = useRouter()
const searchParams = useSearchParams()
const { user, loading } = useAuth();
const router = useRouter();
const searchParams = useSearchParams();
const activeTab = useMemo(() => {
const rawTab = searchParams.get("tab")
const validTabs = ["security", "operation", "system", "other", "status", "info"]
return rawTab && validTabs.includes(rawTab) ? rawTab : "security"
}, [searchParams])
const rawTab = searchParams.get('tab');
const validTabs = [
'security',
'operation',
'system',
'other',
'status',
'info',
];
return rawTab && validTabs.includes(rawTab) ? rawTab : 'security';
}, [searchParams]);
const handleTabChange = (value: string) => {
router.push(`/admin/settings?tab=${value}`)
}
router.push(`/admin/settings?tab=${value}`);
};
const systemConfigsQuery = useQuery({
queryKey: ["admin", "system-configs"],
queryFn: () => services.adminSystemConfig.listSystemConfigs("system"),
queryKey: ['admin', 'system-configs'],
queryFn: () => services.adminSystemConfig.listSystemConfigs('system'),
enabled: !!user?.is_admin,
})
});
const configs = useMemo(
() => systemConfigMap(systemConfigsQuery.data ?? []),
[systemConfigsQuery.data],
)
);
useEffect(() => {
if (!loading && (!user || !user.is_admin)) {
router.replace("/settings/profile")
router.replace('/settings/profile');
}
}, [user, loading, router])
}, [user, loading, router]);
if (loading || !user || !user.is_admin) {
return (
<div className="flex items-center justify-center min-h-[400px]">
<Loader2 className="size-6 animate-spin text-primary" />
<div className='flex items-center justify-center min-h-[400px]'>
<Loader2 className='size-6 animate-spin text-primary' />
</div>
)
);
}
return (
<motion.div
initial={{ opacity: 0, y: 15 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.35, ease: "easeOut" }}
className="py-6 space-y-6 w-full"
transition={{ duration: 0.35, ease: 'easeOut' }}
className='py-6 space-y-6 w-full'
>
<div className="flex items-center gap-2">
<Settings className="size-5 text-primary" />
<div className='flex items-center gap-2'>
<Settings className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">系统设置</h1>
<h1 className='text-2xl font-semibold tracking-tight'>系统设置</h1>
</div>
</div>
<Tabs value={activeTab} onValueChange={handleTabChange} className="w-full">
<TabsList variant="line" className="w-fit inline-flex gap-8 mb-6">
<TabsTrigger value="security" className="px-0 pb-2 text-xs font-semibold">
<Tabs
value={activeTab}
onValueChange={handleTabChange}
className='w-full'
>
<TabsList variant='line' className='w-fit inline-flex gap-8 mb-6'>
<TabsTrigger
value='security'
className='px-0 pb-2 text-xs font-semibold'
>
安全设置
</TabsTrigger>
<TabsTrigger value="operation" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='operation'
className='px-0 pb-2 text-xs font-semibold'
>
业务设置
</TabsTrigger>
<TabsTrigger value="system" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='system'
className='px-0 pb-2 text-xs font-semibold'
>
系统设置
</TabsTrigger>
<TabsTrigger value="other" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='other'
className='px-0 pb-2 text-xs font-semibold'
>
其他设置
</TabsTrigger>
<TabsTrigger value="status" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='status'
className='px-0 pb-2 text-xs font-semibold'
>
系统状态
</TabsTrigger>
<TabsTrigger value="info" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger value='info' className='px-0 pb-2 text-xs font-semibold'>
系统信息
</TabsTrigger>
</TabsList>
<TabsContent value="security" className="focus-visible:outline-none">
<SecurityTab configs={configs} systemConfigsQuery={systemConfigsQuery} />
<TabsContent value='security' className='focus-visible:outline-none'>
<SecurityTab
configs={configs}
systemConfigsQuery={systemConfigsQuery}
/>
</TabsContent>
<TabsContent value="operation" className="focus-visible:outline-none">
<OperationTab configs={configs} systemConfigsQuery={systemConfigsQuery} />
<TabsContent value='operation' className='focus-visible:outline-none'>
<OperationTab
configs={configs}
systemConfigsQuery={systemConfigsQuery}
/>
</TabsContent>
<TabsContent value="system" className="focus-visible:outline-none">
<SystemTab configs={configs} systemConfigsQuery={systemConfigsQuery} />
<TabsContent value='system' className='focus-visible:outline-none'>
<SystemTab
configs={configs}
systemConfigsQuery={systemConfigsQuery}
/>
</TabsContent>
<TabsContent value="status" className="focus-visible:outline-none">
<TabsContent value='status' className='focus-visible:outline-none'>
<SystemStatusManager />
</TabsContent>
<TabsContent value="other" className="focus-visible:outline-none">
<TabsContent value='other' className='focus-visible:outline-none'>
<OtherTab configs={configs} />
</TabsContent>
<TabsContent value="info" className="focus-visible:outline-none">
<TabsContent value='info' className='focus-visible:outline-none'>
<InfoTab />
</TabsContent>
</Tabs>
</motion.div>
)
);
}
+6 -6
View File
@@ -1,20 +1,20 @@
// Copyright 2026 Arctel.net
// SPDX-License-Identifier: Apache-2.0
import {Suspense} from "react"
import {Loader2} from "lucide-react"
import {AdminSettingsPageClient} from "./page-client"
import { Suspense } from 'react';
import { Loader2 } from 'lucide-react';
import { AdminSettingsPageClient } from './page-client';
export default function AdminSettingsPage() {
return (
<Suspense
fallback={
<div className="flex items-center justify-center min-h-[400px]">
<Loader2 className="size-6 animate-spin text-primary" />
<div className='flex items-center justify-center min-h-[400px]'>
<Loader2 className='size-6 animate-spin text-primary' />
</div>
}
>
<AdminSettingsPageClient />
</Suspense>
)
);
}
+180 -95
View File
@@ -1,16 +1,18 @@
"use client"
'use client';
import * as React from "react"
import {Input} from "@/components/ui/input"
import {Switch} from "@/components/ui/switch"
import {ManageDetailPanel, ManagePage} from "@/components/common/general/manage-pannel"
import {Tabs, TabsList, TabsTrigger} from "@/components/ui/tabs"
import {ShieldCheck} from "lucide-react"
import {formatDateTime} from "@/lib/utils"
import type {SystemConfig} from "@/lib/services/admin"
import {AdminProvider, useAdmin} from "@/contexts/admin-context"
import * as React from 'react';
import { Input } from '@/components/ui/input';
import { Switch } from '@/components/ui/switch';
import {
ManageDetailPanel,
ManagePage,
} from '@/components/common/general/manage-pannel';
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { ShieldCheck } from 'lucide-react';
import { formatDateTime } from '@/lib/utils';
import type { SystemConfig } from '@/lib/services/admin';
import { AdminProvider, useAdmin } from '@/contexts/admin-context';
/**
* 系统配置
@@ -38,43 +40,51 @@ function SystemConfigDetailPanel({
editData,
onEditDataChange,
onSave,
saving
saving,
}: {
config: SystemConfig | null
editData: Partial<SystemConfig>
onEditDataChange: (field: keyof SystemConfig, value: SystemConfig[keyof SystemConfig]) => void
onSave: () => void
saving: boolean
config: SystemConfig | null;
editData: Partial<SystemConfig>;
onEditDataChange: (
field: keyof SystemConfig,
value: SystemConfig[keyof SystemConfig],
) => void;
onSave: () => void;
saving: boolean;
}) {
return (
<ManageDetailPanel
isEmpty={!config}
onSave={onSave}
saving={saving}
>
<div className="grid grid-cols-1 gap-0">
<div className="border border-dashed rounded-lg">
<div className="px-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0">
<label className="text-xs font-medium text-muted-foreground">配置键</label>
<p className="text-xs text-muted-foreground font-mono">{config?.key}</p>
<ManageDetailPanel isEmpty={!config} onSave={onSave} saving={saving}>
<div className='grid grid-cols-1 gap-0'>
<div className='border border-dashed rounded-lg'>
<div className='px-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0'>
<label className='text-xs font-medium text-muted-foreground'>
配置键
</label>
<p className='text-xs text-muted-foreground font-mono'>
{config?.key}
</p>
</div>
<div className="pl-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0">
<label className="text-xs font-medium text-muted-foreground">配置值</label>
<div className="flex gap-1 w-[90%] justify-end items-center pr-3">
<div className='pl-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0'>
<label className='text-xs font-medium text-muted-foreground'>
配置值
</label>
<div className='flex gap-1 w-[90%] justify-end items-center pr-3'>
{config?.key.endsWith('_enabled') ? (
<Switch
checked={editData.value !== undefined ? editData.value === 'true' : (config?.value === 'true')}
checked={
editData.value !== undefined
? editData.value === 'true'
: config?.value === 'true'
}
onCheckedChange={(checked) => {
onEditDataChange('value', checked ? 'true' : 'false')
onEditDataChange('value', checked ? 'true' : 'false');
}}
/>
) : (
<Input
type={
config?.key && (
config.key.endsWith('_limit') ||
config?.key &&
(config.key.endsWith('_limit') ||
config.key.endsWith('_minutes') ||
config.key.endsWith('_days') ||
config.key.endsWith('_seconds') ||
@@ -85,79 +95,100 @@ function SystemConfigDetailPanel({
config.key.includes('max_') ||
config.key.includes('min_') ||
config.key.includes('limit') ||
config.key.includes('count')
) ? "number" : "text"
config.key.includes('count'))
? 'number'
: 'text'
}
step='1'
min='0'
value={
editData.value !== undefined
? editData.value
: config?.value || ''
}
placeholder={
editData.value === undefined && !config?.value ? '必需' : ''
}
step="1"
min="0"
value={editData.value !== undefined ? editData.value : (config?.value || '')}
placeholder={editData.value === undefined && !config?.value ? '必需' : ''}
onChange={(e) => {
const value = e.target.value
const value = e.target.value;
if (value === '') {
onEditDataChange('value', '')
return
onEditDataChange('value', '');
return;
}
onEditDataChange('value', value)
onEditDataChange('value', value);
}}
className="!text-[12px] text-right h-4 rounded-none border-none shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:!text-[12px] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-inner-spin-button]"
className='!text-[12px] text-right h-4 rounded-none border-none shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:!text-[12px] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-inner-spin-button]'
style={{
MozAppearance: 'textfield'
MozAppearance: 'textfield',
}}
/>
)}
</div>
</div>
<div className="pl-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0">
<label className="text-xs font-medium text-muted-foreground">配置描述</label>
<div className="flex gap-1 w-[90%]">
<div className='pl-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0'>
<label className='text-xs font-medium text-muted-foreground'>
配置描述
</label>
<div className='flex gap-1 w-[90%]'>
<Input
type="text"
value={editData.description !== undefined ? editData.description : (config?.description || '')}
placeholder="可选描述"
type='text'
value={
editData.description !== undefined
? editData.description
: config?.description || ''
}
placeholder='可选描述'
onChange={(e) => {
const value = e.target.value
onEditDataChange('description', value)
const value = e.target.value;
onEditDataChange('description', value);
}}
className="!text-[12px] text-right h-4 rounded-none border-none shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:!text-[12px]"
className='!text-[12px] text-right h-4 rounded-none border-none shadow-none focus-visible:ring-0 focus-visible:ring-offset-0 placeholder:!text-[12px]'
/>
</div>
</div>
<div className="px-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0">
<label className="text-xs font-medium text-muted-foreground">公共可见</label>
<div className='px-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0'>
<label className='text-xs font-medium text-muted-foreground'>
公共可见
</label>
<Switch
checked={(editData.visibility ?? config?.visibility ?? 0) === 1}
onCheckedChange={(checked) => {
onEditDataChange('visibility', checked ? 1 : 0)
onEditDataChange('visibility', checked ? 1 : 0);
}}
/>
</div>
<div className="px-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0">
<label className="text-xs font-medium text-muted-foreground">配置类型</label>
<span className={`text-[11px] px-1.5 py-0.5 rounded font-medium ${
<div className='px-3 py-2 flex items-center justify-between border-b border-dashed last:border-b-0'>
<label className='text-xs font-medium text-muted-foreground'>
配置类型
</label>
<span
className={`text-[11px] px-1.5 py-0.5 rounded font-medium ${
config?.type === 'system'
? 'bg-blue-500/10 text-blue-600 dark:bg-blue-500/20 dark:text-blue-400'
: 'bg-indigo-500/10 text-indigo-600 dark:bg-indigo-500/20 dark:text-indigo-400'
}`}>
}`}
>
{config?.type === 'system' ? '系统配置' : '业务配置'}
</span>
</div>
<div className="px-3 py-2 flex items-center justify-between">
<label className="text-xs font-medium text-muted-foreground">创建时间</label>
<p className="text-xs text-muted-foreground">{config ? formatDateTime(config.created_at) : ''}</p>
<div className='px-3 py-2 flex items-center justify-between'>
<label className='text-xs font-medium text-muted-foreground'>
创建时间
</label>
<p className='text-xs text-muted-foreground'>
{config ? formatDateTime(config.created_at) : ''}
</p>
</div>
</div>
</div>
</ManageDetailPanel>
)
);
}
/**
* 系统配置管理组件
*
@@ -173,34 +204,39 @@ export function SystemConfigs() {
systemConfigsLoading: loading,
systemConfigsError: error,
refetchSystemConfigs,
updateSystemConfig
} = useAdmin()
updateSystemConfig,
} = useAdmin();
const [activeTab, setActiveTab] = React.useState<'system' | 'business'>('business')
const [activeTab, setActiveTab] = React.useState<'system' | 'business'>(
'business',
);
React.useEffect(() => {
refetchSystemConfigs(activeTab)
}, [activeTab, refetchSystemConfigs])
refetchSystemConfigs(activeTab);
}, [activeTab, refetchSystemConfigs]);
const getInitialEditData = (config: SystemConfig) => ({
value: config.value,
visibility: config.visibility,
description: config.description
})
description: config.description,
});
const handleSave = async (config: SystemConfig, editData: Partial<SystemConfig>) => {
if (!config) return
const handleSave = async (
config: SystemConfig,
editData: Partial<SystemConfig>,
) => {
if (!config) return;
await updateSystemConfig(config.key, {
value: editData.value ?? config.value,
visibility: editData.visibility ?? config.visibility,
description: editData.description ?? config.description
})
}
description: editData.description ?? config.description,
});
};
return (
<ManagePage<SystemConfig>
title="系统配置"
title='系统配置'
icon={ShieldCheck}
data={configs}
loading={loading}
@@ -209,23 +245,72 @@ export function SystemConfigs() {
getInitialEditData={getInitialEditData}
onSave={handleSave}
getId={(config) => config.key}
emptyDescription="未发现系统配置"
loadingDescription="配置加载中"
emptyDescription='未发现系统配置'
loadingDescription='配置加载中'
headerExtra={
<Tabs value={activeTab} onValueChange={(val) => setActiveTab(val as 'system' | 'business')} className="w-[180px]">
<TabsList className="grid w-full grid-cols-2 h-8">
<TabsTrigger value="business" className="text-[11px] h-7">业务配置</TabsTrigger>
<TabsTrigger value="system" className="text-[11px] h-7">系统配置</TabsTrigger>
<Tabs
value={activeTab}
onValueChange={(val) => setActiveTab(val as 'system' | 'business')}
className='w-[180px]'
>
<TabsList className='grid w-full grid-cols-2 h-8'>
<TabsTrigger value='business' className='text-[11px] h-7'>
业务配置
</TabsTrigger>
<TabsTrigger value='system' className='text-[11px] h-7'>
系统配置
</TabsTrigger>
</TabsList>
</Tabs>
}
columns={[
{ header: "配置键", cell: (item) => <span className="font-mono font-medium">{item.key}</span>, width: "200px" },
{ header: "配置值", cell: (item) => <span className="truncate max-w-[120px] inline-block" title={item.value}>{item.value}</span>, width: "120px" },
{ header: "公共可见", cell: (item) => <span>{item.visibility === 1 ? "可见" : "不可见"}</span>, width: "80px" },
{ header: "描述", cell: (item) => <span className="truncate max-w-[200px] inline-block text-muted-foreground" title={item.description}>{item.description}</span>, width: "200px" },
{
header: '配置键',
cell: (item) => (
<span className='font-mono font-medium'>{item.key}</span>
),
width: '200px',
},
{
header: '配置值',
cell: (item) => (
<span
className='truncate max-w-[120px] inline-block'
title={item.value}
>
{item.value}
</span>
),
width: '120px',
},
{
header: '公共可见',
cell: (item) => (
<span>{item.visibility === 1 ? '可见' : '不可见'}</span>
),
width: '80px',
},
{
header: '描述',
cell: (item) => (
<span
className='truncate max-w-[200px] inline-block text-muted-foreground'
title={item.description}
>
{item.description}
</span>
),
width: '200px',
},
]}
renderDetail={({ selected, hovered, editData, onEditDataChange, onSave, saving }) => (
renderDetail={({
selected,
hovered,
editData,
onEditDataChange,
onSave,
saving,
}) => (
<SystemConfigDetailPanel
config={selected || hovered}
editData={editData}
@@ -235,7 +320,7 @@ export function SystemConfigs() {
/>
)}
/>
)
);
}
export default function SystemConfigPage() {
@@ -243,5 +328,5 @@ export default function SystemConfigPage() {
<AdminProvider>
<SystemConfigs />
</AdminProvider>
)
);
}
@@ -1,201 +1,278 @@
"use client"
'use client';
import {useMemo, useState} from "react"
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"
import {toast} from "sonner"
import {format} from "date-fns"
import {Activity, ChevronLeft, ChevronRight, RefreshCw, RotateCcw} from "lucide-react"
import { useMemo, useState } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import { format } from 'date-fns';
import {
Activity,
ChevronLeft,
ChevronRight,
RefreshCw,
RotateCcw,
} from 'lucide-react';
import services from "@/lib/services"
import type {TaskExecution, TaskExecutionStatus, TaskMeta} from "@/lib/services/admin"
import {ErrorInline} from "@/components/layout/error"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {EmptyStateWithBorder} from "@/components/layout/empty"
import {Badge} from "@/components/ui/badge"
import {Button} from "@/components/ui/button"
import {Label} from "@/components/ui/label"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select"
import {Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle} from "@/components/ui/sheet"
import {Spinner} from "@/components/ui/spinner"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import services from '@/lib/services';
import type {
TaskExecution,
TaskExecutionStatus,
TaskMeta,
} from '@/lib/services/admin';
import { ErrorInline } from '@/components/layout/error';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { EmptyStateWithBorder } from '@/components/layout/empty';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import {
Sheet,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
} from '@/components/ui/sheet';
import { Spinner } from '@/components/ui/spinner';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
const STATUS_LABELS: Record<TaskExecutionStatus, string> = {
pending: "等待中",
running: "执行中",
succeeded: "成功",
failed: "失败",
}
pending: '等待中',
running: '执行中',
succeeded: '成功',
failed: '失败',
};
const TRIGGER_LABELS: Record<string, string> = {
system: "系统",
manual: "手动",
retry: "重试",
schedule: "定时",
}
system: '系统',
manual: '手动',
retry: '重试',
schedule: '定时',
};
const PAGE_SIZE = 10
const PAGE_SIZE = 10;
function formatDateTime(value?: string | null) {
if (!value) return "-"
const date = new Date(value)
if (Number.isNaN(date.getTime())) return value
return format(date, "yyyy-MM-dd HH:mm:ss")
if (!value) return '-';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return format(date, 'yyyy-MM-dd HH:mm:ss');
}
function formatDuration(duration: number) {
if (!duration) return "-"
if (duration < 1000) return `${ duration }ms`
return `${ (duration / 1000).toFixed(2) }s`
if (!duration) return '-';
if (duration < 1000) return `${duration}ms`;
return `${(duration / 1000).toFixed(2)}s`;
}
function statusVariant(status: TaskExecutionStatus) {
if (status === "failed") return "destructive"
if (status === "succeeded") return "secondary"
return "outline"
if (status === 'failed') return 'destructive';
if (status === 'succeeded') return 'secondary';
return 'outline';
}
export function TaskExecutionsManager() {
const queryClient = useQueryClient()
const [executionsPage, setExecutionsPage] = useState(1)
const [executionStatus, setExecutionStatus] = useState<TaskExecutionStatus | "all">("all")
const [executionTaskType, setExecutionTaskType] = useState<string>("all")
const [selectedExecutionId, setSelectedExecutionId] = useState<string | null>(null)
const [executionPreview, setExecutionPreview] = useState<TaskExecution | null>(null)
const [detailOpen, setDetailOpen] = useState(false)
const queryClient = useQueryClient();
const [executionsPage, setExecutionsPage] = useState(1);
const [executionStatus, setExecutionStatus] = useState<
TaskExecutionStatus | 'all'
>('all');
const [executionTaskType, setExecutionTaskType] = useState<string>('all');
const [selectedExecutionId, setSelectedExecutionId] = useState<string | null>(
null,
);
const [executionPreview, setExecutionPreview] =
useState<TaskExecution | null>(null);
const [detailOpen, setDetailOpen] = useState(false);
const taskTypesQuery = useQuery({
queryKey: ["admin", "task-types"],
queryKey: ['admin', 'task-types'],
queryFn: () => services.adminTask.getTaskTypes(),
})
});
const executionsQuery = useQuery({
queryKey: ["admin", "task-executions", executionsPage, executionStatus, executionTaskType],
queryFn: () => services.adminTask.listTaskExecutions({
queryKey: [
'admin',
'task-executions',
executionsPage,
executionStatus,
executionTaskType,
],
queryFn: () =>
services.adminTask.listTaskExecutions({
page: executionsPage,
page_size: PAGE_SIZE,
status: executionStatus === "all" ? undefined : executionStatus,
task_type: executionTaskType === "all" ? undefined : executionTaskType,
status: executionStatus === 'all' ? undefined : executionStatus,
task_type: executionTaskType === 'all' ? undefined : executionTaskType,
}),
})
});
const detailQuery = useQuery({
queryKey: ["admin", "task-execution", selectedExecutionId],
queryKey: ['admin', 'task-execution', selectedExecutionId],
queryFn: () => services.adminTask.getTaskExecution(selectedExecutionId!),
enabled: detailOpen && !!selectedExecutionId,
})
});
const retryMutation = useMutation({
mutationFn: (id: string) => services.adminTask.retryTaskExecution(id),
onSuccess: (taskID) => {
toast.success("任务已重新下发", {
toast.success('任务已重新下发', {
description: `新任务 ID:${taskID}`,
})
void queryClient.invalidateQueries({ queryKey: ["admin", "task-executions"] })
});
void queryClient.invalidateQueries({
queryKey: ['admin', 'task-executions'],
});
},
onError: (err: Error) => {
toast.error("任务重试失败", {
description: err.message || "未知错误",
})
toast.error('任务重试失败', {
description: err.message || '未知错误',
});
},
})
});
const taskTypes: TaskMeta[] = taskTypesQuery.data ?? []
const executions = executionsQuery.data?.items ?? []
const executionsTotal = executionsQuery.data?.total ?? 0
const executionsLoading = executionsQuery.isPending || executionsQuery.isFetching
const executionsError = executionsQuery.isError ? executionsQuery.error : null
const selectedExecution = detailQuery.data ?? executionPreview
const detailLoading = detailQuery.isFetching
const taskTypes: TaskMeta[] = taskTypesQuery.data ?? [];
const executions = executionsQuery.data?.items ?? [];
const executionsTotal = executionsQuery.data?.total ?? 0;
const executionsLoading =
executionsQuery.isPending || executionsQuery.isFetching;
const executionsError = executionsQuery.isError
? executionsQuery.error
: null;
const selectedExecution = detailQuery.data ?? executionPreview;
const detailLoading = detailQuery.isFetching;
const totalPages = useMemo(
() => Math.max(1, Math.ceil(executionsTotal / PAGE_SIZE)),
[executionsTotal],
)
);
const openExecutionDetail = (execution: TaskExecution) => {
setExecutionPreview(execution)
setSelectedExecutionId(execution.id)
setDetailOpen(true)
}
setExecutionPreview(execution);
setSelectedExecutionId(execution.id);
setDetailOpen(true);
};
const handleStatusChange = (value: TaskExecutionStatus | "all") => {
setExecutionStatus(value)
setExecutionsPage(1)
}
const handleStatusChange = (value: TaskExecutionStatus | 'all') => {
setExecutionStatus(value);
setExecutionsPage(1);
};
const handleTaskTypeChange = (value: string) => {
setExecutionTaskType(value)
setExecutionsPage(1)
}
setExecutionTaskType(value);
setExecutionsPage(1);
};
return (
<div className="space-y-6">
<div className='space-y-6'>
<br />
<div className="flex flex-col gap-3 border-b border-border pb-3 sm:flex-row sm:items-center sm:justify-between">
<div className="flex flex-wrap items-center gap-2">
<Select value={executionStatus} onValueChange={(value) => handleStatusChange(value as TaskExecutionStatus | "all")}>
<SelectTrigger size="sm" className="w-[120px]">
<SelectValue placeholder="状态" />
<div className='flex flex-col gap-3 border-b border-border pb-3 sm:flex-row sm:items-center sm:justify-between'>
<div className='flex flex-wrap items-center gap-2'>
<Select
value={executionStatus}
onValueChange={(value) =>
handleStatusChange(value as TaskExecutionStatus | 'all')
}
>
<SelectTrigger size='sm' className='w-[120px]'>
<SelectValue placeholder='状态' />
</SelectTrigger>
<SelectContent>
<SelectItem value="all">全部状态</SelectItem>
<SelectItem value="pending">等待中</SelectItem>
<SelectItem value="running">执行中</SelectItem>
<SelectItem value="succeeded">成功</SelectItem>
<SelectItem value="failed">失败</SelectItem>
<SelectItem value='all'>全部状态</SelectItem>
<SelectItem value='pending'>等待中</SelectItem>
<SelectItem value='running'>执行中</SelectItem>
<SelectItem value='succeeded'>成功</SelectItem>
<SelectItem value='failed'>失败</SelectItem>
</SelectContent>
</Select>
<Select value={executionTaskType} onValueChange={handleTaskTypeChange}>
<SelectTrigger size="sm" className="w-[180px]">
<SelectValue placeholder="任务类型" />
<Select
value={executionTaskType}
onValueChange={handleTaskTypeChange}
>
<SelectTrigger size='sm' className='w-[180px]'>
<SelectValue placeholder='任务类型' />
</SelectTrigger>
<SelectContent>
<SelectItem value="all">全部任务</SelectItem>
<SelectItem value='all'>全部任务</SelectItem>
{taskTypes.map((task) => (
<SelectItem key={task.type} value={task.type}>{task.name || task.type}</SelectItem>
<SelectItem key={task.type} value={task.type}>
{task.name || task.type}
</SelectItem>
))}
</SelectContent>
</Select>
<Button variant="outline" size="sm" onClick={() => void executionsQuery.refetch()} disabled={executionsLoading}>
{executionsLoading ? <Spinner className="size-4" /> : <RefreshCw className="size-4" />}
<Button
variant='outline'
size='sm'
onClick={() => void executionsQuery.refetch()}
disabled={executionsLoading}
>
{executionsLoading ? (
<Spinner className='size-4' />
) : (
<RefreshCw className='size-4' />
)}
刷新
</Button>
</div>
</div>
{executionsError ? (
<div className="p-8 border border-dashed rounded-lg bg-card">
<ErrorInline error={executionsError} onRetry={() => void executionsQuery.refetch()} className="justify-center" />
<div className='p-8 border border-dashed rounded-lg bg-card'>
<ErrorInline
error={executionsError}
onRetry={() => void executionsQuery.refetch()}
className='justify-center'
/>
</div>
) : executionsLoading && executions.length === 0 ? (
<LoadingStateWithBorder icon={Activity} description="加载任务执行记录中..." />
<LoadingStateWithBorder
icon={Activity}
description='加载任务执行记录中...'
/>
) : executions.length === 0 ? (
<EmptyStateWithBorder icon={Activity} description="暂无任务执行记录" />
<EmptyStateWithBorder icon={Activity} description='暂无任务执行记录' />
) : (
<div className="rounded-lg border bg-card">
<Table className="min-w-[900px]">
<div className='rounded-lg border bg-card'>
<Table className='min-w-[900px]'>
<TableHeader>
<TableRow className="hover:bg-transparent">
<TableHead className="w-[180px]">任务</TableHead>
<TableHead className="w-[100px]">状态</TableHead>
<TableHead className="w-[110px]">触发</TableHead>
<TableHead className="w-[120px]">重试</TableHead>
<TableHead className="w-[120px]">耗时</TableHead>
<TableHead className="min-w-[220px]">结果/错误</TableHead>
<TableHead className="w-[170px]">创建时间</TableHead>
<TableRow className='hover:bg-transparent'>
<TableHead className='w-[180px]'>任务</TableHead>
<TableHead className='w-[100px]'>状态</TableHead>
<TableHead className='w-[110px]'>触发</TableHead>
<TableHead className='w-[120px]'>重试</TableHead>
<TableHead className='w-[120px]'>耗时</TableHead>
<TableHead className='min-w-[220px]'>结果/错误</TableHead>
<TableHead className='w-[170px]'>创建时间</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{executions.map((execution) => (
<TableRow
key={execution.id}
className="cursor-pointer"
className='cursor-pointer'
onClick={() => openExecutionDetail(execution)}
>
<TableCell>
<div className="flex flex-col gap-1">
<span className="text-sm font-medium">{execution.task_name || execution.task_type}</span>
<span className="font-mono text-[11px] text-muted-foreground">{execution.task_id}</span>
<div className='flex flex-col gap-1'>
<span className='text-sm font-medium'>
{execution.task_name || execution.task_type}
</span>
<span className='font-mono text-[11px] text-muted-foreground'>
{execution.task_id}
</span>
</div>
</TableCell>
<TableCell>
@@ -204,18 +281,21 @@ export function TaskExecutionsManager() {
</Badge>
</TableCell>
<TableCell>
<Badge variant="outline">{TRIGGER_LABELS[execution.triggered_by] || execution.triggered_by}</Badge>
<Badge variant='outline'>
{TRIGGER_LABELS[execution.triggered_by] ||
execution.triggered_by}
</Badge>
</TableCell>
<TableCell className="font-mono text-xs text-muted-foreground">
<TableCell className='font-mono text-xs text-muted-foreground'>
{execution.retry_count}/{execution.max_retry}
</TableCell>
<TableCell className="font-mono text-xs text-muted-foreground">
<TableCell className='font-mono text-xs text-muted-foreground'>
{formatDuration(execution.duration)}
</TableCell>
<TableCell className="max-w-[320px] truncate text-xs text-muted-foreground">
{execution.error_message || execution.result || "-"}
<TableCell className='max-w-[320px] truncate text-xs text-muted-foreground'>
{execution.error_message || execution.result || '-'}
</TableCell>
<TableCell className="font-mono text-[11px] text-muted-foreground">
<TableCell className='font-mono text-[11px] text-muted-foreground'>
{formatDateTime(execution.created_at)}
</TableCell>
</TableRow>
@@ -225,144 +305,186 @@ export function TaskExecutionsManager() {
</div>
)}
<div className="flex items-center justify-between">
<div className="text-xs text-muted-foreground">
<div className='flex items-center justify-between'>
<div className='text-xs text-muted-foreground'>
共 {executionsTotal} 条,当前第 {executionsPage}/{totalPages} 页
</div>
<div className="flex items-center gap-2">
<div className='flex items-center gap-2'>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
onClick={() => setExecutionsPage((page) => Math.max(1, page - 1))}
disabled={executionsPage <= 1 || executionsLoading}
>
<ChevronLeft className="size-4" />
<ChevronLeft className='size-4' />
上一页
</Button>
<Button
variant="outline"
size="sm"
onClick={() => setExecutionsPage((page) => Math.min(totalPages, page + 1))}
variant='outline'
size='sm'
onClick={() =>
setExecutionsPage((page) => Math.min(totalPages, page + 1))
}
disabled={executionsPage >= totalPages || executionsLoading}
>
下一页
<ChevronRight className="size-4" />
<ChevronRight className='size-4' />
</Button>
</div>
</div>
<Sheet open={detailOpen} onOpenChange={setDetailOpen}>
<SheetContent className="w-full p-0 sm:max-w-[640px]">
<SheetHeader className="border-b">
<SheetContent className='w-full p-0 sm:max-w-[640px]'>
<SheetHeader className='border-b'>
<SheetTitle>任务执行详情</SheetTitle>
<SheetDescription>
{selectedExecution?.task_name || selectedExecution?.task_type || "任务记录"}
{selectedExecution?.task_name ||
selectedExecution?.task_type ||
'任务记录'}
</SheetDescription>
</SheetHeader>
<div className="flex-1 overflow-y-auto px-4 pb-4">
<div className='flex-1 overflow-y-auto px-4 pb-4'>
{detailLoading && !selectedExecution ? (
<LoadingStateWithBorder icon={Activity} description="加载任务详情中..." />
<LoadingStateWithBorder
icon={Activity}
description='加载任务详情中...'
/>
) : selectedExecution ? (
<div className="space-y-5 py-4">
<div className="grid grid-cols-2 gap-3">
<div className="rounded-lg border p-3">
<div className="text-xs text-muted-foreground">状态</div>
<div className="mt-2">
<div className='space-y-5 py-4'>
<div className='grid grid-cols-2 gap-3'>
<div className='rounded-lg border p-3'>
<div className='text-xs text-muted-foreground'>状态</div>
<div className='mt-2'>
<Badge variant={statusVariant(selectedExecution.status)}>
{STATUS_LABELS[selectedExecution.status] || selectedExecution.status}
{STATUS_LABELS[selectedExecution.status] ||
selectedExecution.status}
</Badge>
</div>
</div>
<div className="rounded-lg border p-3">
<div className="text-xs text-muted-foreground">触发来源</div>
<div className="mt-2 text-sm font-medium">
{TRIGGER_LABELS[selectedExecution.triggered_by] || selectedExecution.triggered_by}
<div className='rounded-lg border p-3'>
<div className='text-xs text-muted-foreground'>
触发来源
</div>
<div className='mt-2 text-sm font-medium'>
{TRIGGER_LABELS[selectedExecution.triggered_by] ||
selectedExecution.triggered_by}
</div>
</div>
<div className="rounded-lg border p-3">
<div className="text-xs text-muted-foreground">重试次数</div>
<div className="mt-2 font-mono text-sm">
{selectedExecution.retry_count}/{selectedExecution.max_retry}
<div className='rounded-lg border p-3'>
<div className='text-xs text-muted-foreground'>
重试次数
</div>
<div className='mt-2 font-mono text-sm'>
{selectedExecution.retry_count}/
{selectedExecution.max_retry}
</div>
</div>
<div className="rounded-lg border p-3">
<div className="text-xs text-muted-foreground">耗时</div>
<div className="mt-2 font-mono text-sm">
<div className='rounded-lg border p-3'>
<div className='text-xs text-muted-foreground'>耗时</div>
<div className='mt-2 font-mono text-sm'>
{formatDuration(selectedExecution.duration)}
</div>
</div>
</div>
<div className="grid gap-2">
<div className='grid gap-2'>
<Label>任务标识</Label>
<div className="rounded-md border bg-muted/40 px-3 py-2 font-mono text-xs break-all">
<div className='rounded-md border bg-muted/40 px-3 py-2 font-mono text-xs break-all'>
{selectedExecution.task_id}
</div>
</div>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
<div className="grid gap-2">
<div className='grid grid-cols-1 gap-3 sm:grid-cols-2'>
<div className='grid gap-2'>
<Label>创建时间</Label>
<div className="font-mono text-xs text-muted-foreground">{formatDateTime(selectedExecution.created_at)}</div>
<div className='font-mono text-xs text-muted-foreground'>
{formatDateTime(selectedExecution.created_at)}
</div>
<div className="grid gap-2">
</div>
<div className='grid gap-2'>
<Label>开始时间</Label>
<div className="font-mono text-xs text-muted-foreground">{formatDateTime(selectedExecution.started_at)}</div>
<div className='font-mono text-xs text-muted-foreground'>
{formatDateTime(selectedExecution.started_at)}
</div>
<div className="grid gap-2">
</div>
<div className='grid gap-2'>
<Label>结束时间</Label>
<div className="font-mono text-xs text-muted-foreground">{formatDateTime(selectedExecution.finished_at)}</div>
<div className='font-mono text-xs text-muted-foreground'>
{formatDateTime(selectedExecution.finished_at)}
</div>
<div className="grid gap-2">
</div>
<div className='grid gap-2'>
<Label>更新时间</Label>
<div className="font-mono text-xs text-muted-foreground">{formatDateTime(selectedExecution.updated_at)}</div>
<div className='font-mono text-xs text-muted-foreground'>
{formatDateTime(selectedExecution.updated_at)}
</div>
</div>
</div>
<div className="grid gap-2">
<div className='grid gap-2'>
<Label>执行结果</Label>
<div className="min-h-10 rounded-md border bg-muted/30 px-3 py-2 text-sm whitespace-pre-wrap break-all">
{selectedExecution.result || "-"}
<div className='min-h-10 rounded-md border bg-muted/30 px-3 py-2 text-sm whitespace-pre-wrap break-all'>
{selectedExecution.result || '-'}
</div>
</div>
{selectedExecution.error_message && (
<div className="grid gap-2">
<div className='grid gap-2'>
<Label>错误信息</Label>
<div className="rounded-md border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive whitespace-pre-wrap break-all">
<div className='rounded-md border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive whitespace-pre-wrap break-all'>
{selectedExecution.error_message}
</div>
</div>
)}
<div className="grid gap-2">
<div className='grid gap-2'>
<Label>Payload</Label>
<pre className="max-h-40 overflow-auto rounded-md border bg-muted/40 p-3 text-xs leading-relaxed">
{selectedExecution.payload || "{}"}
<pre className='max-h-40 overflow-auto rounded-md border bg-muted/40 p-3 text-xs leading-relaxed'>
{selectedExecution.payload || '{}'}
</pre>
</div>
<div className="grid gap-2">
<div className='grid gap-2'>
<Label>执行日志</Label>
<pre className="min-h-48 max-h-[420px] overflow-auto rounded-md border bg-muted/40 p-3 text-xs leading-relaxed whitespace-pre-wrap">
{selectedExecution.log || "暂无日志"}
<pre className='min-h-48 max-h-[420px] overflow-auto rounded-md border bg-muted/40 p-3 text-xs leading-relaxed whitespace-pre-wrap'>
{selectedExecution.log || '暂无日志'}
</pre>
</div>
</div>
) : (
<EmptyStateWithBorder icon={Activity} description="未选择任务记录" />
<EmptyStateWithBorder
icon={Activity}
description='未选择任务记录'
/>
)}
</div>
<SheetFooter className="border-t">
<Button variant="outline" onClick={() => void detailQuery.refetch()} disabled={!selectedExecutionId || detailLoading}>
{detailLoading ? <Spinner className="size-4" /> : <RefreshCw className="size-4" />}
<SheetFooter className='border-t'>
<Button
variant='outline'
onClick={() => void detailQuery.refetch()}
disabled={!selectedExecutionId || detailLoading}
>
{detailLoading ? (
<Spinner className='size-4' />
) : (
<RefreshCw className='size-4' />
)}
刷新详情
</Button>
{selectedExecution && selectedExecution.status === "failed" && selectedExecution.retryable && (
<Button onClick={() => retryMutation.mutate(selectedExecution.id)} disabled={retryMutation.isPending}>
{retryMutation.isPending ? <Spinner className="size-4" /> : <RotateCcw className="size-4" />}
{selectedExecution &&
selectedExecution.status === 'failed' &&
selectedExecution.retryable && (
<Button
onClick={() => retryMutation.mutate(selectedExecution.id)}
disabled={retryMutation.isPending}
>
{retryMutation.isPending ? (
<Spinner className='size-4' />
) : (
<RotateCcw className='size-4' />
)}
重试任务
</Button>
)}
@@ -370,5 +492,5 @@ export function TaskExecutionsManager() {
</SheetContent>
</Sheet>
</div>
)
);
}
@@ -1,116 +1,150 @@
"use client"
'use client';
import {useCallback, useEffect, useState} from "react"
import {toast} from "sonner"
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Textarea} from "@/components/ui/textarea"
import {Switch} from "@/components/ui/switch"
import {Spinner} from "@/components/ui/spinner"
import {Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Calendar as CalendarIcon, Clock, Info, Layers, Play} from "lucide-react"
import { useCallback, useEffect, useState } from 'react';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Switch } from '@/components/ui/switch';
import { Spinner } from '@/components/ui/spinner';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
Calendar as CalendarIcon,
Clock,
Info,
Layers,
Play,
} from 'lucide-react';
import type {DispatchTaskRequest, TaskMeta} from "@/lib/services/admin"
import services from "@/lib/services"
import {buildTaskPayload} from "@/lib/task-param-utils"
import {ErrorInline} from "@/components/layout/error"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {EmptyStateWithBorder} from "@/components/layout/empty"
import {Badge} from "@/components/ui/badge"
import {cn} from "@/lib/utils"
import type { DispatchTaskRequest, TaskMeta } from '@/lib/services/admin';
import services from '@/lib/services';
import { buildTaskPayload } from '@/lib/task-param-utils';
import { ErrorInline } from '@/components/layout/error';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { EmptyStateWithBorder } from '@/components/layout/empty';
import { Badge } from '@/components/ui/badge';
import { cn } from '@/lib/utils';
import {format} from "date-fns"
import {zhCN} from "date-fns/locale"
import {Calendar} from "@/components/ui/calendar"
import {Popover, PopoverContent, PopoverTrigger,} from "@/components/ui/popover"
import { format } from 'date-fns';
import { zhCN } from 'date-fns/locale';
import { Calendar } from '@/components/ui/calendar';
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover';
const TASK_CONFIGS: Record<string, { icon: React.ComponentType<{ className?: string }>, color: string, gradient: string }> = {
'order_sync': {
const TASK_CONFIGS: Record<
string,
{
icon: React.ComponentType<{ className?: string }>;
color: string;
gradient: string;
}
> = {
order_sync: {
icon: Layers,
color: "text-blue-600 dark:text-blue-400",
gradient: "from-blue-500/10 via-blue-500/5 to-transparent border-blue-200/50 dark:border-blue-800/50 hover:border-blue-400 dark:hover:border-blue-500",
color: 'text-blue-600 dark:text-blue-400',
gradient:
'from-blue-500/10 via-blue-500/5 to-transparent border-blue-200/50 dark:border-blue-800/50 hover:border-blue-400 dark:hover:border-blue-500',
},
'user_gamification': {
user_gamification: {
icon: Play,
color: "text-amber-600 dark:text-amber-400",
gradient: "from-amber-500/10 via-amber-500/5 to-transparent border-amber-200/50 dark:border-amber-800/50 hover:border-amber-400 dark:hover:border-amber-500",
color: 'text-amber-600 dark:text-amber-400',
gradient:
'from-amber-500/10 via-amber-500/5 to-transparent border-amber-200/50 dark:border-amber-800/50 hover:border-amber-400 dark:hover:border-amber-500',
},
'dispute_auto_refund': {
dispute_auto_refund: {
icon: Clock,
color: "text-rose-600 dark:text-rose-400",
gradient: "from-rose-500/10 via-rose-500/5 to-transparent border-rose-200/50 dark:border-rose-800/50 hover:border-rose-400 dark:hover:border-rose-500",
}
}
color: 'text-rose-600 dark:text-rose-400',
gradient:
'from-rose-500/10 via-rose-500/5 to-transparent border-rose-200/50 dark:border-rose-800/50 hover:border-rose-400 dark:hover:border-rose-500',
},
};
const DEFAULT_TASK_CONFIG = {
icon: Layers,
color: "text-zinc-600 dark:text-zinc-400",
gradient: "from-zinc-500/10 via-zinc-500/5 to-transparent border-zinc-200/50 dark:border-zinc-800/50 hover:border-zinc-400 dark:hover:border-zinc-500",
}
color: 'text-zinc-600 dark:text-zinc-400',
gradient:
'from-zinc-500/10 via-zinc-500/5 to-transparent border-zinc-200/50 dark:border-zinc-800/50 hover:border-zinc-400 dark:hover:border-zinc-500',
};
function DatePickerWithTime({ date, setDate }: { date: Date | undefined, setDate: (date: Date | undefined) => void }) {
const timeString = date ? format(date, "HH:mm:ss") : ""
function DatePickerWithTime({
date,
setDate,
}: {
date: Date | undefined;
setDate: (date: Date | undefined) => void;
}) {
const timeString = date ? format(date, 'HH:mm:ss') : '';
const handleDateSelect = (newDate: Date | undefined) => {
if (!newDate) {
setDate(undefined)
return
setDate(undefined);
return;
}
const updated = new Date(newDate)
const updated = new Date(newDate);
if (date) {
updated.setHours(date.getHours())
updated.setMinutes(date.getMinutes())
updated.setSeconds(date.getSeconds())
updated.setHours(date.getHours());
updated.setMinutes(date.getMinutes());
updated.setSeconds(date.getSeconds());
} else {
updated.setHours(0, 0, 0, 0)
updated.setHours(0, 0, 0, 0);
}
setDate(updated)
}
setDate(updated);
};
const handleTimeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newTime = e.target.value
if (!newTime) return
const newTime = e.target.value;
if (!newTime) return;
if (date) {
const [hours, minutes, seconds] = newTime.split(':').map(Number)
const updated = new Date(date)
updated.setHours(hours || 0)
updated.setMinutes(minutes || 0)
updated.setSeconds(seconds || 0)
setDate(updated)
const [hours, minutes, seconds] = newTime.split(':').map(Number);
const updated = new Date(date);
updated.setHours(hours || 0);
updated.setMinutes(minutes || 0);
updated.setSeconds(seconds || 0);
setDate(updated);
} else {
const today = new Date()
const [hours, minutes, seconds] = newTime.split(':').map(Number)
today.setHours(hours || 0)
today.setMinutes(minutes || 0)
today.setSeconds(seconds || 0)
setDate(today)
}
const today = new Date();
const [hours, minutes, seconds] = newTime.split(':').map(Number);
today.setHours(hours || 0);
today.setMinutes(minutes || 0);
today.setSeconds(seconds || 0);
setDate(today);
}
};
return (
<div className="flex items-center gap-2">
<div className="flex-1">
<div className='flex items-center gap-2'>
<div className='flex-1'>
<Popover>
<PopoverTrigger asChild>
<Button
variant={"outline"}
variant={'outline'}
className={cn(
"w-full justify-start text-left font-normal text-xs h-8",
!date && "text-muted-foreground"
'w-full justify-start text-left font-normal text-xs h-8',
!date && 'text-muted-foreground',
)}
>
<CalendarIcon className="mr-1 size-3" />
{date ? format(date, "yyyy-MM-dd") : <span>选择日期</span>}
<CalendarIcon className='mr-1 size-3' />
{date ? format(date, 'yyyy-MM-dd') : <span>选择日期</span>}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0" align="start">
<PopoverContent className='w-auto p-0' align='start'>
<Calendar
mode="single"
mode='single'
selected={date}
onSelect={handleDateSelect}
locale={zhCN}
@@ -118,197 +152,218 @@ function DatePickerWithTime({ date, setDate }: { date: Date | undefined, setDate
</PopoverContent>
</Popover>
</div>
<div className="w-[120px]">
<div className='w-[120px]'>
<Input
type="time"
step="1"
type='time'
step='1'
value={timeString}
onChange={handleTimeChange}
className="text-xs font-mono"
className='text-xs font-mono'
/>
</div>
</div>
)
);
}
export function TaskManager() {
const [loading, setLoading] = useState(false)
const [error, setError] = useState<Error | null>(null)
const [taskTypes, setTaskTypes] = useState<TaskMeta[]>([])
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [taskTypes, setTaskTypes] = useState<TaskMeta[]>([]);
const [dispatching, setDispatching] = useState(false)
const [selectedTaskType, setSelectedTaskType] = useState<string | null>(null)
const [dialogOpen, setDialogOpen] = useState(false)
const [dispatching, setDispatching] = useState(false);
const [selectedTaskType, setSelectedTaskType] = useState<string | null>(null);
const [dialogOpen, setDialogOpen] = useState(false);
const [startTime, setStartTime] = useState<Date | undefined>(undefined)
const [endTime, setEndTime] = useState<Date | undefined>(undefined)
const [userId, setUserId] = useState("")
const [paramValues, setParamValues] = useState<Record<string, string>>({})
const [startTime, setStartTime] = useState<Date | undefined>(undefined);
const [endTime, setEndTime] = useState<Date | undefined>(undefined);
const [userId, setUserId] = useState('');
const [paramValues, setParamValues] = useState<Record<string, string>>({});
const fetchTaskTypes = useCallback(async () => {
try {
setLoading(true)
setError(null)
const data = await services.adminTask.getTaskTypes()
setTaskTypes(data)
setLoading(true);
setError(null);
const data = await services.adminTask.getTaskTypes();
setTaskTypes(data);
} catch (err) {
setError(err instanceof Error ? err : new Error('加载任务类型失败'))
setError(err instanceof Error ? err : new Error('加载任务类型失败'));
} finally {
setLoading(false)
setLoading(false);
}
}, [])
}, []);
useEffect(() => {
fetchTaskTypes()
}, [fetchTaskTypes])
fetchTaskTypes();
}, [fetchTaskTypes]);
useEffect(() => {
if (selectedTaskType) {
const targetTask = taskTypes.find(t => t.type === selectedTaskType)
const targetTask = taskTypes.find((t) => t.type === selectedTaskType);
if (targetTask?.params) {
const initialValues: Record<string, string> = {}
targetTask.params.forEach(p => {
initialValues[p.name] = ""
})
setParamValues(initialValues)
const initialValues: Record<string, string> = {};
targetTask.params.forEach((p) => {
initialValues[p.name] = '';
});
setParamValues(initialValues);
} else {
setParamValues({})
setParamValues({});
}
}
}, [selectedTaskType, taskTypes])
}, [selectedTaskType, taskTypes]);
const handleDispatch = async () => {
if (!selectedTaskType) return
if (!selectedTaskType) return;
try {
setDispatching(true)
setDispatching(true);
const targetTask = taskTypes.find(t => t.type === selectedTaskType)
const targetTask = taskTypes.find((t) => t.type === selectedTaskType);
const params: DispatchTaskRequest = {
task_type: selectedTaskType
}
task_type: selectedTaskType,
};
if (targetTask?.supports_time) {
if (startTime) params.start_time = startTime.toISOString()
if (endTime) params.end_time = endTime.toISOString()
if (startTime) params.start_time = startTime.toISOString();
if (endTime) params.end_time = endTime.toISOString();
}
if (targetTask?.type === 'user_gamification') {
if (userId) params.user_id = userId
if (userId) params.user_id = userId;
}
// Handle dynamic parameters — type coercion (number vs string) is
// centralised in buildTaskPayload; do not inline it here.
if (targetTask?.params && targetTask.params.length > 0) {
const { payload, error } = buildTaskPayload(targetTask.params, paramValues)
const { payload, error } = buildTaskPayload(
targetTask.params,
paramValues,
);
if (error) {
toast.error(error)
setDispatching(false)
return
toast.error(error);
setDispatching(false);
return;
}
params.payload = payload ?? undefined
params.payload = payload ?? undefined;
}
const taskID = await services.adminTask.dispatchTask(params)
const taskID = await services.adminTask.dispatchTask(params);
toast.success('任务下发成功', {
description: `已成功将任务 ${ targetTask?.name || selectedTaskType } 加入队列:${ taskID }`
})
setDialogOpen(false)
description: `已成功将任务 ${targetTask?.name || selectedTaskType} 加入队列:${taskID}`,
});
setDialogOpen(false);
setStartTime(undefined)
setEndTime(undefined)
setUserId("")
setParamValues({})
setStartTime(undefined);
setEndTime(undefined);
setUserId('');
setParamValues({});
} catch (err) {
toast.error('任务下发失败', {
description: err instanceof Error ? err.message : '未知错误'
})
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setDispatching(false)
}
setDispatching(false);
}
};
const openDispatchDialog = (type: string) => {
setSelectedTaskType(type)
setDialogOpen(true)
}
setSelectedTaskType(type);
setDialogOpen(true);
};
const getSelectedTaskMeta = () => {
return taskTypes.find(t => t.type === selectedTaskType)
}
return taskTypes.find((t) => t.type === selectedTaskType);
};
return (
<div className="space-y-6">
<div className='space-y-6'>
<br />
<div className="space-y-6">
<div className='space-y-6'>
{error ? (
<div className="p-8 border border-dashed rounded-xl bg-card">
<ErrorInline error={error} onRetry={fetchTaskTypes} className="justify-center" />
<div className='p-8 border border-dashed rounded-xl bg-card'>
<ErrorInline
error={error}
onRetry={fetchTaskTypes}
className='justify-center'
/>
</div>
) : loading && taskTypes.length === 0 ? (
<LoadingStateWithBorder icon={Layers} description="加载任务类型中..." />
<LoadingStateWithBorder
icon={Layers}
description='加载任务类型中...'
/>
) : taskTypes.length === 0 ? (
<EmptyStateWithBorder icon={Layers} description="暂无可用任务类型" />
<EmptyStateWithBorder icon={Layers} description='暂无可用任务类型' />
) : (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4">
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4'>
{taskTypes.map((task, index) => {
const config = TASK_CONFIGS[task.type] || DEFAULT_TASK_CONFIG
const config = TASK_CONFIGS[task.type] || DEFAULT_TASK_CONFIG;
return (
<div
key={`${task.type}-${index}`}
className={cn(
"relative group overflow-hidden rounded-xl border bg-gradient-to-br transition-all duration-500",
config.gradient
'relative group overflow-hidden rounded-xl border bg-gradient-to-br transition-all duration-500',
config.gradient,
)}
>
<div className="relative h-full bg-card/40 backdrop-blur-sm p-4 flex flex-col justify-between hover:bg-card/0 transition-colors duration-500">
<div className="space-y-2">
<div className="flex items-start justify-between">
<div className="space-y-1">
<h3 className="font-semibold text-base tracking-tight">{task.name}</h3>
<p className="text-xs text-muted-foreground leading-relaxed line-clamp-2 min-h-[36px]">
<div className='relative h-full bg-card/40 backdrop-blur-sm p-4 flex flex-col justify-between hover:bg-card/0 transition-colors duration-500'>
<div className='space-y-2'>
<div className='flex items-start justify-between'>
<div className='space-y-1'>
<h3 className='font-semibold text-base tracking-tight'>
{task.name}
</h3>
<p className='text-xs text-muted-foreground leading-relaxed line-clamp-2 min-h-[36px]'>
{task.description}
</p>
</div>
<Badge variant="secondary" className="font-mono text-[10px] bg-background/50 backdrop-blur-md border px-1.5 h-5">
<Badge
variant='secondary'
className='font-mono text-[10px] bg-background/50 backdrop-blur-md border px-1.5 h-5'
>
{task.queue}
</Badge>
</div>
<div className="flex flex-wrap gap-1.5">
<Badge variant="outline" className="text-[9px] h-4.5 bg-background/50 font-mono text-muted-foreground border-border/50 px-1">
<div className='flex flex-wrap gap-1.5'>
<Badge
variant='outline'
className='text-[9px] h-4.5 bg-background/50 font-mono text-muted-foreground border-border/50 px-1'
>
类型:{task.type}
</Badge>
<Badge variant="outline" className="text-[9px] h-4.5 bg-background/50 font-mono text-muted-foreground border-border/50 px-1">
<Badge
variant='outline'
className='text-[9px] h-4.5 bg-background/50 font-mono text-muted-foreground border-border/50 px-1'
>
重试:{task.max_retry}
</Badge>
</div>
</div>
<div className="pt-4 mt-1">
<div className='pt-4 mt-1'>
<Button
className="w-full h-7 text-xs"
variant="secondary"
className='w-full h-7 text-xs'
variant='secondary'
onClick={() => openDispatchDialog(task.type)}
>
<Play className="size-3 mr-1" />
<Play className='size-3 mr-1' />
立即执行
</Button>
</div>
</div>
</div>
)
);
})}
</div>
)}
</div>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent className="sm:max-w-[500px]">
<DialogContent className='sm:max-w-[500px]'>
<DialogHeader>
<DialogTitle>下发任务</DialogTitle>
<DialogDescription>
@@ -316,81 +371,111 @@ export function TaskManager() {
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid gap-2">
<div className='grid gap-4 py-4'>
<div className='grid gap-2'>
<Label>执行任务</Label>
<div className="flex items-center gap-2 p-2 rounded-lg bg-muted/50">
<div className='flex items-center gap-2 p-2 rounded-lg bg-muted/50'>
{(() => {
const meta = getSelectedTaskMeta()
if (!meta) return null
const config = TASK_CONFIGS[meta.type] || DEFAULT_TASK_CONFIG
const Icon = config.icon
const meta = getSelectedTaskMeta();
if (!meta) return null;
const config = TASK_CONFIGS[meta.type] || DEFAULT_TASK_CONFIG;
const Icon = config.icon;
return (
<>
<div className={cn("p-2", config.color)}>
<Icon className="size-4" />
<div className={cn('p-2', config.color)}>
<Icon className='size-4' />
</div>
<div className="flex flex-col">
<span className="text-xs font-medium">{meta.name}</span>
<span className="text-[10px] text-muted-foreground font-mono">{meta.type}</span>
<div className='flex flex-col'>
<span className='text-xs font-medium'>{meta.name}</span>
<span className='text-[10px] text-muted-foreground font-mono'>
{meta.type}
</span>
</div>
</>
)
);
})()}
</div>
</div>
{(() => {
const targetTask = getSelectedTaskMeta()
const targetTask = getSelectedTaskMeta();
if (targetTask?.supports_time) {
return (
<>
<div className="grid gap-2">
<div className='grid gap-2'>
<Label>开始时间</Label>
<DatePickerWithTime date={startTime} setDate={setStartTime} />
<p className="text-xs text-muted-foreground">选择同步的起始时间点</p>
<DatePickerWithTime
date={startTime}
setDate={setStartTime}
/>
<p className='text-xs text-muted-foreground'>
选择同步的起始时间点
</p>
</div>
<div className="grid gap-2">
<div className='grid gap-2'>
<Label>结束时间</Label>
<DatePickerWithTime date={endTime} setDate={setEndTime} />
<p className="text-xs text-muted-foreground">选择同步的结束时间点</p>
<p className='text-xs text-muted-foreground'>
选择同步的结束时间点
</p>
</div>
</>
)
);
}
return null
return null;
})()}
{(() => {
const targetTask = getSelectedTaskMeta()
if (!targetTask || !targetTask.params || targetTask.params.length === 0) {
return null
const targetTask = getSelectedTaskMeta();
if (
!targetTask ||
!targetTask.params ||
targetTask.params.length === 0
) {
return null;
}
return (
<div className="space-y-4">
<div className='space-y-4'>
{targetTask.params.map((param) => (
<div key={param.name} className="grid gap-2">
<Label htmlFor={`param-${param.name}`} className="flex items-center gap-1">
<div key={param.name} className='grid gap-2'>
<Label
htmlFor={`param-${param.name}`}
className='flex items-center gap-1'
>
{param.label}
{param.required && <span className="text-destructive font-bold">*</span>}
{param.required && (
<span className='text-destructive font-bold'>*</span>
)}
</Label>
{param.type === 'text' ? (
<Textarea
id={`param-${param.name}`}
placeholder={param.placeholder}
className="text-xs min-h-[80px]"
value={paramValues[param.name] || ""}
onChange={(e) => setParamValues(prev => ({ ...prev, [param.name]: e.target.value }))}
className='text-xs min-h-[80px]'
value={paramValues[param.name] || ''}
onChange={(e) =>
setParamValues((prev) => ({
...prev,
[param.name]: e.target.value,
}))
}
/>
) : param.type === 'boolean' ? (
<div className="flex items-center gap-2 pt-1 h-9">
<div className='flex items-center gap-2 pt-1 h-9'>
<Switch
id={`param-${param.name}`}
checked={paramValues[param.name] === 'true'}
onCheckedChange={(checked) => setParamValues(prev => ({ ...prev, [param.name]: checked ? 'true' : 'false' }))}
onCheckedChange={(checked) =>
setParamValues((prev) => ({
...prev,
[param.name]: checked ? 'true' : 'false',
}))
}
/>
<span className="text-xs text-muted-foreground">
{paramValues[param.name] === 'true' ? '开启' : '关闭'}
<span className='text-xs text-muted-foreground'>
{paramValues[param.name] === 'true'
? '开启'
: '关闭'}
</span>
</div>
) : (
@@ -398,47 +483,68 @@ export function TaskManager() {
id={`param-${param.name}`}
type={param.type === 'number' ? 'number' : 'text'}
placeholder={param.placeholder}
className="text-xs"
value={paramValues[param.name] || ""}
onChange={(e) => setParamValues(prev => ({ ...prev, [param.name]: e.target.value }))}
className='text-xs'
value={paramValues[param.name] || ''}
onChange={(e) =>
setParamValues((prev) => ({
...prev,
[param.name]: e.target.value,
}))
}
/>
)}
{param.description && (
<p className="text-[10px] text-muted-foreground">{param.description}</p>
<p className='text-[10px] text-muted-foreground'>
{param.description}
</p>
)}
</div>
))}
</div>
)
);
})()}
{(() => {
const targetTask = getSelectedTaskMeta()
const hasTime = targetTask?.supports_time
const hasParams = targetTask?.params && targetTask.params.length > 0
const targetTask = getSelectedTaskMeta();
const hasTime = targetTask?.supports_time;
const hasParams =
targetTask?.params && targetTask.params.length > 0;
if (!hasTime && !hasParams) {
return (
<div className="flex items-center gap-2 text-sm text-muted-foreground bg-muted/50 p-3 rounded-md border border-dashed">
<Info className="h-4 w-4" />
<div className='flex items-center gap-2 text-sm text-muted-foreground bg-muted/50 p-3 rounded-md border border-dashed'>
<Info className='h-4 w-4' />
<span>此任务无需额外配置参数,确认后将直接下发。</span>
</div>
)
);
}
return null
return null;
})()}
</div>
<DialogFooter>
<Button variant="ghost" onClick={() => setDialogOpen(false)} disabled={dispatching} className="h-8 text-xs">
<Button
variant='ghost'
onClick={() => setDialogOpen(false)}
disabled={dispatching}
className='h-8 text-xs'
>
取消
</Button>
<Button onClick={handleDispatch} disabled={dispatching} className="h-8 text-xs">
{dispatching ? <Spinner className="size-3" /> : <Play className="size-3" />}
<Button
onClick={handleDispatch}
disabled={dispatching}
className='h-8 text-xs'
>
{dispatching ? (
<Spinner className='size-3' />
) : (
<Play className='size-3' />
)}
{dispatching ? '下发中' : '启动'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
);
}
@@ -1,100 +1,119 @@
"use client"
'use client';
import {useCallback, useEffect, useState} from "react"
import {toast} from "sonner"
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Textarea} from "@/components/ui/textarea"
import {Switch} from "@/components/ui/switch"
import {Spinner} from "@/components/ui/spinner"
import {Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import {Clock, Edit2, Info, Plus, RefreshCw, Trash2} from "lucide-react"
import { useCallback, useEffect, useState } from 'react';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Switch } from '@/components/ui/switch';
import { Spinner } from '@/components/ui/spinner';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { Clock, Edit2, Info, Plus, RefreshCw, Trash2 } from 'lucide-react';
import type {CreateScheduleRequest, Schedule, TaskMeta, UpdateScheduleRequest} from "@/lib/services/admin"
import services from "@/lib/services"
import {buildTaskPayload} from "@/lib/task-param-utils"
import {ErrorInline} from "@/components/layout/error"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {EmptyStateWithBorder} from "@/components/layout/empty"
import type {
CreateScheduleRequest,
Schedule,
TaskMeta,
UpdateScheduleRequest,
} from '@/lib/services/admin';
import services from '@/lib/services';
import { buildTaskPayload } from '@/lib/task-param-utils';
import { ErrorInline } from '@/components/layout/error';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { EmptyStateWithBorder } from '@/components/layout/empty';
export function TaskSchedulesManager() {
const [loading, setLoading] = useState(false)
const [error, setError] = useState<Error | null>(null)
const [schedules, setSchedules] = useState<Schedule[]>([])
const [taskTypes, setTaskTypes] = useState<TaskMeta[]>([])
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [schedules, setSchedules] = useState<Schedule[]>([]);
const [taskTypes, setTaskTypes] = useState<TaskMeta[]>([]);
// Modal States
const [dialogOpen, setDialogOpen] = useState(false)
const [submitLoading, setSubmitLoading] = useState(false)
const [editingSchedule, setEditingSchedule] = useState<Schedule | null>(null)
const [dialogOpen, setDialogOpen] = useState(false);
const [submitLoading, setSubmitLoading] = useState(false);
const [editingSchedule, setEditingSchedule] = useState<Schedule | null>(null);
// Delete States
const [deleteOpen, setDeleteOpen] = useState(false)
const [deletingId, setDeletingId] = useState<string | null>(null)
const [deleteLoading, setDeleteLoading] = useState(false)
const [deleteOpen, setDeleteOpen] = useState(false);
const [deletingId, setDeletingId] = useState<string | null>(null);
const [deleteLoading, setDeleteLoading] = useState(false);
// Form Fields
const [name, setName] = useState("")
const [selectedTaskType, setSelectedTaskType] = useState("")
const [cron, setCron] = useState("")
const [isActive, setIsActive] = useState(true)
const [paramValues, setParamValues] = useState<Record<string, string>>({})
const [name, setName] = useState('');
const [selectedTaskType, setSelectedTaskType] = useState('');
const [cron, setCron] = useState('');
const [isActive, setIsActive] = useState(true);
const [paramValues, setParamValues] = useState<Record<string, string>>({});
// Fetch Schedules & Task Types
const fetchData = useCallback(async () => {
try {
setLoading(true)
setError(null)
setLoading(true);
setError(null);
const [schedulesData, taskTypesData] = await Promise.all([
services.adminTask.listSchedules(),
services.adminTask.getTaskTypes()
])
setSchedules(schedulesData || [])
setTaskTypes(taskTypesData || [])
services.adminTask.getTaskTypes(),
]);
setSchedules(schedulesData || []);
setTaskTypes(taskTypesData || []);
} catch (err) {
setError(err instanceof Error ? err : new Error("加载定时任务配置失败"))
setError(err instanceof Error ? err : new Error('加载定时任务配置失败'));
} finally {
setLoading(false)
setLoading(false);
}
}, [])
}, []);
useEffect(() => {
fetchData()
}, [fetchData])
fetchData();
}, [fetchData]);
// Track task type selection to initialize parameters
useEffect(() => {
if (selectedTaskType) {
const targetTask = taskTypes.find(t => t.type === selectedTaskType)
const targetTask = taskTypes.find((t) => t.type === selectedTaskType);
if (editingSchedule && editingSchedule.task_type === selectedTaskType) {
// Parse existing payload when editing the same task type
try {
const parsed = JSON.parse(editingSchedule.payload || "{}")
const values: Record<string, string> = {}
targetTask?.params?.forEach(p => {
values[p.name] = String(parsed[p.name] ?? "")
})
setParamValues(values)
const parsed = JSON.parse(editingSchedule.payload || '{}');
const values: Record<string, string> = {};
targetTask?.params?.forEach((p) => {
values[p.name] = String(parsed[p.name] ?? '');
});
setParamValues(values);
} catch {
const initialValues: Record<string, string> = {}
targetTask?.params?.forEach(p => {
initialValues[p.name] = ""
})
setParamValues(initialValues)
const initialValues: Record<string, string> = {};
targetTask?.params?.forEach((p) => {
initialValues[p.name] = '';
});
setParamValues(initialValues);
}
} else {
const initialValues: Record<string, string> = {}
targetTask?.params?.forEach(p => {
initialValues[p.name] = ""
})
setParamValues(initialValues)
const initialValues: Record<string, string> = {};
targetTask?.params?.forEach((p) => {
initialValues[p.name] = '';
});
setParamValues(initialValues);
}
} else {
setParamValues({})
setParamValues({});
}
}, [selectedTaskType, taskTypes, editingSchedule])
}, [selectedTaskType, taskTypes, editingSchedule]);
const handleToggleActive = async (schedule: Schedule) => {
try {
@@ -103,61 +122,70 @@ export function TaskSchedulesManager() {
task_type: schedule.task_type,
cron: schedule.cron,
payload: schedule.payload,
is_active: !schedule.is_active
})
is_active: !schedule.is_active,
});
setSchedules(prev => prev.map(s => s.id === schedule.id ? updated : s))
toast.success(updated.is_active ? `已启用定时任务:${updated.name}` : `已停用定时任务:${updated.name}`)
setSchedules((prev) =>
prev.map((s) => (s.id === schedule.id ? updated : s)),
);
toast.success(
updated.is_active
? `已启用定时任务:${updated.name}`
: `已停用定时任务:${updated.name}`,
);
} catch (err) {
toast.error("修改任务状态失败", {
description: err instanceof Error ? err.message : "未知错误"
})
}
toast.error('修改任务状态失败', {
description: err instanceof Error ? err.message : '未知错误',
});
}
};
const openCreateDialog = () => {
setEditingSchedule(null)
setName("")
setSelectedTaskType(taskTypes[0]?.type || "")
setCron("0 */2 * * *")
setIsActive(true)
setParamValues({})
setDialogOpen(true)
}
setEditingSchedule(null);
setName('');
setSelectedTaskType(taskTypes[0]?.type || '');
setCron('0 */2 * * *');
setIsActive(true);
setParamValues({});
setDialogOpen(true);
};
const openEditDialog = (schedule: Schedule) => {
setEditingSchedule(schedule)
setName(schedule.name)
setSelectedTaskType(schedule.task_type)
setCron(schedule.cron)
setIsActive(schedule.is_active)
setDialogOpen(true)
}
setEditingSchedule(schedule);
setName(schedule.name);
setSelectedTaskType(schedule.task_type);
setCron(schedule.cron);
setIsActive(schedule.is_active);
setDialogOpen(true);
};
const handleSubmit = async () => {
if (!name.trim()) {
toast.error("任务名称不能为空")
return
toast.error('任务名称不能为空');
return;
}
if (!cron.trim()) {
toast.error("Cron 表达式不能为空")
return
toast.error('Cron 表达式不能为空');
return;
}
try {
setSubmitLoading(true)
setSubmitLoading(true);
const targetTask = taskTypes.find(t => t.type === selectedTaskType)
let payloadStr = '{}'
const targetTask = taskTypes.find((t) => t.type === selectedTaskType);
let payloadStr = '{}';
if (targetTask?.params && targetTask.params.length > 0) {
const { payload, error } = buildTaskPayload(targetTask.params, paramValues)
const { payload, error } = buildTaskPayload(
targetTask.params,
paramValues,
);
if (error) {
toast.error(error)
setSubmitLoading(false)
return
toast.error(error);
setSubmitLoading(false);
return;
}
payloadStr = payload ?? '{}'
payloadStr = payload ?? '{}';
}
if (editingSchedule) {
@@ -166,145 +194,182 @@ export function TaskSchedulesManager() {
task_type: selectedTaskType,
cron,
payload: payloadStr,
is_active: isActive
}
const updated = await services.adminTask.updateSchedule(editingSchedule.id, req)
setSchedules(prev => prev.map(s => s.id === editingSchedule.id ? updated : s))
toast.success("定时任务更新成功")
is_active: isActive,
};
const updated = await services.adminTask.updateSchedule(
editingSchedule.id,
req,
);
setSchedules((prev) =>
prev.map((s) => (s.id === editingSchedule.id ? updated : s)),
);
toast.success('定时任务更新成功');
} else {
const req: CreateScheduleRequest = {
name,
task_type: selectedTaskType,
cron,
payload: payloadStr,
is_active: isActive
}
const created = await services.adminTask.createSchedule(req)
setSchedules(prev => [created, ...prev])
toast.success("定时任务创建成功")
is_active: isActive,
};
const created = await services.adminTask.createSchedule(req);
setSchedules((prev) => [created, ...prev]);
toast.success('定时任务创建成功');
}
setDialogOpen(false)
setDialogOpen(false);
} catch (err) {
toast.error("提交失败", {
description: err instanceof Error ? err.message : "未知错误"
})
toast.error('提交失败', {
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setSubmitLoading(false)
}
setSubmitLoading(false);
}
};
const openDeleteDialog = (id: string) => {
setDeletingId(id)
setDeleteOpen(true)
}
setDeletingId(id);
setDeleteOpen(true);
};
const handleDelete = async () => {
if (!deletingId) return
if (!deletingId) return;
try {
setDeleteLoading(true)
await services.adminTask.deleteSchedule(deletingId)
setSchedules(prev => prev.filter(s => s.id !== deletingId))
toast.success("定时任务删除成功")
setDeleteOpen(false)
setDeleteLoading(true);
await services.adminTask.deleteSchedule(deletingId);
setSchedules((prev) => prev.filter((s) => s.id !== deletingId));
toast.success('定时任务删除成功');
setDeleteOpen(false);
} catch (err) {
toast.error("删除失败", {
description: err instanceof Error ? err.message : "未知错误"
})
toast.error('删除失败', {
description: err instanceof Error ? err.message : '未知错误',
});
} finally {
setDeleteLoading(false)
}
setDeleteLoading(false);
}
};
const getSelectedTaskMeta = () => {
return taskTypes.find(t => t.type === selectedTaskType)
}
return taskTypes.find((t) => t.type === selectedTaskType);
};
const getTaskName = (type: string) => {
const meta = taskTypes.find(t => t.type === type)
return meta ? meta.name : type
}
const meta = taskTypes.find((t) => t.type === type);
return meta ? meta.name : type;
};
return (
<div className="space-y-6">
<div className='space-y-6'>
<br />
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Button variant="outline" size="sm" onClick={fetchData} disabled={loading}>
{loading ? <Spinner className="size-4" /> : <RefreshCw className="size-4" />}
<div className='flex items-center justify-between'>
<div className='flex items-center gap-2'>
<Button
variant='outline'
size='sm'
onClick={fetchData}
disabled={loading}
>
{loading ? (
<Spinner className='size-4' />
) : (
<RefreshCw className='size-4' />
)}
刷新
</Button>
<Button size="sm" onClick={openCreateDialog} disabled={taskTypes.length === 0} variant={'secondary'}>
<Plus className="size-4 mr-1" />
<Button
size='sm'
onClick={openCreateDialog}
disabled={taskTypes.length === 0}
variant={'secondary'}
>
<Plus className='size-4 mr-1' />
新增定时任务
</Button>
</div>
</div>
{error ? (
<div className="p-8 border border-dashed rounded-lg bg-card">
<ErrorInline error={error} onRetry={fetchData} className="justify-center" />
<div className='p-8 border border-dashed rounded-lg bg-card'>
<ErrorInline
error={error}
onRetry={fetchData}
className='justify-center'
/>
</div>
) : loading && schedules.length === 0 ? (
<LoadingStateWithBorder icon={Clock} description="加载定时任务配置中..." />
<LoadingStateWithBorder
icon={Clock}
description='加载定时任务配置中...'
/>
) : schedules.length === 0 ? (
<EmptyStateWithBorder icon={Clock} description="暂无定时任务配置,点击上方按钮新增" />
<EmptyStateWithBorder
icon={Clock}
description='暂无定时任务配置,点击上方按钮新增'
/>
) : (
<div className="rounded-lg border bg-card">
<Table className="min-w-[800px]">
<div className='rounded-lg border bg-card'>
<Table className='min-w-[800px]'>
<TableHeader>
<TableRow className="hover:bg-transparent">
<TableHead className="w-[180px]">任务名称</TableHead>
<TableHead className="w-[160px]">关联异步任务</TableHead>
<TableHead className="w-[120px]">Cron 表达式</TableHead>
<TableHead className="min-w-[200px]">执行参数 (Payload)</TableHead>
<TableHead className="w-[100px] text-center">启用状态</TableHead>
<TableHead className="w-[120px] text-right">操作</TableHead>
<TableRow className='hover:bg-transparent'>
<TableHead className='w-[180px]'>任务名称</TableHead>
<TableHead className='w-[160px]'>关联异步任务</TableHead>
<TableHead className='w-[120px]'>Cron 表达式</TableHead>
<TableHead className='min-w-[200px]'>
执行参数 (Payload)
</TableHead>
<TableHead className='w-[100px] text-center'>
启用状态
</TableHead>
<TableHead className='w-[120px] text-right'>操作</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{schedules.map((schedule) => (
<TableRow key={schedule.id}>
<TableCell className="font-medium">{schedule.name}</TableCell>
<TableCell className='font-medium'>{schedule.name}</TableCell>
<TableCell>
<div className="flex flex-col gap-0.5">
<span className="text-xs font-semibold">{getTaskName(schedule.task_type)}</span>
<span className="font-mono text-[10px] text-muted-foreground">{schedule.task_type}</span>
<div className='flex flex-col gap-0.5'>
<span className='text-xs font-semibold'>
{getTaskName(schedule.task_type)}
</span>
<span className='font-mono text-[10px] text-muted-foreground'>
{schedule.task_type}
</span>
</div>
</TableCell>
<TableCell className="font-mono text-xs text-blue-600 dark:text-blue-400 font-semibold">
<TableCell className='font-mono text-xs text-blue-600 dark:text-blue-400 font-semibold'>
{schedule.cron}
</TableCell>
<TableCell className="max-w-[300px] truncate">
<code className="text-xs bg-muted/60 px-1 py-0.5 rounded font-mono">
{schedule.payload || "{}"}
<TableCell className='max-w-[300px] truncate'>
<code className='text-xs bg-muted/60 px-1 py-0.5 rounded font-mono'>
{schedule.payload || '{}'}
</code>
</TableCell>
<TableCell className="text-center">
<div className="flex justify-center">
<TableCell className='text-center'>
<div className='flex justify-center'>
<Switch
checked={schedule.is_active}
onCheckedChange={() => handleToggleActive(schedule)}
/>
</div>
</TableCell>
<TableCell className="text-right">
<div className="flex items-center justify-end gap-1.5">
<TableCell className='text-right'>
<div className='flex items-center justify-end gap-1.5'>
<Button
variant="ghost"
size="icon"
className="h-7 w-7 text-muted-foreground hover:text-foreground"
variant='ghost'
size='icon'
className='h-7 w-7 text-muted-foreground hover:text-foreground'
onClick={() => openEditDialog(schedule)}
>
<Edit2 className="size-3.5" />
<Edit2 className='size-3.5' />
</Button>
<Button
variant="ghost"
size="icon"
className="h-7 w-7 text-destructive hover:text-destructive/90"
variant='ghost'
size='icon'
className='h-7 w-7 text-destructive hover:text-destructive/90'
onClick={() => openDeleteDialog(schedule.id)}
>
<Trash2 className="size-3.5" />
<Trash2 className='size-3.5' />
</Button>
</div>
</TableCell>
@@ -317,36 +382,38 @@ export function TaskSchedulesManager() {
{/* Add / Edit Dialog */}
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogContent className="sm:max-w-[500px]">
<DialogContent className='sm:max-w-[500px]'>
<DialogHeader>
<DialogTitle>{editingSchedule ? "修改定时任务" : "新增定时任务"}</DialogTitle>
<DialogTitle>
{editingSchedule ? '修改定时任务' : '新增定时任务'}
</DialogTitle>
<DialogDescription>
配置定时任务的调度参数和运行载荷。
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid gap-2">
<Label htmlFor="sched-name">任务名称</Label>
<div className='grid gap-4 py-4'>
<div className='grid gap-2'>
<Label htmlFor='sched-name'>任务名称</Label>
<Input
id="sched-name"
placeholder="例如:每日数据清理"
id='sched-name'
placeholder='例如:每日数据清理'
value={name}
onChange={(e) => setName(e.target.value)}
className="text-xs"
className='text-xs'
/>
</div>
<div className="grid gap-2">
<Label htmlFor="sched-type">关联异步任务</Label>
<div className='grid gap-2'>
<Label htmlFor='sched-type'>关联异步任务</Label>
<select
id="sched-type"
id='sched-type'
value={selectedTaskType}
onChange={(e) => setSelectedTaskType(e.target.value)}
disabled={!!editingSchedule}
className="flex h-8 w-full rounded-md border border-input bg-background px-3 py-1.5 text-xs shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
className='flex h-8 w-full rounded-md border border-input bg-background px-3 py-1.5 text-xs shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50'
>
{taskTypes.map(t => (
{taskTypes.map((t) => (
<option key={t.type} value={t.type}>
{t.name} ({t.type})
</option>
@@ -354,66 +421,96 @@ export function TaskSchedulesManager() {
</select>
</div>
<div className="grid gap-2">
<Label htmlFor="sched-cron">Cron 表达式</Label>
<div className='grid gap-2'>
<Label htmlFor='sched-cron'>Cron 表达式</Label>
<Input
id="sched-cron"
placeholder="e.g. 0 */2 * * * 或 @daily"
id='sched-cron'
placeholder='e.g. 0 */2 * * * 或 @daily'
value={cron}
onChange={(e) => setCron(e.target.value)}
className="text-xs font-mono"
className='text-xs font-mono'
/>
<p className="text-[10px] text-muted-foreground">使用标准 5 位 Cron 字段格式(分钟、小时、日期、月份、星期几)。</p>
<p className='text-[10px] text-muted-foreground'>
使用标准 5 位 Cron 字段格式(分钟、小时、日期、月份、星期几)。
</p>
</div>
<div className="flex items-center justify-between rounded-lg border p-3">
<div className="space-y-0.5">
<Label htmlFor="sched-active">启用状态</Label>
<p className="text-[10px] text-muted-foreground">决定此定时任务是否会定时触发执行。</p>
<div className='flex items-center justify-between rounded-lg border p-3'>
<div className='space-y-0.5'>
<Label htmlFor='sched-active'>启用状态</Label>
<p className='text-[10px] text-muted-foreground'>
决定此定时任务是否会定时触发执行。
</p>
</div>
<Switch
id="sched-active"
id='sched-active'
checked={isActive}
onCheckedChange={setIsActive}
/>
</div>
{(() => {
const targetTask = getSelectedTaskMeta()
if (!targetTask || !targetTask.params || targetTask.params.length === 0) {
const targetTask = getSelectedTaskMeta();
if (
!targetTask ||
!targetTask.params ||
targetTask.params.length === 0
) {
return (
<div className="flex items-center gap-2 text-xs text-muted-foreground bg-muted/40 p-3 rounded-md border border-dashed">
<Info className="h-4 w-4 shrink-0" />
<div className='flex items-center gap-2 text-xs text-muted-foreground bg-muted/40 p-3 rounded-md border border-dashed'>
<Info className='h-4 w-4 shrink-0' />
<span>所选任务类型不需要运行参数参数。</span>
</div>
)
);
}
return (
<div className="space-y-4 pt-2 border-t">
<Label className="text-xs font-semibold">运行参数配置 (Payload)</Label>
<div className='space-y-4 pt-2 border-t'>
<Label className='text-xs font-semibold'>
运行参数配置 (Payload)
</Label>
{targetTask.params.map((param) => (
<div key={param.name} className="grid gap-2 pl-2 border-l-2 border-muted">
<Label htmlFor={`param-${param.name}`} className="flex items-center gap-1 text-xs">
<div
key={param.name}
className='grid gap-2 pl-2 border-l-2 border-muted'
>
<Label
htmlFor={`param-${param.name}`}
className='flex items-center gap-1 text-xs'
>
{param.label}
{param.required && <span className="text-destructive font-bold">*</span>}
{param.required && (
<span className='text-destructive font-bold'>*</span>
)}
</Label>
{param.type === 'text' ? (
<Textarea
id={`param-${param.name}`}
placeholder={param.placeholder}
className="text-xs min-h-[70px]"
value={paramValues[param.name] || ""}
onChange={(e) => setParamValues(prev => ({ ...prev, [param.name]: e.target.value }))}
className='text-xs min-h-[70px]'
value={paramValues[param.name] || ''}
onChange={(e) =>
setParamValues((prev) => ({
...prev,
[param.name]: e.target.value,
}))
}
/>
) : param.type === 'boolean' ? (
<div className="flex items-center gap-2 pt-1 h-9">
<div className='flex items-center gap-2 pt-1 h-9'>
<Switch
id={`param-${param.name}`}
checked={paramValues[param.name] === 'true'}
onCheckedChange={(checked) => setParamValues(prev => ({ ...prev, [param.name]: checked ? 'true' : 'false' }))}
onCheckedChange={(checked) =>
setParamValues((prev) => ({
...prev,
[param.name]: checked ? 'true' : 'false',
}))
}
/>
<span className="text-xs text-muted-foreground">
{paramValues[param.name] === 'true' ? '开启' : '关闭'}
<span className='text-xs text-muted-foreground'>
{paramValues[param.name] === 'true'
? '开启'
: '关闭'}
</span>
</div>
) : (
@@ -421,27 +518,43 @@ export function TaskSchedulesManager() {
id={`param-${param.name}`}
type={param.type === 'number' ? 'number' : 'text'}
placeholder={param.placeholder}
className="text-xs"
value={paramValues[param.name] || ""}
onChange={(e) => setParamValues(prev => ({ ...prev, [param.name]: e.target.value }))}
className='text-xs'
value={paramValues[param.name] || ''}
onChange={(e) =>
setParamValues((prev) => ({
...prev,
[param.name]: e.target.value,
}))
}
/>
)}
{param.description && (
<p className="text-[10px] text-muted-foreground">{param.description}</p>
<p className='text-[10px] text-muted-foreground'>
{param.description}
</p>
)}
</div>
))}
</div>
)
);
})()}
</div>
<DialogFooter>
<Button variant="ghost" onClick={() => setDialogOpen(false)} disabled={submitLoading} className="h-8 text-xs">
<Button
variant='ghost'
onClick={() => setDialogOpen(false)}
disabled={submitLoading}
className='h-8 text-xs'
>
取消
</Button>
<Button onClick={handleSubmit} disabled={submitLoading} className="h-8 text-xs">
{submitLoading && <Spinner className="size-3 mr-1" />}
<Button
onClick={handleSubmit}
disabled={submitLoading}
className='h-8 text-xs'
>
{submitLoading && <Spinner className='size-3 mr-1' />}
{editingSchedule ? '保存修改' : '确认创建'}
</Button>
</DialogFooter>
@@ -450,24 +563,34 @@ export function TaskSchedulesManager() {
{/* Delete Confirmation */}
<Dialog open={deleteOpen} onOpenChange={setDeleteOpen}>
<DialogContent className="sm:max-w-[400px]">
<DialogContent className='sm:max-w-[400px]'>
<DialogHeader>
<DialogTitle>删除定时任务</DialogTitle>
<DialogDescription>
确定要删除此定时任务吗?该操作不可撤销,且会立即取消其在调度器中的调度计划。
</DialogDescription>
</DialogHeader>
<DialogFooter className="gap-2 sm:gap-0">
<Button variant="ghost" onClick={() => setDeleteOpen(false)} disabled={deleteLoading} className="h-8 text-xs">
<DialogFooter className='gap-2 sm:gap-0'>
<Button
variant='ghost'
onClick={() => setDeleteOpen(false)}
disabled={deleteLoading}
className='h-8 text-xs'
>
取消
</Button>
<Button variant="destructive" onClick={handleDelete} disabled={deleteLoading} className="h-8 text-xs">
{deleteLoading && <Spinner className="size-3 mr-1" />}
<Button
variant='destructive'
onClick={handleDelete}
disabled={deleteLoading}
className='h-8 text-xs'
>
{deleteLoading && <Spinner className='size-3 mr-1' />}
确认删除
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
)
);
}
+47 -32
View File
@@ -1,65 +1,80 @@
"use client"
'use client';
import {Suspense} from "react"
import {useRouter, useSearchParams} from "next/navigation"
import {Tabs, TabsContent, TabsList, TabsTrigger} from "@/components/ui/tabs"
import {TaskManager} from "./components/task-manager"
import {TaskSchedulesManager} from "./components/task-schedules"
import {TaskExecutionsManager} from "./components/task-executions"
import {Spinner} from "@/components/ui/spinner"
import {Layers} from "lucide-react"
import { Suspense } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { TaskManager } from './components/task-manager';
import { TaskSchedulesManager } from './components/task-schedules';
import { TaskExecutionsManager } from './components/task-executions';
import { Spinner } from '@/components/ui/spinner';
import { Layers } from 'lucide-react';
function TasksPageContent() {
const searchParams = useSearchParams()
const router = useRouter()
const activeTab = searchParams.get("tab") || "tasks"
const searchParams = useSearchParams();
const router = useRouter();
const activeTab = searchParams.get('tab') || 'tasks';
const handleTabChange = (value: string) => {
router.push(`/admin/tasks?tab=${value}`)
}
router.push(`/admin/tasks?tab=${value}`);
};
return (
<div className="py-6 space-y-6">
<div className="flex items-center gap-2">
<Layers className="size-5 text-primary" />
<div className='py-6 space-y-6'>
<div className='flex items-center gap-2'>
<Layers className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">任务管理</h1>
<h1 className='text-2xl font-semibold tracking-tight'>任务管理</h1>
</div>
</div>
<Tabs value={activeTab} onValueChange={handleTabChange} className="w-full">
<TabsList variant="line" className="w-fit inline-flex gap-8 mb-6">
<TabsTrigger value="tasks" className="px-0 pb-2 text-xs font-semibold">
<Tabs
value={activeTab}
onValueChange={handleTabChange}
className='w-full'
>
<TabsList variant='line' className='w-fit inline-flex gap-8 mb-6'>
<TabsTrigger
value='tasks'
className='px-0 pb-2 text-xs font-semibold'
>
任务管理
</TabsTrigger>
<TabsTrigger value="schedules" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='schedules'
className='px-0 pb-2 text-xs font-semibold'
>
定时任务
</TabsTrigger>
<TabsTrigger value="executions" className="px-0 pb-2 text-xs font-semibold">
<TabsTrigger
value='executions'
className='px-0 pb-2 text-xs font-semibold'
>
任务日志
</TabsTrigger>
</TabsList>
<TabsContent value="tasks" className="space-y-4 outline-none">
<TabsContent value='tasks' className='space-y-4 outline-none'>
<TaskManager />
</TabsContent>
<TabsContent value="schedules" className="space-y-4 outline-none">
<TabsContent value='schedules' className='space-y-4 outline-none'>
<TaskSchedulesManager />
</TabsContent>
<TabsContent value="executions" className="space-y-4 outline-none">
<TabsContent value='executions' className='space-y-4 outline-none'>
<TaskExecutionsManager />
</TabsContent>
</Tabs>
</div>
)
);
}
export default function TasksPage() {
return (
<Suspense fallback={
<div className="flex items-center justify-center min-h-[400px]">
<Spinner className="h-8 w-8" />
<Suspense
fallback={
<div className='flex items-center justify-center min-h-[400px]'>
<Spinner className='h-8 w-8' />
</div>
}>
}
>
<TasksPageContent />
</Suspense>
)
);
}
@@ -1,91 +1,97 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {Button} from "@/components/ui/button"
import {Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Switch} from "@/components/ui/switch"
import {useAdminUsers} from "@/contexts/admin-users-context"
import type {CreateUserRequest} from "@/lib/services/admin"
import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { useAdminUsers } from '@/contexts/admin-users-context';
import type { CreateUserRequest } from '@/lib/services/admin';
const emptyForm: CreateUserRequest = {
username: "",
password: "",
nickname: "",
email: "",
username: '',
password: '',
nickname: '',
email: '',
is_active: true,
is_admin: false,
}
};
export function CreateUserModal({
isOpen,
onClose,
}: {
isOpen: boolean
onClose: () => void
isOpen: boolean;
onClose: () => void;
}) {
const { createUser } = useAdminUsers()
const [form, setForm] = useState<CreateUserRequest>(emptyForm)
const [saving, setSaving] = useState(false)
const [errors, setErrors] = useState<Record<string, string>>({})
const { createUser } = useAdminUsers();
const [form, setForm] = useState<CreateUserRequest>(emptyForm);
const [saving, setSaving] = useState(false);
const [errors, setErrors] = useState<Record<string, string>>({});
useEffect(() => {
if (isOpen) {
setForm(emptyForm)
setErrors({})
setForm(emptyForm);
setErrors({});
} else {
setSaving(false)
setSaving(false);
}
}, [isOpen])
}, [isOpen]);
const validate = () => {
const newErrors: Record<string, string> = {}
const newErrors: Record<string, string> = {};
if (!form.username.trim()) {
newErrors.username = "用户名不能为空"
newErrors.username = '用户名不能为空';
} else if (form.username.trim().length < 3) {
newErrors.username = "用户名长度不能少于 3 位"
newErrors.username = '用户名长度不能少于 3 位';
}
if (!form.email.trim()) {
newErrors.email = "邮箱不能为空"
newErrors.email = '邮箱不能为空';
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email.trim())) {
newErrors.email = "邮箱格式不正确"
newErrors.email = '邮箱格式不正确';
}
if (!form.password) {
newErrors.password = "密码不能为空"
newErrors.password = '密码不能为空';
} else if (form.password.length < 8) {
newErrors.password = "密码长度不能少于 8 位"
newErrors.password = '密码长度不能少于 8 位';
}
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSave = async (e: React.FormEvent) => {
e.preventDefault()
if (!validate()) return
e.preventDefault();
if (!validate()) return;
setSaving(true)
setSaving(true);
try {
await createUser({
...form,
username: form.username.trim(),
nickname: form.nickname?.trim() || undefined,
email: form.email.trim(),
})
onClose()
});
onClose();
} catch {
// Errors are already handled by context toast notifications
} finally {
setSaving(false)
}
setSaving(false);
}
};
return (
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-w-md">
<DialogContent className='max-w-md'>
<DialogHeader>
<DialogTitle>新增用户</DialogTitle>
<DialogDescription>
@@ -93,90 +99,106 @@ export function CreateUserModal({
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSave} className="space-y-4 pt-2">
<div className="space-y-1.5">
<Label htmlFor="username">用户名</Label>
<form onSubmit={handleSave} className='space-y-4 pt-2'>
<div className='space-y-1.5'>
<Label htmlFor='username'>用户名</Label>
<Input
id="username"
id='username'
value={form.username}
onChange={(e) => setForm((prev) => ({ ...prev, username: e.target.value }))}
placeholder="请输入用户名 (至少 3 位)"
onChange={(e) =>
setForm((prev) => ({ ...prev, username: e.target.value }))
}
placeholder='请输入用户名 (至少 3 位)'
/>
{errors.username && (
<p className="text-xs text-destructive">{errors.username}</p>
<p className='text-xs text-destructive'>{errors.username}</p>
)}
</div>
<div className="space-y-1.5">
<Label htmlFor="nickname">昵称 (选填)</Label>
<div className='space-y-1.5'>
<Label htmlFor='nickname'>昵称 (选填)</Label>
<Input
id="nickname"
id='nickname'
value={form.nickname}
onChange={(e) => setForm((prev) => ({ ...prev, nickname: e.target.value }))}
placeholder="请输入昵称"
onChange={(e) =>
setForm((prev) => ({ ...prev, nickname: e.target.value }))
}
placeholder='请输入昵称'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="email">邮箱</Label>
<div className='space-y-1.5'>
<Label htmlFor='email'>邮箱</Label>
<Input
id="email"
type="email"
id='email'
type='email'
value={form.email}
onChange={(e) => setForm((prev) => ({ ...prev, email: e.target.value }))}
placeholder="请输入邮箱地址"
onChange={(e) =>
setForm((prev) => ({ ...prev, email: e.target.value }))
}
placeholder='请输入邮箱地址'
/>
{errors.email && (
<p className="text-xs text-destructive">{errors.email}</p>
<p className='text-xs text-destructive'>{errors.email}</p>
)}
</div>
<div className="space-y-1.5">
<Label htmlFor="password">密码</Label>
<div className='space-y-1.5'>
<Label htmlFor='password'>密码</Label>
<Input
id="password"
type="password"
id='password'
type='password'
value={form.password}
onChange={(e) => setForm((prev) => ({ ...prev, password: e.target.value }))}
placeholder="请输入登录密码 (至少 8 位)"
onChange={(e) =>
setForm((prev) => ({ ...prev, password: e.target.value }))
}
placeholder='请输入登录密码 (至少 8 位)'
/>
{errors.password && (
<p className="text-xs text-destructive">{errors.password}</p>
<p className='text-xs text-destructive'>{errors.password}</p>
)}
</div>
<div className="flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10">
<div className='flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10'>
<div>
<div className="font-medium text-sm">启用账户</div>
<div className="text-xs text-muted-foreground">禁用后此账号将无法登录系统。</div>
<div className='font-medium text-sm'>启用账户</div>
<div className='text-xs text-muted-foreground'>
禁用后此账号将无法登录系统。
</div>
</div>
<Switch
checked={form.is_active}
onCheckedChange={(checked) => setForm((prev) => ({ ...prev, is_active: checked }))}
onCheckedChange={(checked) =>
setForm((prev) => ({ ...prev, is_active: checked }))
}
/>
</div>
<div className="flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10">
<div className='flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10'>
<div>
<div className="font-medium text-sm">管理员权限</div>
<div className="text-xs text-muted-foreground">开启后此账号将拥有后台管理权限。</div>
<div className='font-medium text-sm'>管理员权限</div>
<div className='text-xs text-muted-foreground'>
开启后此账号将拥有后台管理权限。
</div>
</div>
<Switch
checked={form.is_admin}
onCheckedChange={(checked) => setForm((prev) => ({ ...prev, is_admin: checked }))}
onCheckedChange={(checked) =>
setForm((prev) => ({ ...prev, is_admin: checked }))
}
/>
</div>
<div className="flex justify-end gap-2 pt-2 border-t mt-2">
<Button variant="outline" type="button" onClick={onClose}>
<div className='flex justify-end gap-2 pt-2 border-t mt-2'>
<Button variant='outline' type='button' onClick={onClose}>
取消
</Button>
<Button type="submit" disabled={saving} variant="secondary">
{saving ? "创建中..." : "创建"}
<Button type='submit' disabled={saving} variant='secondary'>
{saving ? '创建中...' : '创建'}
</Button>
</div>
</form>
</DialogContent>
</Dialog>
)
);
}
@@ -1,14 +1,20 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {Button} from "@/components/ui/button"
import {Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Switch} from "@/components/ui/switch"
import {useAdminUsers} from "@/contexts/admin-users-context"
import {useAuth} from "@/components/providers/auth-provider"
import type {AdminUser} from "@/lib/services/admin"
import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { useAdminUsers } from '@/contexts/admin-users-context';
import { useAuth } from '@/components/providers/auth-provider';
import type { AdminUser } from '@/lib/services/admin';
import {
AlertDialog,
AlertDialogAction,
@@ -17,15 +23,15 @@ import {
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle
} from "@/components/ui/alert-dialog"
import {Loader2} from "lucide-react"
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Loader2 } from 'lucide-react';
interface EditUserForm {
nickname: string
email: string
is_admin: boolean
password?: string
nickname: string;
email: string;
is_admin: boolean;
password?: string;
}
export function EditUserModal({
@@ -33,115 +39,116 @@ export function EditUserModal({
onClose,
user,
}: {
isOpen: boolean
onClose: () => void
user: AdminUser | null
isOpen: boolean;
onClose: () => void;
user: AdminUser | null;
}) {
const { updateUser, deleteUser, getUserDetail } = useAdminUsers()
const { user: currentUser } = useAuth()
const { updateUser, deleteUser, getUserDetail } = useAdminUsers();
const { user: currentUser } = useAuth();
const isSelf = currentUser && user && currentUser.id.toString() === user.id.toString()
const isSelf =
currentUser && user && currentUser.id.toString() === user.id.toString();
const [form, setForm] = useState<EditUserForm>({
nickname: "",
email: "",
nickname: '',
email: '',
is_admin: false,
password: "",
})
const [saving, setSaving] = useState(false)
const [errors, setErrors] = useState<Record<string, string>>({})
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
password: '',
});
const [saving, setSaving] = useState(false);
const [errors, setErrors] = useState<Record<string, string>>({});
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
useEffect(() => {
let active = true
let active = true;
if (isOpen && user) {
setForm({
nickname: user.nickname || "",
email: user.email || "",
nickname: user.nickname || '',
email: user.email || '',
is_admin: user.is_admin || false,
password: "",
})
setErrors({})
password: '',
});
setErrors({});
getUserDetail(user.id)
.then((detail) => {
if (active && detail) {
setForm({
nickname: detail.nickname || "",
email: detail.email || "",
nickname: detail.nickname || '',
email: detail.email || '',
is_admin: detail.is_admin || false,
password: "",
})
password: '',
});
}
})
.catch(() => {
// ignore fetching error
})
});
} else {
setSaving(false)
setSaving(false);
}
return () => {
active = false
}
}, [isOpen, user, getUserDetail])
active = false;
};
}, [isOpen, user, getUserDetail]);
const validate = () => {
const newErrors: Record<string, string> = {}
const newErrors: Record<string, string> = {};
if (!form.email.trim()) {
newErrors.email = "邮箱不能为空"
newErrors.email = '邮箱不能为空';
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email.trim())) {
newErrors.email = "邮箱格式不正确"
newErrors.email = '邮箱格式不正确';
}
if (form.password && form.password.length < 8) {
newErrors.password = "密码长度不能少于 8 位"
newErrors.password = '密码长度不能少于 8 位';
}
setErrors(newErrors)
return Object.keys(newErrors).length === 0
}
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
};
const handleSave = async (e: React.FormEvent) => {
e.preventDefault()
if (!user || !validate()) return
e.preventDefault();
if (!user || !validate()) return;
setSaving(true)
setSaving(true);
try {
await updateUser(user.id, {
nickname: form.nickname.trim() || undefined,
email: form.email.trim(),
is_admin: form.is_admin,
password: form.password?.trim() || undefined,
})
onClose()
});
onClose();
} catch {
// Errors are handled by context toast notifications
} finally {
setSaving(false)
}
setSaving(false);
}
};
const handleDelete = async () => {
if (!user) return
setSaving(true)
if (!user) return;
setSaving(true);
try {
await deleteUser(user)
onClose()
await deleteUser(user);
onClose();
} catch {
// Errors are handled by context toast notifications
} finally {
setSaving(false)
setShowDeleteConfirm(false)
}
setSaving(false);
setShowDeleteConfirm(false);
}
};
if (!user) return null
if (!user) return null;
return (
<>
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-w-md">
<DialogContent className='max-w-md'>
<DialogHeader>
<DialogTitle>编辑用户</DialogTitle>
<DialogDescription>
@@ -149,89 +156,102 @@ export function EditUserModal({
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSave} className="space-y-4 pt-2">
<div className="space-y-1.5">
<Label htmlFor="username" className="text-xs text-muted-foreground">用户名 (不可修改)</Label>
<form onSubmit={handleSave} className='space-y-4 pt-2'>
<div className='space-y-1.5'>
<Label
htmlFor='username'
className='text-xs text-muted-foreground'
>
用户名 (不可修改)
</Label>
<Input
id="username"
id='username'
value={user.username}
disabled
className="bg-muted/50 cursor-not-allowed select-none font-mono"
className='bg-muted/50 cursor-not-allowed select-none font-mono'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="nickname">昵称 (选填)</Label>
<div className='space-y-1.5'>
<Label htmlFor='nickname'>昵称 (选填)</Label>
<Input
id="nickname"
id='nickname'
value={form.nickname}
onChange={(e) => setForm((prev) => ({ ...prev, nickname: e.target.value }))}
placeholder="请输入昵称"
onChange={(e) =>
setForm((prev) => ({ ...prev, nickname: e.target.value }))
}
placeholder='请输入昵称'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="email">邮箱</Label>
<div className='space-y-1.5'>
<Label htmlFor='email'>邮箱</Label>
<Input
id="email"
type="email"
id='email'
type='email'
value={form.email}
onChange={(e) => setForm((prev) => ({ ...prev, email: e.target.value }))}
placeholder="请输入邮箱地址"
onChange={(e) =>
setForm((prev) => ({ ...prev, email: e.target.value }))
}
placeholder='请输入邮箱地址'
/>
{errors.email && (
<p className="text-xs text-destructive">{errors.email}</p>
<p className='text-xs text-destructive'>{errors.email}</p>
)}
</div>
<div className="space-y-1.5">
<Label htmlFor="password">重置密码 (选填)</Label>
<div className='space-y-1.5'>
<Label htmlFor='password'>重置密码 (选填)</Label>
<Input
id="password"
type="password"
id='password'
type='password'
value={form.password}
onChange={(e) => setForm((prev) => ({ ...prev, password: e.target.value }))}
placeholder="留空表示保持当前密码,输入则重置 (至少 8 位)"
onChange={(e) =>
setForm((prev) => ({ ...prev, password: e.target.value }))
}
placeholder='留空表示保持当前密码,输入则重置 (至少 8 位)'
/>
{errors.password && (
<p className="text-xs text-destructive">{errors.password}</p>
<p className='text-xs text-destructive'>{errors.password}</p>
)}
</div>
<div className="flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10">
<div className='flex items-center justify-between rounded-lg border border-dashed p-3 bg-muted/10'>
<div>
<div className="font-medium text-sm">管理员权限</div>
<div className="text-xs text-muted-foreground">
{isSelf ? "不能撤销当前登录用户的管理员权限。" : "开启后此账号将拥有后台管理权限。"}
<div className='font-medium text-sm'>管理员权限</div>
<div className='text-xs text-muted-foreground'>
{isSelf
? '不能撤销当前登录用户的管理员权限。'
: '开启后此账号将拥有后台管理权限。'}
</div>
</div>
<Switch
checked={form.is_admin}
disabled={!!isSelf || saving}
onCheckedChange={async (checked) => {
setForm((prev) => ({ ...prev, is_admin: checked }))
setSaving(true)
setForm((prev) => ({ ...prev, is_admin: checked }));
setSaving(true);
try {
await updateUser(user.id, {
nickname: form.nickname.trim() || undefined,
email: form.email.trim(),
is_admin: checked,
})
});
} catch {
setForm((prev) => ({ ...prev, is_admin: !checked }))
setForm((prev) => ({ ...prev, is_admin: !checked }));
} finally {
setSaving(false)
setSaving(false);
}
}}
/>
</div>
<div className="flex justify-between items-center pt-2 border-t mt-2">
<div className='flex justify-between items-center pt-2 border-t mt-2'>
<div>
{!user.is_admin && (
<Button
type="button"
variant="destructive"
type='button'
variant='destructive'
onClick={() => setShowDeleteConfirm(true)}
disabled={saving}
>
@@ -239,12 +259,17 @@ export function EditUserModal({
</Button>
)}
</div>
<div className="flex gap-2">
<Button variant="outline" type="button" onClick={onClose} disabled={saving}>
<div className='flex gap-2'>
<Button
variant='outline'
type='button'
onClick={onClose}
disabled={saving}
>
取消
</Button>
<Button type="submit" disabled={saving} variant="secondary">
{saving ? "保存中..." : "保存"}
<Button type='submit' disabled={saving} variant='secondary'>
{saving ? '保存中...' : '保存'}
</Button>
</div>
</div>
@@ -257,24 +282,25 @@ export function EditUserModal({
<AlertDialogHeader>
<AlertDialogTitle>确认删除用户</AlertDialogTitle>
<AlertDialogDescription>
确定要删除用户 {user.nickname || user.username} 吗?该操作会移除用户账号,删除后无法撤销。
确定要删除用户 {user.nickname || user.username}{' '}
吗?该操作会移除用户账号,删除后无法撤销。
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={saving}>取消</AlertDialogCancel>
<AlertDialogAction
onClick={(e) => {
e.preventDefault()
handleDelete()
e.preventDefault();
handleDelete();
}}
disabled={saving}
>
{saving && <Loader2 className="size-3 animate-spin mr-1" />}
{saving && <Loader2 className='size-3 animate-spin mr-1' />}
确认删除
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
)
);
}
@@ -1,21 +1,29 @@
"use client"
'use client';
import * as React from "react"
import {Globe, Loader2, Mail, MapPin, ShieldCheck, Smartphone, UserCheck,} from "lucide-react"
import * as React from 'react';
import {
Globe,
Loader2,
Mail,
MapPin,
ShieldCheck,
Smartphone,
UserCheck,
} from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Badge} from "@/components/ui/badge"
import {Avatar, AvatarFallback, AvatarImage} from "@/components/ui/avatar"
import {Sheet, SheetContent, SheetTitle} from "@/components/ui/sheet"
import type {AdminUser} from "@/lib/services/admin"
import {formatDateTime} from "@/lib/utils"
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Sheet, SheetContent, SheetTitle } from '@/components/ui/sheet';
import type { AdminUser } from '@/lib/services/admin';
import { formatDateTime } from '@/lib/utils';
interface UserDetailSheetProps {
selectedUser: AdminUser | null
isOpen: boolean
onOpenChange: (open: boolean) => void
detailLoading: boolean
onStatusToggle: (user: AdminUser) => Promise<void>
selectedUser: AdminUser | null;
isOpen: boolean;
onOpenChange: (open: boolean) => void;
detailLoading: boolean;
onStatusToggle: (user: AdminUser) => Promise<void>;
}
export function UserDetailSheet({
@@ -25,36 +33,43 @@ export function UserDetailSheet({
detailLoading,
onStatusToggle,
}: UserDetailSheetProps) {
const displayValue = (value?: string) => value && value.trim() ? value : "-"
const displayValue = (value?: string) =>
value && value.trim() ? value : '-';
return (
<Sheet open={isOpen} onOpenChange={onOpenChange}>
<SheetContent className="sm:max-w-[400px] w-full p-0 flex flex-col gap-0">
<SheetTitle className="px-5 py-3">用户档案</SheetTitle>
<SheetContent className='sm:max-w-[400px] w-full p-0 flex flex-col gap-0'>
<SheetTitle className='px-5 py-3'>用户档案</SheetTitle>
{selectedUser && (
<>
<div className="flex-1 overflow-y-auto scrollbar-thin scrollbar-thumb-border scrollbar-track-transparent">
<div className="flex flex-col pb-6">
<div className="px-5 py-6 border-b border-border/50">
<div className="flex flex-col items-center text-center gap-3">
<Avatar className="h-20 w-20 rounded-full border-4 border-background ring-1 ring-border/20">
<div className='flex-1 overflow-y-auto scrollbar-thin scrollbar-thumb-border scrollbar-track-transparent'>
<div className='flex flex-col pb-6'>
<div className='px-5 py-6 border-b border-border/50'>
<div className='flex flex-col items-center text-center gap-3'>
<Avatar className='h-20 w-20 rounded-full border-4 border-background ring-1 ring-border/20'>
<AvatarImage src={selectedUser.avatar_url} />
<AvatarFallback className="rounded-full text-xl font-medium bg-secondary text-secondary-foreground">
<AvatarFallback className='rounded-full text-xl font-medium bg-secondary text-secondary-foreground'>
{selectedUser.username.substring(0, 2).toUpperCase()}
</AvatarFallback>
</Avatar>
<div className="space-y-1.5">
<h3 className="text-lg font-bold tracking-tight">{selectedUser.nickname}</h3>
<div className="flex items-center justify-center gap-2">
<code className="px-1.5 py-0.5 rounded-md bg-muted text-[10px] font-mono text-muted-foreground">@{selectedUser.username}</code>
<Badge variant="secondary" className="h-4.5 px-1.5 text-[9px] uppercase font-medium">
<div className='space-y-1.5'>
<h3 className='text-lg font-bold tracking-tight'>
{selectedUser.nickname}
</h3>
<div className='flex items-center justify-center gap-2'>
<code className='px-1.5 py-0.5 rounded-md bg-muted text-[10px] font-mono text-muted-foreground'>
@{selectedUser.username}
</code>
<Badge
variant='secondary'
className='h-4.5 px-1.5 text-[9px] uppercase font-medium'
>
UID: {selectedUser.id}
</Badge>
{selectedUser.is_admin && (
<Badge className="h-4.5 px-1.5 text-[9px] uppercase font-medium">
<Badge className='h-4.5 px-1.5 text-[9px] uppercase font-medium'>
Admin
</Badge>
)}
@@ -62,91 +77,132 @@ export function UserDetailSheet({
</div>
{detailLoading && (
<div className="flex items-center gap-1 text-[10px] text-muted-foreground">
<Loader2 className="size-3 animate-spin" />
<div className='flex items-center gap-1 text-[10px] text-muted-foreground'>
<Loader2 className='size-3 animate-spin' />
正在刷新详情
</div>
)}
<div className="gap-4 w-full max-w-[240px] mt-1 pt-4 border-t border-border/50">
<div className="flex flex-col gap-0.5">
<span className="text-[9px] uppercase tracking-widest text-muted-foreground font-medium">注册时间</span>
<span className="font-mono text-xs font-semibold">{formatDateTime(selectedUser.created_at).split(' ')[0]}</span>
<div className='gap-4 w-full max-w-[240px] mt-1 pt-4 border-t border-border/50'>
<div className='flex flex-col gap-0.5'>
<span className='text-[9px] uppercase tracking-widest text-muted-foreground font-medium'>
注册时间
</span>
<span className='font-mono text-xs font-semibold'>
{
formatDateTime(selectedUser.created_at).split(
' ',
)[0]
}
</span>
</div>
</div>
</div>
</div>
<div className="p-6 space-y-6">
<div className="space-y-4">
<h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1">个人资料</h4>
<div className="rounded-lg border divide-y bg-background/50">
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<Mail className="size-3" />
<div className='p-6 space-y-6'>
<div className='space-y-4'>
<h4 className='text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1'>
个人资料
</h4>
<div className='rounded-lg border divide-y bg-background/50'>
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<Mail className='size-3' />
邮箱
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.email)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.email)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<Smartphone className="size-3" />
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<Smartphone className='size-3' />
手机
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.phone)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.phone)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<span className="size-3 flex items-center justify-center font-bold text-[9px]">⚧</span>
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<span className='size-3 flex items-center justify-center font-bold text-[9px]'>
⚧
</span>
性别
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.gender)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.gender)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<MapPin className="size-3" />
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<MapPin className='size-3' />
所在地
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.location)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.location)}
</span>
</div>
<div className="flex items-center justify-between gap-4 p-3.5 text-sm">
<span className="flex items-center gap-2 text-[10px] text-muted-foreground">
<Globe className="size-3" />
<div className='flex items-center justify-between gap-4 p-3.5 text-sm'>
<span className='flex items-center gap-2 text-[10px] text-muted-foreground'>
<Globe className='size-3' />
网站
</span>
<span className="min-w-0 truncate text-right text-[10px]">{displayValue(selectedUser.website)}</span>
<span className='min-w-0 truncate text-right text-[10px]'>
{displayValue(selectedUser.website)}
</span>
</div>
<div className="flex flex-col gap-2 p-3.5 text-sm">
<span className="text-[10px] text-muted-foreground">简介</span>
<span className="break-words text-[10px] leading-5">{displayValue(selectedUser.bio)}</span>
<div className='flex flex-col gap-2 p-3.5 text-sm'>
<span className='text-[10px] text-muted-foreground'>
简介
</span>
<span className='break-words text-[10px] leading-5'>
{displayValue(selectedUser.bio)}
</span>
</div>
</div>
</div>
<div className="space-y-4">
<h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1">系统记录</h4>
<div className="rounded-lg border divide-y bg-background/50">
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">账户状态</span>
<Badge variant={selectedUser.is_active ? "secondary" : "outline"} className="text-[10px]">
{selectedUser.is_active ? "正常" : "禁用"}
<div className='space-y-4'>
<h4 className='text-xs font-semibold text-muted-foreground uppercase tracking-wider px-1'>
系统记录
</h4>
<div className='rounded-lg border divide-y bg-background/50'>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>账户状态</span>
<Badge
variant={
selectedUser.is_active ? 'secondary' : 'outline'
}
className='text-[10px]'
>
{selectedUser.is_active ? '正常' : '禁用'}
</Badge>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">管理员</span>
<span className="font-mono text-[10px]">{selectedUser.is_admin ? "是" : "否"}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>管理员</span>
<span className='font-mono text-[10px]'>
{selectedUser.is_admin ? '是' : '否'}
</span>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">最后登录</span>
<span className="font-mono text-[10px]">{formatDateTime(selectedUser.last_login_at)}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>最后登录</span>
<span className='font-mono text-[10px]'>
{formatDateTime(selectedUser.last_login_at)}
</span>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">注册时间</span>
<span className="font-mono text-[10px]">{formatDateTime(selectedUser.created_at)}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>注册时间</span>
<span className='font-mono text-[10px]'>
{formatDateTime(selectedUser.created_at)}
</span>
</div>
<div className="flex items-center justify-between p-3.5 text-sm">
<span className="text-[10px]">最后更新</span>
<span className="font-mono text-[10px]">{formatDateTime(selectedUser.updated_at)}</span>
<div className='flex items-center justify-between p-3.5 text-sm'>
<span className='text-[10px]'>最后更新</span>
<span className='font-mono text-[10px]'>
{formatDateTime(selectedUser.updated_at)}
</span>
</div>
</div>
</div>
@@ -154,20 +210,20 @@ export function UserDetailSheet({
</div>
{!selectedUser.is_admin && (
<div className="p-4 border-t bg-background/80 backdrop-blur-md shrink-0">
<div className='p-4 border-t bg-background/80 backdrop-blur-md shrink-0'>
<Button
variant={selectedUser.is_active ? "destructive" : "default"}
className="w-full h-9 text-xs font-medium transition-all active:scale-[0.98]"
variant={selectedUser.is_active ? 'destructive' : 'default'}
className='w-full h-9 text-xs font-medium transition-all active:scale-[0.98]'
onClick={() => onStatusToggle(selectedUser)}
>
{selectedUser.is_active ? (
<>
<ShieldCheck className="size-3 mr-1" />
<ShieldCheck className='size-3 mr-1' />
封禁账户
</>
) : (
<>
<UserCheck className="size-3 mr-1" />
<UserCheck className='size-3 mr-1' />
解除封禁
</>
)}
@@ -179,5 +235,5 @@ export function UserDetailSheet({
)}
</SheetContent>
</Sheet>
)
);
}
@@ -1,14 +1,27 @@
"use client"
'use client';
import * as React from "react"
import {ChevronDown, ChevronLeft, ChevronRight, Filter, Loader2, Search, X} from "lucide-react"
import * as React from 'react';
import {
ChevronDown,
ChevronLeft,
ChevronRight,
Filter,
Loader2,
Search,
X,
} from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Badge} from "@/components/ui/badge"
import {Separator} from "@/components/ui/separator"
import {DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger} from "@/components/ui/dropdown-menu"
import {useAdminUsers} from "@/contexts/admin-users-context"
import {cn} from "@/lib/utils"
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Separator } from '@/components/ui/separator';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { useAdminUsers } from '@/contexts/admin-users-context';
import { cn } from '@/lib/utils';
export function UserFilterBar() {
const {
@@ -26,33 +39,35 @@ export function UserFilterBar() {
setSearchUsername,
setSearchEmail,
setStatusFilter,
fetchUsers
} = useAdminUsers()
fetchUsers,
} = useAdminUsers();
const totalPages = Math.ceil(total / pageSize)
const hasSearchFilter = Boolean(searchUserId || searchUsername || searchEmail)
const totalPages = Math.ceil(total / pageSize);
const hasSearchFilter = Boolean(
searchUserId || searchUsername || searchEmail,
);
return (
<div className="flex flex-col lg:flex-row lg:items-center lg:justify-between gap-3">
<div className="flex items-center gap-2 flex-wrap">
<div className='flex flex-col lg:flex-row lg:items-center lg:justify-between gap-3'>
<div className='flex items-center gap-2 flex-wrap'>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
className={cn(
"h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0",
hasSearchFilter && "bg-primary/5 border-primary/20"
'h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0',
hasSearchFilter && 'bg-primary/5 border-primary/20',
)}
>
<Search className="size-3 mr-1" />
<Search className='size-3 mr-1' />
搜索
{hasSearchFilter && (
<>
<Separator orientation="vertical" className="mx-1" />
<Separator orientation='vertical' className='mx-1' />
<Badge
variant="default"
className="text-[10px] h-3 px-1 rounded-full"
variant='default'
className='text-[10px] h-3 px-1 rounded-full'
>
!
</Badge>
@@ -60,35 +75,35 @@ export function UserFilterBar() {
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-56 p-3" align="start">
<div className="space-y-2.5">
<DropdownMenuContent className='w-56 p-3' align='start'>
<div className='space-y-2.5'>
<input
className="w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background"
placeholder="输入用户 ID..."
className='w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background'
placeholder='输入用户 ID...'
value={searchUserId}
onChange={(e) => setSearchUserId(e.target.value)}
/>
<input
className="w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background"
placeholder="输入 username..."
className='w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background'
placeholder='输入 username...'
value={searchUsername}
onChange={(e) => setSearchUsername(e.target.value)}
/>
<input
className="w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background"
placeholder="输入邮箱..."
className='w-full h-7 px-2 text-xs border border-dashed rounded-md outline-none focus:border-primary bg-background'
placeholder='输入邮箱...'
value={searchEmail}
onChange={(e) => setSearchEmail(e.target.value)}
/>
{hasSearchFilter && (
<Button
variant="ghost"
size="sm"
className="w-full h-6 text-xs"
variant='ghost'
size='sm'
className='w-full h-6 text-xs'
onClick={() => {
setSearchUserId("")
setSearchUsername("")
setSearchEmail("")
setSearchUserId('');
setSearchUsername('');
setSearchEmail('');
}}
>
清除
@@ -101,21 +116,21 @@ export function UserFilterBar() {
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button
variant="outline"
size="sm"
variant='outline'
size='sm'
className={cn(
"h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0",
statusFilter !== 'all' && "bg-primary/5 border-primary/20"
'h-5 border-dashed text-[10px] font-medium shadow-none focus-visible:ring-0',
statusFilter !== 'all' && 'bg-primary/5 border-primary/20',
)}
>
<Filter className="size-3" />
<Filter className='size-3' />
状态
{statusFilter !== 'all' && (
<>
<Separator orientation="vertical" className="mx-1" />
<Separator orientation='vertical' className='mx-1' />
<Badge
variant="default"
className="text-[10px] h-3 px-1 rounded-full"
variant='default'
className='text-[10px] h-3 px-1 rounded-full'
>
1
</Badge>
@@ -123,107 +138,130 @@ export function UserFilterBar() {
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent className="w-[120px]" align="start">
<DropdownMenuContent className='w-[120px]' align='start'>
<DropdownMenuItem
onSelect={(e) => { e.preventDefault(); setStatusFilter('all') }}
onSelect={(e) => {
e.preventDefault();
setStatusFilter('all');
}}
>
<div className={cn(
"mr-2 flex size-3 items-center justify-center rounded-sm border border-primary",
<div
className={cn(
'mr-2 flex size-3 items-center justify-center rounded-sm border border-primary',
statusFilter === 'all'
? "bg-primary text-primary-foreground"
: "opacity-50"
)} />
<span className="text-xs">全部状态</span>
? 'bg-primary text-primary-foreground'
: 'opacity-50',
)}
/>
<span className='text-xs'>全部状态</span>
</DropdownMenuItem>
<DropdownMenuItem
onSelect={(e) => { e.preventDefault(); setStatusFilter('active') }}
onSelect={(e) => {
e.preventDefault();
setStatusFilter('active');
}}
>
<div className={cn(
"mr-2 flex size-3 items-center justify-center rounded-sm border border-primary",
<div
className={cn(
'mr-2 flex size-3 items-center justify-center rounded-sm border border-primary',
statusFilter === 'active'
? "bg-primary text-primary-foreground"
: "opacity-50"
)} />
<span className="text-xs">正常</span>
? 'bg-primary text-primary-foreground'
: 'opacity-50',
)}
/>
<span className='text-xs'>正常</span>
</DropdownMenuItem>
<DropdownMenuItem
onSelect={(e) => { e.preventDefault(); setStatusFilter('inactive') }}
onSelect={(e) => {
e.preventDefault();
setStatusFilter('inactive');
}}
>
<div className={cn(
"mr-2 flex size-3 items-center justify-center rounded-sm border border-primary",
<div
className={cn(
'mr-2 flex size-3 items-center justify-center rounded-sm border border-primary',
statusFilter === 'inactive'
? "bg-primary text-primary-foreground"
: "opacity-50"
)} />
<span className="text-xs">禁用</span>
? 'bg-primary text-primary-foreground'
: 'opacity-50',
)}
/>
<span className='text-xs'>禁用</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
{(hasSearchFilter || statusFilter !== 'all') && (
<>
<Separator orientation="vertical" className="h-6 hidden sm:block" />
<Separator orientation='vertical' className='h-6 hidden sm:block' />
<Button
variant="ghost"
size="sm"
variant='ghost'
size='sm'
onClick={() => {
setSearchUserId("")
setSearchUsername("")
setSearchEmail("")
setStatusFilter('all')
setSearchUserId('');
setSearchUsername('');
setSearchEmail('');
setStatusFilter('all');
}}
className="h-5 px-2 lg:px-3 text-[11px] font-medium text-muted-foreground hover:text-foreground"
className='h-5 px-2 lg:px-3 text-[11px] font-medium text-muted-foreground hover:text-foreground'
>
<X className="size-3" />
<X className='size-3' />
清空筛选
</Button>
</>
)}
</div>
<Separator className="lg:hidden" />
<Separator className='lg:hidden' />
<div className="flex items-center gap-1.5 self-end lg:self-auto">
<span className="text-[10px] text-muted-foreground whitespace-nowrap">
<div className='flex items-center gap-1.5 self-end lg:self-auto'>
<span className='text-[10px] text-muted-foreground whitespace-nowrap'>
{total} 条记录
</span>
<div className="flex items-center border border-dashed rounded-md shadow-none">
<div className='flex items-center border border-dashed rounded-md shadow-none'>
<Button
variant="ghost"
size="icon"
className="h-5.5 w-6 rounded-none rounded-l-md disabled:opacity-30"
variant='ghost'
size='icon'
className='h-5.5 w-6 rounded-none rounded-l-md disabled:opacity-30'
onClick={() => setPage(Math.max(1, page - 1))}
disabled={page <= 1 || loading}
>
<ChevronLeft className="size-3" />
<ChevronLeft className='size-3' />
</Button>
<span className="text-[10px] font-mono text-muted-foreground px-2 border-x border-dashed">
<span className='text-[10px] font-mono text-muted-foreground px-2 border-x border-dashed'>
{page}/{totalPages}
</span>
<Button
variant="ghost"
size="icon"
className="h-5.5 w-6 rounded-none rounded-r-md disabled:opacity-30"
variant='ghost'
size='icon'
className='h-5.5 w-6 rounded-none rounded-r-md disabled:opacity-30'
onClick={() => setPage(Math.min(totalPages, page + 1))}
disabled={page >= totalPages || loading}
>
<ChevronRight className="size-3" />
<ChevronRight className='size-3' />
</Button>
</div>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="sm" className="h-6 border-dashed text-[10px] px-2 font-mono shadow-none" disabled={loading}>
<Button
variant='outline'
size='sm'
className='h-6 border-dashed text-[10px] px-2 font-mono shadow-none'
disabled={loading}
>
{pageSize}条/页
<ChevronDown className="size-3 opacity-50" />
<ChevronDown className='size-3 opacity-50' />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{[20, 50, 100].map(size => (
<DropdownMenuContent align='end'>
{[20, 50, 100].map((size) => (
<DropdownMenuItem
key={size}
onClick={() => setPageSize(size)}
className={cn("font-mono text-xs", pageSize === size && "bg-accent")}
className={cn(
'font-mono text-xs',
pageSize === size && 'bg-accent',
)}
>
{size}条/页
</DropdownMenuItem>
@@ -232,16 +270,16 @@ export function UserFilterBar() {
</DropdownMenu>
<Button
variant="outline"
size="icon"
className="h-6 w-6 border-dashed shadow-none"
variant='outline'
size='icon'
className='h-6 w-6 border-dashed shadow-none'
onClick={() => fetchUsers(true)}
disabled={loading}
title="刷新数据"
title='刷新数据'
>
<Loader2 className={cn("size-3", loading && "animate-spin")} />
<Loader2 className={cn('size-3', loading && 'animate-spin')} />
</Button>
</div>
</div>
)
);
}
+160 -103
View File
@@ -1,82 +1,93 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {Button} from "@/components/ui/button"
import {Switch} from "@/components/ui/switch"
import {Table, TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import {Badge} from "@/components/ui/badge"
import {Avatar, AvatarFallback, AvatarImage} from "@/components/ui/avatar"
import {Eye, Layers, Plus, Pencil, UserRound, UserX,} from "lucide-react"
import {Tooltip, TooltipContent, TooltipProvider, TooltipTrigger} from "@/components/ui/tooltip"
import { useEffect, useState } from 'react';
import { Button } from '@/components/ui/button';
import { Switch } from '@/components/ui/switch';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { Badge } from '@/components/ui/badge';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Eye, Layers, Pencil, Plus, UserRound, UserX } from 'lucide-react';
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from '@/components/ui/tooltip';
import type {AdminUser} from "@/lib/services/admin"
import {formatDateTime} from "@/lib/utils"
import {EmptyStateWithBorder} from "@/components/layout/empty"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {ErrorInline} from "@/components/layout/error"
import {useAdminUsers} from "@/contexts/admin-users-context"
import {CreateUserModal} from "./components/create-user-modal"
import {UserFilterBar} from "./components/user-filter-bar"
import {UserDetailSheet} from "./components/user-detail-sheet"
import {EditUserModal} from "./components/edit-user-modal"
import type { AdminUser } from '@/lib/services/admin';
import { formatDateTime } from '@/lib/utils';
import { EmptyStateWithBorder } from '@/components/layout/empty';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { ErrorInline } from '@/components/layout/error';
import { useAdminUsers } from '@/contexts/admin-users-context';
import { CreateUserModal } from './components/create-user-modal';
import { UserFilterBar } from './components/user-filter-bar';
import { UserDetailSheet } from './components/user-detail-sheet';
import { EditUserModal } from './components/edit-user-modal';
export default function UsersPage() {
const {
users,
loading,
error,
fetchUsers,
getUserDetail,
updateUserStatus
} = useAdminUsers()
const { users, loading, error, fetchUsers, getUserDetail, updateUserStatus } =
useAdminUsers();
const [selectedUser, setSelectedUser] = useState<AdminUser | null>(null)
const [detailOpen, setDetailOpen] = useState(false)
const [detailLoading, setDetailLoading] = useState(false)
const [createModalOpen, setCreateModalOpen] = useState(false)
const [editTarget, setEditTarget] = useState<AdminUser | null>(null)
const [selectedUser, setSelectedUser] = useState<AdminUser | null>(null);
const [detailOpen, setDetailOpen] = useState(false);
const [detailLoading, setDetailLoading] = useState(false);
const [createModalOpen, setCreateModalOpen] = useState(false);
const [editTarget, setEditTarget] = useState<AdminUser | null>(null);
useEffect(() => {
fetchUsers()
}, [fetchUsers])
fetchUsers();
}, [fetchUsers]);
const handleStatusToggle = async (user: AdminUser) => {
await updateUserStatus(user)
await updateUserStatus(user);
if (selectedUser?.id === user.id) {
setSelectedUser(prev => prev ? { ...prev, is_active: !prev.is_active } : null)
}
setSelectedUser((prev) =>
prev ? { ...prev, is_active: !prev.is_active } : null,
);
}
};
const handleShowDetail = async (user: AdminUser) => {
setSelectedUser(user)
setDetailOpen(true)
setDetailLoading(true)
setSelectedUser(user);
setDetailOpen(true);
setDetailLoading(true);
try {
const detail = await getUserDetail(user.id)
setSelectedUser(detail)
const detail = await getUserDetail(user.id);
setSelectedUser(detail);
} catch {
setSelectedUser(user)
setSelectedUser(user);
} finally {
setDetailLoading(false)
setDetailLoading(false);
}
}
};
return (
<div className="py-6 space-y-4">
<div className='py-6 space-y-4'>
{/* 顶部标题栏 */}
<div className="flex items-center justify-between pb-2">
<div className="flex items-center gap-2">
<UserRound className="size-5 text-primary" />
<div className='flex items-center justify-between pb-2'>
<div className='flex items-center gap-2'>
<UserRound className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">用户管理</h1>
<h1 className='text-2xl font-semibold tracking-tight'>用户管理</h1>
</div>
</div>
<Button variant="secondary" size="sm" className="h-7 text-xs" onClick={() => setCreateModalOpen(true)}>
<Plus className="size-3.5 mr-1" />
<Button
variant='secondary'
size='sm'
className='h-7 text-xs'
onClick={() => setCreateModalOpen(true)}
>
<Plus className='size-3.5 mr-1' />
新增用户
</Button>
</div>
@@ -85,75 +96,108 @@ export default function UsersPage() {
<UserFilterBar />
{error ? (
<div className="p-8 border border-dashed rounded-lg">
<ErrorInline error={error} onRetry={() => fetchUsers(true)} className="justify-center" />
<div className='p-8 border border-dashed rounded-lg'>
<ErrorInline
error={error}
onRetry={() => fetchUsers(true)}
className='justify-center'
/>
</div>
) : loading && users.length === 0 ? (
<LoadingStateWithBorder icon={Layers} description="加载用户列表中..." />
<LoadingStateWithBorder icon={Layers} description='加载用户列表中...' />
) : users.length === 0 ? (
<EmptyStateWithBorder icon={UserX} description="暂无用户数据" />
<EmptyStateWithBorder icon={UserX} description='暂无用户数据' />
) : (
<div className="border border-dashed shadow-none rounded-lg overflow-hidden">
<div className='border border-dashed shadow-none rounded-lg overflow-hidden'>
<TooltipProvider delayDuration={0}>
<Table className="w-full caption-bottom text-sm min-w-full">
<TableHeader className="sticky top-0 z-20 bg-background">
<TableRow className="border-b border-dashed hover:bg-transparent">
<TableHead className="w-[90px] whitespace-nowrap py-2 h-8">ID</TableHead>
<TableHead className="w-[120px] whitespace-nowrap py-2 h-8">用户</TableHead>
<TableHead className="whitespace-nowrap min-w-[160px] py-2 h-8 pl-4">邮箱</TableHead>
<TableHead className="whitespace-nowrap min-w-[140px] py-2 h-8">上次登陆</TableHead>
<TableHead className="whitespace-nowrap min-w-[140px] py-2 h-8">注册时间</TableHead>
<TableHead className="whitespace-nowrap min-w-[140px] py-2 h-8">上次更新</TableHead>
<TableHead className="sticky right-0 text-center bg-background z-10 w-[110px] py-2 h-8">操作</TableHead>
<Table className='w-full caption-bottom text-sm min-w-full'>
<TableHeader className='sticky top-0 z-20 bg-background'>
<TableRow className='border-b border-dashed hover:bg-transparent'>
<TableHead className='w-[90px] whitespace-nowrap py-2 h-8'>
ID
</TableHead>
<TableHead className='w-[120px] whitespace-nowrap py-2 h-8'>
用户
</TableHead>
<TableHead className='whitespace-nowrap min-w-[160px] py-2 h-8 pl-4'>
邮箱
</TableHead>
<TableHead className='whitespace-nowrap min-w-[140px] py-2 h-8'>
上次登陆
</TableHead>
<TableHead className='whitespace-nowrap min-w-[140px] py-2 h-8'>
注册时间
</TableHead>
<TableHead className='whitespace-nowrap min-w-[140px] py-2 h-8'>
上次更新
</TableHead>
<TableHead className='sticky right-0 text-center bg-background z-10 w-[110px] py-2 h-8'>
操作
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{users.map((user) => (
<TableRow
key={user.id}
className="border-dashed hover:bg-muted/30 cursor-pointer group"
className='border-dashed hover:bg-muted/30 cursor-pointer group'
onClick={() => handleShowDetail(user)}
>
<TableCell className="font-mono text-[11px] text-muted-foreground py-1">{user.id}</TableCell>
<TableCell className="py-1">
<div className="flex items-center gap-2">
<Avatar className="h-7 w-7 rounded-sm border">
<TableCell className='font-mono text-[11px] text-muted-foreground py-1'>
{user.id}
</TableCell>
<TableCell className='py-1'>
<div className='flex items-center gap-2'>
<Avatar className='h-7 w-7 rounded-sm border'>
<AvatarImage src={user.avatar_url} />
<AvatarFallback className="rounded-sm text-[10px]">
<AvatarFallback className='rounded-sm text-[10px]'>
{user.username.substring(0, 2).toUpperCase()}
</AvatarFallback>
</Avatar>
<div className="flex flex-col gap-0">
<div className="flex items-center gap-1.5">
<span className="font-medium text-[11px] leading-tight max-w-[100px] truncate" title={user.nickname}>{user.nickname}</span>
<div className='flex flex-col gap-0'>
<div className='flex items-center gap-1.5'>
<span
className='font-medium text-[11px] leading-tight max-w-[100px] truncate'
title={user.nickname}
>
{user.nickname}
</span>
{user.is_admin && (
<Badge variant="secondary" className="text-[9px] h-3.5 px-0.5 rounded-[2px] font-normal leading-none tracking-tighter">
<Badge
variant='secondary'
className='text-[9px] h-3.5 px-0.5 rounded-[2px] font-normal leading-none tracking-tighter'
>
ADM
</Badge>
)}
</div>
<div className="flex items-center gap-1.5">
<span className="text-[10px] text-muted-foreground font-mono leading-tight">@{user.username}</span>
<div className='flex items-center gap-1.5'>
<span className='text-[10px] text-muted-foreground font-mono leading-tight'>
@{user.username}
</span>
</div>
</div>
</div>
</TableCell>
<TableCell className="text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1 pl-4">
{user.email || "-"}
<TableCell className='text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1 pl-4'>
{user.email || '-'}
</TableCell>
<TableCell className="text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1 pl-4">
<TableCell className='text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1 pl-4'>
{formatDateTime(user.last_login_at)}
</TableCell>
<TableCell className="text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1">
<TableCell className='text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1'>
{formatDateTime(user.created_at)}
</TableCell>
<TableCell className="text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1">
<TableCell className='text-[10px] text-muted-foreground font-mono whitespace-nowrap py-1'>
{formatDateTime(user.updated_at)}
</TableCell>
<TableCell className="sticky right-0 text-center bg-background z-10 py-1" onClick={(e) => e.stopPropagation()}>
<div className="flex items-center justify-center gap-0.5">
<TableCell
className='sticky right-0 text-center bg-background z-10 py-1'
onClick={(e) => e.stopPropagation()}
>
<div className='flex items-center justify-center gap-0.5'>
<Tooltip>
<TooltipTrigger asChild>
<div>
@@ -161,38 +205,50 @@ export default function UsersPage() {
checked={user.is_active}
onCheckedChange={() => handleStatusToggle(user)}
disabled={user.is_admin}
className="scale-75"
className='scale-75'
/>
</div>
</TooltipTrigger>
<TooltipContent side="top" className="text-xs">
{user.is_admin ? '管理员账户' : user.is_active ? '禁用账户' : '启用账户'}
<TooltipContent side='top' className='text-xs'>
{user.is_admin
? '管理员账户'
: user.is_active
? '禁用账户'
: '启用账户'}
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button variant="ghost" size="icon" className="h-6 w-6 text-muted-foreground hover:text-foreground" onClick={() => handleShowDetail(user)}>
<Eye className="size-3" />
<Button
variant='ghost'
size='icon'
className='h-6 w-6 text-muted-foreground hover:text-foreground'
onClick={() => handleShowDetail(user)}
>
<Eye className='size-3' />
</Button>
</TooltipTrigger>
<TooltipContent side="top" className="text-xs">
<TooltipContent side='top' className='text-xs'>
查看详情
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<Button variant="ghost" size="icon" className="h-6 w-6 text-muted-foreground hover:text-foreground" onClick={() => setEditTarget(user)}>
<Pencil className="size-3" />
<Button
variant='ghost'
size='icon'
className='h-6 w-6 text-muted-foreground hover:text-foreground'
onClick={() => setEditTarget(user)}
>
<Pencil className='size-3' />
</Button>
</TooltipTrigger>
<TooltipContent side="top" className="text-xs">
<TooltipContent side='top' className='text-xs'>
编辑用户
</TooltipContent>
</Tooltip>
</div>
</TableCell>
</TableRow>
@@ -219,10 +275,11 @@ export default function UsersPage() {
onClose={() => setEditTarget(null)}
/>
{/* 新建用户模态弹窗 */}
<CreateUserModal isOpen={createModalOpen} onClose={() => setCreateModalOpen(false)} />
<CreateUserModal
isOpen={createModalOpen}
onClose={() => setCreateModalOpen(false)}
/>
</div>
)
);
}
+15 -13
View File
@@ -1,11 +1,11 @@
"use client"
'use client';
import * as React from "react"
import {motion} from "motion/react"
import * as React from 'react';
import { motion } from 'motion/react';
import {RequireAuth} from "@/components/auth/require-auth"
import {UserFileManager} from "@/components/common/user/file-manager"
import {FolderOpen} from "lucide-react"
import { RequireAuth } from '@/components/auth/require-auth';
import { UserFileManager } from '@/components/common/user/file-manager';
import { FolderOpen } from 'lucide-react';
export default function UserFilesPage() {
return (
@@ -13,15 +13,17 @@ export default function UserFilesPage() {
<motion.div
initial={{ opacity: 0, y: 15 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.35, ease: "easeOut" }}
className="flex w-full flex-col gap-6 py-6"
transition={{ duration: 0.35, ease: 'easeOut' }}
className='flex w-full flex-col gap-6 py-6'
>
{/* 顶部标题区 */}
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 pb-5">
<div className="flex items-center gap-2">
<FolderOpen className="size-5 text-primary" />
<div className='flex flex-col md:flex-row md:items-center justify-between gap-4 pb-5'>
<div className='flex items-center gap-2'>
<FolderOpen className='size-5 text-primary' />
<div>
<h1 className="text-2xl font-semibold tracking-tight">我的文件</h1>
<h1 className='text-2xl font-semibold tracking-tight'>
我的文件
</h1>
</div>
</div>
</div>
@@ -29,5 +31,5 @@ export default function UserFilesPage() {
<UserFileManager />
</motion.div>
</RequireAuth>
)
);
}
+2 -2
View File
@@ -1,5 +1,5 @@
import { HomeMain } from "@/components/common/home/home-main"
import { HomeMain } from '@/components/common/home/home-main';
export default function HomePage() {
return <HomeMain />
return <HomeMain />;
}
+26 -22
View File
@@ -1,47 +1,51 @@
"use client"
import {motion} from "motion/react"
import {usePathname} from "next/navigation"
import {useState} from "react"
import {AppSidebar} from "@/components/layout/sidebar"
import {SiteHeader} from "@/components/layout/header"
import {SidebarInset, SidebarProvider} from "@/components/ui/sidebar"
import {useAuthRedirect} from "@/hooks/use-auth-redirect"
'use client';
import { motion } from 'motion/react';
import { usePathname } from 'next/navigation';
import { useState } from 'react';
import { AppSidebar } from '@/components/layout/sidebar';
import { SiteHeader } from '@/components/layout/header';
import { SidebarInset, SidebarProvider } from '@/components/ui/sidebar';
import { useAuthRedirect } from '@/hooks/use-auth-redirect';
export default function MainLayout({
children,
}: {
children: React.ReactNode
children: React.ReactNode;
}) {
const pathname = usePathname()
const [isFullWidth, setIsFullWidth] = useState(false)
const pathname = usePathname();
const [isFullWidth, setIsFullWidth] = useState(false);
useAuthRedirect()
useAuthRedirect();
return (
<SidebarProvider
className="h-screen"
className='h-screen'
style={
{
"--header-height": "60px",
'--header-height': '60px',
} as React.CSSProperties
}
>
<AppSidebar />
<SidebarInset className="flex flex-col min-w-0 h-screen">
<SiteHeader isFullWidth={isFullWidth} onToggleFullWidth={setIsFullWidth} />
<div className="flex flex-1 flex-col bg-background overflow-y-auto overflow-x-hidden min-w-0 hide-scrollbar">
<div className={`w-full mx-auto px-4 sm:px-6 md:px-8 lg:px-12 min-w-0 transition-all duration-300 ease-in-out ${!isFullWidth ? "max-w-[1320px]" : "max-w-full"}`}>
<SidebarInset className='flex flex-col min-w-0 h-screen'>
<SiteHeader
isFullWidth={isFullWidth}
onToggleFullWidth={setIsFullWidth}
/>
<div className='flex flex-1 flex-col bg-background overflow-y-auto overflow-x-hidden min-w-0 hide-scrollbar'>
<div
className={`w-full mx-auto px-4 sm:px-6 md:px-8 lg:px-12 min-w-0 transition-all duration-300 ease-in-out ${!isFullWidth ? 'max-w-[1320px]' : 'max-w-full'}`}
>
<motion.div
key={pathname}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{
duration: 0.5,
ease: "easeOut",
ease: 'easeOut',
}}
className="w-full"
className='w-full'
>
{children}
</motion.div>
@@ -49,5 +53,5 @@ export default function MainLayout({
</div>
</SidebarInset>
</SidebarProvider>
)
);
}
@@ -1,7 +1,7 @@
"use client"
'use client';
import {AccessTokenMain} from "@/components/common/settings/access-token"
import { AccessTokenMain } from '@/components/common/settings/access-token';
export default function AccessTokenPage() {
return <AccessTokenMain />
return <AccessTokenMain />;
}
@@ -1,5 +1,5 @@
import { AppearanceMain } from "@/components/common/settings/appearance"
import { AppearanceMain } from '@/components/common/settings/appearance';
export default function AppearancePage() {
return <AppearanceMain />
return <AppearanceMain />;
}
+4 -4
View File
@@ -1,11 +1,11 @@
"use client"
'use client';
import {RequireAuth} from "@/components/auth/require-auth"
import { RequireAuth } from '@/components/auth/require-auth';
export default function SettingsLayout({
children,
}: {
children: React.ReactNode
children: React.ReactNode;
}) {
return <RequireAuth>{children}</RequireAuth>
return <RequireAuth>{children}</RequireAuth>;
}
@@ -1,5 +1,5 @@
import { NotificationsMain } from "@/components/common/settings/notifications"
import { NotificationsMain } from '@/components/common/settings/notifications';
export default function NotificationsPage() {
return <NotificationsMain />
return <NotificationsMain />;
}
+49 -37
View File
@@ -1,67 +1,79 @@
"use client"
'use client';
import Link from "next/link"
import {Card, CardContent, CardDescription, CardTitle} from "@/components/ui/card"
import {Bell, Key, Palette, UserRound} from "lucide-react"
import Link from 'next/link';
import {
Card,
CardContent,
CardDescription,
CardTitle,
} from '@/components/ui/card';
import { Bell, Key, Palette, UserRound } from 'lucide-react';
/* 设置项 */
const settingsItems = [
{
title: "个人资料",
description: "查看您的账户信息",
title: '个人资料',
description: '查看您的账户信息',
icon: UserRound,
href: "/settings/profile",
category: "个人设置",
href: '/settings/profile',
category: '个人设置',
},
{
title: "访问令牌 (AccessToken)",
description: "管理用于 API 访问的个人 Token",
title: '访问令牌 (AccessToken)',
description: '管理用于 API 访问的个人 Token',
icon: Key,
href: "/settings/access-token",
category: "个人设置",
href: '/settings/access-token',
category: '个人设置',
},
{
title: "通知设置",
description: "设置您的通知偏好",
title: '通知设置',
description: '设置您的通知偏好',
icon: Bell,
href: "/settings/notifications",
category: "账户设置",
href: '/settings/notifications',
category: '账户设置',
},
{
title: "外观设置",
description: "自定义界面主题 and 显示",
title: '外观设置',
description: '自定义界面主题 and 显示',
icon: Palette,
href: "/settings/appearance",
category: "个人设置",
href: '/settings/appearance',
category: '个人设置',
},
]
];
export default function SettingsPage() {
const groupedSettings = settingsItems.reduce((acc, item) => {
const groupedSettings = settingsItems.reduce(
(acc, item) => {
if (!acc[item.category]) {
acc[item.category] = []
acc[item.category] = [];
}
acc[item.category].push(item)
return acc
}, {} as Record<string, typeof settingsItems>)
acc[item.category].push(item);
return acc;
},
{} as Record<string, typeof settingsItems>,
);
return (
<div className="space-y-6 py-6">
<div className="font-semibold text-lg">设置</div>
<div className='space-y-6 py-6'>
<div className='font-semibold text-lg'>设置</div>
{Object.entries(groupedSettings).map(([category, items]) => (
<div key={category} className="space-y-4">
<div className="font-medium text-sm text-muted-foreground">{category}</div>
<div className="grid grid-cols-2 gap-4">
<div key={category} className='space-y-4'>
<div className='font-medium text-sm text-muted-foreground'>
{category}
</div>
<div className='grid grid-cols-2 gap-4'>
{items.map((item) => (
<Link key={item.href} href={item.href}>
<Card className="py-2 border border-dashed shadow-none hover:bg-muted/50 transition-colors cursor-pointer h-full">
<Card className='py-2 border border-dashed shadow-none hover:bg-muted/50 transition-colors cursor-pointer h-full'>
<CardContent>
<div className="flex items-center gap-4">
<item.icon className="size-5 text-primary" />
<div className='flex items-center gap-4'>
<item.icon className='size-5 text-primary' />
<div>
<CardTitle className="mb-1 text-sm">{item.title}</CardTitle>
<CardDescription className="text-xs">
<CardTitle className='mb-1 text-sm'>
{item.title}
</CardTitle>
<CardDescription className='text-xs'>
{item.description}
</CardDescription>
</div>
@@ -74,5 +86,5 @@ export default function SettingsPage() {
</div>
))}
</div>
)
);
}
@@ -1,5 +1,5 @@
import { ProfileMain } from "@/components/common/settings/profile"
import { ProfileMain } from '@/components/common/settings/profile';
export default function ProfilePage() {
return <ProfileMain />
return <ProfileMain />;
}
@@ -1,5 +1,5 @@
import {redirect} from "next/navigation"
import { redirect } from 'next/navigation';
export default function SecurityPage() {
redirect("/admin/settings")
redirect('/admin/settings');
}
+60 -20
View File
@@ -1,5 +1,5 @@
@import "tailwindcss";
@import "tw-animate-css";
@import 'tailwindcss';
@import 'tw-animate-css';
@plugin "@tailwindcss/typography";
@custom-variant dark (&:is(.dark *));
@@ -8,7 +8,9 @@
--color-background: var(--background);
--color-foreground: var(--foreground);
--font-sans: var(--font-inter), 'PingFang SC', 'Microsoft YaHei', sans-serif;
--font-mono: var(--font-geist-mono), 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', 'PingFang SC', 'Microsoft YaHei', monospace;
--font-mono:
var(--font-geist-mono), 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono',
'PingFang SC', 'Microsoft YaHei', monospace;
--color-sidebar-ring: var(--sidebar-ring);
--color-sidebar-border: var(--sidebar-border);
--color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
@@ -163,19 +165,37 @@
--animate-gold-particle: gold-particle 1s ease-out forwards;
@keyframes redenvelope-shake {
0%, 100% { transform: rotate(0deg); }
25% { transform: rotate(-5deg); }
75% { transform: rotate(5deg); }
0%,
100% {
transform: rotate(0deg);
}
25% {
transform: rotate(-5deg);
}
75% {
transform: rotate(5deg);
}
}
@keyframes redenvelope-glow {
0%, 100% { box-shadow: 0 0 20px rgba(255, 215, 0, 0.3); }
50% { box-shadow: 0 0 40px rgba(255, 215, 0, 0.6); }
0%,
100% {
box-shadow: 0 0 20px rgba(255, 215, 0, 0.3);
}
50% {
box-shadow: 0 0 40px rgba(255, 215, 0, 0.6);
}
}
@keyframes gold-particle {
0% { transform: translateY(0) scale(1); opacity: 1; }
100% { transform: translateY(-100px) scale(0); opacity: 0; }
0% {
transform: translateY(0) scale(1);
opacity: 1;
}
100% {
transform: translateY(-100px) scale(0);
opacity: 0;
}
}
/*
@@ -185,15 +205,35 @@
--animate-bell-ring: bell-ring 0.6s cubic-bezier(0.36, 0.07, 0.19, 0.97);
@keyframes bell-ring {
0% { transform: rotate(0deg); }
10% { transform: rotate(14deg); }
20% { transform: rotate(-12deg); }
30% { transform: rotate(10deg); }
40% { transform: rotate(-8deg); }
50% { transform: rotate(6deg); }
60% { transform: rotate(-4deg); }
70% { transform: rotate(2deg); }
80% { transform: rotate(-1deg); }
100% { transform: rotate(0deg); }
0% {
transform: rotate(0deg);
}
10% {
transform: rotate(14deg);
}
20% {
transform: rotate(-12deg);
}
30% {
transform: rotate(10deg);
}
40% {
transform: rotate(-8deg);
}
50% {
transform: rotate(6deg);
}
60% {
transform: rotate(-4deg);
}
70% {
transform: rotate(2deg);
}
80% {
transform: rotate(-1deg);
}
100% {
transform: rotate(0deg);
}
}
}
+9 -11
View File
@@ -1,17 +1,16 @@
import {ImageResponse} from 'next/og'
import {WavesIcon} from 'lucide-react'
import { ImageResponse } from 'next/og';
import { WavesIcon } from 'lucide-react';
export const dynamic = 'force-static'
export const dynamic = 'force-static';
export const size = {
width: 32,
height: 32,
}
export const contentType = 'image/png'
};
export const contentType = 'image/png';
export default function Icon() {
return new ImageResponse(
(
<div
style={{
background: 'black',
@@ -23,11 +22,10 @@ export default function Icon() {
borderRadius: '20%',
}}
>
<WavesIcon color="white" size={20} />
</div>
),
<WavesIcon color='white' size={20} />
</div>,
{
...size,
}
)
},
);
}
+22 -24
View File
@@ -1,25 +1,25 @@
import type {Metadata} from "next";
import {Inter} from "next/font/google";
import {Toaster} from "@/components/ui/sonner";
import {ThemeProvider} from "@/components/layout/theme-provider";
import {CustomThemeProvider} from "@/lib/theme";
import {BellRingProvider} from "@/contexts/bell-ring-context";
import {NotificationSettingsProvider} from "@/contexts/notification-settings-context";
import {UserProvider} from "@/contexts/user-context";
import {AppQueryProvider} from "@/components/providers/query-provider";
import {SiteTitleUpdater} from "@/components/providers/title-updater";
import {RobotsMeta} from "@/components/layout/robots-meta";
import "./globals.css";
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import { Toaster } from '@/components/ui/sonner';
import { ThemeProvider } from '@/components/layout/theme-provider';
import { CustomThemeProvider } from '@/lib/theme';
import { BellRingProvider } from '@/contexts/bell-ring-context';
import { NotificationSettingsProvider } from '@/contexts/notification-settings-context';
import { UserProvider } from '@/contexts/user-context';
import { AppQueryProvider } from '@/components/providers/query-provider';
import { SiteTitleUpdater } from '@/components/providers/title-updater';
import { RobotsMeta } from '@/components/layout/robots-meta';
import './globals.css';
const inter = Inter({
subsets: ["latin"],
variable: "--font-inter",
display: "swap",
subsets: ['latin'],
variable: '--font-inter',
display: 'swap',
});
export const metadata: Metadata = {
title: "Wavelet Platform",
description: "通用二次开发标准范式平台",
title: 'Wavelet Platform',
description: '通用二次开发标准范式平台',
};
export default function RootLayout({
@@ -29,16 +29,14 @@ export default function RootLayout({
}>) {
return (
<html
lang="zh-CN"
lang='zh-CN'
className={`hide-scrollbar font-sans ${inter.variable}`}
suppressHydrationWarning
>
<body
className="hide-scrollbar font-sans antialiased"
>
<body className='hide-scrollbar font-sans antialiased'>
<ThemeProvider
attribute="class"
defaultTheme="system"
attribute='class'
defaultTheme='system'
enableSystem
disableTransitionOnChange
>
@@ -50,7 +48,7 @@ export default function RootLayout({
<NotificationSettingsProvider>
<BellRingProvider>
{children}
<Toaster position="top-center" />
<Toaster position='top-center' />
</BellRingProvider>
</NotificationSettingsProvider>
</UserProvider>
+30 -28
View File
@@ -1,10 +1,10 @@
"use client"
'use client';
import { useRouter } from "next/navigation";
import Link from "next/link";
import { motion } from "motion/react";
import { Home, ArrowLeft } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import { motion } from 'motion/react';
import { ArrowLeft, Home } from 'lucide-react';
import { Button } from '@/components/ui/button';
/**
* 404 Not Found Page
@@ -14,23 +14,25 @@ export default function NotFound() {
const router = useRouter();
return (
<div className="relative min-h-screen w-full flex flex-col items-center justify-center bg-background overflow-hidden selection:bg-primary/20">
<div className="absolute inset-0 -z-10 overflow-hidden">
<div className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[500px] h-[500px] bg-primary/20 rounded-full blur-[120px] opacity-20 animate-pulse" />
<div className='relative min-h-screen w-full flex flex-col items-center justify-center bg-background overflow-hidden selection:bg-primary/20'>
<div className='absolute inset-0 -z-10 overflow-hidden'>
<div className='absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[500px] h-[500px] bg-primary/20 rounded-full blur-[120px] opacity-20 animate-pulse' />
</div>
<div className="container px-6 flex flex-col items-center text-center z-10">
<div className='container px-6 flex flex-col items-center text-center z-10'>
<motion.div
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
transition={{ duration: 0.8, ease: "easeOut" }}
className="relative mb-6"
transition={{ duration: 0.8, ease: 'easeOut' }}
className='relative mb-6'
>
<h1 className="text-[12rem] md:text-[16rem] font-bold leading-none tracking-tighter text-transparent bg-clip-text bg-gradient-to-b from-foreground/10 to-foreground/5 select-none">
<h1 className='text-[12rem] md:text-[16rem] font-bold leading-none tracking-tighter text-transparent bg-clip-text bg-gradient-to-b from-foreground/10 to-foreground/5 select-none'>
404
</h1>
<div className="absolute inset-0 flex items-center justify-center">
<span className="text-2xl font-medium tracking-[0.2em] text-foreground/80 uppercase">Page Not Found</span>
<div className='absolute inset-0 flex items-center justify-center'>
<span className='text-2xl font-medium tracking-[0.2em] text-foreground/80 uppercase'>
Page Not Found
</span>
</div>
</motion.div>
@@ -38,32 +40,32 @@ export default function NotFound() {
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.2, duration: 0.8 }}
className="space-y-8"
className='space-y-8'
>
<p className="text-muted-foreground max-w-[400px] mx-auto text-sm leading-relaxed">
<p className='text-muted-foreground max-w-[400px] mx-auto text-sm leading-relaxed'>
抱歉,您访问的页面似已迷失在数字星云中。
<br />
请检查路径或返回安全地带。
</p>
<div className="flex justify-center gap-4">
<div className='flex justify-center gap-4'>
<Button
variant="secondary"
size="sm"
variant='secondary'
size='sm'
onClick={() => router.back()}
className="rounded-full w-24 text-xs border-foreground/10 hover:bg-foreground/10 transition-all duration-300"
className='rounded-full w-24 text-xs border-foreground/10 hover:bg-foreground/10 transition-all duration-300'
>
<ArrowLeft className="size-3 opacity-70" />
<ArrowLeft className='size-3 opacity-70' />
上一页
</Button>
<Link href="/">
<Link href='/'>
<Button
variant="default"
size="sm"
className="rounded-full w-24 text-xs hover:bg-primary/80 transition-all duration-300"
variant='default'
size='sm'
className='rounded-full w-24 text-xs hover:bg-primary/80 transition-all duration-300'
>
<Home className="size-3" />
<Home className='size-3' />
首页
</Button>
</Link>
@@ -71,7 +73,7 @@ export default function NotFound() {
</motion.div>
</div>
<div className="absolute bottom-8 text-xs text-muted-foreground/30 font-mono">
<div className='absolute bottom-8 text-xs text-muted-foreground/30 font-mono'>
ERR_HTTP_NOT_FOUND
</div>
</div>
+6 -6
View File
@@ -1,15 +1,15 @@
"use client";
'use client';
import { HeroSection } from "@/components/home/hero-section";
import { DeveloperSection } from "@/components/home/developer-section";
import { FooterSection } from "@/components/home/footer-section";
import { HeroSection } from '@/components/home/hero-section';
import { DeveloperSection } from '@/components/home/developer-section';
import { FooterSection } from '@/components/home/footer-section';
export default function HomePage() {
return (
<div className="relative w-full min-h-screen bg-transparent text-foreground font-sans overflow-x-hidden selection:bg-primary selection:text-primary-foreground z-10">
<div className='relative w-full min-h-screen bg-transparent text-foreground font-sans overflow-x-hidden selection:bg-primary selection:text-primary-foreground z-10'>
<HeroSection />
<DeveloperSection />
<FooterSection />
</div>
)
);
}
+11 -11
View File
@@ -5,9 +5,9 @@ import { motion, type Variants } from 'motion/react';
import {
getVariants,
useAnimateIconContext,
IconWrapper,
type IconProps,
IconWrapper,
useAnimateIconContext,
} from '@/components/animate-ui/icons/icon';
type CheckProps = IconProps<keyof typeof animations>;
@@ -39,23 +39,23 @@ function IconComponent({ size, ...props }: CheckProps) {
return (
<motion.svg
xmlns="http://www.w3.org/2000/svg"
xmlns='http://www.w3.org/2000/svg'
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
viewBox='0 0 24 24'
fill='none'
stroke='currentColor'
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
initial="initial"
strokeLinecap='round'
strokeLinejoin='round'
initial='initial'
animate={controls}
{...props}
>
<motion.path
d="m4 12 5 5L20 6"
d='m4 12 5 5L20 6'
variants={variants.path}
initial="initial"
initial='initial'
animate={controls}
/>
</motion.svg>
@@ -5,9 +5,9 @@ import { motion, type Variants } from 'motion/react';
import {
getVariants,
useAnimateIconContext,
IconWrapper,
type IconProps,
IconWrapper,
useAnimateIconContext,
} from '@/components/animate-ui/icons/icon';
type ChevronDownProps = IconProps<keyof typeof animations>;
@@ -44,21 +44,21 @@ function IconComponent({ size, ...props }: ChevronDownProps) {
return (
<motion.svg
xmlns="http://www.w3.org/2000/svg"
xmlns='http://www.w3.org/2000/svg'
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
viewBox='0 0 24 24'
fill='none'
stroke='currentColor'
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
strokeLinecap='round'
strokeLinejoin='round'
{...props}
>
<motion.path
d="m6 9 6 6 6-6"
d='m6 9 6 6 6-6'
variants={variants.path}
initial="initial"
initial='initial'
animate={controls}
/>
</motion.svg>
@@ -5,9 +5,9 @@ import { motion, type Variants } from 'motion/react';
import {
getVariants,
useAnimateIconContext,
IconWrapper,
type IconProps,
IconWrapper,
useAnimateIconContext,
} from '@/components/animate-ui/icons/icon';
type ChevronLeftProps = IconProps<keyof typeof animations>;
@@ -44,21 +44,21 @@ function IconComponent({ size, ...props }: ChevronLeftProps) {
return (
<motion.svg
xmlns="http://www.w3.org/2000/svg"
xmlns='http://www.w3.org/2000/svg'
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
viewBox='0 0 24 24'
fill='none'
stroke='currentColor'
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
strokeLinecap='round'
strokeLinejoin='round'
{...props}
>
<motion.path
d="m15 18-6-6 6-6"
d='m15 18-6-6 6-6'
variants={variants.path}
initial="initial"
initial='initial'
animate={controls}
/>
</motion.svg>
@@ -5,9 +5,9 @@ import { motion, type Variants } from 'motion/react';
import {
getVariants,
useAnimateIconContext,
IconWrapper,
type IconProps,
IconWrapper,
useAnimateIconContext,
} from '@/components/animate-ui/icons/icon';
type ChevronRightProps = IconProps<keyof typeof animations>;
@@ -44,21 +44,21 @@ function IconComponent({ size, ...props }: ChevronRightProps) {
return (
<motion.svg
xmlns="http://www.w3.org/2000/svg"
xmlns='http://www.w3.org/2000/svg'
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
viewBox='0 0 24 24'
fill='none'
stroke='currentColor'
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
strokeLinecap='round'
strokeLinejoin='round'
{...props}
>
<motion.path
d="m9 18 6-6-6-6"
d='m9 18 6-6-6-6'
variants={variants.path}
initial="initial"
initial='initial'
animate={controls}
/>
</motion.svg>
@@ -5,9 +5,9 @@ import { motion, type Variants } from 'motion/react';
import {
getVariants,
useAnimateIconContext,
IconWrapper,
type IconProps,
IconWrapper,
useAnimateIconContext,
} from '@/components/animate-ui/icons/icon';
type CircleCheckProps = IconProps<keyof typeof animations>;
@@ -40,29 +40,29 @@ function IconComponent({ size, ...props }: CircleCheckProps) {
return (
<motion.svg
xmlns="http://www.w3.org/2000/svg"
xmlns='http://www.w3.org/2000/svg'
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
viewBox='0 0 24 24'
fill='none'
stroke='currentColor'
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
strokeLinecap='round'
strokeLinejoin='round'
{...props}
>
<motion.circle
cx="12"
cy="12"
r="10"
cx='12'
cy='12'
r='10'
variants={variants.circle}
initial="initial"
initial='initial'
animate={controls}
/>
<motion.path
d="m9 12 2 2 4-4"
d='m9 12 2 2 4-4'
variants={variants.path}
initial="initial"
initial='initial'
animate={controls}
/>
</motion.svg>
+10 -7
View File
@@ -2,18 +2,21 @@
import * as React from 'react';
import {
motion,
useAnimation,
type SVGMotionProps,
type UseInViewOptions,
type LegacyAnimationControls,
type Variants,
type HTMLMotionProps,
type LegacyAnimationControls,
motion,
type SVGMotionProps,
useAnimation,
type UseInViewOptions,
type Variants,
} from 'motion/react';
import { cn } from '@/lib/utils';
import { useIsInView } from '@/hooks/use-is-in-view';
import { Slot, type WithAsChild } from '@/components/animate-ui/primitives/animate/slot';
import {
Slot,
type WithAsChild,
} from '@/components/animate-ui/primitives/animate/slot';
const staticAnimations = {
path: {
+13 -13
View File
@@ -5,9 +5,9 @@ import { motion, type Variants } from 'motion/react';
import {
getVariants,
useAnimateIconContext,
IconWrapper,
type IconProps,
IconWrapper,
useAnimateIconContext,
} from '@/components/animate-ui/icons/icon';
type SendProps = IconProps<keyof typeof animations>;
@@ -50,28 +50,28 @@ function IconComponent({ size, ...props }: SendProps) {
return (
<motion.svg
xmlns="http://www.w3.org/2000/svg"
xmlns='http://www.w3.org/2000/svg'
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
viewBox='0 0 24 24'
fill='none'
stroke='currentColor'
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
strokeLinecap='round'
strokeLinejoin='round'
{...props}
>
<motion.g variants={variants.group} initial="initial" animate={controls}>
<motion.g variants={variants.group} initial='initial' animate={controls}>
<motion.path
d="M14.5,21.7c.1.3.4.4.7.3.1,0,.2-.2.3-.3L22,2.7c0-.3,0-.5-.3-.6-.1,0-.2,0-.3,0L2.3,8.5c-.3,0-.4.4-.3.6,0,.1.2.2.3.3l7.9,3.2c.5.2.9.6,1.1,1.1l3.2,7.9Z"
d='M14.5,21.7c.1.3.4.4.7.3.1,0,.2-.2.3-.3L22,2.7c0-.3,0-.5-.3-.6-.1,0-.2,0-.3,0L2.3,8.5c-.3,0-.4.4-.3.6,0,.1.2.2.3.3l7.9,3.2c.5.2.9.6,1.1,1.1l3.2,7.9Z'
variants={variants.path1}
initial="initial"
initial='initial'
animate={controls}
/>
<motion.path
d="M21.9,2.1l-10.9,10.9"
d='M21.9,2.1l-10.9,10.9'
variants={variants.path2}
initial="initial"
initial='initial'
animate={controls}
/>
</motion.g>
@@ -1,9 +1,12 @@
'use client';
import * as React from 'react';
import { motion, type HTMLMotionProps } from 'motion/react';
import { type HTMLMotionProps, motion } from 'motion/react';
import { Slot, type WithAsChild } from '@/components/animate-ui/primitives/animate/slot';
import {
Slot,
type WithAsChild,
} from '@/components/animate-ui/primitives/animate/slot';
type ButtonProps = WithAsChild<
HTMLMotionProps<'button'> & {
@@ -1,10 +1,13 @@
'use client';
import * as React from 'react';
import { motion, type HTMLMotionProps } from 'motion/react';
import { type HTMLMotionProps, motion } from 'motion/react';
import { getStrictContext } from '@/lib/utils/get-strict-context';
import { Slot, type WithAsChild } from '@/components/animate-ui/primitives/animate/slot';
import {
Slot,
type WithAsChild,
} from '@/components/animate-ui/primitives/animate/slot';
type Ripple = {
id: number;
@@ -80,7 +83,7 @@ function RippleButton({
<RippleButtonProvider value={{ ripples, setRipples }}>
<Component
ref={buttonRef}
data-slot="ripple-button"
data-slot='ripple-button'
onClick={handleClick}
whileTap={{ scale: tapScale }}
whileHover={{ scale: hoverScale }}
@@ -2,7 +2,7 @@
import * as React from 'react';
import { Collapsible as CollapsiblePrimitive } from 'radix-ui';
import { AnimatePresence, motion, type HTMLMotionProps } from 'motion/react';
import { AnimatePresence, type HTMLMotionProps, motion } from 'motion/react';
import { getStrictContext } from '@/lib/utils/get-strict-context';
import { useControlledState } from '@/hooks/use-controlled-state';
@@ -27,7 +27,7 @@ function Collapsible(props: CollapsibleProps) {
return (
<CollapsibleProvider value={{ isOpen, setIsOpen }}>
<CollapsiblePrimitive.Root
data-slot="collapsible"
data-slot='collapsible'
{...props}
onOpenChange={setIsOpen}
/>
@@ -41,7 +41,7 @@ type CollapsibleTriggerProps = React.ComponentProps<
function CollapsibleTrigger(props: CollapsibleTriggerProps) {
return (
<CollapsiblePrimitive.Trigger data-slot="collapsible-trigger" {...props} />
<CollapsiblePrimitive.Trigger data-slot='collapsible-trigger' {...props} />
);
}
@@ -65,8 +65,8 @@ function CollapsibleContent({
{keepRendered ? (
<CollapsiblePrimitive.Content asChild forceMount>
<motion.div
key="collapsible-content"
data-slot="collapsible-content"
key='collapsible-content'
data-slot='collapsible-content'
layout
initial={{ opacity: 0, height: 0, overflow: 'hidden', y: 20 }}
animate={
@@ -82,8 +82,8 @@ function CollapsibleContent({
isOpen && (
<CollapsiblePrimitive.Content asChild forceMount>
<motion.div
key="collapsible-content"
data-slot="collapsible-content"
key='collapsible-content'
data-slot='collapsible-content'
layout
initial={{ opacity: 0, height: 0, overflow: 'hidden', y: 20 }}
animate={{ opacity: 1, height: 'auto', overflow: 'hidden', y: 0 }}
@@ -3,10 +3,7 @@
import * as React from 'react';
import { useMotionValue, useSpring, type SpringOptions } from 'motion/react';
import {
useIsInView,
type UseIsInViewOptions,
} from '@/hooks/use-is-in-view';
import { useIsInView, type UseIsInViewOptions } from '@/hooks/use-is-in-view';
type CountingNumberProps = Omit<React.ComponentProps<'span'>, 'children'> & {
number: number;
@@ -98,7 +95,15 @@ function CountingNumber({
}
});
return () => unsubscribe();
}, [springVal, decimals, padStart, number, decimalSeparator, localRef, thousandSeparator]);
}, [
springVal,
decimals,
padStart,
number,
decimalSeparator,
localRef,
thousandSeparator,
]);
const finalIntLength = Math.floor(Math.abs(number)).toString().length;
@@ -114,7 +119,9 @@ function CountingNumber({
const [intPart, fracPart] = out.split(decimalSeparator);
const regex = /\B(?=(\d{3})+(?!\d))/g;
const separatedInt = intPart.replace(regex, thousandSeparator);
out = fracPart ? `${ separatedInt }${ decimalSeparator }${ fracPart }` : separatedInt;
out = fracPart
? `${separatedInt}${decimalSeparator}${fracPart}`
: separatedInt;
}
return out;
};
@@ -127,7 +134,7 @@ function CountingNumber({
const initialText = initiallyStable ? formatValue(number) : zeroText;
return (
<span ref={localRef} data-slot="counting-number" {...props}>
<span ref={localRef} data-slot='counting-number' {...props}>
{initialText}
</span>
);
+33 -29
View File
@@ -1,67 +1,71 @@
"use client"
'use client';
import type {ReactNode} from "react"
import {motion} from "motion/react"
import {WavesIcon} from "lucide-react"
import type { ReactNode } from 'react';
import { motion } from 'motion/react';
import { WavesIcon } from 'lucide-react';
import {cn} from "@/lib/utils"
import { cn } from '@/lib/utils';
interface AuthShellProps {
children: ReactNode
wide?: boolean
children: ReactNode;
wide?: boolean;
}
export function AuthShell({ children, wide = false }: AuthShellProps) {
return (
<main className="relative flex min-h-screen w-full items-center justify-center overflow-x-hidden bg-background px-4 py-6 sm:px-8 sm:py-8 [@media(max-height:700px)]:py-3">
<main className='relative flex min-h-screen w-full items-center justify-center overflow-x-hidden bg-background px-4 py-6 sm:px-8 sm:py-8 [@media(max-height:700px)]:py-3'>
<div
aria-hidden="true"
className="pointer-events-none absolute inset-0 bg-[linear-gradient(to_right,var(--border)_1px,transparent_1px),linear-gradient(to_bottom,var(--border)_1px,transparent_1px)] bg-[size:56px_56px] opacity-45 [mask-image:linear-gradient(to_bottom,transparent,black_12%,black_88%,transparent)]"
aria-hidden='true'
className='pointer-events-none absolute inset-0 bg-[linear-gradient(to_right,var(--border)_1px,transparent_1px),linear-gradient(to_bottom,var(--border)_1px,transparent_1px)] bg-[size:56px_56px] opacity-45 [mask-image:linear-gradient(to_bottom,transparent,black_12%,black_88%,transparent)]'
/>
<div
aria-hidden="true"
className="pointer-events-none absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-border/70"
aria-hidden='true'
className='pointer-events-none absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-border/70'
/>
<motion.section
initial={{ opacity: 0, y: 18 }}
animate={{ opacity: 1, y: 0 }}
transition={{duration: 0.45, ease: "easeOut"}}
transition={{ duration: 0.45, ease: 'easeOut' }}
className={cn(
"relative w-full border-x border-dashed border-border/80 bg-background/95 px-6 py-8 backdrop-blur-sm sm:px-10 sm:py-10 [@media(max-height:700px)]:py-5",
wide ? "max-w-2xl" : "max-w-xl",
'relative w-full border-x border-dashed border-border/80 bg-background/95 px-6 py-8 backdrop-blur-sm sm:px-10 sm:py-10 [@media(max-height:700px)]:py-5',
wide ? 'max-w-2xl' : 'max-w-xl',
)}
>
{children}
</motion.section>
</main>
)
);
}
interface AuthHeadingProps {
title: string
description: string
siteName?: string
title: string;
description: string;
siteName?: string;
}
export function AuthHeading({
title,
description,
siteName = "Wavelet",
siteName = 'Wavelet',
}: AuthHeadingProps) {
return (
<header className="flex flex-col gap-6 [@media(max-height:700px)]:gap-3">
<div className="flex items-center gap-3">
<span className="flex size-9 items-center justify-center rounded-full bg-foreground text-background [@media(max-height:700px)]:size-8">
<WavesIcon aria-hidden="true" />
<header className='flex flex-col gap-6 [@media(max-height:700px)]:gap-3'>
<div className='flex items-center gap-3'>
<span className='flex size-9 items-center justify-center rounded-full bg-foreground text-background [@media(max-height:700px)]:size-8'>
<WavesIcon aria-hidden='true' />
</span>
<span className='font-semibold tracking-tight [@media(min-height:761px)]:text-lg'>
{siteName}
</span>
<span className="font-semibold tracking-tight [@media(min-height:761px)]:text-lg">{siteName}</span>
</div>
<div className="flex flex-col gap-1.5 [@media(max-height:700px)]:gap-1">
<h1 className="text-2xl font-semibold tracking-tight text-foreground sm:text-3xl [@media(min-height:900px)]:sm:text-4xl">
<div className='flex flex-col gap-1.5 [@media(max-height:700px)]:gap-1'>
<h1 className='text-2xl font-semibold tracking-tight text-foreground sm:text-3xl [@media(min-height:900px)]:sm:text-4xl'>
{title}
</h1>
<p className="text-sm text-muted-foreground [@media(min-height:900px)]:text-base">{description}</p>
<p className='text-sm text-muted-foreground [@media(min-height:900px)]:text-base'>
{description}
</p>
</div>
</header>
)
);
}
+59 -46
View File
@@ -1,20 +1,26 @@
'use client'
'use client';
import {useEffect, useRef, useState} from 'react'
import {CheckCircle2, Loader2, ShieldAlert, ShieldCheck, ShieldQuestion} from 'lucide-react'
import {getCapToken} from '@/lib/cap-solver'
import { useEffect, useRef, useState } from 'react';
import {
CheckCircle2,
Loader2,
ShieldAlert,
ShieldCheck,
ShieldQuestion,
} from 'lucide-react';
import { getCapToken } from '@/lib/cap-solver';
type CapStatus = 'idle' | 'solving' | 'solved' | 'error'
type CapStatus = 'idle' | 'solving' | 'solved' | 'error';
interface CapWidgetProps {
/** Called with the one-time token once the challenge is solved */
onToken: (token: string) => void
onToken: (token: string) => void;
/** Called when the challenge fails or encounters an error */
onError?: (err: Error) => void
onError?: (err: Error) => void;
/** Whether to auto-start solving when the component mounts. Defaults to true. */
autoStart?: boolean
autoStart?: boolean;
/** PoW scope sent to the backend */
scope?: string
scope?: string;
}
/**
@@ -23,88 +29,95 @@ interface CapWidgetProps {
* autoStart=true(默认):挂载后立即在后台运行 PoW 求解,完成后回调 onToken。
* autoStart=false:显示「点击开始验证」按钮,用户手动触发后才开始求解。
*/
export function CapWidget({ onToken, onError, autoStart = true, scope = 'login' }: CapWidgetProps) {
const [status, setStatus] = useState<CapStatus>('idle')
const [errorMsg, setErrorMsg] = useState('')
const solving = useRef(false)
export function CapWidget({
onToken,
onError,
autoStart = true,
scope = 'login',
}: CapWidgetProps) {
const [status, setStatus] = useState<CapStatus>('idle');
const [errorMsg, setErrorMsg] = useState('');
const solving = useRef(false);
const solve = async () => {
if (solving.current) return
solving.current = true
setStatus('solving')
setErrorMsg('')
if (solving.current) return;
solving.current = true;
setStatus('solving');
setErrorMsg('');
try {
const token = await getCapToken(scope)
setStatus('solved')
onToken(token)
const token = await getCapToken(scope);
setStatus('solved');
onToken(token);
} catch (err) {
const error = err instanceof Error ? err : new Error(String(err))
setStatus('error')
setErrorMsg(error.message)
onError?.(error)
const error = err instanceof Error ? err : new Error(String(err));
setStatus('error');
setErrorMsg(error.message);
onError?.(error);
} finally {
solving.current = false
}
solving.current = false;
}
};
// Auto-start on mount (only when autoStart is enabled)
useEffect(() => {
if (autoStart) {
void solve()
void solve();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
}, []);
return (
<div className="flex items-center gap-2 rounded-lg border border-border/60 bg-muted/30 px-3 py-2 text-sm">
<div className='flex items-center gap-2 rounded-lg border border-border/60 bg-muted/30 px-3 py-2 text-sm'>
{/* idle + autoStart=false: 手动触发按钮 */}
{status === 'idle' && !autoStart && (
<button
type="button"
className="flex w-full items-center gap-2 text-left"
type='button'
className='flex w-full items-center gap-2 text-left'
onClick={() => void solve()}
>
<ShieldQuestion className="size-4 shrink-0 text-muted-foreground" />
<span className="flex-1 text-muted-foreground">点击开始人机验证</span>
<ShieldQuestion className='size-4 shrink-0 text-muted-foreground' />
<span className='flex-1 text-muted-foreground'>点击开始人机验证</span>
</button>
)}
{/* idle + autoStart=true: 等待自动开始(极短暂状态) */}
{status === 'idle' && autoStart && (
<>
<ShieldAlert className="size-4 shrink-0 text-muted-foreground" />
<span className="text-muted-foreground">等待人机验证…</span>
<ShieldAlert className='size-4 shrink-0 text-muted-foreground' />
<span className='text-muted-foreground'>等待人机验证…</span>
</>
)}
{status === 'solving' && (
<>
<Loader2 className="size-4 shrink-0 animate-spin text-primary" />
<span className="text-muted-foreground">正在完成人机验证…</span>
<Loader2 className='size-4 shrink-0 animate-spin text-primary' />
<span className='text-muted-foreground'>正在完成人机验证…</span>
</>
)}
{status === 'solved' && (
<>
<CheckCircle2 className="size-4 shrink-0 text-green-500" />
<span className="text-green-600 dark:text-green-400">人机验证通过</span>
<ShieldCheck className="ml-auto size-4 shrink-0 text-green-500" />
<CheckCircle2 className='size-4 shrink-0 text-green-500' />
<span className='text-green-600 dark:text-green-400'>
人机验证通过
</span>
<ShieldCheck className='ml-auto size-4 shrink-0 text-green-500' />
</>
)}
{status === 'error' && (
<button
type="button"
className="flex w-full items-center gap-2 text-left"
type='button'
className='flex w-full items-center gap-2 text-left'
onClick={() => void solve()}
>
<ShieldAlert className="size-4 shrink-0 text-destructive" />
<span className="flex-1 text-destructive">
<ShieldAlert className='size-4 shrink-0 text-destructive' />
<span className='flex-1 text-destructive'>
{errorMsg || '人机验证失败'}
</span>
<span className="text-xs text-muted-foreground underline">重试</span>
<span className='text-xs text-muted-foreground underline'>重试</span>
</button>
)}
</div>
)
);
}
+196 -171
View File
@@ -1,213 +1,235 @@
"use client"
'use client';
import {useEffect, useRef, useState} from "react"
import {useMutation, useQuery} from "@tanstack/react-query"
import {useSearchParams} from "next/navigation"
import {EyeIcon, EyeOffIcon} from "lucide-react"
import {toast} from "sonner"
import Link from "next/link"
import { useEffect, useRef, useState } from 'react';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useSearchParams } from 'next/navigation';
import { EyeIcon, EyeOffIcon } from 'lucide-react';
import { toast } from 'sonner';
import Link from 'next/link';
import {useAuth} from "@/components/providers/auth-provider"
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Separator} from "@/components/ui/separator"
import {Spinner} from "@/components/ui/spinner"
import {Field, FieldGroup, FieldLabel} from "@/components/ui/field"
import {CapWidget} from "@/components/auth/cap-widget"
import {AuthHeading} from "@/components/auth/auth-shell"
import {OTPForm} from "./otp-form"
import {AuthService} from "@/lib/services/auth"
import {ConfigService} from "@/lib/services/config"
import type {LoginRequest} from "@/lib/services/auth/types"
import {safeRedirectTarget} from "@/lib/utils"
import { useAuth } from '@/components/providers/auth-provider';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Separator } from '@/components/ui/separator';
import { Spinner } from '@/components/ui/spinner';
import { Field, FieldGroup, FieldLabel } from '@/components/ui/field';
import { CapWidget } from '@/components/auth/cap-widget';
import { AuthHeading } from '@/components/auth/auth-shell';
import { OTPForm } from './otp-form';
import { AuthService } from '@/lib/services/auth';
import { ConfigService } from '@/lib/services/config';
import type { LoginRequest } from '@/lib/services/auth/types';
import { safeRedirectTarget } from '@/lib/utils';
function persistRedirectTarget(searchParams: ReturnType<typeof useSearchParams>) {
const callbackUrl = searchParams.get("callbackUrl")
if (callbackUrl && typeof window !== "undefined") {
sessionStorage.setItem("redirect_after_login", safeRedirectTarget(callbackUrl))
function persistRedirectTarget(
searchParams: ReturnType<typeof useSearchParams>,
) {
const callbackUrl = searchParams.get('callbackUrl');
if (callbackUrl && typeof window !== 'undefined') {
sessionStorage.setItem(
'redirect_after_login',
safeRedirectTarget(callbackUrl),
);
}
}
function configBool(value: string | undefined, fallback: boolean) {
if (value === undefined) return fallback
return value === "true"
if (value === undefined) return fallback;
return value === 'true';
}
export function LoginForm({ onOTPStateChange }: { onOTPStateChange?: (show: boolean) => void }) {
const searchParams = useSearchParams()
const { setUser } = useAuth()
const [username, setUsername] = useState("")
const [password, setPassword] = useState("")
const [showPassword, setShowPassword] = useState(false)
const [code, setCode] = useState("")
const [showLoginCodeInput, setShowLoginCodeInput] = useState(false)
const [loginCooldown, setLoginCooldown] = useState(0)
const [errorMessage, setErrorMessage] = useState("")
const [loginCodeTip, setLoginCodeTip] = useState<React.ReactNode>(null)
export function LoginForm({
onOTPStateChange,
}: {
onOTPStateChange?: (show: boolean) => void;
}) {
const searchParams = useSearchParams();
const { setUser } = useAuth();
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [showPassword, setShowPassword] = useState(false);
const [code, setCode] = useState('');
const [showLoginCodeInput, setShowLoginCodeInput] = useState(false);
const [loginCooldown, setLoginCooldown] = useState(0);
const [errorMessage, setErrorMessage] = useState('');
const [loginCodeTip, setLoginCodeTip] = useState<React.ReactNode>(null);
useEffect(() => {
onOTPStateChange?.(showLoginCodeInput)
}, [showLoginCodeInput, onOTPStateChange])
onOTPStateChange?.(showLoginCodeInput);
}, [showLoginCodeInput, onOTPStateChange]);
useEffect(() => {
if (loginCooldown > 0) {
const timer = setTimeout(() => setLoginCooldown(loginCooldown - 1), 1000)
return () => clearTimeout(timer)
const timer = setTimeout(() => setLoginCooldown(loginCooldown - 1), 1000);
return () => clearTimeout(timer);
}
}, [loginCooldown])
}, [loginCooldown]);
// Cap token management — ref to hold latest token without triggering re-render
const capTokenRef = useRef<string | null>(null)
const [capReady, setCapReady] = useState(false)
const [capError, setCapError] = useState(false)
const [capResetKey, setCapResetKey] = useState(0)
const capTokenRef = useRef<string | null>(null);
const [capReady, setCapReady] = useState(false);
const [capError, setCapError] = useState(false);
const [capResetKey, setCapResetKey] = useState(0);
const publicConfigQuery = useQuery({
queryKey: ["public-config"],
queryKey: ['public-config'],
queryFn: () => ConfigService.getPublicConfig(),
})
});
const authSourcesQuery = useQuery({
queryKey: ["auth-sources"],
queryKey: ['auth-sources'],
queryFn: () => AuthService.getAuthSources(),
})
});
const capEnabled = configBool(publicConfigQuery.data?.cap_login_enabled, false)
const capAutoSolve = configBool(publicConfigQuery.data?.cap_auto_solve, true)
const capEnabled = configBool(
publicConfigQuery.data?.cap_login_enabled,
false,
);
const capAutoSolve = configBool(publicConfigQuery.data?.cap_auto_solve, true);
const loginMutation = useMutation({
mutationFn: (req: LoginRequest) => {
const headers: Record<string, string> = {}
const headers: Record<string, string> = {};
if (capEnabled && capTokenRef.current) {
headers["X-Cap-Token"] = capTokenRef.current
headers['X-Cap-Token'] = capTokenRef.current;
// Consume the token — next login attempt will need a new one
capTokenRef.current = null
setCapReady(false)
capTokenRef.current = null;
setCapReady(false);
}
return AuthService.login(req, Object.keys(headers).length ? headers : undefined)
return AuthService.login(
req,
Object.keys(headers).length ? headers : undefined,
);
},
onSuccess: (user) => {
setUser(user)
toast.success("登录成功")
setUser(user);
toast.success('登录成功');
},
onError: (error: Error) => {
const errorMsg = error.message || ""
if (errorMsg.startsWith("need_email_code:")) {
const emailMasked = errorMsg.substring("need_email_code:".length)
const errorMsg = error.message || '';
if (errorMsg.startsWith('need_email_code:')) {
const emailMasked = errorMsg.substring('need_email_code:'.length);
setLoginCodeTip(
<>
已向您的安全邮箱 <span className="font-medium text-foreground">{emailMasked}</span> 发送了登录验证码。
</>
)
setShowLoginCodeInput(true)
setLoginCooldown(60)
toast.success("登录验证码已发送至您的邮箱,请注意查收")
已向您的安全邮箱{' '}
<span className='font-medium text-foreground'>{emailMasked}</span>{' '}
发送了登录验证码。
</>,
);
setShowLoginCodeInput(true);
setLoginCooldown(60);
toast.success('登录验证码已发送至您的邮箱,请注意查收');
if (capEnabled) {
capTokenRef.current = null
setCapReady(false)
setCapResetKey((key) => key + 1)
capTokenRef.current = null;
setCapReady(false);
setCapResetKey((key) => key + 1);
}
return
return;
}
if (errorMsg.startsWith("smtp_invalid:")) {
const tip = errorMsg.substring("smtp_invalid:".length)
setLoginCodeTip("请输入您的登录验证码。")
setShowLoginCodeInput(true)
setLoginCooldown(0)
toast.warning(tip)
if (errorMsg.startsWith('smtp_invalid:')) {
const tip = errorMsg.substring('smtp_invalid:'.length);
setLoginCodeTip('请输入您的登录验证码。');
setShowLoginCodeInput(true);
setLoginCooldown(0);
toast.warning(tip);
if (capEnabled) {
capTokenRef.current = null
setCapReady(false)
setCapResetKey((key) => key + 1)
capTokenRef.current = null;
setCapReady(false);
setCapResetKey((key) => key + 1);
}
return
return;
}
setErrorMessage(errorMsg || "登录失败,请重试")
setErrorMessage(errorMsg || '登录失败,请重试');
if (capEnabled) {
capTokenRef.current = null
setCapReady(false)
setCapResetKey((key) => key + 1)
capTokenRef.current = null;
setCapReady(false);
setCapResetKey((key) => key + 1);
}
},
})
});
const handlePasswordLogin = () => {
setErrorMessage("")
const trimmedUsername = username.trim()
setErrorMessage('');
const trimmedUsername = username.trim();
if (!trimmedUsername || !password) {
toast.error("邮箱/用户名或密码未填写完整", {
description: "请先输入邮箱/用户名和密码后再登录",
})
return
toast.error('邮箱/用户名或密码未填写完整', {
description: '请先输入邮箱/用户名和密码后再登录',
});
return;
}
if (capEnabled && !capReady) {
toast.error(
capAutoSolve
? "人机验证尚未完成,请稍候…"
: "请先点击「开始验证」完成人机验证",
)
return
? '人机验证尚未完成,请稍候…'
: '请先点击「开始验证」完成人机验证',
);
return;
}
loginMutation.mutate({
username: trimmedUsername,
password,
code: showLoginCodeInput ? code.trim() : undefined,
})
}
});
};
const handleResendLoginCode = () => {
setCode("")
setCode('');
loginMutation.mutate({
username: username.trim(),
password,
})
}
});
};
const handleOAuthLogin = async (sourceName: string) => {
try {
setErrorMessage("")
persistRedirectTarget(searchParams)
const { authorize_url } = await AuthService.getAuthorizeUrl(sourceName)
window.location.href = authorize_url
setErrorMessage('');
persistRedirectTarget(searchParams);
const { authorize_url } = await AuthService.getAuthorizeUrl(sourceName);
window.location.href = authorize_url;
} catch (error) {
toast.error(error instanceof Error ? error.message : "第三方登录失败")
}
toast.error(error instanceof Error ? error.message : '第三方登录失败');
}
};
const handleCapToken = (token: string) => {
capTokenRef.current = token
setCapReady(true)
setCapError(false)
}
capTokenRef.current = token;
setCapReady(true);
setCapError(false);
};
const handleCapError = () => {
capTokenRef.current = null
setCapReady(false)
setCapError(true)
}
capTokenRef.current = null;
setCapReady(false);
setCapError(true);
};
const registrationEnabled =
configBool(publicConfigQuery.data?.registration_enabled, true) &&
configBool(publicConfigQuery.data?.password_register_enabled, true)
configBool(publicConfigQuery.data?.password_register_enabled, true);
const passwordLoginEnabled = configBool(publicConfigQuery.data?.password_login_enabled, true)
const oidcLoginEnabled = configBool(publicConfigQuery.data?.oidc_login_enabled, true)
const authSources = oidcLoginEnabled ? (authSourcesQuery.data ?? []) : []
const passwordLoginEnabled = configBool(
publicConfigQuery.data?.password_login_enabled,
true,
);
const oidcLoginEnabled = configBool(
publicConfigQuery.data?.oidc_login_enabled,
true,
);
const authSources = oidcLoginEnabled ? (authSourcesQuery.data ?? []) : [];
const loginDisabled =
!passwordLoginEnabled ||
loginMutation.isPending ||
(capEnabled && capAutoSolve && !capReady && !capError)
(capEnabled && capAutoSolve && !capReady && !capError);
if (publicConfigQuery.isPending) {
return (
<div className="flex items-center justify-center py-24">
<div className='flex items-center justify-center py-24'>
<Spinner />
</div>
)
);
}
if (showLoginCodeInput) {
@@ -221,54 +243,54 @@ export function LoginForm({ onOTPStateChange }: { onOTPStateChange?: (show: bool
onResend={handleResendLoginCode}
onSubmit={handlePasswordLogin}
/>
)
);
}
return (
<div className="flex flex-col gap-6 [@media(max-height:700px)]:gap-4">
<div className='flex flex-col gap-6 [@media(max-height:700px)]:gap-4'>
<AuthHeading
siteName={publicConfigQuery.data?.site_name}
title="登录到您的账号"
description="欢迎回来,请输入您的账号信息继续。"
title='登录到您的账号'
description='欢迎回来,请输入您的账号信息继续。'
/>
<div className="flex flex-col gap-5 [@media(max-height:700px)]:gap-3">
<FieldGroup className="gap-4 [@media(max-height:700px)]:gap-3">
<Field className="gap-1.5">
<FieldLabel htmlFor="username">
邮箱或用户名 <span className="text-destructive">*</span>
<div className='flex flex-col gap-5 [@media(max-height:700px)]:gap-3'>
<FieldGroup className='gap-4 [@media(max-height:700px)]:gap-3'>
<Field className='gap-1.5'>
<FieldLabel htmlFor='username'>
邮箱或用户名 <span className='text-destructive'>*</span>
</FieldLabel>
<Input
id="username"
id='username'
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="输入邮箱或用户名"
autoComplete="username"
className="h-10 text-sm [@media(max-height:700px)]:h-9"
onKeyDown={(e) => e.key === "Enter" && handlePasswordLogin()}
placeholder='输入邮箱或用户名'
autoComplete='username'
className='h-10 text-sm [@media(max-height:700px)]:h-9'
onKeyDown={(e) => e.key === 'Enter' && handlePasswordLogin()}
/>
</Field>
<Field className="gap-1.5">
<FieldLabel htmlFor="password">
密码 <span className="text-destructive">*</span>
<Field className='gap-1.5'>
<FieldLabel htmlFor='password'>
密码 <span className='text-destructive'>*</span>
</FieldLabel>
<div className="relative">
<div className='relative'>
<Input
id="password"
id='password'
value={password}
onChange={(e) => setPassword(e.target.value)}
type={showPassword ? "text" : "password"}
placeholder="输入您的密码"
autoComplete="current-password"
className="h-10 pr-11 text-sm [@media(max-height:700px)]:h-9"
onKeyDown={(e) => e.key === "Enter" && handlePasswordLogin()}
type={showPassword ? 'text' : 'password'}
placeholder='输入您的密码'
autoComplete='current-password'
className='h-10 pr-11 text-sm [@media(max-height:700px)]:h-9'
onKeyDown={(e) => e.key === 'Enter' && handlePasswordLogin()}
/>
<Button
type="button"
variant="ghost"
size="icon-sm"
aria-label={showPassword ? "隐藏密码" : "显示密码"}
className="absolute right-1.5 top-1/2 -translate-y-1/2 text-muted-foreground"
type='button'
variant='ghost'
size='icon-sm'
aria-label={showPassword ? '隐藏密码' : '显示密码'}
className='absolute right-1.5 top-1/2 -translate-y-1/2 text-muted-foreground'
onClick={() => setShowPassword((visible) => !visible)}
>
{showPassword ? <EyeOffIcon /> : <EyeIcon />}
@@ -288,15 +310,15 @@ export function LoginForm({ onOTPStateChange }: { onOTPStateChange?: (show: bool
)}
{errorMessage ? (
<div className="rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive">
<div className='rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive'>
{errorMessage}
</div>
) : null}
<Button
type="button"
className="h-10 w-full [@media(max-height:700px)]:h-9"
variant="auth"
type='button'
className='h-10 w-full [@media(max-height:700px)]:h-9'
variant='auth'
onClick={handlePasswordLogin}
disabled={loginDisabled}
>
@@ -306,25 +328,25 @@ export function LoginForm({ onOTPStateChange }: { onOTPStateChange?: (show: bool
登录中...
</>
) : (
"登录"
'登录'
)}
</Button>
</div>
{authSources.length > 0 ? (
<div className="flex flex-col gap-3">
<div className="flex items-center gap-3">
<Separator className="flex-1" />
<span className="text-xs text-muted-foreground">或者使用</span>
<Separator className="flex-1" />
<div className='flex flex-col gap-3'>
<div className='flex items-center gap-3'>
<Separator className='flex-1' />
<span className='text-xs text-muted-foreground'>或者使用</span>
<Separator className='flex-1' />
</div>
<div className="grid gap-2">
<div className='grid gap-2'>
{authSources.map((source) => (
<Button
key={source.id}
type="button"
variant="outline"
className="h-10 [@media(max-height:700px)]:h-9"
type='button'
variant='outline'
className='h-10 [@media(max-height:700px)]:h-9'
onClick={() => void handleOAuthLogin(source.name)}
>
{source.display_name || source.name} 登录
@@ -335,13 +357,16 @@ export function LoginForm({ onOTPStateChange }: { onOTPStateChange?: (show: bool
) : null}
{registrationEnabled && (
<div className="text-center text-sm text-muted-foreground">
还没有账号?{" "}
<Link href="/register" className="font-medium text-foreground underline underline-offset-4">
<div className='text-center text-sm text-muted-foreground'>
还没有账号?{' '}
<Link
href='/register'
className='font-medium text-foreground underline underline-offset-4'
>
立即注册
</Link>
</div>
)}
</div>
)
);
}
+106 -89
View File
@@ -1,18 +1,17 @@
"use client"
'use client';
import {useCallback, useEffect, useRef, useState} from "react"
import {AnimatePresence, motion} from "motion/react"
import {useRouter, useSearchParams} from "next/navigation"
import {toast} from "sonner"
import {Spinner} from "@/components/ui/spinner"
import {LoginForm} from "@/components/auth/login-form"
import {AuthShell} from "@/components/auth/auth-shell"
import {Check} from "lucide-react"
import {AuthService} from "@/lib/services/auth"
import {useAuth} from "@/components/providers/auth-provider"
import {safeRedirectTarget} from "@/lib/utils"
import { useCallback, useEffect, useRef, useState } from 'react';
import { AnimatePresence, motion } from 'motion/react';
import { useRouter, useSearchParams } from 'next/navigation';
import { toast } from 'sonner';
import { Spinner } from '@/components/ui/spinner';
import { LoginForm } from '@/components/auth/login-form';
import { AuthShell } from '@/components/auth/auth-shell';
import { Check } from 'lucide-react';
import { AuthService } from '@/lib/services/auth';
import { useAuth } from '@/components/providers/auth-provider';
import { safeRedirectTarget } from '@/lib/utils';
/**
* 登录页面组件
@@ -25,151 +24,169 @@ import {safeRedirectTarget} from "@/lib/utils"
* @returns {React.ReactNode} 登录页面组件
*/
export function LoginPage() {
const router = useRouter()
const searchParams = useSearchParams()
const { user, loading, setUser } = useAuth()
const [showOTP, setShowOTP] = useState(false)
const router = useRouter();
const searchParams = useSearchParams();
const { user, loading, setUser } = useAuth();
const [showOTP, setShowOTP] = useState(false);
/* 处理OAuth回调 */
const isOAuthCallback = !!(searchParams.get('state') && searchParams.get('code'))
const [isProcessingCallback, setIsProcessingCallback] = useState(isOAuthCallback)
const isCheckingSession = !isOAuthCallback && loading
const isOAuthCallback = !!(
searchParams.get('state') && searchParams.get('code')
);
const [isProcessingCallback, setIsProcessingCallback] =
useState(isOAuthCallback);
const isCheckingSession = !isOAuthCallback && loading;
const [loginSuccess, setLoginSuccess] = useState(false)
const redirectedRef = useRef(false)
const callbackProcessedRef = useRef(false)
const [loginSuccess, setLoginSuccess] = useState(false);
const redirectedRef = useRef(false);
const callbackProcessedRef = useRef(false);
const resolveRedirectTarget = useCallback(() => {
const callbackUrl = searchParams.get('callbackUrl')
const storedRedirect = sessionStorage.getItem('redirect_after_login')
const target = callbackUrl || storedRedirect || '/home'
const callbackUrl = searchParams.get('callbackUrl');
const storedRedirect = sessionStorage.getItem('redirect_after_login');
const target = callbackUrl || storedRedirect || '/home';
if (storedRedirect) {
sessionStorage.removeItem('redirect_after_login')
sessionStorage.removeItem('redirect_after_login');
}
return safeRedirectTarget(target)
}, [searchParams])
return safeRedirectTarget(target);
}, [searchParams]);
const resolveRedirectTargetRef = useRef(resolveRedirectTarget)
const resolveRedirectTargetRef = useRef(resolveRedirectTarget);
useEffect(() => {
resolveRedirectTargetRef.current = resolveRedirectTarget
}, [resolveRedirectTarget])
resolveRedirectTargetRef.current = resolveRedirectTarget;
}, [resolveRedirectTarget]);
/* 登录页兜底:已登录用户直接跳转 */
useEffect(() => {
const state = searchParams.get('state')
const code = searchParams.get('code')
const state = searchParams.get('state');
const code = searchParams.get('code');
if ((state && code) || loading || !user) {
return
return;
}
if (!redirectedRef.current) {
redirectedRef.current = true
router.replace(resolveRedirectTargetRef.current())
redirectedRef.current = true;
router.replace(resolveRedirectTargetRef.current());
}
}, [loading, router, searchParams, user])
}, [loading, router, searchParams, user]);
/* 回调逻辑 */
useEffect(() => {
const handleOAuthCallback = async () => {
const state = searchParams.get('state')
const code = searchParams.get('code')
const state = searchParams.get('state');
const code = searchParams.get('code');
if (state && code) {
if (callbackProcessedRef.current) return
callbackProcessedRef.current = true
if (callbackProcessedRef.current) return;
callbackProcessedRef.current = true;
setIsProcessingCallback(true)
setIsProcessingCallback(true);
try {
const result = await AuthService.handleCallback({ state, code })
if (result.status === "need_bind") {
toast.info("您的第三方账号未绑定本地账号,系统已关闭注册。请登录已有本地账号进行绑定。")
setIsProcessingCallback(false)
router.replace('/login')
return
const result = await AuthService.handleCallback({ state, code });
if (result.status === 'need_bind') {
toast.info(
'您的第三方账号未绑定本地账号,系统已关闭注册。请登录已有本地账号进行绑定。',
);
setIsProcessingCallback(false);
router.replace('/login');
return;
}
if (result.user) {
setUser(result.user)
setUser(result.user);
}
setLoginSuccess(true)
toast.success(result.status === "bound" ? "绑定成功" : "登录成功")
setLoginSuccess(true);
toast.success(result.status === 'bound' ? '绑定成功' : '登录成功');
setTimeout(() => {
if (!redirectedRef.current) {
redirectedRef.current = true
router.replace(resolveRedirectTargetRef.current())
redirectedRef.current = true;
router.replace(resolveRedirectTargetRef.current());
}
}, 1500)
}, 1500);
} catch (error) {
console.error('OAuth callback error:', error)
toast.error(error instanceof Error ? error.message : "登录失败,请重试")
setIsProcessingCallback(false)
router.replace('/login')
console.error('OAuth callback error:', error);
toast.error(
error instanceof Error ? error.message : '登录失败,请重试',
);
setIsProcessingCallback(false);
router.replace('/login');
}
}
}
handleOAuthCallback()
}, [router, searchParams, setUser])
};
handleOAuthCallback();
}, [router, searchParams, setUser]);
return (
<AuthShell wide={showOTP}>
<div className="w-full">
<AnimatePresence mode="wait">
<div className='w-full'>
<AnimatePresence mode='wait'>
{isProcessingCallback || isCheckingSession ? (
<motion.div
key={isProcessingCallback ? "processing" : "session-check"}
key={isProcessingCallback ? 'processing' : 'session-check'}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="w-full"
className='w-full'
>
{isCheckingSession ? (
<div className="flex flex-col items-center justify-center gap-4 py-16">
<div className="relative">
<Spinner className="size-8" />
<div className='flex flex-col items-center justify-center gap-4 py-16'>
<div className='relative'>
<Spinner className='size-8' />
</div>
<div className="flex flex-col gap-2 text-center">
<h3 className="font-semibold tracking-tight text-foreground">正在检查登录状态</h3>
<p className="text-xs text-muted-foreground">请稍候,我们正在确认当前会话...</p>
<div className='flex flex-col gap-2 text-center'>
<h3 className='font-semibold tracking-tight text-foreground'>
正在检查登录状态
</h3>
<p className='text-xs text-muted-foreground'>
请稍候,我们正在确认当前会话...
</p>
</div>
</div>
) : loginSuccess ? (
<div className="flex flex-col items-center justify-center gap-4 py-16">
<div className='flex flex-col items-center justify-center gap-4 py-16'>
<motion.div
initial={{ scale: 0.5, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
transition={{ type: "spring", stiffness: 300, damping: 20 }}
className="flex size-8 items-center justify-center rounded-full bg-primary/10 text-primary ring-1 ring-primary/20"
transition={{ type: 'spring', stiffness: 300, damping: 20 }}
className='flex size-8 items-center justify-center rounded-full bg-primary/10 text-primary ring-1 ring-primary/20'
>
<Check className="size-6" strokeWidth={3} />
<Check className='size-6' strokeWidth={3} />
</motion.div>
<div className="flex flex-col gap-2 text-center">
<h3 className="font-semibold tracking-tight text-foreground">登录成功</h3>
<p className="text-xs text-muted-foreground">正在跳转至控制台...</p>
<div className='flex flex-col gap-2 text-center'>
<h3 className='font-semibold tracking-tight text-foreground'>
登录成功
</h3>
<p className='text-xs text-muted-foreground'>
正在跳转至控制台...
</p>
</div>
</div>
) : (
<div className="flex flex-col items-center justify-center gap-4 py-16">
<div className="relative">
<Spinner className="size-8" />
<div className='flex flex-col items-center justify-center gap-4 py-16'>
<div className='relative'>
<Spinner className='size-8' />
</div>
<div className="flex flex-col gap-2 text-center">
<h3 className="font-semibold tracking-tight text-foreground">正在验证凭据</h3>
<p className="text-xs text-muted-foreground">请稍候,我们正在为您建立安全会话...</p>
<div className='flex flex-col gap-2 text-center'>
<h3 className='font-semibold tracking-tight text-foreground'>
正在验证凭据
</h3>
<p className='text-xs text-muted-foreground'>
请稍候,我们正在为您建立安全会话...
</p>
</div>
</div>
)}
</motion.div>
) : (
<motion.div
key="login-form-wrapper"
key='login-form-wrapper'
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ duration: 0.4 }}
className="w-full"
className='w-full'
>
<LoginForm onOTPStateChange={setShowOTP} />
</motion.div>
@@ -177,5 +194,5 @@ export function LoginPage() {
</AnimatePresence>
</div>
</AuthShell>
)
);
}
+53 -43
View File
@@ -1,22 +1,27 @@
"use client"
'use client';
import * as React from "react"
import {RefreshCwIcon} from "lucide-react"
import * as React from 'react';
import { RefreshCwIcon } from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Field, FieldLabel} from "@/components/ui/field"
import {InputOTP, InputOTPGroup, InputOTPSeparator, InputOTPSlot,} from "@/components/ui/input-otp"
import {AuthHeading} from "@/components/auth/auth-shell"
import {cn} from "@/lib/utils"
import { Button } from '@/components/ui/button';
import { Field, FieldLabel } from '@/components/ui/field';
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
} from '@/components/ui/input-otp';
import { AuthHeading } from '@/components/auth/auth-shell';
import { cn } from '@/lib/utils';
interface OTPFormProps {
code: string
setCode: (val: string) => void
loginCodeTip: React.ReactNode
loginCooldown: number
isPending: boolean
onResend: () => void
onSubmit: () => void
code: string;
setCode: (val: string) => void;
loginCodeTip: React.ReactNode;
loginCooldown: number;
isPending: boolean;
onResend: () => void;
onSubmit: () => void;
}
export function OTPForm({
@@ -29,49 +34,54 @@ export function OTPForm({
onSubmit,
}: OTPFormProps) {
return (
<div className="flex flex-col gap-6 [@media(max-height:700px)]:gap-4">
<div className='flex flex-col gap-6 [@media(max-height:700px)]:gap-4'>
<AuthHeading
title="验证您的登录"
description="输入发送到安全邮箱的 6 位验证码。"
title='验证您的登录'
description='输入发送到安全邮箱的 6 位验证码。'
/>
{loginCodeTip ? (
<p className="text-sm leading-6 text-muted-foreground">{loginCodeTip}</p>
<p className='text-sm leading-6 text-muted-foreground'>
{loginCodeTip}
</p>
) : null}
<div className="flex flex-col gap-5 [@media(max-height:700px)]:gap-3">
<Field className="gap-3">
<div className="flex items-center justify-between">
<FieldLabel htmlFor="otp-verification" className="text-sm font-medium">
<div className='flex flex-col gap-5 [@media(max-height:700px)]:gap-3'>
<Field className='gap-3'>
<div className='flex items-center justify-between'>
<FieldLabel
htmlFor='otp-verification'
className='text-sm font-medium'
>
验证码
</FieldLabel>
<Button
variant="outline"
size="sm"
type="button"
variant='outline'
size='sm'
type='button'
onClick={onResend}
disabled={loginCooldown > 0 || isPending}
className="h-8 text-xs"
className='h-8 text-xs'
>
<RefreshCwIcon className={cn(isPending && "animate-spin")} />
{loginCooldown > 0 ? `${loginCooldown}秒后重发` : "重新发送"}
<RefreshCwIcon className={cn(isPending && 'animate-spin')} />
{loginCooldown > 0 ? `${loginCooldown}秒后重发` : '重新发送'}
</Button>
</div>
<div className="flex justify-start">
<div className='flex justify-start'>
<InputOTP
maxLength={6}
id="otp-verification"
id='otp-verification'
required
value={code}
onChange={setCode}
onComplete={onSubmit}
disabled={isPending}
>
<InputOTPGroup className="*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl">
<InputOTPGroup className='*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl'>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator className="mx-2" />
<InputOTPGroup className="*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl">
<InputOTPSeparator className='mx-2' />
<InputOTPGroup className='*:data-[slot=input-otp-slot]:h-12 *:data-[slot=input-otp-slot]:w-11 *:data-[slot=input-otp-slot]:text-xl'>
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
@@ -80,24 +90,24 @@ export function OTPForm({
</div>
</Field>
<Button
type="button"
className="h-10 w-full [@media(max-height:700px)]:h-9"
variant="auth"
type='button'
className='h-10 w-full [@media(max-height:700px)]:h-9'
variant='auth'
onClick={onSubmit}
disabled={isPending || code.length < 6}
>
{isPending ? "验证中..." : "验证"}
{isPending ? '验证中...' : '验证'}
</Button>
<div className="text-center text-sm text-muted-foreground">
遇到登录问题?{" "}
<div className='text-center text-sm text-muted-foreground'>
遇到登录问题?{' '}
<a
href="#"
className="underline underline-offset-4 transition-colors hover:text-primary"
href='#'
className='underline underline-offset-4 transition-colors hover:text-primary'
>
联系客服
</a>
</div>
</div>
</div>
)
);
}
+228 -188
View File
@@ -1,225 +1,250 @@
"use client"
'use client';
import {useEffect, useMemo, useRef, useState} from "react"
import {useMutation, useQuery} from "@tanstack/react-query"
import {useRouter, useSearchParams} from "next/navigation"
import {toast} from "sonner"
import Link from "next/link"
import { useEffect, useMemo, useRef, useState } from 'react';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useRouter, useSearchParams } from 'next/navigation';
import { toast } from 'sonner';
import Link from 'next/link';
import {useAuth} from "@/components/providers/auth-provider"
import {Button} from "@/components/ui/button"
import {Input} from "@/components/ui/input"
import {Spinner} from "@/components/ui/spinner"
import {Field, FieldGroup, FieldLabel} from "@/components/ui/field"
import {AuthHeading} from "@/components/auth/auth-shell"
import {CapWidget} from "@/components/auth/cap-widget"
import {AuthService} from "@/lib/services/auth"
import {ConfigService} from "@/lib/services/config"
import type {RegisterRequest} from "@/lib/services/auth/types"
import {safeRedirectTarget} from "@/lib/utils"
import { useAuth } from '@/components/providers/auth-provider';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Spinner } from '@/components/ui/spinner';
import { Field, FieldGroup, FieldLabel } from '@/components/ui/field';
import { AuthHeading } from '@/components/auth/auth-shell';
import { CapWidget } from '@/components/auth/cap-widget';
import { AuthService } from '@/lib/services/auth';
import { ConfigService } from '@/lib/services/config';
import type { RegisterRequest } from '@/lib/services/auth/types';
import { safeRedirectTarget } from '@/lib/utils';
function getRedirectTarget(searchParams: ReturnType<typeof useSearchParams>) {
const callbackUrl = searchParams.get("callbackUrl")
const callbackUrl = searchParams.get('callbackUrl');
const storedRedirect =
typeof window === "undefined"
typeof window === 'undefined'
? null
: sessionStorage.getItem("redirect_after_login")
return safeRedirectTarget(callbackUrl || storedRedirect || "/home")
: sessionStorage.getItem('redirect_after_login');
return safeRedirectTarget(callbackUrl || storedRedirect || '/home');
}
function configBool(value: string | undefined, fallback: boolean) {
if (value === undefined) return fallback
return value === "true"
if (value === undefined) return fallback;
return value === 'true';
}
export function RegisterForm() {
const router = useRouter()
const searchParams = useSearchParams()
const { setUser } = useAuth()
const [username, setUsername] = useState("")
const [password, setPassword] = useState("")
const [nickname, setNickname] = useState("")
const [email, setEmail] = useState("")
const [code, setCode] = useState("")
const [registerCooldown, setRegisterCooldown] = useState(0)
const [errorMessage, setErrorMessage] = useState("")
const router = useRouter();
const searchParams = useSearchParams();
const { setUser } = useAuth();
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [nickname, setNickname] = useState('');
const [email, setEmail] = useState('');
const [code, setCode] = useState('');
const [registerCooldown, setRegisterCooldown] = useState(0);
const [errorMessage, setErrorMessage] = useState('');
useEffect(() => {
if (registerCooldown > 0) {
const timer = setTimeout(() => setRegisterCooldown(registerCooldown - 1), 1000)
return () => clearTimeout(timer)
const timer = setTimeout(
() => setRegisterCooldown(registerCooldown - 1),
1000,
);
return () => clearTimeout(timer);
}
}, [registerCooldown])
}, [registerCooldown]);
const publicConfigQuery = useQuery({
queryKey: ["public-config"],
queryKey: ['public-config'],
queryFn: () => ConfigService.getPublicConfig(),
})
});
const redirectTarget = useMemo(
() => getRedirectTarget(searchParams),
[searchParams],
)
);
const registrationEnabled =
configBool(publicConfigQuery.data?.registration_enabled, true) &&
configBool(publicConfigQuery.data?.password_register_enabled, true)
configBool(publicConfigQuery.data?.password_register_enabled, true);
const emailRegisterEnabled = configBool(publicConfigQuery.data?.email_register_verification_enabled, false)
const emailRegisterEnabled = configBool(
publicConfigQuery.data?.email_register_verification_enabled,
false,
);
const capEnabled = configBool(publicConfigQuery.data?.cap_login_enabled, false)
const capAutoSolve = configBool(publicConfigQuery.data?.cap_auto_solve, true)
const capEnabled = configBool(
publicConfigQuery.data?.cap_login_enabled,
false,
);
const capAutoSolve = configBool(publicConfigQuery.data?.cap_auto_solve, true);
const [capScope, setCapScope] = useState<'send_email_code' | 'register'>('send_email_code')
const [capScope, setCapScope] = useState<'send_email_code' | 'register'>(
'send_email_code',
);
// 监听 emailRegisterEnabled 改变初始 scope
useEffect(() => {
setCapScope(emailRegisterEnabled ? 'send_email_code' : 'register')
}, [emailRegisterEnabled])
setCapScope(emailRegisterEnabled ? 'send_email_code' : 'register');
}, [emailRegisterEnabled]);
const capTokenRef = useRef<string | null>(null)
const [capReady, setCapReady] = useState(false)
const [capError, setCapError] = useState(false)
const [capResetKey, setCapResetKey] = useState(0)
const capTokenRef = useRef<string | null>(null);
const [capReady, setCapReady] = useState(false);
const [capError, setCapError] = useState(false);
const [capResetKey, setCapResetKey] = useState(0);
const handleCapToken = (token: string) => {
capTokenRef.current = token
setCapReady(true)
setCapError(false)
}
capTokenRef.current = token;
setCapReady(true);
setCapError(false);
};
const handleCapError = () => {
capTokenRef.current = null
setCapReady(false)
setCapError(true)
}
capTokenRef.current = null;
setCapReady(false);
setCapError(true);
};
// Redirect to login if registration is closed
useEffect(() => {
if (publicConfigQuery.isSuccess && !registrationEnabled) {
toast.error("系统注册功能已关闭")
router.replace("/login")
toast.error('系统注册功能已关闭');
router.replace('/login');
}
}, [publicConfigQuery.isSuccess, registrationEnabled, router])
}, [publicConfigQuery.isSuccess, registrationEnabled, router]);
const registerMutation = useMutation({
mutationFn: (req: RegisterRequest) => {
const headers: Record<string, string> = {}
const headers: Record<string, string> = {};
if (capEnabled && capTokenRef.current) {
headers["X-Cap-Token"] = capTokenRef.current
capTokenRef.current = null
setCapReady(false)
headers['X-Cap-Token'] = capTokenRef.current;
capTokenRef.current = null;
setCapReady(false);
}
return AuthService.register(req, Object.keys(headers).length ? headers : undefined)
return AuthService.register(
req,
Object.keys(headers).length ? headers : undefined,
);
},
onSuccess: (user) => {
setUser(user)
router.replace(redirectTarget)
toast.success("注册并登录成功")
setUser(user);
router.replace(redirectTarget);
toast.success('注册并登录成功');
},
onError: (error: Error) => {
setErrorMessage(error.message || "注册失败,请重试")
setErrorMessage(error.message || '注册失败,请重试');
if (capEnabled) {
capTokenRef.current = null
setCapReady(false)
setCapResetKey((key) => key + 1)
capTokenRef.current = null;
setCapReady(false);
setCapResetKey((key) => key + 1);
}
},
})
});
const sendRegisterCodeMutation = useMutation({
mutationFn: (targetEmail: string) => {
const headers: Record<string, string> = {}
const headers: Record<string, string> = {};
if (capEnabled && capTokenRef.current) {
headers["X-Cap-Token"] = capTokenRef.current
capTokenRef.current = null
setCapReady(false)
headers['X-Cap-Token'] = capTokenRef.current;
capTokenRef.current = null;
setCapReady(false);
}
return AuthService.sendEmailCode(targetEmail, "register", Object.keys(headers).length ? headers : undefined)
return AuthService.sendEmailCode(
targetEmail,
'register',
Object.keys(headers).length ? headers : undefined,
);
},
onSuccess: () => {
setRegisterCooldown(60)
toast.success("验证码已发送至您的邮箱,请查收")
setRegisterCooldown(60);
toast.success('验证码已发送至您的邮箱,请查收');
if (capEnabled) {
setCapScope('register')
capTokenRef.current = null
setCapReady(false)
setCapResetKey((key) => key + 1)
setCapScope('register');
capTokenRef.current = null;
setCapReady(false);
setCapResetKey((key) => key + 1);
}
},
onError: (error: Error) => {
toast.error(error.message || "发送验证码失败,请重试")
toast.error(error.message || '发送验证码失败,请重试');
if (capEnabled) {
capTokenRef.current = null
setCapReady(false)
setCapResetKey((key) => key + 1)
capTokenRef.current = null;
setCapReady(false);
setCapResetKey((key) => key + 1);
}
},
})
});
const registerDisabled =
registerMutation.isPending ||
(capEnabled && capScope === 'register' && capAutoSolve && !capReady && !capError)
(capEnabled &&
capScope === 'register' &&
capAutoSolve &&
!capReady &&
!capError);
const sendCodeDisabled =
registerCooldown > 0 ||
sendRegisterCodeMutation.isPending ||
(capEnabled && capScope === 'send_email_code' && capAutoSolve && !capReady && !capError)
(capEnabled &&
capScope === 'send_email_code' &&
capAutoSolve &&
!capReady &&
!capError);
const handleSendRegisterCode = () => {
const trimmedEmail = email.trim()
const trimmedEmail = email.trim();
if (!trimmedEmail) {
toast.error("请先输入邮箱地址")
return
toast.error('请先输入邮箱地址');
return;
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(trimmedEmail)) {
toast.error("请输入有效的邮箱地址")
return
toast.error('请输入有效的邮箱地址');
return;
}
if (capEnabled && capScope === 'send_email_code' && !capReady) {
toast.error(
capAutoSolve
? "人机验证尚未完成,请稍候…"
: "请先点击「开始验证」完成人机验证",
)
return
}
sendRegisterCodeMutation.mutate(trimmedEmail)
? '人机验证尚未完成,请稍候…'
: '请先点击「开始验证」完成人机验证',
);
return;
}
sendRegisterCodeMutation.mutate(trimmedEmail);
};
const handleRegister = () => {
setErrorMessage("")
setErrorMessage('');
if (!username.trim() || !password) {
toast.error("用户名和密码不能为空")
return
toast.error('用户名和密码不能为空');
return;
}
if (password.length < 8) {
toast.error("密码长度不能少于 8 位")
return
toast.error('密码长度不能少于 8 位');
return;
}
const trimmedEmail = email.trim()
const trimmedEmail = email.trim();
if (!trimmedEmail) {
toast.error("邮箱不能为空")
return
toast.error('邮箱不能为空');
return;
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(trimmedEmail)) {
toast.error("请输入有效的邮箱地址")
return
toast.error('请输入有效的邮箱地址');
return;
}
if (emailRegisterEnabled && !code.trim()) {
toast.error("验证码不能为空")
return
toast.error('验证码不能为空');
return;
}
if (capEnabled && capScope === 'register' && !capReady) {
toast.error(
capAutoSolve
? "人机验证尚未完成,请稍候…"
: "请先点击「开始验证」完成人机验证",
)
return
? '人机验证尚未完成,请稍候…'
: '请先点击「开始验证」完成人机验证',
);
return;
}
registerMutation.mutate({
username: username.trim(),
@@ -227,104 +252,116 @@ export function RegisterForm() {
nickname: nickname.trim() || undefined,
email: trimmedEmail,
code: code.trim() || undefined,
})
}
});
};
if (publicConfigQuery.isPending) {
return (
<div className="flex items-center justify-center py-24">
<div className='flex items-center justify-center py-24'>
<Spinner />
</div>
)
);
}
if (!registrationEnabled) {
return null
return null;
}
return (
<div className="flex flex-col gap-6 [@media(max-height:700px)]:gap-4">
<div className='flex flex-col gap-6 [@media(max-height:700px)]:gap-4'>
<AuthHeading
siteName={publicConfigQuery.data?.site_name}
title="创建您的账号"
description="填写以下信息,开始使用平台服务。"
title='创建您的账号'
description='填写以下信息,开始使用平台服务。'
/>
<div className="flex flex-col gap-5 [@media(max-height:700px)]:gap-3">
<FieldGroup className="gap-4 [@media(min-width:500px)]:grid [@media(min-width:500px)]:grid-cols-2 [@media(max-height:700px)]:gap-3">
<Field className="gap-1.5">
<FieldLabel htmlFor="username">用户名 <span className="text-destructive">*</span></FieldLabel>
<Input
id="username"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="请输入用户名"
autoComplete="username"
className="h-10 text-sm [@media(max-height:700px)]:h-9"
onKeyDown={(e) => e.key === "Enter" && handleRegister()}
/>
</Field>
<Field className="gap-1.5">
<FieldLabel htmlFor="nickname">
昵称
<span className="ml-1 text-xs font-normal text-muted-foreground">(可选)</span>
<div className='flex flex-col gap-5 [@media(max-height:700px)]:gap-3'>
<FieldGroup className='gap-4 [@media(min-width:500px)]:grid [@media(min-width:500px)]:grid-cols-2 [@media(max-height:700px)]:gap-3'>
<Field className='gap-1.5'>
<FieldLabel htmlFor='username'>
用户名 <span className='text-destructive'>*</span>
</FieldLabel>
<Input
id="nickname"
id='username'
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder='请输入用户名'
autoComplete='username'
className='h-10 text-sm [@media(max-height:700px)]:h-9'
onKeyDown={(e) => e.key === 'Enter' && handleRegister()}
/>
</Field>
<Field className='gap-1.5'>
<FieldLabel htmlFor='nickname'>
昵称
<span className='ml-1 text-xs font-normal text-muted-foreground'>
(可选)
</span>
</FieldLabel>
<Input
id='nickname'
value={nickname}
onChange={(e) => setNickname(e.target.value)}
placeholder="请输入昵称"
autoComplete="nickname"
className="h-10 text-sm [@media(max-height:700px)]:h-9"
onKeyDown={(e) => e.key === "Enter" && handleRegister()}
placeholder='请输入昵称'
autoComplete='nickname'
className='h-10 text-sm [@media(max-height:700px)]:h-9'
onKeyDown={(e) => e.key === 'Enter' && handleRegister()}
/>
</Field>
<Field className="gap-1.5">
<FieldLabel htmlFor="email">电子邮箱 <span className="text-destructive">*</span></FieldLabel>
<Field className='gap-1.5'>
<FieldLabel htmlFor='email'>
电子邮箱 <span className='text-destructive'>*</span>
</FieldLabel>
<Input
id="email"
id='email'
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="请输入电子邮箱"
autoComplete="email"
className="h-10 text-sm [@media(max-height:700px)]:h-9"
onKeyDown={(e) => e.key === "Enter" && handleRegister()}
placeholder='请输入电子邮箱'
autoComplete='email'
className='h-10 text-sm [@media(max-height:700px)]:h-9'
onKeyDown={(e) => e.key === 'Enter' && handleRegister()}
/>
</Field>
<Field className="gap-1.5">
<FieldLabel htmlFor="password">密码 <span className="text-destructive">*</span></FieldLabel>
<Field className='gap-1.5'>
<FieldLabel htmlFor='password'>
密码 <span className='text-destructive'>*</span>
</FieldLabel>
<Input
id="password"
id='password'
value={password}
onChange={(e) => setPassword(e.target.value)}
type="password"
placeholder="请输入密码(至少 8 位)"
autoComplete="new-password"
className="h-10 text-sm [@media(max-height:700px)]:h-9"
onKeyDown={(e) => e.key === "Enter" && handleRegister()}
type='password'
placeholder='请输入密码(至少 8 位)'
autoComplete='new-password'
className='h-10 text-sm [@media(max-height:700px)]:h-9'
onKeyDown={(e) => e.key === 'Enter' && handleRegister()}
/>
</Field>
{emailRegisterEnabled && (
<Field className="gap-1.5 [@media(min-width:500px)]:col-span-2">
<FieldLabel htmlFor="code">邮箱验证码 <span className="text-destructive">*</span></FieldLabel>
<div className="flex gap-2">
<Field className='gap-1.5 [@media(min-width:500px)]:col-span-2'>
<FieldLabel htmlFor='code'>
邮箱验证码 <span className='text-destructive'>*</span>
</FieldLabel>
<div className='flex gap-2'>
<Input
id="code"
id='code'
value={code}
onChange={(e) => setCode(e.target.value)}
placeholder="请输入 6 位邮箱验证码"
placeholder='请输入 6 位邮箱验证码'
maxLength={6}
className="h-10 flex-1 text-sm [@media(max-height:700px)]:h-9"
onKeyDown={(e) => e.key === "Enter" && handleRegister()}
className='h-10 flex-1 text-sm [@media(max-height:700px)]:h-9'
onKeyDown={(e) => e.key === 'Enter' && handleRegister()}
/>
<Button
type="button"
variant="outline"
type='button'
variant='outline'
onClick={handleSendRegisterCode}
disabled={sendCodeDisabled}
className="h-10 w-[120px] text-xs [@media(max-height:700px)]:h-9"
className='h-10 w-[120px] text-xs [@media(max-height:700px)]:h-9'
>
{registerCooldown > 0 ? `${registerCooldown}秒后重发` : "获取验证码"}
{registerCooldown > 0
? `${registerCooldown}秒后重发`
: '获取验证码'}
</Button>
</div>
</Field>
@@ -342,15 +379,15 @@ export function RegisterForm() {
)}
{errorMessage ? (
<div className="rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive">
<div className='rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2 text-sm text-destructive'>
{errorMessage}
</div>
) : null}
<Button
type="button"
className="h-10 w-full [@media(max-height:700px)]:h-9"
variant="auth"
type='button'
className='h-10 w-full [@media(max-height:700px)]:h-9'
variant='auth'
onClick={handleRegister}
disabled={registerDisabled}
>
@@ -360,17 +397,20 @@ export function RegisterForm() {
注册中...
</>
) : (
"创建账号"
'创建账号'
)}
</Button>
</div>
<div className="text-center text-sm text-muted-foreground">
已经有账号?{" "}
<Link href="/login" className="font-medium text-foreground underline underline-offset-4">
<div className='text-center text-sm text-muted-foreground'>
已经有账号?{' '}
<Link
href='/login'
className='font-medium text-foreground underline underline-offset-4'
>
返回登录
</Link>
</div>
</div>
)
);
}
+4 -4
View File
@@ -1,12 +1,12 @@
"use client"
'use client';
import {RegisterForm} from "@/components/auth/register-form"
import {AuthShell} from "@/components/auth/auth-shell"
import { RegisterForm } from '@/components/auth/register-form';
import { AuthShell } from '@/components/auth/auth-shell';
export function RegisterPage() {
return (
<AuthShell wide>
<RegisterForm />
</AuthShell>
)
);
}
+28 -29
View File
@@ -1,16 +1,16 @@
"use client"
'use client';
import type {ReactNode} from "react"
import {Loader2} from "lucide-react"
import type { ReactNode } from 'react';
import { Loader2 } from 'lucide-react';
import {ErrorPage} from "@/components/layout/error"
import {useUser} from "@/contexts/user-context"
import { ErrorPage } from '@/components/layout/error';
import { useUser } from '@/contexts/user-context';
type RequireAuthProps = {
children: ReactNode
fallback?: ReactNode
minHeightClassName?: string
}
children: ReactNode;
fallback?: ReactNode;
minHeightClassName?: string;
};
/**
* Guards page content until the session user is available.
@@ -19,49 +19,48 @@ type RequireAuthProps = {
export function RequireAuth({
children,
fallback,
minHeightClassName = "min-h-[400px]",
minHeightClassName = 'min-h-[400px]',
}: RequireAuthProps) {
const {user, loading} = useUser()
const { user, loading } = useUser();
if (loading) {
return fallback ?? (
<div className={`flex items-center justify-center ${minHeightClassName}`}>
<Loader2 className="size-6 animate-spin text-primary" />
return (
fallback ?? (
<div
className={`flex items-center justify-center ${minHeightClassName}`}
>
<Loader2 className='size-6 animate-spin text-primary' />
</div>
)
);
}
if (!user) {
return null
return null;
}
return <>{children}</>
return <>{children}</>;
}
type RequireAdminAuthProps = {
children: ReactNode
}
children: ReactNode;
};
/** Guards admin routes after the shared shell has rendered. */
export function RequireAdminAuth({ children }: RequireAdminAuthProps) {
const {user, loading} = useUser()
const { user, loading } = useUser();
if (loading) {
return (
<div className="flex min-h-[400px] items-center justify-center">
<Loader2 className="size-6 animate-spin text-primary" />
<div className='flex min-h-[400px] items-center justify-center'>
<Loader2 className='size-6 animate-spin text-primary' />
</div>
)
);
}
if (!user?.is_admin) {
return (
<ErrorPage
title="访问被拒绝"
message="您没有权限访问此页面"
/>
)
return <ErrorPage title='访问被拒绝' message='您没有权限访问此页面' />;
}
return <>{children}</>
return <>{children}</>;
}
+311 -108
View File
@@ -1,5 +1,5 @@
import {type PolicySection} from "./types"
import {CodeBlock} from "@/components/ui/code-block"
import { type PolicySection } from './types';
import { CodeBlock } from '@/components/ui/code-block';
import {
DocsTable,
DocsTableBody,
@@ -7,9 +7,9 @@ import {
DocsTableHead,
DocsTableHeader,
DocsTableRow,
} from "@/components/ui/docs-table"
} from '@/components/ui/docs-table';
export const DOCS_LAST_UPDATED = "2026-06-07"
export const DOCS_LAST_UPDATED = '2026-06-07';
/**
* ------------------------------------------------------------------
@@ -18,39 +18,53 @@ export const DOCS_LAST_UPDATED = "2026-06-07"
*/
export const apiSections: PolicySection[] = [
{
value: "api-specs",
title: "1. 接口规范与鉴权说明",
value: 'api-specs',
title: '1. 接口规范与鉴权说明',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className="bg-muted/50 border border-border/50 rounded-lg px-3 py-2 mb-6">
<p className="text-muted-foreground m-0">平台统一接口调用格式规范以及开发者访问令牌鉴权方式说明</p>
<div className='space-y-4 text-sm leading-relaxed'>
<div className='bg-muted/50 border border-border/50 rounded-lg px-3 py-2 mb-6'>
<p className='text-muted-foreground m-0'>
平台统一接口调用格式规范以及开发者访问令牌鉴权方式说明
</p>
</div>
<h3 id="1-1-response-format" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-3">1.1 统一响应格式</h3>
<h3
id='1-1-response-format'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-3'
>
1.1 统一响应格式
</h3>
<p>系统所有 API 接口均遵循标准 JSON 响应结构:</p>
<DocsTable>
<DocsTableHeader>
<DocsTableRow>
<DocsTableHead className="w-[120px]">字段</DocsTableHead>
<DocsTableHead className="w-[100px]">类型</DocsTableHead>
<DocsTableHead className='w-[120px]'>字段</DocsTableHead>
<DocsTableHead className='w-[100px]'>类型</DocsTableHead>
<DocsTableHead>说明</DocsTableHead>
</DocsTableRow>
</DocsTableHeader>
<DocsTableBody>
<DocsTableRow>
<DocsTableCell className="font-mono text-xs">error_msg</DocsTableCell>
<DocsTableCell className='font-mono text-xs'>
error_msg
</DocsTableCell>
<DocsTableCell>string</DocsTableCell>
<DocsTableCell>错误信息。请求成功时为空字符串 `&quot;&quot;`,失败时包含错误详情描述。</DocsTableCell>
<DocsTableCell>
错误信息。请求成功时为空字符串
`&quot;&quot;`,失败时包含错误详情描述。
</DocsTableCell>
</DocsTableRow>
<DocsTableRow>
<DocsTableCell className="font-mono text-xs">data</DocsTableCell>
<DocsTableCell className='font-mono text-xs'>data</DocsTableCell>
<DocsTableCell>any</DocsTableCell>
<DocsTableCell>接口返回的具体数据内容。请求失败或无数据返回时为 `null`。</DocsTableCell>
<DocsTableCell>
接口返回的具体数据内容。请求失败或无数据返回时为 `null`。
</DocsTableCell>
</DocsTableRow>
</DocsTableBody>
</DocsTable>
<p className="mt-2">成功响应示例:</p>
<p className='mt-2'>成功响应示例:</p>
<CodeBlock
code={`{
"error_msg": "",
@@ -60,70 +74,112 @@ export const apiSections: PolicySection[] = [
"nickname": "Ryan"
}
}`}
language="json"
language='json'
/>
<p className="mt-2">失败响应示例:</p>
<p className='mt-2'>失败响应示例:</p>
<CodeBlock
code={`{
"error_msg": "用户密码错误",
"data": null
}`}
language="json"
language='json'
/>
<h3 id="1-2-authentication" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-3">1.2 鉴权方式</h3>
<p>除了公共公开接口(如登录、注册、配置)外,受保护的接口需要携带凭证才能正常访问:</p>
<ul className="list-disc pl-5 space-y-2">
<li><strong>Session 凭证:</strong>浏览器环境下支持利用常规 Session Cookie 会话保持登录。</li>
<li><strong>AccessToken 令牌:</strong>供后台调用或第三方应用集成使用。客户端生成 API 访问令牌后,需要在请求头(Request Header)中携带以进行身份校验。</li>
<h3
id='1-2-authentication'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-3'
>
1.2 鉴权方式
</h3>
<p>
除了公共公开接口(如登录、注册、配置)外,受保护的接口需要携带凭证才能正常访问:
</p>
<ul className='list-disc pl-5 space-y-2'>
<li>
<strong>Session 凭证:</strong>浏览器环境下支持利用常规 Session
Cookie 会话保持登录。
</li>
<li>
<strong>AccessToken 令牌:</strong>
供后台调用或第三方应用集成使用。客户端生成 API
访问令牌后,需要在请求头(Request Header)中携带以进行身份校验。
</li>
</ul>
<div className="bg-muted border rounded-xl p-4 mt-2 space-y-2">
<p className="font-bold text-xs">支持携带令牌的请求头格式(二选一):</p>
<ul className="list-disc pl-5 text-xs text-muted-foreground space-y-1">
<li><code className="bg-muted-foreground/10 px-1 rounded text-[11px] font-mono">Authorization: Bearer at_xxx</code></li>
<li><code className="bg-muted-foreground/10 px-1 rounded text-[11px] font-mono">X-Access-Token: at_xxx</code></li>
<div className='bg-muted border rounded-xl p-4 mt-2 space-y-2'>
<p className='font-bold text-xs'>
支持携带令牌的请求头格式(二选一):
</p>
<ul className='list-disc pl-5 text-xs text-muted-foreground space-y-1'>
<li>
<code className='bg-muted-foreground/10 px-1 rounded text-[11px] font-mono'>
Authorization: Bearer at_xxx
</code>
</li>
<li>
<code className='bg-muted-foreground/10 px-1 rounded text-[11px] font-mono'>
X-Access-Token: at_xxx
</code>
</li>
</ul>
</div>
</div>
),
children: [
{ value: "1-1-response-format", title: "1.1 统一响应格式" },
{ value: "1-2-authentication", title: "1.2 鉴权方式" },
]
{ value: '1-1-response-format', title: '1.1 统一响应格式' },
{ value: '1-2-authentication', title: '1.2 鉴权方式' },
],
},
{
value: "auth-apis",
title: "2. 用户与认证接口",
value: 'auth-apis',
title: '2. 用户与认证接口',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<h3 id="2-1-register" className="text-base md:text-lg font-semibold text-foreground mt-4 mb-2">2.1 用户注册</h3>
<p><strong>接口:</strong>POST <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/register</code></p>
<p><strong>说明:</strong>注册本地账户(在后台注册开关开启状态下)。</p>
<div className='space-y-4 text-sm leading-relaxed'>
<h3
id='2-1-register'
className='text-base md:text-lg font-semibold text-foreground mt-4 mb-2'
>
2.1 用户注册
</h3>
<p>
<strong>接口:</strong>POST{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/register
</code>
</p>
<p>
<strong>说明:</strong>注册本地账户(在后台注册开关开启状态下)。
</p>
<DocsTable>
<DocsTableHeader>
<DocsTableRow>
<DocsTableHead className="w-[120px]">参数</DocsTableHead>
<DocsTableHead className="w-[80px]">必填</DocsTableHead>
<DocsTableHead className='w-[120px]'>参数</DocsTableHead>
<DocsTableHead className='w-[80px]'>必填</DocsTableHead>
<DocsTableHead>类型</DocsTableHead>
<DocsTableHead>说明</DocsTableHead>
</DocsTableRow>
</DocsTableHeader>
<DocsTableBody>
<DocsTableRow>
<DocsTableCell className="font-mono text-xs">username</DocsTableCell>
<DocsTableCell className='font-mono text-xs'>
username
</DocsTableCell>
<DocsTableCell>是</DocsTableCell>
<DocsTableCell>string</DocsTableCell>
<DocsTableCell>用户名,必须唯一且无空格。</DocsTableCell>
</DocsTableRow>
<DocsTableRow>
<DocsTableCell className="font-mono text-xs">password</DocsTableCell>
<DocsTableCell className='font-mono text-xs'>
password
</DocsTableCell>
<DocsTableCell>是</DocsTableCell>
<DocsTableCell>string</DocsTableCell>
<DocsTableCell>密码,长度必须大于等于 8 位。</DocsTableCell>
</DocsTableRow>
<DocsTableRow>
<DocsTableCell className="font-mono text-xs">nickname</DocsTableCell>
<DocsTableCell className='font-mono text-xs'>
nickname
</DocsTableCell>
<DocsTableCell>否</DocsTableCell>
<DocsTableCell>string</DocsTableCell>
<DocsTableCell>昵称。未传时默认与用户名一致。</DocsTableCell>
@@ -131,27 +187,44 @@ export const apiSections: PolicySection[] = [
</DocsTableBody>
</DocsTable>
<h3 id="2-2-login" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-2">2.2 密码登录</h3>
<p><strong>接口:</strong>POST <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/login</code></p>
<p><strong>说明:</strong>通过常规用户名密码进行登录校验,成功后建立 Session Cookie 会话。</p>
<h3
id='2-2-login'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-2'
>
2.2 密码登录
</h3>
<p>
<strong>接口:</strong>POST{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/login
</code>
</p>
<p>
<strong>说明:</strong>通过常规用户名密码进行登录校验,成功后建立
Session Cookie 会话。
</p>
<DocsTable>
<DocsTableHeader>
<DocsTableRow>
<DocsTableHead className="w-[120px]">参数</DocsTableHead>
<DocsTableHead className="w-[80px]">必填</DocsTableHead>
<DocsTableHead className='w-[120px]'>参数</DocsTableHead>
<DocsTableHead className='w-[80px]'>必填</DocsTableHead>
<DocsTableHead>类型</DocsTableHead>
<DocsTableHead>说明</DocsTableHead>
</DocsTableRow>
</DocsTableHeader>
<DocsTableBody>
<DocsTableRow>
<DocsTableCell className="font-mono text-xs">username</DocsTableCell>
<DocsTableCell className='font-mono text-xs'>
username
</DocsTableCell>
<DocsTableCell>是</DocsTableCell>
<DocsTableCell>string</DocsTableCell>
<DocsTableCell>用户名</DocsTableCell>
</DocsTableRow>
<DocsTableRow>
<DocsTableCell className="font-mono text-xs">password</DocsTableCell>
<DocsTableCell className='font-mono text-xs'>
password
</DocsTableCell>
<DocsTableCell>是</DocsTableCell>
<DocsTableCell>string</DocsTableCell>
<DocsTableCell>密码</DocsTableCell>
@@ -159,41 +232,103 @@ export const apiSections: PolicySection[] = [
</DocsTableBody>
</DocsTable>
<h3 id="2-3-logout" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-2">2.3 退出登录</h3>
<p><strong>接口:</strong>GET <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/logout</code></p>
<p><strong>说明:</strong>销毁当前会话 Cookie 并退出登录状态。</p>
<h3
id='2-3-logout'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-2'
>
2.3 退出登录
</h3>
<p>
<strong>接口:</strong>GET{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/logout
</code>
</p>
<p>
<strong>说明:</strong>销毁当前会话 Cookie 并退出登录状态。
</p>
<h3 id="2-4-profile" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-2">2.4 获取个人资料</h3>
<p><strong>接口:</strong>GET <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/self</code></p>
<p><strong>说明:</strong>获取当前登录账户的基本数据模型(包含 ID、角色、昵称等)。</p>
<h3
id='2-4-profile'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-2'
>
2.4 获取个人资料
</h3>
<p>
<strong>接口:</strong>GET{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/self
</code>
</p>
<p>
<strong>说明:</strong>获取当前登录账户的基本数据模型(包含
ID、角色、昵称等)。
</p>
</div>
),
children: [
{ value: "2-1-register", title: "2.1 用户注册" },
{ value: "2-2-login", title: "2.2 密码登录" },
{ value: "2-3-logout", title: "2.3 退出登录" },
{ value: "2-4-profile", title: "2.4 获取个人资料" },
]
{ value: '2-1-register', title: '2.1 用户注册' },
{ value: '2-2-login', title: '2.2 密码登录' },
{ value: '2-3-logout', title: '2.3 退出登录' },
{ value: '2-4-profile', title: '2.4 获取个人资料' },
],
},
{
value: "token-apis",
title: "3. 个人访问令牌 (AccessToken) 接口",
value: 'token-apis',
title: '3. 个人访问令牌 (AccessToken) 接口',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className="bg-muted/50 border border-border/50 rounded-lg px-3 py-2 mb-4">
<p className="text-muted-foreground m-0">AccessToken 管理相关接口均要求通过 Session 登录后调用,支持普通用户权限。</p>
<div className='space-y-4 text-sm leading-relaxed'>
<div className='bg-muted/50 border border-border/50 rounded-lg px-3 py-2 mb-4'>
<p className='text-muted-foreground m-0'>
AccessToken 管理相关接口均要求通过 Session
登录后调用,支持普通用户权限。
</p>
</div>
<h3 id="3-1-list-token" className="text-base md:text-lg font-semibold text-foreground mt-4 mb-2">3.1 获取令牌列表</h3>
<p><strong>接口:</strong>GET <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/access-tokens</code></p>
<p><strong>说明:</strong>查询当前用户已创建的所有令牌详情(令牌明文已被脱敏)。</p>
<h3
id='3-1-list-token'
className='text-base md:text-lg font-semibold text-foreground mt-4 mb-2'
>
3.1 获取令牌列表
</h3>
<p>
<strong>接口:</strong>GET{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/access-tokens
</code>
</p>
<p>
<strong>说明:</strong>
查询当前用户已创建的所有令牌详情(令牌明文已被脱敏)。
</p>
<h3 id="3-2-create-token" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-2">3.2 新建访问令牌</h3>
<p><strong>接口:</strong>POST <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/access-tokens</code></p>
<p><strong>参数:</strong>JSON Body <code className="bg-muted px-1.5 rounded text-xs font-mono">{`{"name": "token名称", "is_admin": false}`}</code></p>
<p><strong>说明:</strong>生成一个全新访问令牌。返回体中包含一次性明文 Token,切勿遗失。</p>
<p className="mt-1 text-xs text-muted-foreground"><code className="bg-muted px-1 rounded">is_admin</code>(可选,默认 <code className="bg-muted px-1 rounded">false</code>):是否赋予令牌管理员权限,仅管理员用户可设置。非管理员令牌无法访问 <code className="bg-muted px-1 rounded">/admin/**</code> 端点。</p>
<p className="mt-2">成功返回样例:</p>
<h3
id='3-2-create-token'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-2'
>
3.2 新建访问令牌
</h3>
<p>
<strong>接口:</strong>POST{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/access-tokens
</code>
</p>
<p>
<strong>参数:</strong>JSON Body{' '}
<code className='bg-muted px-1.5 rounded text-xs font-mono'>{`{"name": "token名称", "is_admin": false}`}</code>
</p>
<p>
<strong>说明:</strong>生成一个全新访问令牌。返回体中包含一次性明文
Token,切勿遗失。
</p>
<p className='mt-1 text-xs text-muted-foreground'>
<code className='bg-muted px-1 rounded'>is_admin</code>(可选,默认{' '}
<code className='bg-muted px-1 rounded'>false</code>
):是否赋予令牌管理员权限,仅管理员用户可设置。非管理员令牌无法访问{' '}
<code className='bg-muted px-1 rounded'>/admin/**</code> 端点。
</p>
<p className='mt-2'>成功返回样例:</p>
<CodeBlock
code={`{
"error_msg": "",
@@ -209,34 +344,74 @@ export const apiSections: PolicySection[] = [
}
}
}`}
language="json"
language='json'
/>
<h3 id="3-3-delete-token" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-2">3.3 撤销/删除令牌</h3>
<p><strong>接口:</strong>DELETE <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/access-tokens/:id</code></p>
<p><strong>说明:</strong>通过 ID 物理删除对应访问令牌,该令牌将立即失效。</p>
<h3
id='3-3-delete-token'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-2'
>
3.3 撤销/删除令牌
</h3>
<p>
<strong>接口:</strong>DELETE{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/access-tokens/:id
</code>
</p>
<p>
<strong>说明:</strong>通过 ID
物理删除对应访问令牌,该令牌将立即失效。
</p>
<h3 id="3-4-rotate-token" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-2">3.4 轮换令牌密钥</h3>
<p><strong>接口:</strong>POST <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/user/access-tokens/:id/rotate</code></p>
<p><strong>说明:</strong>轮换指定令牌的物理密钥值。系统将废弃原有密钥,返回新生成的明文 Token,令牌名称与 ID 保持一致。</p>
<h3
id='3-4-rotate-token'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-2'
>
3.4 轮换令牌密钥
</h3>
<p>
<strong>接口:</strong>POST{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/user/access-tokens/:id/rotate
</code>
</p>
<p>
<strong>说明:</strong>
轮换指定令牌的物理密钥值。系统将废弃原有密钥,返回新生成的明文
Token,令牌名称与 ID 保持一致。
</p>
</div>
),
children: [
{ value: "3-1-list-token", title: "3.1 获取令牌列表" },
{ value: "3-2-create-token", title: "3.2 新建访问令牌" },
{ value: "3-3-delete-token", title: "3.3 撤销/删除令牌" },
{ value: "3-4-rotate-token", title: "3.4 轮换令牌密钥" },
]
{ value: '3-1-list-token', title: '3.1 获取令牌列表' },
{ value: '3-2-create-token', title: '3.2 新建访问令牌' },
{ value: '3-3-delete-token', title: '3.3 撤销/删除令牌' },
{ value: '3-4-rotate-token', title: '3.4 轮换令牌密钥' },
],
},
{
value: "config-apis",
title: "4. 公共配置与管理接口",
value: 'config-apis',
title: '4. 公共配置与管理接口',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<h3 id="4-1-public-config" className="text-base md:text-lg font-semibold text-foreground mt-4 mb-2">4.1 公共系统配置</h3>
<p><strong>接口:</strong>GET <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">/api/v1/config/public</code></p>
<p><strong>说明:</strong>无感获取当前系统配置表中公共可见的键值集合。供前端页面动态渲染使用。</p>
<p className="mt-2">返回数据结构样例:</p>
<div className='space-y-4 text-sm leading-relaxed'>
<h3
id='4-1-public-config'
className='text-base md:text-lg font-semibold text-foreground mt-4 mb-2'
>
4.1 公共系统配置
</h3>
<p>
<strong>接口:</strong>GET{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
/api/v1/config/public
</code>
</p>
<p>
<strong>说明:</strong>
无感获取当前系统配置表中公共可见的键值集合。供前端页面动态渲染使用。
</p>
<p className='mt-2'>返回数据结构样例:</p>
<CodeBlock
code={`{
"error_msg": "",
@@ -248,22 +423,50 @@ export const apiSections: PolicySection[] = [
"oidc_login_enabled": "true"
}
}`}
language="json"
language='json'
/>
<h3 id="4-2-admin-configs" className="text-base md:text-lg font-semibold text-foreground mt-6 mb-2">4.2 系统配置项 CRUD (管理员)</h3>
<p><strong>说明:</strong>用于在后台对 `system_configs` 配置进行动态变更,要求管理员权限会话调用。</p>
<ul className="list-disc pl-5 space-y-2">
<li><strong>获取配置列表:</strong>GET <code className="bg-muted px-1 rounded text-xs font-mono">/api/v1/admin/system-configs?type=system</code></li>
<li><strong>新建配置项:</strong>POST <code className="bg-muted px-1 rounded text-xs font-mono">/api/v1/admin/system-configs</code></li>
<li><strong>修改指定配置值:</strong>PUT <code className="bg-muted px-1 rounded text-xs font-mono">/api/v1/admin/system-configs/:key</code></li>
<li><strong>删除配置项:</strong>DELETE <code className="bg-muted px-1 rounded text-xs font-mono">/api/v1/admin/system-configs/:key</code></li>
<h3
id='4-2-admin-configs'
className='text-base md:text-lg font-semibold text-foreground mt-6 mb-2'
>
4.2 系统配置项 CRUD (管理员)
</h3>
<p>
<strong>说明:</strong>用于在后台对 `system_configs`
配置进行动态变更,要求管理员权限会话调用。
</p>
<ul className='list-disc pl-5 space-y-2'>
<li>
<strong>获取配置列表:</strong>GET{' '}
<code className='bg-muted px-1 rounded text-xs font-mono'>
/api/v1/admin/system-configs?type=system
</code>
</li>
<li>
<strong>新建配置项:</strong>POST{' '}
<code className='bg-muted px-1 rounded text-xs font-mono'>
/api/v1/admin/system-configs
</code>
</li>
<li>
<strong>修改指定配置值:</strong>PUT{' '}
<code className='bg-muted px-1 rounded text-xs font-mono'>
/api/v1/admin/system-configs/:key
</code>
</li>
<li>
<strong>删除配置项:</strong>DELETE{' '}
<code className='bg-muted px-1 rounded text-xs font-mono'>
/api/v1/admin/system-configs/:key
</code>
</li>
</ul>
</div>
),
children: [
{ value: "4-1-public-config", title: "4.1 公共系统配置" },
{ value: "4-2-admin-configs", title: "4.2 系统配置项 CRUD (管理员)" },
]
}
]
{ value: '4-1-public-config', title: '4.1 公共系统配置' },
{ value: '4-2-admin-configs', title: '4.2 系统配置项 CRUD (管理员)' },
],
},
];
+193 -77
View File
@@ -1,5 +1,5 @@
import {type PolicySection} from "./types"
import {CodeBlock} from "@/components/ui/code-block"
import { type PolicySection } from './types';
import { CodeBlock } from '@/components/ui/code-block';
/**
* ------------------------------------------------------------------
@@ -8,131 +8,247 @@ import {CodeBlock} from "@/components/ui/code-block"
*/
export const howToUseSections: PolicySection[] = [
{
value: "quick-start",
title: "1. 快速开始",
value: 'quick-start',
title: '1. 快速开始',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className="bg-muted/50 border border-border/50 rounded-lg px-3 py-2 mb-6">
<p className="text-muted-foreground m-0">为开发者提供通用全栈开发脚手架 (Boilerplate) 平台使用说明</p>
<div className='space-y-4 text-sm leading-relaxed'>
<div className='bg-muted/50 border border-border/50 rounded-lg px-3 py-2 mb-6'>
<p className='text-muted-foreground m-0'>
为开发者提供通用全栈开发脚手架 (Boilerplate) 平台使用说明
</p>
</div>
<ul className="list-disc pl-5 space-y-2">
<li><strong>架构底座:</strong>Go (Gin + GORM + Redis + Asynq) 后端 + React (Next.js 16 + Tailwind CSS 4 + Shadcn UI) 前端</li>
<li><strong>认证体系:</strong>支持本地常规账号密码注册登录 + 第三方自定义 OIDC (OAuth2) 认证源绑定</li>
<li><strong>访问令牌:</strong>提供开发者个人 AccessToken (API Key),用于通过 Http Header 鉴权直接调用系统 API</li>
<li><strong>可观测性:</strong>集成 Zap 结构化日志与 OpenTelemetry 全链路 Tracing 追踪</li>
<ul className='list-disc pl-5 space-y-2'>
<li>
<strong>架构底座:</strong>Go (Gin + GORM + Redis + Asynq) 后端 +
React (Next.js 16 + Tailwind CSS 4 + Shadcn UI) 前端
</li>
<li>
<strong>认证体系:</strong>支持本地常规账号密码注册登录 +
第三方自定义 OIDC (OAuth2) 认证源绑定
</li>
<li>
<strong>访问令牌:</strong>提供开发者个人 AccessToken (API
Key),用于通过 Http Header 鉴权直接调用系统 API
</li>
<li>
<strong>可观测性:</strong>集成 Zap 结构化日志与 OpenTelemetry
全链路 Tracing 追踪
</li>
</ul>
</div>
)
),
},
{
value: "auth-security",
title: "2. 身份认证与安全设置",
value: 'auth-security',
title: '2. 身份认证与安全设置',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className='space-y-4 text-sm leading-relaxed'>
<p>平台采用混合式身份认证,满足不同的部署和业务场景需求:</p>
<h3 id="2-1-login" className="text-base md:text-lg font-semibold text-foreground mt-4 mb-2">2.1 常规账号密码认证</h3>
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>自主注册与密码登录:</strong>支持普通用户通过用户名及密码直接进行注册与会话建立,密码在后端采用 <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">bcrypt</code> 高强度加盐哈希存储。</li>
<li><strong>系统开关控制:</strong>管理员可在后台配置动态开关,随时禁用自主密码注册或密码登录,以转为纯第三方认证模式。</li>
<h3
id='2-1-login'
className='text-base md:text-lg font-semibold text-foreground mt-4 mb-2'
>
2.1 常规账号密码认证
</h3>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>自主注册与密码登录:</strong>
支持普通用户通过用户名及密码直接进行注册与会话建立,密码在后端采用{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
bcrypt
</code>{' '}
高强度加盐哈希存储。
</li>
<li>
<strong>系统开关控制:</strong>
管理员可在后台配置动态开关,随时禁用自主密码注册或密码登录,以转为纯第三方认证模式。
</li>
</ul>
<h3 id="2-2-oidc" className="text-base md:text-lg font-semibold text-foreground mt-4 mb-2">2.2 第三方 OIDC 认证源</h3>
<h3
id='2-2-oidc'
className='text-base md:text-lg font-semibold text-foreground mt-4 mb-2'
>
2.2 第三方 OIDC 认证源
</h3>
<p>用户可以在个人资料页面关联绑定外部授权账户:</p>
<ol className="list-decimal pl-4 md:pl-5 space-y-1">
<li>进入 <strong>设置 / 个人资料</strong> 页面。</li>
<li>在“第三方账号绑定”栏目下查看当前绑定的账号,或点击未绑定的可用 OIDC 认证源直接触发 OAuth2 绑定流。</li>
<li>绑定成功后,用户在登录界面可直接点击 OIDC 登录按钮实现快捷跳转。</li>
<ol className='list-decimal pl-4 md:pl-5 space-y-1'>
<li>
进入 <strong>设置 / 个人资料</strong> 页面。
</li>
<li>
在“第三方账号绑定”栏目下查看当前绑定的账号,或点击未绑定的可用 OIDC
认证源直接触发 OAuth2 绑定流。
</li>
<li>
绑定成功后,用户在登录界面可直接点击 OIDC 登录按钮实现快捷跳转。
</li>
</ol>
</div>
),
children: [
{ value: "2-1-login", title: "2.1 常规账号密码认证" },
{ value: "2-2-oidc", title: "2.2 第三方 OIDC 认证源" },
]
{ value: '2-1-login', title: '2.1 常规账号密码认证' },
{ value: '2-2-oidc', title: '2.2 第三方 OIDC 认证源' },
],
},
{
value: "access-token",
title: "3. 个人访问令牌 (AccessToken) 接口对接",
value: 'access-token',
title: '3. 个人访问令牌 (AccessToken) 接口对接',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p>为便于开发者或第三方工具直接调用系统 API,平台提供个人访问令牌管理功能。</p>
<h3 id="3-1-generation" className="text-base md:text-lg font-semibold text-foreground mt-4 mb-2">3.1 令牌生成与存储规范</h3>
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>一次性明文展示:</strong>创建令牌时生成的随机明文 Token 值 (形如 <code className="bg-muted px-1.5 py-0.5 rounded text-xs font-mono">at_xxx</code>) 仅会在弹窗中展示一次。请立即复制保存,关闭弹窗后系统将无法重新获取。</li>
<li><strong>安全哈希存储:</strong>数据库仅存储 Token 的 SHA-256 哈希指纹,即使数据库泄漏,攻击者也无法通过摘要逆向恢复令牌原文。</li>
<div className='space-y-4 text-sm leading-relaxed'>
<p>
为便于开发者或第三方工具直接调用系统
API,平台提供个人访问令牌管理功能。
</p>
<h3
id='3-1-generation'
className='text-base md:text-lg font-semibold text-foreground mt-4 mb-2'
>
3.1 令牌生成与存储规范
</h3>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>一次性明文展示:</strong>创建令牌时生成的随机明文 Token 值
(形如{' '}
<code className='bg-muted px-1.5 py-0.5 rounded text-xs font-mono'>
at_xxx
</code>
)
仅会在弹窗中展示一次。请立即复制保存,关闭弹窗后系统将无法重新获取。
</li>
<li>
<strong>安全哈希存储:</strong>数据库仅存储 Token 的 SHA-256
哈希指纹,即使数据库泄漏,攻击者也无法通过摘要逆向恢复令牌原文。
</li>
</ul>
<h3 id="3-2-usage" className="text-base md:text-lg font-semibold text-foreground mt-4 mb-2">3.2 携带 Header 进行 API 调用</h3>
<p>您可以凭借保存的明文 Token 随时调用系统开放接口,系统认证支持以下两种 Http 请求头携带方式之一:</p>
<div className="space-y-2">
<p className="font-semibold text-xs text-muted-foreground">方式一:Authorization Bearer 头</p>
<h3
id='3-2-usage'
className='text-base md:text-lg font-semibold text-foreground mt-4 mb-2'
>
3.2 携带 Header 进行 API 调用
</h3>
<p>
您可以凭借保存的明文 Token 随时调用系统开放接口,系统认证支持以下两种
Http 请求头携带方式之一:
</p>
<div className='space-y-2'>
<p className='font-semibold text-xs text-muted-foreground'>
方式一:Authorization Bearer 头
</p>
<CodeBlock
code={`GET /api/v1/user/self HTTP/1.1
Host: localhost:8000
Authorization: Bearer at_628d022b7a95e26b...`}
language="http"
language='http'
/>
</div>
<div className="space-y-2">
<p className="font-semibold text-xs text-muted-foreground">方式二:X-Access-Token 自定义头</p>
<div className='space-y-2'>
<p className='font-semibold text-xs text-muted-foreground'>
方式二:X-Access-Token 自定义头
</p>
<CodeBlock
code={`GET /api/v1/user/self HTTP/1.1
Host: localhost:8000
X-Access-Token: at_628d022b7a95e26b...`}
language="http"
language='http'
/>
</div>
</div>
),
children: [
{ value: "3-1-generation", title: "3.1 令牌生成与存储规范" },
{ value: "3-2-usage", title: "3.2 携带 Header 进行 API 调用" },
]
{ value: '3-1-generation', title: '3.1 令牌生成与存储规范' },
{ value: '3-2-usage', title: '3.2 携带 Header 进行 API 调用' },
],
},
{
value: "config-system",
title: "4. 动态系统配置项",
value: 'config-system',
title: '4. 动态系统配置项',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p>平台内置了完备的 KV 配置管理模块,允许管理员动态调整系统运行状态:</p>
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>缓存读取加速:</strong>配置加载基于 GORM 读取数据库,并辅以 Redis Hash 结构进行多层缓存加速,大幅降低配置查询耗时。</li>
<li><strong>核心系统配置项说明:</strong>
<ul className="list-[circle] pl-5 mt-1 space-y-1 text-muted-foreground">
<li><code className="bg-muted px-1 rounded text-xs font-mono">site_name</code>:平台展示名称</li>
<li><code className="bg-muted px-1 rounded text-xs font-mono">password_login_enabled</code>:密码登录开关</li>
<li><code className="bg-muted px-1 rounded text-xs font-mono">registration_enabled</code>:用户自主注册开关</li>
<li><code className="bg-muted px-1 rounded text-xs font-mono">max_api_keys_per_user</code>:普通用户创建令牌的最大数限制 (默认5)</li>
<div className='space-y-4 text-sm leading-relaxed'>
<p>
平台内置了完备的 KV 配置管理模块,允许管理员动态调整系统运行状态:
</p>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>缓存读取加速:</strong>配置加载基于 GORM 读取数据库,并辅以
Redis Hash 结构进行多层缓存加速,大幅降低配置查询耗时。
</li>
<li>
<strong>核心系统配置项说明:</strong>
<ul className='list-[circle] pl-5 mt-1 space-y-1 text-muted-foreground'>
<li>
<code className='bg-muted px-1 rounded text-xs font-mono'>
site_name
</code>
:平台展示名称
</li>
<li>
<code className='bg-muted px-1 rounded text-xs font-mono'>
password_login_enabled
</code>
:密码登录开关
</li>
<li>
<code className='bg-muted px-1 rounded text-xs font-mono'>
registration_enabled
</code>
:用户自主注册开关
</li>
<li>
<code className='bg-muted px-1 rounded text-xs font-mono'>
max_api_keys_per_user
</code>
:普通用户创建令牌的最大数限制 (默认5)
</li>
</ul>
</li>
</ul>
</div>
)
),
},
{
value: "worker-scheduler",
title: "5. 异步任务与定时调度",
value: 'worker-scheduler',
title: '5. 异步任务与定时调度',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p>项目借助 Cobra 实现多命令入口分发,通过独立部署 Worker 服务解耦复杂计算或高延迟IO:</p>
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>定时任务派发 (CMD scheduler):</strong>负责按 Cron 表达式配置,定时将待执行任务推送到 Redis 队列中。</li>
<li><strong>多优先级 Worker (CMD worker):</strong>基于 Asynq 驱动,按优先级拉取任务并并发调度执行(如定时清理临时上传目录、同步外部系统日志等)。</li>
<div className='space-y-4 text-sm leading-relaxed'>
<p>
项目借助 Cobra 实现多命令入口分发,通过独立部署 Worker
服务解耦复杂计算或高延迟IO:
</p>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>定时任务派发 (CMD scheduler):</strong>负责按 Cron
表达式配置,定时将待执行任务推送到 Redis 队列中。
</li>
<li>
<strong>多优先级 Worker (CMD worker):</strong>基于 Asynq
驱动,按优先级拉取任务并并发调度执行(如定时清理临时上传目录、同步外部系统日志等)。
</li>
</ul>
</div>
)
),
},
{
value: "tracing-metrics",
title: "6. 链路追踪与结构化日志",
value: 'tracing-metrics',
title: '6. 链路追踪与结构化日志',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className='space-y-4 text-sm leading-relaxed'>
<p>为了保障分布式微服务架构下的可观测性,平台接入了高级监控组件:</p>
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>OpenTelemetry Tracing:</strong>自动传递 Tracing 上下文,所有经由 Gin 中间件、外部请求或 GORM 数据库的事务操作都将带有全局唯一的 Span,用于排查链路耗时或调用异常。</li>
<li><strong>Zap 结构化日志:</strong>将后端控制台或日志输出格式统一规范化为 JSON,方便与 ELK、Loki 等日志收集分析工具无缝对接。</li>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>OpenTelemetry Tracing:</strong>自动传递 Tracing
上下文,所有经由 Gin 中间件、外部请求或 GORM
数据库的事务操作都将带有全局唯一的
Span,用于排查链路耗时或调用异常。
</li>
<li>
<strong>Zap 结构化日志:</strong>
将后端控制台或日志输出格式统一规范化为 JSON,方便与 ELK、Loki
等日志收集分析工具无缝对接。
</li>
</ul>
</div>
)
}
]
),
},
];
@@ -1,18 +1,18 @@
"use client"
'use client';
import * as React from "react"
import Link from "next/link"
import {ArrowLeft, Calendar, Check, Copy, FileText} from "lucide-react"
import {Button} from "@/components/ui/button"
import {cn} from "@/lib/utils"
import {motion, useScroll, useSpring} from "motion/react"
import type {PolicySection} from "./types"
import * as React from 'react';
import Link from 'next/link';
import { ArrowLeft, Calendar, Check, Copy, FileText } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';
import { motion, useScroll, useSpring } from 'motion/react';
import type { PolicySection } from './types';
interface LegalPageLayoutProps {
title: string
lastUpdated: string
sections: PolicySection[]
description?: React.ReactNode
title: string;
lastUpdated: string;
sections: PolicySection[];
description?: React.ReactNode;
}
export function LegalPageLayout({
@@ -21,245 +21,288 @@ export function LegalPageLayout({
sections,
description,
}: LegalPageLayoutProps) {
const [activeSection, setActiveSection] = React.useState<string>(sections[0]?.value || "")
const [hasCopied, setHasCopied] = React.useState(false)
const { scrollYProgress } = useScroll()
const contentRef = React.useRef<HTMLDivElement | null>(null)
const [activeSection, setActiveSection] = React.useState<string>(
sections[0]?.value || '',
);
const [hasCopied, setHasCopied] = React.useState(false);
const { scrollYProgress } = useScroll();
const contentRef = React.useRef<HTMLDivElement | null>(null);
const scaleX = useSpring(scrollYProgress, {
stiffness: 100,
damping: 30,
restDelta: 0.001
})
restDelta: 0.001,
});
const allSectionIds = React.useMemo(() => {
return sections.flatMap(section => [
return sections.flatMap((section) => [
section.value,
...(section.children?.map(child => child.value) || [])
])
}, [sections])
...(section.children?.map((child) => child.value) || []),
]);
}, [sections]);
React.useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
setActiveSection(entry.target.id)
setActiveSection(entry.target.id);
}
})
});
},
{ rootMargin: "-20% 0px -35% 0px" }
)
{ rootMargin: '-20% 0px -35% 0px' },
);
allSectionIds.forEach((id) => {
const element = document.getElementById(id)
if (element) observer.observe(element)
})
const element = document.getElementById(id);
if (element) observer.observe(element);
});
return () => observer.disconnect()
}, [sections, allSectionIds])
return () => observer.disconnect();
}, [sections, allSectionIds]);
const convertInlineNodes = (nodes: ChildNode[]): string => {
return nodes.map((node) => {
return nodes
.map((node) => {
if (node.nodeType === Node.TEXT_NODE) {
return node.textContent || ""
return node.textContent || '';
}
if (node.nodeType !== Node.ELEMENT_NODE) {
return ""
return '';
}
const element = node as HTMLElement
const tagName = element.tagName.toLowerCase()
if (tagName === "code") {
return `\`${element.textContent?.trim() || ""}\``
const element = node as HTMLElement;
const tagName = element.tagName.toLowerCase();
if (tagName === 'code') {
return `\`${element.textContent?.trim() || ''}\``;
}
if (tagName === "strong" || tagName === "b") {
return `**${convertInlineNodes(Array.from(element.childNodes))}**`
if (tagName === 'strong' || tagName === 'b') {
return `**${convertInlineNodes(Array.from(element.childNodes))}**`;
}
if (tagName === "em" || tagName === "i") {
return `*${convertInlineNodes(Array.from(element.childNodes))}*`
if (tagName === 'em' || tagName === 'i') {
return `*${convertInlineNodes(Array.from(element.childNodes))}*`;
}
if (tagName === "a") {
const text = convertInlineNodes(Array.from(element.childNodes)) || element.textContent || ""
const href = element.getAttribute("href") || ""
return href ? `[${text}](${href})` : text
if (tagName === 'a') {
const text =
convertInlineNodes(Array.from(element.childNodes)) ||
element.textContent ||
'';
const href = element.getAttribute('href') || '';
return href ? `[${text}](${href})` : text;
}
if (tagName === "br") {
return "\n"
}
return convertInlineNodes(Array.from(element.childNodes))
}).join("")
if (tagName === 'br') {
return '\n';
}
return convertInlineNodes(Array.from(element.childNodes));
})
.join('');
};
const convertList = (
list: HTMLOListElement | HTMLUListElement,
indentLevel: number
indentLevel: number,
): string => {
const isOrdered = list.tagName.toLowerCase() === "ol"
const items = Array.from(list.children).filter((child) => child.tagName.toLowerCase() === "li")
return items.map((item, index) => {
const prefix = isOrdered ? `${index + 1}. ` : "- "
const indent = " ".repeat(indentLevel)
const parts: string[] = []
const itemElement = item as HTMLElement
const childNodes = Array.from(itemElement.childNodes)
const nestedLists: HTMLElement[] = []
const isOrdered = list.tagName.toLowerCase() === 'ol';
const items = Array.from(list.children).filter(
(child) => child.tagName.toLowerCase() === 'li',
);
return items
.map((item, index) => {
const prefix = isOrdered ? `${index + 1}. ` : '- ';
const indent = ' '.repeat(indentLevel);
const parts: string[] = [];
const itemElement = item as HTMLElement;
const childNodes = Array.from(itemElement.childNodes);
const nestedLists: HTMLElement[] = [];
childNodes.forEach((child) => {
if (child.nodeType === Node.ELEMENT_NODE) {
const childElement = child as HTMLElement
if (childElement.tagName.toLowerCase() === "ul" || childElement.tagName.toLowerCase() === "ol") {
nestedLists.push(childElement)
return
const childElement = child as HTMLElement;
if (
childElement.tagName.toLowerCase() === 'ul' ||
childElement.tagName.toLowerCase() === 'ol'
) {
nestedLists.push(childElement);
return;
}
}
parts.push(convertInlineNodes([child]).trim())
})
parts.push(convertInlineNodes([child]).trim());
});
const baseLine = `${indent}${prefix}${parts.join("").trim()}`
const nestedMarkdown = nestedLists.map((nested) => convertList(nested as HTMLOListElement | HTMLUListElement, indentLevel + 1))
return [baseLine, ...nestedMarkdown].filter(Boolean).join("\n")
}).join("\n")
}
const baseLine = `${indent}${prefix}${parts.join('').trim()}`;
const nestedMarkdown = nestedLists.map((nested) =>
convertList(
nested as HTMLOListElement | HTMLUListElement,
indentLevel + 1,
),
);
return [baseLine, ...nestedMarkdown].filter(Boolean).join('\n');
})
.join('\n');
};
const convertTable = (table: HTMLTableElement): string => {
const rows = Array.from(table.querySelectorAll("tr")).map((row) =>
Array.from(row.querySelectorAll("th,td")).map((cell) => (cell.textContent || "").trim())
)
if (!rows.length) return ""
const header = rows[0]
const body = rows.slice(1)
const headerLine = `| ${header.join(" | ")} |`
const separatorLine = `| ${header.map(() => "---").join(" | ")} |`
const bodyLines = body.map((row) => `| ${row.join(" | ")} |`)
return [headerLine, separatorLine, ...bodyLines].join("\n")
}
const rows = Array.from(table.querySelectorAll('tr')).map((row) =>
Array.from(row.querySelectorAll('th,td')).map((cell) =>
(cell.textContent || '').trim(),
),
);
if (!rows.length) return '';
const header = rows[0];
const body = rows.slice(1);
const headerLine = `| ${header.join(' | ')} |`;
const separatorLine = `| ${header.map(() => '---').join(' | ')} |`;
const bodyLines = body.map((row) => `| ${row.join(' | ')} |`);
return [headerLine, separatorLine, ...bodyLines].join('\n');
};
const convertBlockNode = (node: HTMLElement, indentLevel: number): string => {
const tagName = node.tagName.toLowerCase()
if (tagName === "h3") return `### ${node.textContent?.trim() || ""}`
if (tagName === "h4") return `#### ${node.textContent?.trim() || ""}`
if (tagName === "p") return convertInlineNodes(Array.from(node.childNodes)).trim()
if (tagName === "ul" || tagName === "ol") return convertList(node as HTMLOListElement | HTMLUListElement, indentLevel)
if (tagName === "pre") {
const code = node.textContent || ""
return `\`\`\`\n${code.trim()}\n\`\`\``
const tagName = node.tagName.toLowerCase();
if (tagName === 'h3') return `### ${node.textContent?.trim() || ''}`;
if (tagName === 'h4') return `#### ${node.textContent?.trim() || ''}`;
if (tagName === 'p')
return convertInlineNodes(Array.from(node.childNodes)).trim();
if (tagName === 'ul' || tagName === 'ol')
return convertList(
node as HTMLOListElement | HTMLUListElement,
indentLevel,
);
if (tagName === 'pre') {
const code = node.textContent || '';
return `\`\`\`\n${code.trim()}\n\`\`\``;
}
if (tagName === "table") return convertTable(node as HTMLTableElement)
if (tagName === "div") {
const hasTable = node.querySelector("table")
const hasList = node.querySelector("ul,ol")
const hasPre = node.querySelector("pre")
if (tagName === 'table') return convertTable(node as HTMLTableElement);
if (tagName === 'div') {
const hasTable = node.querySelector('table');
const hasList = node.querySelector('ul,ol');
const hasPre = node.querySelector('pre');
if (hasTable || hasList || hasPre) {
return Array.from(node.children)
.map((child) => convertBlockNode(child as HTMLElement, indentLevel))
.filter(Boolean)
.join("\n\n")
.join('\n\n');
}
const text = convertInlineNodes(Array.from(node.childNodes)).trim()
return text
}
return convertInlineNodes(Array.from(node.childNodes)).trim()
const text = convertInlineNodes(Array.from(node.childNodes)).trim();
return text;
}
return convertInlineNodes(Array.from(node.childNodes)).trim();
};
const buildMarkdown = (): string => {
const contentRoot = contentRef.current
if (!contentRoot) return ""
const lines: string[] = []
lines.push(`# ${title}`)
lines.push("")
lines.push(`最后更新:${lastUpdated}`)
const contentRoot = contentRef.current;
if (!contentRoot) return '';
const lines: string[] = [];
lines.push(`# ${title}`);
lines.push('');
lines.push(`最后更新:${lastUpdated}`);
if (description) {
const descriptionElement = contentRoot.querySelector("[data-docs-description]")
const descriptionElement = contentRoot.querySelector(
'[data-docs-description]',
);
if (descriptionElement) {
const descriptionText = convertInlineNodes(Array.from(descriptionElement.childNodes)).trim()
const descriptionText = convertInlineNodes(
Array.from(descriptionElement.childNodes),
).trim();
if (descriptionText) {
lines.push("")
lines.push(descriptionText)
lines.push('');
lines.push(descriptionText);
}
}
}
lines.push("")
lines.push('');
const sectionNodes = Array.from(contentRoot.querySelectorAll("[data-docs-section]")) as HTMLElement[]
const sectionNodes = Array.from(
contentRoot.querySelectorAll('[data-docs-section]'),
) as HTMLElement[];
sectionNodes.forEach((section) => {
const heading = section.querySelector("h2")
const heading = section.querySelector('h2');
if (heading?.textContent) {
lines.push(`## ${heading.textContent.trim()}`)
lines.push("")
lines.push(`## ${heading.textContent.trim()}`);
lines.push('');
}
const prose = section.querySelector("[data-docs-content]")
const prose = section.querySelector('[data-docs-content]');
if (prose) {
const blocks = Array.from(prose.children)
.map((child) => convertBlockNode(child as HTMLElement, 0))
.filter(Boolean)
.filter(Boolean);
if (blocks.length) {
lines.push(blocks.join("\n\n"))
lines.push("")
lines.push(blocks.join('\n\n'));
lines.push('');
}
}
})
});
return lines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n"
}
return (
lines
.join('\n')
.replace(/\n{3,}/g, '\n\n')
.trim() + '\n'
);
};
const handleCopyMarkdown = async () => {
const markdown = buildMarkdown()
if (!markdown) return
const markdown = buildMarkdown();
if (!markdown) return;
try {
await navigator.clipboard.writeText(markdown)
setHasCopied(true)
setTimeout(() => setHasCopied(false), 2000)
await navigator.clipboard.writeText(markdown);
setHasCopied(true);
setTimeout(() => setHasCopied(false), 2000);
} catch {
const textArea = document.createElement("textarea")
textArea.value = markdown
textArea.style.position = "fixed"
textArea.style.opacity = "0"
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
document.execCommand("copy")
document.body.removeChild(textArea)
setHasCopied(true)
setTimeout(() => setHasCopied(false), 2000)
}
const textArea = document.createElement('textarea');
textArea.value = markdown;
textArea.style.position = 'fixed';
textArea.style.opacity = '0';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
setHasCopied(true);
setTimeout(() => setHasCopied(false), 2000);
}
};
const scrollToSection = (e: React.MouseEvent<HTMLAnchorElement>, id: string) => {
e.preventDefault()
const element = document.getElementById(id)
const scrollToSection = (
e: React.MouseEvent<HTMLAnchorElement>,
id: string,
) => {
e.preventDefault();
const element = document.getElementById(id);
if (element) {
const offset = 80
const bodyRect = document.body.getBoundingClientRect().top
const elementRect = element.getBoundingClientRect().top
const elementPosition = elementRect - bodyRect
const offsetPosition = elementPosition - offset
const offset = 80;
const bodyRect = document.body.getBoundingClientRect().top;
const elementRect = element.getBoundingClientRect().top;
const elementPosition = elementRect - bodyRect;
const offsetPosition = elementPosition - offset;
window.scrollTo({
top: offsetPosition,
behavior: "smooth"
})
}
behavior: 'smooth',
});
}
};
return (
<div className="min-h-screen bg-background relative selection:bg-primary/20">
<div className='min-h-screen bg-background relative selection:bg-primary/20'>
<motion.div
className="fixed top-0 left-0 right-0 h-1 bg-primary origin-left z-50"
className='fixed top-0 left-0 right-0 h-1 bg-primary origin-left z-50'
style={{ scaleX }}
/>
<div className="fixed inset-0 pointer-events-none">
<div className="absolute top-0 left-1/4 w-[500px] h-[500px] bg-primary/5 rounded-full blur-[120px] -translate-y-1/2" />
<div className="absolute bottom-0 right-1/4 w-[500px] h-[500px] bg-blue-500/5 rounded-full blur-[120px] translate-y-1/2" />
<div className='fixed inset-0 pointer-events-none'>
<div className='absolute top-0 left-1/4 w-[500px] h-[500px] bg-primary/5 rounded-full blur-[120px] -translate-y-1/2' />
<div className='absolute bottom-0 right-1/4 w-[500px] h-[500px] bg-blue-500/5 rounded-full blur-[120px] translate-y-1/2' />
</div>
<div ref={contentRef} className="container relative z-10 max-w-7xl mx-auto px-4 md:px-6 py-8 lg:py-16">
<header className="mb-8 md:mb-12">
<Link href="/" className="inline-block mb-8">
<Button
variant="ghost"
className="gap-2 pl-0 text-muted-foreground hover:text-foreground hover:bg-transparent -ml-2"
<div
ref={contentRef}
className='container relative z-10 max-w-7xl mx-auto px-4 md:px-6 py-8 lg:py-16'
>
<ArrowLeft className="w-4 h-4" />
<header className='mb-8 md:mb-12'>
<Link href='/' className='inline-block mb-8'>
<Button
variant='ghost'
className='gap-2 pl-0 text-muted-foreground hover:text-foreground hover:bg-transparent -ml-2'
>
<ArrowLeft className='w-4 h-4' />
返回首页
</Button>
</Link>
@@ -269,32 +312,39 @@ export function LegalPageLayout({
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
>
<h1 className="text-2xl md:text-3xl lg:text-5xl font-bold tracking-tight text-foreground mb-6">
<h1 className='text-2xl md:text-3xl lg:text-5xl font-bold tracking-tight text-foreground mb-6'>
{title}
</h1>
<div className="flex flex-wrap items-center gap-4 text-xs text-muted-foreground mb-8">
<span className="flex items-center gap-1.5 bg-muted/50 px-3 py-1 rounded-full border border-border/50">
<FileText className="w-3.5 h-3.5" />
<div className='flex flex-wrap items-center gap-4 text-xs text-muted-foreground mb-8'>
<span className='flex items-center gap-1.5 bg-muted/50 px-3 py-1 rounded-full border border-border/50'>
<FileText className='w-3.5 h-3.5' />
{sections.length} 个内容
</span>
<span className="flex items-center gap-1.5 bg-muted/50 px-3 py-1 rounded-full border border-border/50">
<Calendar className="w-3.5 h-3.5" />
<span className='flex items-center gap-1.5 bg-muted/50 px-3 py-1 rounded-full border border-border/50'>
<Calendar className='w-3.5 h-3.5' />
最后更新:{lastUpdated}
</span>
<Button
type="button"
variant="outline"
size="sm"
type='button'
variant='outline'
size='sm'
onClick={handleCopyMarkdown}
className="h-8 gap-2 text-xs"
className='h-8 gap-2 text-xs'
>
{hasCopied ? <Check className="w-3.5 h-3.5" /> : <Copy className="w-3.5 h-3.5" />}
{hasCopied ? "已复制" : "复制 Markdown"}
{hasCopied ? (
<Check className='w-3.5 h-3.5' />
) : (
<Copy className='w-3.5 h-3.5' />
)}
{hasCopied ? '已复制' : '复制 Markdown'}
</Button>
</div>
{description && (
<div className="w-full p-4 md:p-6 rounded-2xl bg-muted/30 border border-border/50 backdrop-blur-sm overflow-hidden">
<div data-docs-description className="text-sm md:text-base text-muted-foreground leading-relaxed m-0 break-all">
<div className='w-full p-4 md:p-6 rounded-2xl bg-muted/30 border border-border/50 backdrop-blur-sm overflow-hidden'>
<div
data-docs-description
className='text-sm md:text-base text-muted-foreground leading-relaxed m-0 break-all'
>
{description}
</div>
</div>
@@ -302,44 +352,46 @@ export function LegalPageLayout({
</motion.div>
</header>
<div className="grid lg:grid-cols-12 gap-8 lg:gap-12 relative">
<aside className="hidden lg:block lg:col-span-3">
<div className="sticky top-24">
<div className="space-y-2 max-h-[calc(100vh-8rem)] overflow-y-auto pr-4 scrollbar-thin">
<div className="text-sm font-semibold text-foreground mb-4 pl-3">目录</div>
<nav className="space-y-1">
<div className='grid lg:grid-cols-12 gap-8 lg:gap-12 relative'>
<aside className='hidden lg:block lg:col-span-3'>
<div className='sticky top-24'>
<div className='space-y-2 max-h-[calc(100vh-8rem)] overflow-y-auto pr-4 scrollbar-thin'>
<div className='text-sm font-semibold text-foreground mb-4 pl-3'>
目录
</div>
<nav className='space-y-1'>
{sections.map((section) => (
<div key={section.value} className="space-y-1">
<div key={section.value} className='space-y-1'>
<a
href={`#${section.value}`}
onClick={(e) => scrollToSection(e, section.value)}
className={cn(
"group flex items-center justify-between py-2 px-3 text-sm rounded-lg transition-all duration-200",
'group flex items-center justify-between py-2 px-3 text-sm rounded-lg transition-all duration-200',
activeSection === section.value
? "bg-primary/10 text-primary font-medium"
: "text-muted-foreground hover:text-foreground hover:bg-muted/50"
? 'bg-primary/10 text-primary font-medium'
: 'text-muted-foreground hover:text-foreground hover:bg-muted/50',
)}
>
<span className="truncate">{section.title}</span>
<span className='truncate'>{section.title}</span>
{activeSection === section.value && (
<motion.div
layoutId="active-dot"
className="w-1.5 h-1.5 rounded-full bg-primary"
layoutId='active-dot'
className='w-1.5 h-1.5 rounded-full bg-primary'
/>
)}
</a>
{section.children && (
<div className="pl-4 space-y-1">
<div className='pl-4 space-y-1'>
{section.children.map((child) => (
<a
key={child.value}
href={`#${child.value}`}
onClick={(e) => scrollToSection(e, child.value)}
className={cn(
"block py-1.5 px-3 text-xs rounded-lg transition-all duration-200 truncate",
'block py-1.5 px-3 text-xs rounded-lg transition-all duration-200 truncate',
activeSection === child.value
? "text-primary font-medium bg-primary/5"
: "text-muted-foreground/80 hover:text-foreground hover:bg-muted/30"
? 'text-primary font-medium bg-primary/5'
: 'text-muted-foreground/80 hover:text-foreground hover:bg-muted/30',
)}
>
{child.title}
@@ -354,23 +406,23 @@ export function LegalPageLayout({
</div>
</aside>
<main className="lg:col-span-9 lg:col-start-4 space-y-8 md:space-y-16 pb-20">
<main className='lg:col-span-9 lg:col-start-4 space-y-8 md:space-y-16 pb-20'>
{sections.map((section, index) => (
<motion.section
key={section.value}
id={section.value}
initial={{ opacity: 0, y: 20 }}
whileInView={{ opacity: 1, y: 0 }}
viewport={{ once: true, margin: "100px" }}
viewport={{ once: true, margin: '100px' }}
transition={{ duration: 0.3 }}
className="scroll-mt-24 group"
className='scroll-mt-24 group'
data-docs-section
>
<div className="sticky top-0 z-20 flex items-center gap-4 py-6 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 mb-2 -mx-4 px-4 md:-mx-0 md:px-0">
<span className="flex items-center justify-center w-8 h-8 rounded-lg bg-primary/10 text-primary text-sm font-bold opacity-50 group-hover:opacity-100 transition-opacity flex-shrink-0">
<div className='sticky top-0 z-20 flex items-center gap-4 py-6 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 mb-2 -mx-4 px-4 md:-mx-0 md:px-0'>
<span className='flex items-center justify-center w-8 h-8 rounded-lg bg-primary/10 text-primary text-sm font-bold opacity-50 group-hover:opacity-100 transition-opacity flex-shrink-0'>
{index + 1}
</span>
<h2 className="text-xl md:text-2xl font-semibold tracking-tight text-foreground group-hover:text-primary transition-colors duration-300">
<h2 className='text-xl md:text-2xl font-semibold tracking-tight text-foreground group-hover:text-primary transition-colors duration-300'>
{section.title.replace(/^\d+\.\s*/, '')}
</h2>
</div>
@@ -378,25 +430,29 @@ export function LegalPageLayout({
<div
data-docs-content
className={cn(
"prose prose-sm md:prose-base prose-neutral dark:prose-invert max-w-none",
"prose-p:text-muted-foreground prose-p:leading-7",
"prose-li:text-muted-foreground",
"prose-strong:text-foreground",
"pl-4 md:pl-12 border-l border-border/30 group-hover:border-primary/20 transition-colors duration-500"
)}>
'prose prose-sm md:prose-base prose-neutral dark:prose-invert max-w-none',
'prose-p:text-muted-foreground prose-p:leading-7',
'prose-li:text-muted-foreground',
'prose-strong:text-foreground',
'pl-4 md:pl-12 border-l border-border/30 group-hover:border-primary/20 transition-colors duration-500',
)}
>
{section.content}
</div>
</motion.section>
))}
<div className="pt-16 border-t border-border mt-20">
<div className="bg-muted/30 rounded-2xl p-8 text-center">
<p className="text-muted-foreground text-sm mb-4">
<div className='pt-16 border-t border-border mt-20'>
<div className='bg-muted/30 rounded-2xl p-8 text-center'>
<p className='text-muted-foreground text-sm mb-4'>
对这些内容有疑问?
</p>
<div className="flex justify-center gap-4">
<Link href="https://github.com/Rain-kl/Wavelet/issues" target="_blank">
<Button variant="outline" className="h-9 text-xs">
<div className='flex justify-center gap-4'>
<Link
href='https://github.com/Rain-kl/Wavelet/issues'
target='_blank'
>
<Button variant='outline' className='h-9 text-xs'>
联系支持
</Button>
</Link>
@@ -407,5 +463,5 @@ export function LegalPageLayout({
</div>
</div>
</div>
)
);
}
+106 -46
View File
@@ -1,4 +1,4 @@
import {type PolicySection} from "./types"
import { type PolicySection } from './types';
/**
* ------------------------------------------------------------------
@@ -7,94 +7,154 @@ import {type PolicySection} from "./types"
*/
export const privacySections: PolicySection[] = [
{
value: "collection-details",
title: "1. 详细信息收集说明",
value: 'collection-details',
title: '1. 详细信息收集说明',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p>我们仅遵循合法、正当、必要的原则,收集为您提供服务所必需的信息。我们的数据收集范围严格限制如下:</p>
<div className="space-y-3">
<div className='space-y-4 text-sm leading-relaxed'>
<p>
我们仅遵循合法、正当、必要的原则,收集为您提供服务所必需的信息。我们的数据收集范围严格限制如下:
</p>
<div className='space-y-3'>
<div>
<span className="font-semibold text-foreground">1.1 身份鉴权信息:</span>
<p className="mt-1 text-muted-foreground">当您通过本地账号注册或绑定第三方 OIDC 认证源登录时,我们会收集您的用户名、关联的邮箱、加密后的密码哈希指纹及关联的第三方 OpenID 标识符。<strong>我们不强制要求绑定手机号、身份证件或任何真实社会信用实体信息。</strong></p>
<span className='font-semibold text-foreground'>
1.1 身份鉴权信息:
</span>
<p className='mt-1 text-muted-foreground'>
当您通过本地账号注册或绑定第三方 OIDC
认证源登录时,我们会收集您的用户名、关联的邮箱、加密后的密码哈希指纹及关联的第三方
OpenID 标识符。
<strong>
我们不强制要求绑定手机号、身份证件或任何真实社会信用实体信息。
</strong>
</p>
</div>
<div>
<span className="font-semibold text-foreground">1.2 服务与接口日志信息:</span>
<p className="mt-1 text-muted-foreground">为保障系统运行安全及满足安全审计要求,我们会自动收集您的操作日志,包括 IP 地址、访问日期和时间、个人访问令牌 API 调用历史记录、User-Agent(浏览器/设备/请求工具类型)。</p>
<span className='font-semibold text-foreground'>
1.2 服务与接口日志信息:
</span>
<p className='mt-1 text-muted-foreground'>
为保障系统运行安全及满足安全审计要求,我们会自动收集您的操作日志,包括
IP 地址、访问日期和时间、个人访问令牌 API
调用历史记录、User-Agent(浏览器/设备/请求工具类型)。
</p>
</div>
</div>
</div>
),
},
{
value: "storage-security",
title: "2. 数据存储与安全保护",
value: 'storage-security',
title: '2. 数据存储与安全保护',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className='space-y-4 text-sm leading-relaxed'>
<p>我们深知数据安全的重要性,并采取业界领先的技术措施保护您的数据:</p>
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>存储安全:</strong>用户数据独立存储于专用的云数据库或本地容器化持久层中,仅供授权系统应用挂载读取。</li>
<li><strong>加密技术:</strong>敏感的密码指纹和个人访问令牌哈希在数据库中均采用高强度算法加密存储。API 数据传输链路强制使用 SSL/TLS 进行安全加密。</li>
<li><strong>访问控制:</strong>我们实行严格的最小权限原则(Least Privilege),平台数据不会对外共享,所有系统级内部运维操作均有审计日志可查。</li>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>存储安全:</strong>
用户数据独立存储于专用的云数据库或本地容器化持久层中,仅供授权系统应用挂载读取。
</li>
<li>
<strong>加密技术:</strong>
敏感的密码指纹和个人访问令牌哈希在数据库中均采用高强度算法加密存储。API
数据传输链路强制使用 SSL/TLS 进行安全加密。
</li>
<li>
<strong>访问控制:</strong>我们实行严格的最小权限原则(Least
Privilege),平台数据不会对外共享,所有系统级内部运维操作均有审计日志可查。
</li>
</ul>
</div>
),
},
{
value: "usage-rules",
title: "3. 信息使用规范",
value: 'usage-rules',
title: '3. 信息使用规范',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className='space-y-4 text-sm leading-relaxed'>
<p>我们收集的信息将仅用于以下目的:</p>
<ul className="list-disc pl-4 md:pl-5 space-y-1">
<li><strong>身份识别:</strong>用于确认您的注册身份,展示您的个人中心及设置页面数据。</li>
<li><strong>业务功能:</strong>用于识别并处理您的个人访问令牌 API 鉴权指令。</li>
<li><strong>安全风控:</strong>利用 IP 及行为日志进行接口反作弊、反暴力破解分析,保障后台服务稳定性。</li>
<ul className='list-disc pl-4 md:pl-5 space-y-1'>
<li>
<strong>身份识别:</strong>
用于确认您的注册身份,展示您的个人中心及设置页面数据。
</li>
<li>
<strong>业务功能:</strong>用于识别并处理您的个人访问令牌 API
鉴权指令。
</li>
<li>
<strong>安全风控:</strong>利用 IP
及行为日志进行接口反作弊、反暴力破解分析,保障后台服务稳定性。
</li>
</ul>
<p><strong>禁止用途:</strong>我们承诺<strong>绝不</strong>将您的数据出售给第三方,亦不会向任何机构提供任何用户画像分析或广告推送服务。</p>
<p>
<strong>禁止用途:</strong>我们承诺<strong>绝不</strong>
将您的数据出售给第三方,亦不会向任何机构提供任何用户画像分析或广告推送服务。
</p>
</div>
),
},
{
value: "sharing-disclosure",
title: "4. 信息共享与对外披露",
value: 'sharing-disclosure',
title: '4. 信息共享与对外披露',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p><strong>4.1 共享原则:</strong>除以下极端情况外,我们不会向任何第三方(包括且不限于关联公司、商业合作伙伴)共享您的个人信息:</p>
<ul className="list-disc pl-4 md:pl-5 space-y-1 text-muted-foreground">
<div className='space-y-4 text-sm leading-relaxed'>
<p>
<strong>4.1 共享原则:</strong>
除以下极端情况外,我们不会向任何第三方(包括且不限于关联公司、商业合作伙伴)共享您的个人信息:
</p>
<ul className='list-disc pl-4 md:pl-5 space-y-1 text-muted-foreground'>
<li>事先获得您的明确授权或同意;</li>
<li>根据适用的法律法规、法律程序的要求、强制性的行政或司法要求所必须的情况下进行提供。</li>
<li>
根据适用的法律法规、法律程序的要求、强制性的行政或司法要求所必须的情况下进行提供。
</li>
</ul>
<p><strong>4.2 转让与公开披露:</strong>我们不会将您的个人信息转让给任何公司、组织和个人,亦不进行任何公开商业披露。</p>
<p>
<strong>4.2 转让与公开披露:</strong>
我们不会将您的个人信息转让给任何公司、组织和个人,亦不进行任何公开商业披露。
</p>
</div>
),
},
{
value: "user-rights",
title: "5. 您的权利与数据管理",
value: 'user-rights',
title: '5. 您的权利与数据管理',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p>依照《中华人民共和国个人信息保护法》,您对您的个人信息享有完整的控制权:</p>
<div className="space-y-3">
<div className='space-y-4 text-sm leading-relaxed'>
<p>
依照《中华人民共和国个人信息保护法》,您对您的个人信息享有完整的控制权:
</p>
<div className='space-y-3'>
<div>
<span className="font-semibold text-foreground">5.1 查阅与管理权:</span>
<p className="mt-1 text-muted-foreground">您可以随时登录本平台,查阅您的基础个人信息,生成、轮换或撤销您的 AccessToken (API 密钥)。</p>
<span className='font-semibold text-foreground'>
5.1 查阅与管理权:
</span>
<p className='mt-1 text-muted-foreground'>
您可以随时登录本平台,查阅您的基础个人信息,生成、轮换或撤销您的
AccessToken (API 密钥)。
</p>
</div>
<div>
<span className="font-semibold text-foreground">5.2 删除与注销权:</span>
<p className="mt-1 text-muted-foreground">若您决定停止使用本服务,您可以申请注销账户。注销后,我们将立即从活跃存储媒介中删除您的所有敏感信息,法律法规要求留存的安全日志除外。</p>
<span className='font-semibold text-foreground'>
5.2 删除与注销权:
</span>
<p className='mt-1 text-muted-foreground'>
若您决定停止使用本服务,您可以申请注销账户。注销后,我们将立即从活跃存储媒介中删除您的所有敏感信息,法律法规要求留存的安全日志除外。
</p>
</div>
</div>
</div>
),
},
{
value: "policy-update",
title: "6. 政策更新与通知",
value: 'policy-update',
title: '6. 政策更新与通知',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<div className='space-y-4 text-sm leading-relaxed'>
<p>随着业务的发展或法律法规的变动,本《隐私政策》条款可能发生变更。</p>
<p>当条款发生重大变更时,我们会以显著方式(如平台公告、站内弹窗等)予以通知。如果您继续使用本服务,即表示您同意接受修订后的政策约束。</p>
<p>
当条款发生重大变更时,我们会以显著方式(如平台公告、站内弹窗等)予以通知。如果您继续使用本服务,即表示您同意接受修订后的政策约束。
</p>
</div>
),
},
]
];
+112 -45
View File
@@ -1,6 +1,6 @@
import {type PolicySection} from "./types"
import { type PolicySection } from './types';
export const TERMS_LAST_UPDATED = "2026-06-07"
export const TERMS_LAST_UPDATED = '2026-06-07';
/**
* ------------------------------------------------------------------
@@ -9,68 +9,128 @@ export const TERMS_LAST_UPDATED = "2026-06-07"
*/
export const termsSections: PolicySection[] = [
{
value: "contract-establishment",
title: "1. 缔约申明与服务综述",
value: 'contract-establishment',
title: '1. 缔约申明与服务综述',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p><strong>1.1 缔约主体:</strong>本《服务协议》(以下简称“本协议”)是您(以下称“用户”或“开发者”)与本通用开发脚手架平台(以下简称“本系统”或“平台”)运营维护方之间关于使用本系统所订立的契约。</p>
<p><strong>1.2 审慎阅读:</strong>本系统作为一个通用的、面向二次开发的全栈软件底座,旨在为用户提供基础的注册、会话、OIDC 接入、API Key 令牌鉴权及后台管理服务。若您使用本系统,请务必仔细阅读本协议各条款。</p>
<p><strong>1.3 协议构成:</strong>本协议内容包括协议正文及所有我们已经发布或将来可能发布的各类规则、声明、说明。所有规则为本协议不可分割的组成部分,与协议正文具有同等法律效力。</p>
<div className='space-y-4 text-sm leading-relaxed'>
<p>
<strong>1.1 缔约主体:</strong>
本《服务协议》(以下简称“本协议”)是您(以下称“用户”或“开发者”)与本通用开发脚手架平台(以下简称“本系统”或“平台”)运营维护方之间关于使用本系统所订立的契约。
</p>
<p>
<strong>1.2 审慎阅读:</strong>
本系统作为一个通用的、面向二次开发的全栈软件底座,旨在为用户提供基础的注册、会话、OIDC
接入、API Key
令牌鉴权及后台管理服务。若您使用本系统,请务必仔细阅读本协议各条款。
</p>
<p>
<strong>1.3 协议构成:</strong>
本协议内容包括协议正文及所有我们已经发布或将来可能发布的各类规则、声明、说明。所有规则为本协议不可分割的组成部分,与协议正文具有同等法律效力。
</p>
</div>
),
},
{
value: "service-definition",
title: "2. 服务定义与性质界定",
value: 'service-definition',
title: '2. 服务定义与性质界定',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>责任自担:</strong>关于用户对本系统进行二次开发并应用于其他生产环境产生的任何业务行为,由二开部署运营主体承担全部合规责任。</li>
<div className='space-y-4 text-sm leading-relaxed'>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>责任自担:</strong>
关于用户对本系统进行二次开发并应用于其他生产环境产生的任何业务行为,由二开部署运营主体承担全部合规责任。
</li>
</ul>
</div>
),
},
{
value: "account-specifications",
title: "3. 账号注册与使用规范",
value: 'account-specifications',
title: '3. 账号注册与使用规范',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p><strong>3.1 账号体系:</strong>用户可通过本系统的前端注册表单自助创建账户,或通过系统管理员配置并启用的自定义第三方 OIDC 认证源进行登录关联。</p>
<p><strong>3.2 密码及令牌安全责任:</strong></p>
<ul className="list-disc pl-4 md:pl-5 space-y-2">
<li><strong>密码安全:</strong>您应妥善保管您账户的登录密码。</li>
<li><strong>API Token 安全:</strong>个人生成的 AccessToken (API Key) 代表您账户的完整调用权限。<strong>因您保管不善导致 Token 泄漏而造成的一切数据丢失或系统损失,均由您自行承担。</strong></li>
<div className='space-y-4 text-sm leading-relaxed'>
<p>
<strong>3.1 账号体系:</strong>
用户可通过本系统的前端注册表单自助创建账户,或通过系统管理员配置并启用的自定义第三方
OIDC 认证源进行登录关联。
</p>
<p>
<strong>3.2 密码及令牌安全责任:</strong>
</p>
<ul className='list-disc pl-4 md:pl-5 space-y-2'>
<li>
<strong>密码安全:</strong>您应妥善保管您账户的登录密码。
</li>
<li>
<strong>API Token 安全:</strong>个人生成的 AccessToken (API Key)
代表您账户的完整调用权限。
<strong>
因您保管不善导致 Token
泄漏而造成的一切数据丢失或系统损失,均由您自行承担。
</strong>
</li>
</ul>
</div>
),
},
{
value: "user-conduct",
title: "4. 用户行为准则(负面清单)",
value: 'user-conduct',
title: '4. 用户行为准则(负面清单)',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p>您在使用本系统时,必须严格遵守《中华人民共和国网络安全法》、《计算机信息网络国际联网安全保护管理办法》等法律法规。<strong>严禁利用本平台从事以下活动(“红线条款”):</strong></p>
<div className="bg-red-500/5 border border-red-500/20 rounded-lg p-3 md:p-4 space-y-3">
<ul className="list-disc pl-4 md:pl-5 space-y-2 text-red-600 dark:text-red-400 font-medium">
<li><strong>危害国家安全:</strong>反对宪法所确定的基本原则、危害国家安全、泄露国家秘密、颠覆国家政权、破坏国家统一的;</li>
<li><strong>非法信息服务:</strong>黑客攻击工具、DDoS 攻击服务、服务器爆破等其他非法信息服务平台;</li>
<li><strong>黄赌毒关联:</strong>制作、复制、发布、传播淫秽、色情、赌博、暴力、凶杀、恐怖或者教唆犯罪的;</li>
<li><strong>侵犯知识产权:</strong>销售盗版软件、盗版影视资源、非法游戏外挂、私服、黑号、社工库数据等;</li>
<li><strong>其他违法信息:</strong>涉及散布谣言、宣扬邪教/封建迷信、侮辱/诽谤他人、侵害他人合法权益的。</li>
<div className='space-y-4 text-sm leading-relaxed'>
<p>
您在使用本系统时,必须严格遵守《中华人民共和国网络安全法》、《计算机信息网络国际联网安全保护管理办法》等法律法规。
<strong>严禁利用本平台从事以下活动(“红线条款”):</strong>
</p>
<div className='bg-red-500/5 border border-red-500/20 rounded-lg p-3 md:p-4 space-y-3'>
<ul className='list-disc pl-4 md:pl-5 space-y-2 text-red-600 dark:text-red-400 font-medium'>
<li>
<strong>危害国家安全:</strong>
反对宪法所确定的基本原则、危害国家安全、泄露国家秘密、颠覆国家政权、破坏国家统一的;
</li>
<li>
<strong>非法信息服务:</strong>黑客攻击工具、DDoS
攻击服务、服务器爆破等其他非法信息服务平台;
</li>
<li>
<strong>黄赌毒关联:</strong>
制作、复制、发布、传播淫秽、色情、赌博、暴力、凶杀、恐怖或者教唆犯罪的;
</li>
<li>
<strong>侵犯知识产权:</strong>
销售盗版软件、盗版影视资源、非法游戏外挂、私服、黑号、社工库数据等;
</li>
<li>
<strong>其他违法信息:</strong>
涉及散布谣言、宣扬邪教/封建迷信、侮辱/诽谤他人、侵害他人合法权益的。
</li>
</ul>
</div>
<p><strong>处理规则:</strong>一旦发现您违反上述规定,平台运维主体有权不经通知<strong>立即永久封禁您的账号、拦截所有 AccessToken 请求,并依法向公安机关、网安部门移交相关线索。</strong></p>
<p>
<strong>处理规则:</strong>
一旦发现您违反上述规定,平台运维主体有权不经通知
<strong>
立即永久封禁您的账号、拦截所有 AccessToken
请求,并依法向公安机关、网安部门移交相关线索。
</strong>
</p>
</div>
),
},
{
value: "liability-limitation",
title: "5. 免责声明与不可抗力",
value: 'liability-limitation',
title: '5. 免责声明与不可抗力',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p><strong>5.1 基础免责:</strong>本开发底座按“现状”(As-Is)及“现有”(As-Available)状态提供。我们不保证服务一定能满足您的特定开发需求,对服务的及时性、安全性、准确性都不作额外担保。</p>
<p><strong>5.2 技术服务中断:</strong>对于因以下不可抗力原因导致的服务中断、数据丢失或账号损坏,平台不承担赔偿责任:</p>
<ul className="list-disc pl-4 md:pl-5 space-y-1 text-muted-foreground">
<div className='space-y-4 text-sm leading-relaxed'>
<p>
<strong>5.1 基础免责:</strong>
本开发底座按“现状”(As-Is)及“现有”(As-Available)状态提供。我们不保证服务一定能满足您的特定开发需求,对服务的及时性、安全性、准确性都不作额外担保。
</p>
<p>
<strong>5.2 技术服务中断:</strong>
对于因以下不可抗力原因导致的服务中断、数据丢失或账号损坏,平台不承担赔偿责任:
</p>
<ul className='list-disc pl-4 md:pl-5 space-y-1 text-muted-foreground'>
<li>自然灾害(台风、地震、海啸、洪水等);</li>
<li>政府行为、网络安全法律法规调整或行政命令;</li>
<li>电信部门线路技术故障、机房海底光缆损毁;</li>
@@ -80,13 +140,20 @@ export const termsSections: PolicySection[] = [
),
},
{
value: "governing-law",
title: "6. 法律适用与争议解决",
value: 'governing-law',
title: '6. 法律适用与争议解决',
content: (
<div className="space-y-4 text-sm leading-relaxed">
<p><strong>6.1 法律适用:</strong>本协议的订立、执行、解释及争议的解决均适用<strong>中华人民共和国法律</strong>(不包括港澳台地区冲突法)。</p>
<p><strong>6.2 争议解决:</strong>若发生任何争议或纠纷,首先应友好协商解决;协商不成的,应提交至本系统部署或运营方所在地有管辖权的人民法院管辖。</p>
<div className='space-y-4 text-sm leading-relaxed'>
<p>
<strong>6.1 法律适用:</strong>
本协议的订立、执行、解释及争议的解决均适用
<strong>中华人民共和国法律</strong>(不包括港澳台地区冲突法)。
</p>
<p>
<strong>6.2 争议解决:</strong>
若发生任何争议或纠纷,首先应友好协商解决;协商不成的,应提交至本系统部署或运营方所在地有管辖权的人民法院管辖。
</p>
</div>
),
},
]
];
+8 -8
View File
@@ -1,11 +1,11 @@
import { type ReactNode } from "react"
import { type ReactNode } from 'react';
export type PolicySection = {
value: string
title: string
content: ReactNode
value: string;
title: string;
content: ReactNode;
children?: {
value: string
title: string
}[]
}
value: string;
title: string;
}[];
};
@@ -1,51 +1,56 @@
"use client"
'use client';
import * as React from "react"
import {ImageOff} from "lucide-react"
import * as React from 'react';
import { ImageOff } from 'lucide-react';
import {cn} from "@/lib/utils"
import {getFileUrl, type ImageQuality} from "@/lib/services/upload/utils"
import { cn } from '@/lib/utils';
import { getFileUrl, type ImageQuality } from '@/lib/services/upload/utils';
type FileImagePreviewProps = {
fileId: string | number
alt: string
quality?: ImageQuality
className?: string
fallbackClassName?: string
variant?: "compact" | "default"
}
fileId: string | number;
alt: string;
quality?: ImageQuality;
className?: string;
fallbackClassName?: string;
variant?: 'compact' | 'default';
};
export function FileImagePreview({
fileId,
alt,
quality = "low",
quality = 'low',
className,
fallbackClassName,
variant = "default",
variant = 'default',
}: FileImagePreviewProps) {
const [failed, setFailed] = React.useState(false)
const src = getFileUrl(fileId, quality)
const [failed, setFailed] = React.useState(false);
const src = getFileUrl(fileId, quality);
React.useEffect(() => {
setFailed(false)
}, [fileId, quality])
setFailed(false);
}, [fileId, quality]);
if (!src || failed) {
return (
<div
role="img"
role='img'
aria-label={`${alt} 加载失败`}
className={cn(
"flex size-full flex-col items-center justify-center gap-1 bg-muted/50 text-muted-foreground",
'flex size-full flex-col items-center justify-center gap-1 bg-muted/50 text-muted-foreground',
fallbackClassName,
)}
>
<ImageOff className={cn("shrink-0 opacity-70", variant === "compact" ? "size-3.5" : "size-5")} />
{variant === "default" && (
<span className="text-[10px] leading-none">加载失败</span>
<ImageOff
className={cn(
'shrink-0 opacity-70',
variant === 'compact' ? 'size-3.5' : 'size-5',
)}
/>
{variant === 'default' && (
<span className='text-[10px] leading-none'>加载失败</span>
)}
</div>
)
);
}
return (
@@ -53,10 +58,10 @@ export function FileImagePreview({
<img
src={src}
alt={alt}
loading="lazy"
decoding="async"
loading='lazy'
decoding='async'
className={className}
onError={() => setFailed(true)}
/>
)
);
}
@@ -1,11 +1,11 @@
import {useState} from "react"
import {toast} from "sonner"
import {Button} from "@/components/ui/button"
import {Spinner} from "@/components/ui/spinner"
import {ErrorInline} from "@/components/layout/error"
import {EmptyStateWithBorder} from "@/components/layout/empty"
import {LoadingStateWithBorder} from "@/components/layout/loading"
import {Layers, ListRestart, LucideIcon, Trash2} from "lucide-react"
import { useState } from 'react';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Spinner } from '@/components/ui/spinner';
import { ErrorInline } from '@/components/layout/error';
import { EmptyStateWithBorder } from '@/components/layout/empty';
import { LoadingStateWithBorder } from '@/components/layout/loading';
import { Layers, ListRestart, LucideIcon, Trash2 } from 'lucide-react';
import {
AlertDialog,
AlertDialogAction,
@@ -15,49 +15,54 @@ import {
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog"
import {TableBody, TableCell, TableHead, TableHeader, TableRow} from "@/components/ui/table"
import {ScrollArea, ScrollBar} from "@/components/ui/scroll-area"
} from '@/components/ui/alert-dialog';
import {
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area';
interface ManagePageProps<T> {
title: string
icon?: LucideIcon
data: T[]
loading: boolean
error: Error | null
onReload: () => void
title: string;
icon?: LucideIcon;
data: T[];
loading: boolean;
error: Error | null;
onReload: () => void;
/** 获取初始编辑数据 */
getInitialEditData: (item: T) => Partial<T>
onSave: (item: T, editData: Partial<T>) => Promise<void>
onDelete?: (item: T) => Promise<void>
getInitialEditData: (item: T) => Partial<T>;
onSave: (item: T, editData: Partial<T>) => Promise<void>;
onDelete?: (item: T) => Promise<void>;
/** 渲染表格 (Config-Driven) */
columns: {
header: string
cell: (item: T) => React.ReactNode
width?: string
align?: 'left' | 'center' | 'right'
className?: string
}[]
header: string;
cell: (item: T) => React.ReactNode;
width?: string;
align?: 'left' | 'center' | 'right';
className?: string;
}[];
/** 渲染详情 */
renderDetail: (props: {
selected: T | null
hovered: T | null
editData: Partial<T>
onEditDataChange: (field: keyof T, value: T[keyof T]) => void
onSave: () => void
saving: boolean
}) => React.ReactNode
selected: T | null;
hovered: T | null;
editData: Partial<T>;
onEditDataChange: (field: keyof T, value: T[keyof T]) => void;
onSave: () => void;
saving: boolean;
}) => React.ReactNode;
/** 空状态图标 */
emptyIcon?: LucideIcon
emptyDescription?: string
loadingDescription?: string
getId: (item: T) => string | number
headerExtra?: React.ReactNode
emptyIcon?: LucideIcon;
emptyDescription?: string;
loadingDescription?: string;
getId: (item: T) => string | number;
headerExtra?: React.ReactNode;
}
export function ManagePage<T>({
@@ -73,85 +78,85 @@ export function ManagePage<T>({
columns,
renderDetail,
emptyIcon = Layers,
emptyDescription = "暂无数据",
loadingDescription = "加载中",
emptyDescription = '暂无数据',
loadingDescription = '加载中',
getId,
headerExtra
headerExtra,
}: ManagePageProps<T>) {
/** 悬停状态 */
const [hoveredItem, setHoveredItem] = useState<T | null>(null)
const [selectedItem, setSelectedItem] = useState<T | null>(null)
const [editData, setEditData] = useState<Partial<T>>({})
const [saving, setSaving] = useState(false)
const [deletingItem, setDeletingItem] = useState<T | null>(null)
const [hoveredItem, setHoveredItem] = useState<T | null>(null);
const [selectedItem, setSelectedItem] = useState<T | null>(null);
const [editData, setEditData] = useState<Partial<T>>({});
const [saving, setSaving] = useState(false);
const [deletingItem, setDeletingItem] = useState<T | null>(null);
/** 悬停处理 */
const handleHover = (item: T | null) => {
setHoveredItem(item)
}
setHoveredItem(item);
};
/** 选择处理 */
const handleSelect = (item: T) => {
const itemId = getId(item)
const selectedId = selectedItem ? getId(selectedItem) : null
const itemId = getId(item);
const selectedId = selectedItem ? getId(selectedItem) : null;
if (itemId === selectedId) {
setSelectedItem(null)
setEditData({})
setSelectedItem(null);
setEditData({});
} else {
setSelectedItem(item)
setEditData(getInitialEditData(item))
}
setHoveredItem(null)
setSelectedItem(item);
setEditData(getInitialEditData(item));
}
setHoveredItem(null);
};
/** 编辑数据处理 */
const handleEditDataChange = (field: keyof T, value: T[keyof T]) => {
setEditData(prev => ({
setEditData((prev) => ({
...prev,
[field]: value
}))
}
[field]: value,
}));
};
/** 保存处理 */
const handleSave = async () => {
if (!selectedItem) return
if (!selectedItem) return;
try {
setSaving(true)
await onSave(selectedItem, editData)
toast.success('保存成功')
setSaving(true);
await onSave(selectedItem, editData);
toast.success('保存成功');
} catch (error) {
toast.error('保存失败', {
description: error instanceof Error ? error.message : '未知错误'
})
description: error instanceof Error ? error.message : '未知错误',
});
} finally {
setSaving(false)
}
setSaving(false);
}
};
/** 删除处理 */
const handleDeleteClick = (item: T) => {
setDeletingItem(item)
}
setDeletingItem(item);
};
const handleConfirmDelete = async () => {
if (!deletingItem || !onDelete) return
if (!deletingItem || !onDelete) return;
try {
await onDelete(deletingItem)
toast.success('删除成功')
setDeletingItem(null)
await onDelete(deletingItem);
toast.success('删除成功');
setDeletingItem(null);
// 如果删除的是当前选中的项,清除选中状态
if (selectedItem && getId(selectedItem) === getId(deletingItem)) {
setSelectedItem(null)
setEditData({})
setSelectedItem(null);
setEditData({});
}
} catch (error) {
toast.error('删除失败', {
description: error instanceof Error ? error.message : '未知错误'
})
}
description: error instanceof Error ? error.message : '未知错误',
});
}
};
/** 渲染内容 */
const renderContent = () => {
@@ -161,28 +166,25 @@ export function ManagePage<T>({
icon={ListRestart}
description={loadingDescription}
/>
)
);
}
if (error) {
return (
<div className="p-8 border border-dashed rounded-lg">
<div className='p-8 border border-dashed rounded-lg'>
<ErrorInline
error={error}
onRetry={onReload}
className="justify-center"
className='justify-center'
/>
</div>
)
);
}
if (!data || data.length === 0) {
return (
<EmptyStateWithBorder
icon={emptyIcon}
description={emptyDescription}
/>
)
<EmptyStateWithBorder icon={emptyIcon} description={emptyDescription} />
);
}
return (
@@ -196,27 +198,25 @@ export function ManagePage<T>({
onDelete={onDelete ? handleDeleteClick : undefined}
getId={getId}
/>
)
}
);
};
return (
<div className="py-6">
<div className="flex pb-2 mb-6 items-center justify-between">
<div className="flex items-center gap-2">
{Icon && <Icon className="size-5 text-primary" />}
<div className='py-6'>
<div className='flex pb-2 mb-6 items-center justify-between'>
<div className='flex items-center gap-2'>
{Icon && <Icon className='size-5 text-primary' />}
<div>
<h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
<h1 className='text-2xl font-semibold tracking-tight'>{title}</h1>
</div>
</div>
{headerExtra}
</div>
<div className="space-y-6">
<div className='space-y-6'>
<div>
<div className="mb-4">
<div className="font-semibold">配置列表</div>
<div className='mb-4'>
<div className='font-semibold'>配置列表</div>
</div>
{renderContent()}
</div>
@@ -228,12 +228,15 @@ export function ManagePage<T>({
editData,
onEditDataChange: handleEditDataChange,
onSave: handleSave,
saving
saving,
})}
</div>
</div>
<AlertDialog open={!!deletingItem} onOpenChange={(open) => !open && setDeletingItem(null)}>
<AlertDialog
open={!!deletingItem}
onOpenChange={(open) => !open && setDeletingItem(null)}
>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>确认删除?</AlertDialogTitle>
@@ -243,67 +246,73 @@ export function ManagePage<T>({
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>取消</AlertDialogCancel>
<AlertDialogAction onClick={handleConfirmDelete} className="bg-red-600 hover:bg-red-700">
<AlertDialogAction
onClick={handleConfirmDelete}
className='bg-red-600 hover:bg-red-700'
>
确认删除
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
)
);
}
/** 详情面板 props */
interface ManageDetailPanelProps {
title?: string
isEmpty: boolean
emptyDescription?: string
onSave?: () => void
saving?: boolean
children: React.ReactNode
title?: string;
isEmpty: boolean;
emptyDescription?: string;
onSave?: () => void;
saving?: boolean;
children: React.ReactNode;
}
/** 详情面板 */
export function ManageDetailPanel({
title = "配置信息",
title = '配置信息',
isEmpty,
emptyDescription = "请选择配置查看详情",
emptyDescription = '请选择配置查看详情',
onSave,
saving = false,
children
children,
}: ManageDetailPanelProps) {
if (isEmpty) {
return (
<div className="space-y-4">
<div className="font-semibold mb-4">{title}</div>
<EmptyStateWithBorder
icon={Layers}
description={emptyDescription}
/>
<div className='space-y-4'>
<div className='font-semibold mb-4'>{title}</div>
<EmptyStateWithBorder icon={Layers} description={emptyDescription} />
</div>
)
);
}
return (
<div className="space-y-4 sticky top-6">
<div className='space-y-4 sticky top-6'>
<div>
<div className="flex items-center justify-between mb-4">
<div className="font-semibold">{title}</div>
<div className='flex items-center justify-between mb-4'>
<div className='font-semibold'>{title}</div>
{onSave && (
<Button
onClick={onSave}
disabled={saving}
size="sm"
className="px-3 h-7 text-xs"
size='sm'
className='px-3 h-7 text-xs'
>
{saving ? (<><Spinner /> 更新中</>) : '更新'}
{saving ? (
<>
<Spinner /> 更新中
</>
) : (
'更新'
)}
</Button>
)}
</div>
{children}
</div>
</div>
)
);
}
/** 表格面板 */
@@ -315,30 +324,30 @@ export function ManageTable<T>({
onSelect,
onHover,
onDelete,
getId
getId,
}: {
data: T[]
data: T[];
columns: {
header: string
cell: (item: T) => React.ReactNode
width?: string
align?: 'left' | 'center' | 'right'
className?: string
}[]
selected: T | null
hovered: T | null
onSelect: (item: T) => void
onHover: (item: T | null) => void
onDelete?: (item: T) => void
getId: (item: T) => string | number
header: string;
cell: (item: T) => React.ReactNode;
width?: string;
align?: 'left' | 'center' | 'right';
className?: string;
}[];
selected: T | null;
hovered: T | null;
onSelect: (item: T) => void;
onHover: (item: T | null) => void;
onDelete?: (item: T) => void;
getId: (item: T) => string | number;
}) {
return (
<div className="border border-dashed shadow-none rounded-lg overflow-hidden">
<ScrollArea className="w-full">
<div className="relative w-full">
<table className="w-full caption-bottom text-sm">
<div className='border border-dashed shadow-none rounded-lg overflow-hidden'>
<ScrollArea className='w-full'>
<div className='relative w-full'>
<table className='w-full caption-bottom text-sm'>
<TableHeader>
<TableRow className="border-b border-dashed">
<TableRow className='border-b border-dashed'>
{columns.map((col, index) => (
<TableHead
key={index}
@@ -347,19 +356,24 @@ export function ManageTable<T>({
{col.header}
</TableHead>
))}
{onDelete && <TableHead className="whitespace-nowrap text-center w-[120px]">操作</TableHead>}
{onDelete && (
<TableHead className='whitespace-nowrap text-center w-[120px]'>
操作
</TableHead>
)}
</TableRow>
</TableHeader>
<TableBody className="animate-in fade-in duration-200">
<TableBody className='animate-in fade-in duration-200'>
{data.map((item) => {
const id = getId(item)
const isSelected = selected && getId(selected) === id
const isHovered = hovered && getId(hovered) === id
const id = getId(item);
const isSelected = selected && getId(selected) === id;
const isHovered = hovered && getId(hovered) === id;
return (
<TableRow
key={id}
className={`border-b border-dashed cursor-pointer transition-colors ${ isSelected
className={`border-b border-dashed cursor-pointer transition-colors ${
isSelected
? 'bg-primary/5 hover:bg-primary/10'
: isHovered
? 'bg-gray-100 hover:bg-muted/50'
@@ -378,28 +392,28 @@ export function ManageTable<T>({
</TableCell>
))}
{onDelete && (
<TableCell className="text-xs py-1 text-center">
<TableCell className='text-xs py-1 text-center'>
<Button
variant="ghost"
size="icon"
className="h-6 w-6 text-muted-foreground hover:text-red-600 hover:bg-red-50"
variant='ghost'
size='icon'
className='h-6 w-6 text-muted-foreground hover:text-red-600 hover:bg-red-50'
onClick={(e) => {
e.stopPropagation()
onDelete(item)
e.stopPropagation();
onDelete(item);
}}
>
<Trash2 className="h-3.5 w-3.5" />
<Trash2 className='h-3.5 w-3.5' />
</Button>
</TableCell>
)}
</TableRow>
)
);
})}
</TableBody>
</table>
</div>
<ScrollBar orientation="horizontal" />
<ScrollBar orientation='horizontal' />
</ScrollArea>
</div>
)
);
}
@@ -1,29 +1,29 @@
"use client"
'use client';
import * as React from "react"
import { useState } from "react"
import * as React from 'react';
import { useState } from 'react';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
} from '@/components/ui/dialog';
import {
InputOTP,
InputOTPGroup,
InputOTPSlot,
} from "@/components/ui/input-otp"
import { Button } from "@/components/ui/button"
import { Spinner } from "@/components/ui/spinner"
} from '@/components/ui/input-otp';
import { Button } from '@/components/ui/button';
import { Spinner } from '@/components/ui/spinner';
interface PasswordDialogProps {
isOpen: boolean
onOpenChange: (open: boolean) => void
onConfirm: (password: string) => void
loading?: boolean
title?: string
description?: string
isOpen: boolean;
onOpenChange: (open: boolean) => void;
onConfirm: (password: string) => void;
loading?: boolean;
title?: string;
description?: string;
}
/**
@@ -36,35 +36,33 @@ export function PasswordDialog({
onOpenChange,
onConfirm,
loading = false,
title = "请输入密码",
description = "为了您的账户安全,请输入6位认证密码进行验证"
title = '请输入密码',
description = '为了您的账户安全,请输入6位认证密码进行验证',
}: PasswordDialogProps) {
const [password, setPassword] = useState("")
const [password, setPassword] = useState('');
const handleConfirm = () => {
if (password.length === 6) {
onConfirm(password)
}
onConfirm(password);
}
};
/* 当对话框关闭时重置密码*/
React.useEffect(() => {
if (!isOpen) {
setPassword("")
setPassword('');
}
}, [isOpen])
}, [isOpen]);
return (
<Dialog open={isOpen} onOpenChange={onOpenChange}>
<DialogContent className="w-xs sm:w-md">
<DialogContent className='w-xs sm:w-md'>
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
<DialogDescription>
{description}
</DialogDescription>
<DialogDescription>{description}</DialogDescription>
</DialogHeader>
<div className="py-2 flex justify-center">
<div className='py-2 flex justify-center'>
<InputOTP
maxLength={6}
value={password}
@@ -72,47 +70,47 @@ export function PasswordDialog({
disabled={loading}
onKeyDown={(e) => {
if (e.key === 'Enter' && password.length === 6 && !loading) {
handleConfirm()
handleConfirm();
}
}}
>
<InputOTPGroup className="gap-2">
<InputOTPSlot index={0} className="w-10 h-10 text-base" />
<InputOTPSlot index={1} className="w-10 h-10 text-base" />
<InputOTPSlot index={2} className="w-10 h-10 text-base" />
<InputOTPSlot index={3} className="w-10 h-10 text-base" />
<InputOTPSlot index={4} className="w-10 h-10 text-base" />
<InputOTPSlot index={5} className="w-10 h-10 text-base" />
<InputOTPGroup className='gap-2'>
<InputOTPSlot index={0} className='w-10 h-10 text-base' />
<InputOTPSlot index={1} className='w-10 h-10 text-base' />
<InputOTPSlot index={2} className='w-10 h-10 text-base' />
<InputOTPSlot index={3} className='w-10 h-10 text-base' />
<InputOTPSlot index={4} className='w-10 h-10 text-base' />
<InputOTPSlot index={5} className='w-10 h-10 text-base' />
</InputOTPGroup>
</InputOTP>
</div>
<div className="flex justify-end gap-2 pt-2">
<div className='flex justify-end gap-2 pt-2'>
<Button
variant="ghost"
variant='ghost'
onClick={() => onOpenChange(false)}
disabled={loading}
className="h-8 text-xs"
className='h-8 text-xs'
>
取消
</Button>
<Button
type="button"
className="bg-primary hover:bg-primary/90 h-8 text-xs"
type='button'
className='bg-primary hover:bg-primary/90 h-8 text-xs'
onClick={handleConfirm}
disabled={password.length !== 6 || loading}
>
{loading ? (
<>
<Spinner className="mr-2 h-4 w-4" />
<Spinner className='mr-2 h-4 w-4' />
验证中...
</>
) : (
"确认"
'确认'
)}
</Button>
</div>
</DialogContent>
</Dialog>
)
);
}
+146 -107
View File
@@ -1,127 +1,140 @@
"use client"
'use client';
import * as React from "react"
import {AnimatePresence, motion} from "motion/react"
import {useUser} from "@/contexts/user-context"
import {Card, CardHeader, CardTitle} from "@/components/ui/card"
import {Button} from "@/components/ui/button"
import {ArrowRight, ExternalLink, FileText, HelpCircle, Layers, Shield, ShieldCheck, Terminal, User} from "lucide-react"
import Link from "next/link"
import * as React from 'react';
import { AnimatePresence, motion } from 'motion/react';
import { useUser } from '@/contexts/user-context';
import { Card, CardHeader, CardTitle } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import {
ArrowRight,
ExternalLink,
FileText,
HelpCircle,
Layers,
Shield,
ShieldCheck,
Terminal,
User,
} from 'lucide-react';
import Link from 'next/link';
export function HomeMain() {
const { user } = useUser()
const [isBannerVisible, setIsBannerVisible] = React.useState(true)
const { user } = useUser();
const [isBannerVisible, setIsBannerVisible] = React.useState(true);
const quickLinks = [
{
title: "个人资料",
description: "管理您的个人账户信息及个性化配置",
title: '个人资料',
description: '管理您的个人账户信息及个性化配置',
icon: User,
url: "/settings/profile",
color: "text-blue-500",
bgColor: "bg-blue-500/10",
borderColor: "hover:border-blue-500/30",
url: '/settings/profile',
color: 'text-blue-500',
bgColor: 'bg-blue-500/10',
borderColor: 'hover:border-blue-500/30',
},
{
title: "开发接口文档",
description: "查看开放平台的 RESTful 接口规格说明",
title: '开发接口文档',
description: '查看开放平台的 RESTful 接口规格说明',
icon: FileText,
url: "/docs/api",
color: "text-emerald-500",
bgColor: "bg-emerald-500/10",
borderColor: "hover:border-emerald-500/30",
url: '/docs/api',
color: 'text-emerald-500',
bgColor: 'bg-emerald-500/10',
borderColor: 'hover:border-emerald-500/30',
external: true,
},
{
title: "使用文档",
description: "学习如何集成 API 及日常操作帮助指南",
title: '使用文档',
description: '学习如何集成 API 及日常操作帮助指南',
icon: HelpCircle,
url: "/docs/how-to-use",
color: "text-purple-500",
bgColor: "bg-purple-500/10",
borderColor: "hover:border-purple-500/30",
url: '/docs/how-to-use',
color: 'text-purple-500',
bgColor: 'bg-purple-500/10',
borderColor: 'hover:border-purple-500/30',
external: true,
},
]
];
const adminLinks = [
{
title: "系统设置",
description: "管理登录注册开关与认证源安全策略",
title: '系统设置',
description: '管理登录注册开关与认证源安全策略',
icon: Shield,
url: "/admin/settings",
color: "text-amber-500",
bgColor: "bg-amber-500/10",
borderColor: "hover:border-amber-500/30",
url: '/admin/settings',
color: 'text-amber-500',
bgColor: 'bg-amber-500/10',
borderColor: 'hover:border-amber-500/30',
},
{
title: "全局系统配置",
description: "动态管理平台运行时核心配置参数",
title: '全局系统配置',
description: '动态管理平台运行时核心配置参数',
icon: ShieldCheck,
url: "/admin/system",
color: "text-indigo-500",
bgColor: "bg-indigo-500/10",
borderColor: "hover:border-indigo-500/30",
url: '/admin/system',
color: 'text-indigo-500',
bgColor: 'bg-indigo-500/10',
borderColor: 'hover:border-indigo-500/30',
},
{
title: "用户权限管理",
description: "集中查询并管理系统注册用户的启用状态",
title: '用户权限管理',
description: '集中查询并管理系统注册用户的启用状态',
icon: User,
url: "/admin/users",
color: "text-rose-500",
bgColor: "bg-rose-500/10",
borderColor: "hover:border-rose-500/30",
url: '/admin/users',
color: 'text-rose-500',
bgColor: 'bg-rose-500/10',
borderColor: 'hover:border-rose-500/30',
},
{
title: "后台任务调度",
description: "分发与观测系统异步定时任务的执行情况",
title: '后台任务调度',
description: '分发与观测系统异步定时任务的执行情况',
icon: Layers,
url: "/admin/tasks",
color: "text-teal-500",
bgColor: "bg-teal-500/10",
borderColor: "hover:border-teal-500/30",
url: '/admin/tasks',
color: 'text-teal-500',
bgColor: 'bg-teal-500/10',
borderColor: 'hover:border-teal-500/30',
},
{
title: "系统日志",
description: "查看异步任务执行日志与系统运行状态详情",
title: '系统日志',
description: '查看异步任务执行日志与系统运行状态详情',
icon: Terminal,
url: "/admin/logs",
color: "text-cyan-500",
bgColor: "bg-cyan-500/10",
borderColor: "hover:border-cyan-500/30",
url: '/admin/logs',
color: 'text-cyan-500',
bgColor: 'bg-cyan-500/10',
borderColor: 'hover:border-cyan-500/30',
},
]
];
return (
<div className="py-6 space-y-8 max-w-6xl mx-auto">
<div className='py-6 space-y-8 max-w-6xl mx-auto'>
{/* 积分收益提示 banner */}
<AnimatePresence>
{isBannerVisible && (
<motion.div
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: "auto" }}
animate={{ opacity: 1, height: 'auto' }}
exit={{ opacity: 0, height: 0 }}
transition={{ duration: 0.3 }}
className="overflow-hidden"
className='overflow-hidden'
>
<div className="bg-gradient-to-r from-indigo-500/10 via-purple-500/10 to-pink-500/10 border border-dashed border-indigo-500/20 rounded-xl p-6 relative">
<div className="max-w-xl space-y-3">
<h3 className="text-lg font-bold text-indigo-600 dark:text-indigo-400">Modern Platform</h3>
<p className="text-xs text-muted-foreground leading-relaxed">
通用的、现代化的后台管理系统脚手架, 开箱即用、完整基建、极易扩展,助您快速构建企业级应用。
<div className='bg-gradient-to-r from-indigo-500/10 via-purple-500/10 to-pink-500/10 border border-dashed border-indigo-500/20 rounded-xl p-6 relative'>
<div className='max-w-xl space-y-3'>
<h3 className='text-lg font-bold text-indigo-600 dark:text-indigo-400'>
Modern Platform
</h3>
<p className='text-xs text-muted-foreground leading-relaxed'>
通用的、现代化的后台管理系统脚手架,
开箱即用、完整基建、极易扩展,助您快速构建企业级应用。
</p>
<div className="flex gap-2.5">
<div className='flex gap-2.5'>
<Button
size="sm"
className="bg-indigo-600 hover:bg-indigo-700 text-xs font-semibold px-4 shadow-sm h-7"
size='sm'
className='bg-indigo-600 hover:bg-indigo-700 text-xs font-semibold px-4 shadow-sm h-7'
asChild
>
<Link href="/home">开始使用</Link>
<Link href='/home'>开始使用</Link>
</Button>
<Button
size="sm"
variant="ghost"
className="text-xs font-medium hover:bg-black/5 dark:hover:bg-white/5 h-7"
size='sm'
variant='ghost'
className='text-xs font-medium hover:bg-black/5 dark:hover:bg-white/5 h-7'
onClick={() => setIsBannerVisible(false)}
>
隐藏提示
@@ -134,9 +147,9 @@ export function HomeMain() {
</AnimatePresence>
{/* 快捷导航 */}
<div className="space-y-4">
<h2 className="text-lg font-semibold tracking-tight">常用功能导航</h2>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<div className='space-y-4'>
<h2 className='text-lg font-semibold tracking-tight'>常用功能导航</h2>
<div className='grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4'>
{quickLinks.map((link, idx) => (
<motion.div
key={link.title}
@@ -144,32 +157,50 @@ export function HomeMain() {
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.4, delay: idx * 0.05 }}
whileHover={{ y: -4 }}
className="h-full"
className='h-full'
>
<Card className={`h-full border border-dashed flex flex-col justify-between hover:bg-muted/40 transition-all ${link.borderColor} duration-300 shadow-none`}>
<CardHeader className="p-5">
<div className={`size-10 rounded-lg flex items-center justify-center ${link.bgColor} ${link.color} mb-3`}>
<link.icon className="size-5" />
<Card
className={`h-full border border-dashed flex flex-col justify-between hover:bg-muted/40 transition-all ${link.borderColor} duration-300 shadow-none`}
>
<CardHeader className='p-5'>
<div
className={`size-10 rounded-lg flex items-center justify-center ${link.bgColor} ${link.color} mb-3`}
>
<link.icon className='size-5' />
</div>
<CardTitle className="text-sm font-semibold mb-1 flex items-center gap-1">
<CardTitle className='text-sm font-semibold mb-1 flex items-center gap-1'>
{link.title}
{link.external && <ExternalLink className="size-3 text-muted-foreground" />}
{link.external && (
<ExternalLink className='size-3 text-muted-foreground' />
)}
</CardTitle>
<p className="text-xs text-muted-foreground leading-normal font-normal">
<p className='text-xs text-muted-foreground leading-normal font-normal'>
{link.description}
</p>
</CardHeader>
<div className="px-5 pb-5">
<div className='px-5 pb-5'>
{link.external ? (
<Button variant="link" className="p-0 h-auto text-xs text-indigo-500 font-medium" asChild>
<Link href={link.url} target="_blank" rel="noopener noreferrer">
立即跳转 <ArrowRight className="size-3 ml-1" />
<Button
variant='link'
className='p-0 h-auto text-xs text-indigo-500 font-medium'
asChild
>
<Link
href={link.url}
target='_blank'
rel='noopener noreferrer'
>
立即跳转 <ArrowRight className='size-3 ml-1' />
</Link>
</Button>
) : (
<Button variant="link" className="p-0 h-auto text-xs text-indigo-500 font-medium" asChild>
<Button
variant='link'
className='p-0 h-auto text-xs text-indigo-500 font-medium'
asChild
>
<Link href={link.url}>
立即进入 <ArrowRight className="size-3 ml-1" />
立即进入 <ArrowRight className='size-3 ml-1' />
</Link>
</Button>
)}
@@ -182,12 +213,12 @@ export function HomeMain() {
{/* 管理面板 (仅管理员可见) */}
{user?.is_admin && (
<div className="space-y-4 pt-2">
<h2 className="text-lg font-semibold tracking-tight text-rose-500 flex items-center gap-1.5">
<Shield className="size-5" />
<div className='space-y-4 pt-2'>
<h2 className='text-lg font-semibold tracking-tight text-rose-500 flex items-center gap-1.5'>
<Shield className='size-5' />
后台管理控制台
</h2>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<div className='grid grid-cols-1 md:grid-cols-3 gap-4'>
{adminLinks.map((link, idx) => (
<motion.div
key={link.title}
@@ -195,24 +226,32 @@ export function HomeMain() {
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.4, delay: (idx + 4) * 0.05 }}
whileHover={{ y: -4 }}
className="h-full"
className='h-full'
>
<Card className={`h-full border border-dashed flex flex-col justify-between hover:bg-muted/40 transition-all ${link.borderColor} duration-300 shadow-none`}>
<CardHeader className="p-5">
<div className={`size-10 rounded-lg flex items-center justify-center ${link.bgColor} ${link.color} mb-3`}>
<link.icon className="size-5" />
<Card
className={`h-full border border-dashed flex flex-col justify-between hover:bg-muted/40 transition-all ${link.borderColor} duration-300 shadow-none`}
>
<CardHeader className='p-5'>
<div
className={`size-10 rounded-lg flex items-center justify-center ${link.bgColor} ${link.color} mb-3`}
>
<link.icon className='size-5' />
</div>
<CardTitle className="text-sm font-semibold mb-1">
<CardTitle className='text-sm font-semibold mb-1'>
{link.title}
</CardTitle>
<p className="text-xs text-muted-foreground leading-normal font-normal">
<p className='text-xs text-muted-foreground leading-normal font-normal'>
{link.description}
</p>
</CardHeader>
<div className="px-5 pb-5">
<Button variant="link" className="p-0 h-auto text-xs text-indigo-500 font-medium" asChild>
<div className='px-5 pb-5'>
<Button
variant='link'
className='p-0 h-auto text-xs text-indigo-500 font-medium'
asChild
>
<Link href={link.url}>
立即进入 <ArrowRight className="size-3 ml-1" />
立即进入 <ArrowRight className='size-3 ml-1' />
</Link>
</Button>
</div>
@@ -223,5 +262,5 @@ export function HomeMain() {
</div>
)}
</div>
)
);
}
@@ -1,17 +1,34 @@
"use client"
'use client';
import * as React from "react"
import Link from "next/link"
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"
import {motion} from "motion/react"
import {AlertTriangle, Check, Copy, Info, Key, Loader2, Plus, RefreshCw, Shield, Trash2} from "lucide-react"
import * as React from 'react';
import Link from 'next/link';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { motion } from 'motion/react';
import {
AlertTriangle,
Check,
Copy,
Info,
Key,
Loader2,
Plus,
RefreshCw,
Shield,
Trash2,
} from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Badge} from "@/components/ui/badge"
import {Switch} from "@/components/ui/switch"
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Badge } from '@/components/ui/badge';
import { Switch } from '@/components/ui/switch';
import {
Dialog,
DialogContent,
@@ -19,7 +36,7 @@ import {
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
} from '@/components/ui/dialog';
import {
Breadcrumb,
BreadcrumbItem,
@@ -27,240 +44,283 @@ import {
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb"
import {UserService} from "@/lib/services/user"
import type {CreateTokenResponse} from "@/lib/services/user"
import {useAuth} from "@/components/providers/auth-provider"
import {toast} from "sonner"
} from '@/components/ui/breadcrumb';
import type { CreateTokenResponse } from '@/lib/services/user';
import { UserService } from '@/lib/services/user';
import { useAuth } from '@/components/providers/auth-provider';
import { toast } from 'sonner';
export function AccessTokenMain() {
const { user } = useAuth()
const queryClient = useQueryClient()
const [createDialogOpen, setCreateDialogOpen] = React.useState(false)
const [viewDialogOpen, setViewDialogOpen] = React.useState(false)
const [tokenName, setTokenName] = React.useState("")
const [tokenIsAdmin, setTokenIsAdmin] = React.useState(false)
const [copiedId, setCopiedId] = React.useState<number | null>(null)
const [newCreatedToken, setNewCreatedToken] = React.useState<CreateTokenResponse | null>(null)
const { user } = useAuth();
const queryClient = useQueryClient();
const [createDialogOpen, setCreateDialogOpen] = React.useState(false);
const [viewDialogOpen, setViewDialogOpen] = React.useState(false);
const [tokenName, setTokenName] = React.useState('');
const [tokenIsAdmin, setTokenIsAdmin] = React.useState(false);
const [copiedId, setCopiedId] = React.useState<number | null>(null);
const [newCreatedToken, setNewCreatedToken] =
React.useState<CreateTokenResponse | null>(null);
// 获取 Token 列表
const accessTokensQuery = useQuery({
queryKey: ["user", "access-tokens"],
queryKey: ['user', 'access-tokens'],
queryFn: () => UserService.getAccessTokens(),
})
});
// 创建 Token
const createTokenMutation = useMutation({
mutationFn: ({ name, isAdmin }: { name: string; isAdmin: boolean }) => UserService.createAccessToken(name, isAdmin),
mutationFn: ({ name, isAdmin }: { name: string; isAdmin: boolean }) =>
UserService.createAccessToken(name, isAdmin),
onSuccess: (data) => {
setNewCreatedToken(data)
setTokenName("")
setTokenIsAdmin(false)
setCreateDialogOpen(false)
setViewDialogOpen(true)
void queryClient.invalidateQueries({ queryKey: ["user", "access-tokens"] })
toast.success("访问令牌创建成功")
setNewCreatedToken(data);
setTokenName('');
setTokenIsAdmin(false);
setCreateDialogOpen(false);
setViewDialogOpen(true);
void queryClient.invalidateQueries({
queryKey: ['user', 'access-tokens'],
});
toast.success('访问令牌创建成功');
},
onError: (error: Error) => {
toast.error(error.message || "创建访问令牌失败")
toast.error(error.message || '创建访问令牌失败');
},
})
});
// 删除 Token
const deleteTokenMutation = useMutation({
mutationFn: (id: number) => UserService.deleteAccessToken(id),
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: ["user", "access-tokens"] })
toast.success("访问令牌已撤销")
void queryClient.invalidateQueries({
queryKey: ['user', 'access-tokens'],
});
toast.success('访问令牌已撤销');
},
onError: (error: Error) => {
toast.error(error.message || "删除访问令牌失败")
toast.error(error.message || '删除访问令牌失败');
},
})
});
// 轮换 Token
const rotateTokenMutation = useMutation({
mutationFn: (id: number) => UserService.rotateAccessToken(id),
onSuccess: (data) => {
setNewCreatedToken(data)
setViewDialogOpen(true)
void queryClient.invalidateQueries({ queryKey: ["user", "access-tokens"] })
toast.success("访问令牌轮换成功,旧密钥已失效")
setNewCreatedToken(data);
setViewDialogOpen(true);
void queryClient.invalidateQueries({
queryKey: ['user', 'access-tokens'],
});
toast.success('访问令牌轮换成功,旧密钥已失效');
},
onError: (error: Error) => {
toast.error(error.message || "轮换访问令牌失败")
toast.error(error.message || '轮换访问令牌失败');
},
})
});
const handleCreateToken = (e: React.FormEvent) => {
e.preventDefault()
e.preventDefault();
if (!tokenName.trim()) {
toast.error("请输入令牌名称")
return
}
createTokenMutation.mutate({ name: tokenName.trim(), isAdmin: tokenIsAdmin })
toast.error('请输入令牌名称');
return;
}
createTokenMutation.mutate({
name: tokenName.trim(),
isAdmin: tokenIsAdmin,
});
};
const handleDeleteToken = (id: number, name: string) => {
if (window.confirm(`确定要删除并撤销令牌「${name}」吗?删除后此令牌将立即失效且不可恢复。`)) {
deleteTokenMutation.mutate(id)
}
if (
window.confirm(
`确定要删除并撤销令牌「${name}」吗?删除后此令牌将立即失效且不可恢复。`,
)
) {
deleteTokenMutation.mutate(id);
}
};
const handleRotateToken = (id: number, name: string) => {
if (window.confirm(`确定要轮换令牌「${name}」的密钥吗?轮换后系统将生成全新密钥,原令牌密钥将立即失效。`)) {
rotateTokenMutation.mutate(id)
}
if (
window.confirm(
`确定要轮换令牌「${name}」的密钥吗?轮换后系统将生成全新密钥,原令牌密钥将立即失效。`,
)
) {
rotateTokenMutation.mutate(id);
}
};
const handleCopyText = async (text: string, id: number) => {
try {
await navigator.clipboard.writeText(text)
setCopiedId(id)
toast.success("复制成功")
setTimeout(() => setCopiedId(null), 2000)
await navigator.clipboard.writeText(text);
setCopiedId(id);
toast.success('复制成功');
setTimeout(() => setCopiedId(null), 2000);
} catch {
toast.error("复制失败")
}
toast.error('复制失败');
}
};
const formatDate = (dateStr?: string) => {
if (!dateStr) return "未使用"
return new Date(dateStr).toLocaleString("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
})
}
if (!dateStr) return '未使用';
return new Date(dateStr).toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
});
};
return (
<motion.div
initial={{ opacity: 0, y: 15 }}
animate={{ opacity: 1, y: 0 }}
transition={{ duration: 0.35, ease: "easeOut" }}
className="py-6 space-y-6 max-w-4xl mx-auto px-4"
transition={{ duration: 0.35, ease: 'easeOut' }}
className='py-6 space-y-6 max-w-4xl mx-auto px-4'
>
<div className="font-semibold">
<div className='font-semibold'>
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink asChild>
<Link href="/settings" className="text-base text-primary">设置</Link>
<Link href='/settings' className='text-base text-primary'>
设置
</Link>
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem>
<BreadcrumbPage className="text-base font-semibold">访问令牌</BreadcrumbPage>
<BreadcrumbPage className='text-base font-semibold'>
访问令牌
</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
</div>
<div className="flex flex-col md:flex-row md:items-center md:justify-between gap-4 border-b pb-5">
<div className="flex items-center gap-4">
<div className='flex flex-col md:flex-row md:items-center md:justify-between gap-4 border-b pb-5'>
<div className='flex items-center gap-4'>
<div>
<h1 className="text-xl font-bold tracking-tight bg-gradient-to-r from-foreground via-foreground/90 to-muted-foreground bg-clip-text text-transparent">个人访问令牌 (AccessToken)</h1>
<p className="text-sm text-muted-foreground">管理您的 API 访问密钥,用于开发或第三方工具直接调用系统 API</p>
<h1 className='text-xl font-bold tracking-tight bg-gradient-to-r from-foreground via-foreground/90 to-muted-foreground bg-clip-text text-transparent'>
个人访问令牌 (AccessToken)
</h1>
<p className='text-sm text-muted-foreground'>
管理您的 API 访问密钥,用于开发或第三方工具直接调用系统 API
</p>
</div>
</div>
<Button
type="button"
type='button'
onClick={() => setCreateDialogOpen(true)}
variant={'secondary'}
>
<Plus className="mr-1.5 size-4" />
<Plus className='mr-1.5 size-4' />
生成新令牌
</Button>
</div>
{/* 安全警告提示 */}
<div className="rounded-xl border border-amber-500/20 bg-amber-500/5 p-4 flex gap-3 text-amber-600 text-xs leading-relaxed">
<AlertTriangle className="size-4 shrink-0 mt-0.5" />
<div className="space-y-1">
<span className="font-bold">安全提示:</span>
<p className="text-muted-foreground">
<div className='rounded-xl border border-amber-500/20 bg-amber-500/5 p-4 flex gap-3 text-amber-600 text-xs leading-relaxed'>
<AlertTriangle className='size-4 shrink-0 mt-0.5' />
<div className='space-y-1'>
<span className='font-bold'>安全提示:</span>
<p className='text-muted-foreground'>
访问令牌具有您账户的完整接口调用权限。为了您的账户与资产安全,请切勿通过任何代码库提交、即时通讯工具或公共媒介泄露此令牌。推荐按需创建,不使用时及时撤销。
</p>
</div>
</div>
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<CardTitle className="text-base font-semibold">活动令牌列表</CardTitle>
<CardDescription className="text-xs">当前可用的所有访问令牌</CardDescription>
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<CardTitle className='text-base font-semibold'>
活动令牌列表
</CardTitle>
<CardDescription className='text-xs'>
当前可用的所有访问令牌
</CardDescription>
</CardHeader>
<CardContent className="pt-6 space-y-4">
<CardContent className='pt-6 space-y-4'>
{accessTokensQuery.isPending ? (
<div className="flex items-center justify-center py-8">
<Loader2 className="size-6 animate-spin text-primary" />
<div className='flex items-center justify-center py-8'>
<Loader2 className='size-6 animate-spin text-primary' />
</div>
) : (accessTokensQuery.data ?? []).length > 0 ? (
<div className="space-y-3">
<div className='space-y-3'>
{(accessTokensQuery.data ?? []).map((token) => (
<div
key={token.id}
className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 transition-all duration-300 shadow-sm"
className='flex flex-col sm:flex-row sm:items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 transition-all duration-300 shadow-sm'
>
<div className="space-y-1">
<div className="flex items-center gap-2">
<span className="font-semibold text-sm text-foreground">{token.name}</span>
<div className='space-y-1'>
<div className='flex items-center gap-2'>
<span className='font-semibold text-sm text-foreground'>
{token.name}
</span>
{token.is_admin ? (
<Badge variant="outline" className="text-[10px] px-1.5 py-0 h-4 border-rose-500/40 text-rose-500 bg-rose-500/5 font-semibold">
<Shield className="size-2.5 mr-0.5" />
<Badge
variant='outline'
className='text-[10px] px-1.5 py-0 h-4 border-rose-500/40 text-rose-500 bg-rose-500/5 font-semibold'
>
<Shield className='size-2.5 mr-0.5' />
管理员
</Badge>
) : (
<Badge variant="outline" className="text-[10px] px-1.5 py-0 h-4 border-border/50 text-muted-foreground bg-muted/10 font-semibold">
<Badge
variant='outline'
className='text-[10px] px-1.5 py-0 h-4 border-border/50 text-muted-foreground bg-muted/10 font-semibold'
>
用户令牌
</Badge>
)}
</div>
<div className="flex flex-col gap-1 text-xs text-muted-foreground">
<div className="font-mono bg-muted/30 px-2 py-0.5 rounded border border-border/50 w-fit select-all">
<div className='flex flex-col gap-1 text-xs text-muted-foreground'>
<div className='font-mono bg-muted/30 px-2 py-0.5 rounded border border-border/50 w-fit select-all'>
{token.masked_token}
</div>
<div className="flex flex-wrap gap-x-4 gap-y-0.5 pt-1">
<div className='flex flex-wrap gap-x-4 gap-y-0.5 pt-1'>
<span>创建于: {formatDate(token.created_at)}</span>
</div>
</div>
</div>
<div className="flex items-center gap-2 shrink-0 sm:self-center">
<div className='flex items-center gap-2 shrink-0 sm:self-center'>
<Button
type="button"
variant="outline"
size="sm"
className="text-xs border-dashed text-muted-foreground hover:text-primary hover:bg-primary/5 rounded-lg h-8 px-2.5"
onClick={() => handleCopyText(token.masked_token, token.id)}
type='button'
variant='outline'
size='sm'
className='text-xs border-dashed text-muted-foreground hover:text-primary hover:bg-primary/5 rounded-lg h-8 px-2.5'
onClick={() =>
handleCopyText(token.masked_token, token.id)
}
>
{copiedId === token.id ? (
<Check className="size-3.5 mr-1 text-emerald-500" />
<Check className='size-3.5 mr-1 text-emerald-500' />
) : (
<Copy className="size-3.5 mr-1" />
<Copy className='size-3.5 mr-1' />
)}
复制
</Button>
<Button
type="button"
variant="outline"
size="sm"
className="text-xs border-dashed text-muted-foreground hover:text-primary hover:bg-primary/5 rounded-lg h-8 px-2.5"
type='button'
variant='outline'
size='sm'
className='text-xs border-dashed text-muted-foreground hover:text-primary hover:bg-primary/5 rounded-lg h-8 px-2.5'
onClick={() => handleRotateToken(token.id, token.name)}
disabled={rotateTokenMutation.isPending}
>
<RefreshCw className={`size-3.5 mr-1 ${rotateTokenMutation.isPending && rotateTokenMutation.variables === token.id ? 'animate-spin' : ''}`} />
<RefreshCw
className={`size-3.5 mr-1 ${rotateTokenMutation.isPending && rotateTokenMutation.variables === token.id ? 'animate-spin' : ''}`}
/>
轮换
</Button>
<Button
type="button"
variant="outline"
size="sm"
className="text-xs border-dashed text-muted-foreground hover:text-rose-500 hover:bg-rose-500/10 hover:border-rose-500/20 rounded-lg h-8 px-2.5"
type='button'
variant='outline'
size='sm'
className='text-xs border-dashed text-muted-foreground hover:text-rose-500 hover:bg-rose-500/10 hover:border-rose-500/20 rounded-lg h-8 px-2.5'
onClick={() => handleDeleteToken(token.id, token.name)}
disabled={deleteTokenMutation.isPending}
>
<Trash2 className="size-3.5 mr-1" />
<Trash2 className='size-3.5 mr-1' />
撤销
</Button>
</div>
@@ -268,17 +328,17 @@ export function AccessTokenMain() {
))}
</div>
) : (
<div className="rounded-xl border border-dashed border-border/50 px-4 py-10 text-center text-xs text-muted-foreground bg-muted/5 flex flex-col items-center justify-center gap-3">
<Key className="size-8 text-muted-foreground/30" />
<div className='rounded-xl border border-dashed border-border/50 px-4 py-10 text-center text-xs text-muted-foreground bg-muted/5 flex flex-col items-center justify-center gap-3'>
<Key className='size-8 text-muted-foreground/30' />
<span>您当前暂无生成任何访问令牌</span>
<Button
type="button"
variant="outline"
size="sm"
className="border-dashed"
type='button'
variant='outline'
size='sm'
className='border-dashed'
onClick={() => setCreateDialogOpen(true)}
>
<Plus className="mr-1 size-3.5" />
<Plus className='mr-1 size-3.5' />
生成第一个令牌
</Button>
</div>
@@ -288,39 +348,46 @@ export function AccessTokenMain() {
{/* 创建令牌 Dialog */}
<Dialog open={createDialogOpen} onOpenChange={setCreateDialogOpen}>
<DialogContent className="sm:max-w-[425px]">
<DialogContent className='sm:max-w-[425px]'>
<form onSubmit={handleCreateToken}>
<DialogHeader>
<DialogTitle className="text-base font-semibold">生成新令牌</DialogTitle>
<DialogDescription className="text-xs text-muted-foreground">
<DialogTitle className='text-base font-semibold'>
生成新令牌
</DialogTitle>
<DialogDescription className='text-xs text-muted-foreground'>
请为新的访问令牌设置一个易于识别的名称,以便将来管理。
</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<div className="space-y-2">
<Label htmlFor="token-name" className="text-xs font-semibold">令牌名称</Label>
<div className='space-y-4 py-4'>
<div className='space-y-2'>
<Label htmlFor='token-name' className='text-xs font-semibold'>
令牌名称
</Label>
<Input
id="token-name"
placeholder="例如:my-development-key"
id='token-name'
placeholder='例如:my-development-key'
value={tokenName}
onChange={(e) => setTokenName(e.target.value)}
disabled={createTokenMutation.isPending}
className="rounded-xl border border-dashed focus:border-primary focus:ring-0 focus-visible:ring-0"
className='rounded-xl border border-dashed focus:border-primary focus:ring-0 focus-visible:ring-0'
/>
</div>
{user?.is_admin && (
<div className="flex items-center justify-between rounded-xl border border-dashed p-3 bg-muted/5">
<div className="space-y-0.5">
<Label htmlFor="token-admin" className="text-xs font-semibold flex items-center gap-1.5">
<Shield className="size-3.5 text-rose-500" />
<div className='flex items-center justify-between rounded-xl border border-dashed p-3 bg-muted/5'>
<div className='space-y-0.5'>
<Label
htmlFor='token-admin'
className='text-xs font-semibold flex items-center gap-1.5'
>
<Shield className='size-3.5 text-rose-500' />
管理员权限
</Label>
<p className="text-[11px] text-muted-foreground leading-normal">
<p className='text-[11px] text-muted-foreground leading-normal'>
开启后此令牌可访问 /admin/** 管理端点,默认关闭
</p>
</div>
<Switch
id="token-admin"
id='token-admin'
checked={tokenIsAdmin}
onCheckedChange={setTokenIsAdmin}
disabled={createTokenMutation.isPending}
@@ -328,28 +395,28 @@ export function AccessTokenMain() {
</div>
)}
</div>
<DialogFooter className="gap-2">
<DialogFooter className='gap-2'>
<Button
type="button"
variant="outline"
type='button'
variant='outline'
onClick={() => setCreateDialogOpen(false)}
disabled={createTokenMutation.isPending}
className="rounded-xl text-xs h-9 border-dashed"
className='rounded-xl text-xs h-9 border-dashed'
>
取消
</Button>
<Button
type="submit"
type='submit'
disabled={createTokenMutation.isPending}
variant={'secondary'}
>
{createTokenMutation.isPending ? (
<>
<Loader2 className="mr-1.5 size-3.5 animate-spin" />
<Loader2 className='mr-1.5 size-3.5 animate-spin' />
正在生成...
</>
) : (
"生成令牌"
'生成令牌'
)}
</Button>
</DialogFooter>
@@ -358,48 +425,51 @@ export function AccessTokenMain() {
</Dialog>
{/* 明文 Token 显示 Dialog (仅显示一次) */}
<Dialog open={viewDialogOpen} onOpenChange={(open) => {
<Dialog
open={viewDialogOpen}
onOpenChange={(open) => {
if (!open) {
setNewCreatedToken(null)
setViewDialogOpen(false)
setNewCreatedToken(null);
setViewDialogOpen(false);
}
}}>
<DialogContent className="sm:max-w-[500px]">
}}
>
<DialogContent className='sm:max-w-[500px]'>
<DialogHeader>
<DialogTitle className="text-base font-bold text-foreground flex items-center gap-1.5">
<Check className="size-5 text-emerald-500 border border-emerald-500 rounded-full p-0.5" />
<DialogTitle className='text-base font-bold text-foreground flex items-center gap-1.5'>
<Check className='size-5 text-emerald-500 border border-emerald-500 rounded-full p-0.5' />
令牌密钥已就绪
</DialogTitle>
</DialogHeader>
{newCreatedToken && (
<div className="space-y-4 py-2">
<div className='space-y-4 py-2'>
{/* 明文 Token 文本框 */}
<div className="flex items-center gap-2 rounded-xl bg-primary/5 border border-dashed border-primary/30 p-3">
<span className="font-mono text-xs select-all break-all flex-1 text-primary font-semibold leading-relaxed">
<div className='flex items-center gap-2 rounded-xl bg-primary/5 border border-dashed border-primary/30 p-3'>
<span className='font-mono text-xs select-all break-all flex-1 text-primary font-semibold leading-relaxed'>
{newCreatedToken.token}
</span>
<Button
type="button"
size="icon"
variant="outline"
className="size-8 rounded-lg shrink-0 border-dashed text-primary hover:bg-primary/10 hover:border-primary/30 transition-colors"
type='button'
size='icon'
variant='outline'
className='size-8 rounded-lg shrink-0 border-dashed text-primary hover:bg-primary/10 hover:border-primary/30 transition-colors'
onClick={() => handleCopyText(newCreatedToken.token, 9999)}
>
{copiedId === 9999 ? (
<Check className="size-3.5 text-emerald-500" />
<Check className='size-3.5 text-emerald-500' />
) : (
<Copy className="size-3.5" />
<Copy className='size-3.5' />
)}
</Button>
</div>
{/* 强提示 */}
<div className="rounded-xl border border-rose-500/20 bg-rose-500/5 p-4 flex gap-3 text-rose-600 text-xs leading-relaxed">
<Info className="size-4 shrink-0 mt-0.5" />
<div className="space-y-1">
<span className="font-bold">重要提示:</span>
<p className="text-muted-foreground">
<div className='rounded-xl border border-rose-500/20 bg-rose-500/5 p-4 flex gap-3 text-rose-600 text-xs leading-relaxed'>
<Info className='size-4 shrink-0 mt-0.5' />
<div className='space-y-1'>
<span className='font-bold'>重要提示:</span>
<p className='text-muted-foreground'>
这是您唯一一次能够查看此访问令牌明文密钥的机会。请立即将其复制并安全地保存。
</p>
</div>
@@ -409,12 +479,12 @@ export function AccessTokenMain() {
<DialogFooter>
<Button
type="button"
type='button'
onClick={() => {
setNewCreatedToken(null)
setViewDialogOpen(false)
setNewCreatedToken(null);
setViewDialogOpen(false);
}}
className="rounded-xl w-full"
className='rounded-xl w-full'
>
我已经复制并妥善保存
</Button>
@@ -422,5 +492,5 @@ export function AccessTokenMain() {
</DialogContent>
</Dialog>
</motion.div>
)
);
}
@@ -1,209 +1,251 @@
"use client"
'use client';
import * as React from "react"
import Link from "next/link"
import { Moon, Sun, Loader2, Check, Monitor } from "lucide-react"
import { useTheme } from "next-themes"
import { toast } from "sonner"
import { Button } from "@/components/ui/button"
import { useCustomTheme } from "@/lib/theme"
import { cn } from "@/lib/utils"
import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator } from "@/components/ui/breadcrumb"
import * as React from 'react';
import Link from 'next/link';
import { Check, Loader2, Monitor, Moon, Sun } from 'lucide-react';
import { useTheme } from 'next-themes';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { useCustomTheme } from '@/lib/theme';
import { cn } from '@/lib/utils';
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from '@/components/ui/breadcrumb';
function ThemeModeSection() {
const { theme, setTheme } = useTheme()
const [mounted, setMounted] = React.useState(false)
const { theme, setTheme } = useTheme();
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => {
setMounted(true)
}, [])
setMounted(true);
}, []);
const modes = [
{ id: "light", name: "明亮", icon: Sun },
{ id: "dark", name: "黑暗", icon: Moon },
{ id: "system", name: "自动", icon: Monitor }
]
{ id: 'light', name: '明亮', icon: Sun },
{ id: 'dark', name: '黑暗', icon: Moon },
{ id: 'system', name: '自动', icon: Monitor },
];
if (!mounted) {
return (
<div className="space-y-6">
<div className='space-y-6'>
<div>
<h2 className="font-medium text-sm text-foreground">主题模式</h2>
<p className="text-xs text-muted-foreground">
<h2 className='font-medium text-sm text-foreground'>主题模式</h2>
<p className='text-xs text-muted-foreground'>
选择应用程序的整体明暗主题
</p>
</div>
<div className="flex gap-2">
<div className='flex gap-2'>
{modes.map((mode) => {
const Icon = mode.icon
const Icon = mode.icon;
return (
<Button
key={mode.id}
variant="secondary"
size="sm"
className="flex-1 text-xs"
variant='secondary'
size='sm'
className='flex-1 text-xs'
>
{Icon && <Icon className="size-3 mr-1" />}
{Icon && <Icon className='size-3 mr-1' />}
{mode.name}
</Button>
)
);
})}
</div>
</div>
)
);
}
return (
<div className="space-y-6">
<div className='space-y-6'>
<div>
<h2 className="font-medium text-sm text-foreground">主题模式</h2>
<p className="text-xs text-muted-foreground">
<h2 className='font-medium text-sm text-foreground'>主题模式</h2>
<p className='text-xs text-muted-foreground'>
选择应用程序的整体明暗主题
</p>
</div>
<div className="flex gap-2">
<div className='flex gap-2'>
{modes.map((mode) => {
const Icon = mode.icon
const isActive = theme === mode.id
const Icon = mode.icon;
const isActive = theme === mode.id;
return (
<Button
key={mode.id}
variant={isActive ? "default" : "secondary"}
size="sm"
variant={isActive ? 'default' : 'secondary'}
size='sm'
onClick={() => setTheme(mode.id)}
className="flex-1 text-xs"
className='flex-1 text-xs'
>
{Icon && <Icon className="size-3 mr-1" />}
{Icon && <Icon className='size-3 mr-1' />}
{mode.name}
</Button>
)
);
})}
</div>
</div>
)
);
}
function InterfaceAppearanceSection() {
const { themes, currentThemeId, setTheme: setCustomTheme, isLoading } = useCustomTheme()
const { theme: mode } = useTheme()
const [mounted, setMounted] = React.useState(false)
const [switching, setSwitching] = React.useState<string | null>(null)
const {
themes,
currentThemeId,
setTheme: setCustomTheme,
isLoading,
} = useCustomTheme();
const { theme: mode } = useTheme();
const [mounted, setMounted] = React.useState(false);
const [switching, setSwitching] = React.useState<string | null>(null);
React.useEffect(() => {
setMounted(true)
}, [])
setMounted(true);
}, []);
const handleSetTheme = (themeId: string) => {
if (switching === themeId) return
setSwitching(themeId)
if (switching === themeId) return;
setSwitching(themeId);
try {
setCustomTheme(themeId)
const themeName = themes.find(t => t.id === themeId)?.name || themeId
toast.success(`外观已切换为 ${ themeName }`)
setCustomTheme(themeId);
const themeName = themes.find((t) => t.id === themeId)?.name || themeId;
toast.success(`外观已切换为 ${themeName}`);
} catch {
toast.error("切换失败")
toast.error('切换失败');
} finally {
setSwitching(null)
}
setSwitching(null);
}
};
const isDark = mounted && (mode === 'dark' || (mode === 'system' && typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches))
const isDark =
mounted &&
(mode === 'dark' ||
(mode === 'system' &&
typeof window !== 'undefined' &&
window.matchMedia('(prefers-color-scheme: dark)').matches));
return (
<div className="space-y-6">
<div className='space-y-6'>
<div>
<h2 className="font-medium text-sm text-foreground">界面外观</h2>
<p className="text-xs text-muted-foreground">
<h2 className='font-medium text-sm text-foreground'>界面外观</h2>
<p className='text-xs text-muted-foreground'>
选择界面的配色方案和视觉风格
</p>
</div>
<div>
{isLoading ? (
<div className="flex items-center justify-center p-12">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground/50" />
<div className='flex items-center justify-center p-12'>
<Loader2 className='h-8 w-8 animate-spin text-muted-foreground/50' />
</div>
) : (
<div className="grid grid-cols-2 xs:grid-cols-3 sm:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-6">
<div className='grid grid-cols-2 xs:grid-cols-3 sm:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6 gap-6'>
{themes.map((theme) => {
const isActive = currentThemeId === theme.id || (!currentThemeId && theme.id === "default.css")
const isSwitching = switching === theme.id
const colors = isDark ? theme.colors.dark : theme.colors.light
const isActive =
currentThemeId === theme.id ||
(!currentThemeId && theme.id === 'default.css');
const isSwitching = switching === theme.id;
const colors = isDark ? theme.colors.dark : theme.colors.light;
return (
<div
key={theme.id}
onClick={() => handleSetTheme(theme.id)}
className={cn(
"group relative cursor-pointer rounded-xl border transition-all overflow-hidden",
'group relative cursor-pointer rounded-xl border transition-all overflow-hidden',
isActive
? "border-primary ring-2 ring-primary/20"
: "border-border hover:border-border/50 hover:shadow-md"
? 'border-primary ring-2 ring-primary/20'
: 'border-border hover:border-border/50 hover:shadow-md',
)}
>
<div
className="aspect-[4/3] w-full relative"
style={{ backgroundColor: colors.background || colors['background'] }}
className='aspect-[4/3] w-full relative'
style={{
backgroundColor:
colors.background || colors['background'],
}}
>
<div className="flex h-full p-2 gap-2">
<div className='flex h-full p-2 gap-2'>
<div
className="w-1/4 h-full rounded-sm opacity-80"
style={{ backgroundColor: colors.sidebar || colors['sidebar'] }}
className='w-1/4 h-full rounded-sm opacity-80'
style={{
backgroundColor: colors.sidebar || colors['sidebar'],
}}
/>
<div className="flex-1 space-y-1.5 pt-1">
<div className='flex-1 space-y-1.5 pt-1'>
<div
className="h-1.5 w-1/3 rounded-full opacity-90"
style={{ backgroundColor: colors.primary || colors['primary'] }}
className='h-1.5 w-1/3 rounded-full opacity-90'
style={{
backgroundColor:
colors.primary || colors['primary'],
}}
/>
<div
className="h-8 w-full rounded-sm opacity-40"
style={{ backgroundColor: colors.sidebar || colors['sidebar'] }}
className='h-8 w-full rounded-sm opacity-40'
style={{
backgroundColor:
colors.sidebar || colors['sidebar'],
}}
/>
<div
className="h-4 w-1/2 rounded-sm opacity-30"
style={{ backgroundColor: colors.sidebar || colors['sidebar'] }}
className='h-4 w-1/2 rounded-sm opacity-30'
style={{
backgroundColor:
colors.sidebar || colors['sidebar'],
}}
/>
</div>
</div>
{isActive && !isSwitching && (
<div className="absolute top-2 right-2 z-10 bg-primary text-primary-foreground rounded-full p-0.5 shadow-sm">
<Check className="h-3 w-3" />
<div className='absolute top-2 right-2 z-10 bg-primary text-primary-foreground rounded-full p-0.5 shadow-sm'>
<Check className='h-3 w-3' />
</div>
)}
</div>
<div className="p-2 border-t border-border/50 bg-card">
<div className={cn("text-[10px] font-medium text-center truncate transition-colors", isActive ? "text-primary" : "text-muted-foreground")}>
<div className='p-2 border-t border-border/50 bg-card'>
<div
className={cn(
'text-[10px] font-medium text-center truncate transition-colors',
isActive ? 'text-primary' : 'text-muted-foreground',
)}
>
{theme.name}
</div>
</div>
</div>
)
);
})}
</div>
)}
</div>
</div>
)
);
}
export function AppearanceMain() {
return (
<div className="py-6 space-y-6">
<div className="font-semibold">
<div className='py-6 space-y-6'>
<div className='font-semibold'>
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink asChild>
<Link href="/settings" className="text-base text-primary">设置</Link>
<Link href='/settings' className='text-base text-primary'>
设置
</Link>
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem>
<BreadcrumbPage className="text-base font-semibold">外观设置</BreadcrumbPage>
<BreadcrumbPage className='text-base font-semibold'>
外观设置
</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
@@ -213,12 +255,14 @@ export function AppearanceMain() {
<InterfaceAppearanceSection />
<div className="space-y-4 opacity-50 pointer-events-none grayscale">
<h2 className="font-medium text-sm text-muted-foreground">即将推出</h2>
<div className="bg-muted/30 rounded-xl p-6 h-32 flex items-center justify-center border border-border/50">
<span className="text-sm text-muted-foreground">更多自定义选项正在开发中...</span>
<div className='space-y-4 opacity-50 pointer-events-none grayscale'>
<h2 className='font-medium text-sm text-muted-foreground'>即将推出</h2>
<div className='bg-muted/30 rounded-xl p-6 h-32 flex items-center justify-center border border-border/50'>
<span className='text-sm text-muted-foreground'>
更多自定义选项正在开发中...
</span>
</div>
</div>
</div>
)
);
}
@@ -1,26 +1,32 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {toast} from "sonner"
import {Button} from "@/components/ui/button"
import {Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Switch} from "@/components/ui/switch"
import services from "@/lib/services"
import type {AuthSource, AuthSourceRequest} from "@/lib/services/admin"
import { useEffect, useState } from 'react';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import services from '@/lib/services';
import type { AuthSource, AuthSourceRequest } from '@/lib/services/admin';
const emptyForm: AuthSourceRequest = {
name: "",
type: "oidc",
display_name: "",
name: '',
type: 'oidc',
display_name: '',
is_active: false,
client_id: "",
client_secret: "",
openid_discovery_url: "",
scopes: "openid profile email",
icon_url: "",
}
client_id: '',
client_secret: '',
openid_discovery_url: '',
scopes: 'openid profile email',
icon_url: '',
};
export function AuthSourceModal({
isOpen,
@@ -28,13 +34,13 @@ export function AuthSourceModal({
onClose,
onChanged,
}: {
isOpen: boolean
source: AuthSource | null
onClose: () => void
onChanged: () => Promise<void>
isOpen: boolean;
source: AuthSource | null;
onClose: () => void;
onChanged: () => Promise<void>;
}) {
const [form, setForm] = useState<AuthSourceRequest>(emptyForm)
const [saving, setSaving] = useState(false)
const [form, setForm] = useState<AuthSourceRequest>(emptyForm);
const [saving, setSaving] = useState(false);
useEffect(() => {
if (isOpen) {
@@ -45,122 +51,152 @@ export function AuthSourceModal({
display_name: source.display_name,
is_active: source.is_active,
client_id: source.client_id,
client_secret: "",
client_secret: '',
openid_discovery_url: source.openid_discovery_url,
scopes: source.scopes || "openid profile email",
scopes: source.scopes || 'openid profile email',
icon_url: source.icon_url,
})
});
} else {
setForm(emptyForm)
setForm(emptyForm);
}
} else {
setForm(emptyForm)
setSaving(false)
setForm(emptyForm);
setSaving(false);
}
}, [isOpen, source])
}, [isOpen, source]);
const saveSource = async () => {
setSaving(true)
setSaving(true);
try {
if (source) {
await services.adminAuthSource.updateAuthSource(source.id, form)
await services.adminAuthSource.updateAuthSource(source.id, form);
} else {
await services.adminAuthSource.createAuthSource(form)
await services.adminAuthSource.createAuthSource(form);
}
await onChanged()
toast.success("认证源已保存")
onClose()
await onChanged();
toast.success('认证源已保存');
onClose();
} catch (error) {
toast.error(error instanceof Error ? error.message : "保存认证源失败")
toast.error(error instanceof Error ? error.message : '保存认证源失败');
} finally {
setSaving(false)
}
setSaving(false);
}
};
return (
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
<DialogContent className="max-w-xl">
<DialogContent className='max-w-xl'>
<DialogHeader>
<DialogTitle>{source ? "编辑认证源" : "新增认证源"}</DialogTitle>
<DialogTitle>{source ? '编辑认证源' : '新增认证源'}</DialogTitle>
<DialogDescription>
{source ? "修改系统自定义的 OIDC 认证源参数。" : "配置新的自定义 OIDC 认证源。"}
{source
? '修改系统自定义的 OIDC 认证源参数。'
: '配置新的自定义 OIDC 认证源。'}
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 md:grid-cols-2 pt-2">
<div className="space-y-2">
<div className='grid gap-4 md:grid-cols-2 pt-2'>
<div className='space-y-2'>
<Label>标识符 (英文名称)</Label>
<Input
value={form.name}
disabled={!!source}
onChange={(e) => setForm((prev) => ({ ...prev, name: e.target.value }))}
placeholder="例如: github"
onChange={(e) =>
setForm((prev) => ({ ...prev, name: e.target.value }))
}
placeholder='例如: github'
/>
</div>
<div className="space-y-2">
<div className='space-y-2'>
<Label>展示名称</Label>
<Input
value={form.display_name}
onChange={(e) => setForm((prev) => ({ ...prev, display_name: e.target.value }))}
placeholder="例如: GitHub 登录"
onChange={(e) =>
setForm((prev) => ({ ...prev, display_name: e.target.value }))
}
placeholder='例如: GitHub 登录'
/>
</div>
<div className="space-y-2">
<div className='space-y-2'>
<Label>Client ID</Label>
<Input
value={form.client_id}
onChange={(e) => setForm((prev) => ({ ...prev, client_id: e.target.value }))}
onChange={(e) =>
setForm((prev) => ({ ...prev, client_id: e.target.value }))
}
/>
</div>
<div className="space-y-2">
<div className='space-y-2'>
<Label>Client Secret</Label>
<Input
value={form.client_secret}
onChange={(e) => setForm((prev) => ({ ...prev, client_secret: e.target.value }))}
placeholder={source ? "留空则保留原值" : ""}
onChange={(e) =>
setForm((prev) => ({ ...prev, client_secret: e.target.value }))
}
placeholder={source ? '留空则保留原值' : ''}
/>
</div>
<div className="space-y-2 md:col-span-2">
<div className='space-y-2 md:col-span-2'>
<Label>Discovery URL (OIDC 发行方 URL)</Label>
<Input
value={form.openid_discovery_url}
onChange={(e) => setForm((prev) => ({ ...prev, openid_discovery_url: e.target.value }))}
placeholder="https://..."
onChange={(e) =>
setForm((prev) => ({
...prev,
openid_discovery_url: e.target.value,
}))
}
placeholder='https://...'
/>
</div>
<div className="space-y-2">
<div className='space-y-2'>
<Label>Scopes</Label>
<Input
value={form.scopes}
onChange={(e) => setForm((prev) => ({ ...prev, scopes: e.target.value }))}
onChange={(e) =>
setForm((prev) => ({ ...prev, scopes: e.target.value }))
}
/>
</div>
<div className="space-y-2">
<div className='space-y-2'>
<Label>图标 URL (可选)</Label>
<Input
value={form.icon_url}
onChange={(e) => setForm((prev) => ({ ...prev, icon_url: e.target.value }))}
placeholder="https://... 或留空"
onChange={(e) =>
setForm((prev) => ({ ...prev, icon_url: e.target.value }))
}
placeholder='https://... 或留空'
/>
</div>
<div className="flex items-center justify-between rounded-xl border border-dashed p-3 md:col-span-2 bg-muted/10">
<div className='flex items-center justify-between rounded-xl border border-dashed p-3 md:col-span-2 bg-muted/10'>
<div>
<div className="font-medium text-sm">启用认证源</div>
<div className="text-xs text-muted-foreground">启用后会立即显示在登录页和账号绑定中。</div>
<div className='font-medium text-sm'>启用认证源</div>
<div className='text-xs text-muted-foreground'>
启用后会立即显示在登录页和账号绑定中。
</div>
<Switch checked={form.is_active} onCheckedChange={(checked) => setForm((prev) => ({ ...prev, is_active: checked }))} />
</div>
<div className="md:col-span-2 flex justify-end gap-2 pt-2 border-t mt-2">
<Button variant="outline" type="button" onClick={onClose}>
<Switch
checked={form.is_active}
onCheckedChange={(checked) =>
setForm((prev) => ({ ...prev, is_active: checked }))
}
/>
</div>
<div className='md:col-span-2 flex justify-end gap-2 pt-2 border-t mt-2'>
<Button variant='outline' type='button' onClick={onClose}>
取消
</Button>
<Button type="button" onClick={saveSource} disabled={saving} variant="secondary">
{saving ? "保存中..." : "保存"}
<Button
type='button'
onClick={saveSource}
disabled={saving}
variant='secondary'
>
{saving ? '保存中...' : '保存'}
</Button>
</div>
</div>
</DialogContent>
</Dialog>
)
);
}
+108 -64
View File
@@ -1,10 +1,10 @@
"use client"
'use client';
import {useMutation, useQuery} from "@tanstack/react-query"
import {ExternalLink, RefreshCw, Sparkles} from "lucide-react"
import {toast} from "sonner"
import { useMutation, useQuery } from '@tanstack/react-query';
import { ExternalLink, RefreshCw, Sparkles } from 'lucide-react';
import { toast } from 'sonner';
import services from "@/lib/services"
import services from '@/lib/services';
import {
AlertDialog,
AlertDialogAction,
@@ -14,54 +14,64 @@ import {
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger
} from "@/components/ui/alert-dialog"
import {Badge} from "@/components/ui/badge"
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Spinner} from "@/components/ui/spinner"
AlertDialogTrigger,
} from '@/components/ui/alert-dialog';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Spinner } from '@/components/ui/spinner';
function InfoRow({ label, value }: { label: string; value: React.ReactNode }) {
return (
<div className="flex items-center justify-between gap-4 border-b border-dashed py-2 last:border-b-0">
<span className="text-xs text-muted-foreground">{label}</span>
<span className="text-right text-xs font-medium text-foreground break-all">{value || "-"}</span>
<div className='flex items-center justify-between gap-4 border-b border-dashed py-2 last:border-b-0'>
<span className='text-xs text-muted-foreground'>{label}</span>
<span className='text-right text-xs font-medium text-foreground break-all'>
{value || '-'}
</span>
</div>
)
);
}
export function InfoTab() {
const updateQuery = useQuery({
queryKey: ["admin", "update"],
queryKey: ['admin', 'update'],
queryFn: () => services.adminStatus.getUpdateStatus(),
refetchInterval: 30 * 60 * 1000,
staleTime: 5 * 60 * 1000,
})
});
const applyUpdateMutation = useMutation({
mutationFn: () => services.adminStatus.applyUpdate(),
onSuccess: () => {
toast.success("升级包已校验完成,服务正在重启")
toast.success('升级包已校验完成,服务正在重启');
},
onError: (error: Error) => {
toast.error(error.message || "应用升级失败")
toast.error(error.message || '应用升级失败');
},
})
});
const update = updateQuery.data
const update = updateQuery.data;
return (
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<div className="flex items-start justify-between gap-4">
<div className="flex items-center gap-2">
<div className="rounded-lg bg-muted p-1.5 text-muted-foreground">
<Sparkles className="size-4" />
<div className='grid grid-cols-1 gap-4 lg:grid-cols-2'>
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<div className='flex items-start justify-between gap-4'>
<div className='flex items-center gap-2'>
<div className='rounded-lg bg-muted p-1.5 text-muted-foreground'>
<Sparkles className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">应用更新</CardTitle>
<CardDescription className="text-xs">
<CardTitle className='text-base font-semibold'>
应用更新
</CardTitle>
<CardDescription className='text-xs'>
检查上游 GitHub Actions Release 并升级当前服务
</CardDescription>
</div>
@@ -69,75 +79,109 @@ export function InfoTab() {
{update?.update_available ? (
<Badge>发现新版本</Badge>
) : update ? (
<Badge variant="secondary">已是最新</Badge>
<Badge variant='secondary'>已是最新</Badge>
) : null}
</div>
</CardHeader>
<CardContent className="flex flex-col gap-4 pt-4">
<CardContent className='flex flex-col gap-4 pt-4'>
{updateQuery.isLoading ? (
<div className="flex min-h-32 items-center justify-center">
<div className='flex min-h-32 items-center justify-center'>
<Spinner />
</div>
) : updateQuery.isError ? (
<div className="flex min-h-32 flex-col items-center justify-center gap-3 text-center">
<p className="text-xs text-muted-foreground">
{updateQuery.error.message || "无法获取上游版本信息"}
<div className='flex min-h-32 flex-col items-center justify-center gap-3 text-center'>
<p className='text-xs text-muted-foreground'>
{updateQuery.error.message || '无法获取上游版本信息'}
</p>
<Button type="button" variant="outline" size="sm" onClick={() => updateQuery.refetch()}>
<RefreshCw data-icon="inline-start" />
<Button
type='button'
variant='outline'
size='sm'
onClick={() => updateQuery.refetch()}
>
<RefreshCw data-icon='inline-start' />
重新检查
</Button>
</div>
) : update ? (
<>
<div>
<InfoRow label="当前版本" value={update.current_version} />
<InfoRow label="最新版本" value={update.latest_version} />
{update.build_time && <InfoRow label="构建时间" value={update.build_time} />}
<InfoRow label="运行平台" value={update.platform} />
<InfoRow label="上游仓库" value={update.upstream_repository} />
<InfoRow label="Release 资产" value={update.asset_name} />
<InfoRow label='当前版本' value={update.current_version} />
<InfoRow label='最新版本' value={update.latest_version} />
{update.build_time && (
<InfoRow label='构建时间' value={update.build_time} />
)}
<InfoRow label='运行平台' value={update.platform} />
<InfoRow label='上游仓库' value={update.upstream_repository} />
<InfoRow label='Release 资产' value={update.asset_name} />
</div>
{update.release_notes && (
<div className="flex flex-col gap-2 rounded-md border border-dashed p-3">
<p className="text-xs font-medium">更新说明</p>
<p className="max-h-40 overflow-y-auto whitespace-pre-wrap text-xs leading-relaxed text-muted-foreground">
<div className='flex flex-col gap-2 rounded-md border border-dashed p-3'>
<p className='text-xs font-medium'>更新说明</p>
<p className='max-h-40 overflow-y-auto whitespace-pre-wrap text-xs leading-relaxed text-muted-foreground'>
{update.release_notes}
</p>
</div>
)}
<div className="flex flex-wrap justify-end gap-2">
<Button type="button" variant="ghost" size="sm" onClick={() => updateQuery.refetch()} disabled={updateQuery.isFetching}>
{updateQuery.isFetching ? <Spinner data-icon="inline-start" /> : <RefreshCw data-icon="inline-start" />}
<div className='flex flex-wrap justify-end gap-2'>
<Button
type='button'
variant='ghost'
size='sm'
onClick={() => updateQuery.refetch()}
disabled={updateQuery.isFetching}
>
{updateQuery.isFetching ? (
<Spinner data-icon='inline-start' />
) : (
<RefreshCw data-icon='inline-start' />
)}
检查更新
</Button>
{update.release_url && (
<Button asChild variant="outline" size="sm">
<a href={update.release_url} target="_blank" rel="noreferrer">
<ExternalLink data-icon="inline-start" />
<Button asChild variant='outline' size='sm'>
<a
href={update.release_url}
target='_blank'
rel='noreferrer'
>
<ExternalLink data-icon='inline-start' />
查看 Release
</a>
</Button>
)}
<AlertDialog>
<AlertDialogTrigger asChild>
<Button type="button" size="sm" disabled={!update.can_upgrade || applyUpdateMutation.isPending}>
{applyUpdateMutation.isPending && <Spinner data-icon="inline-start" />}
<Button
type='button'
size='sm'
disabled={
!update.can_upgrade || applyUpdateMutation.isPending
}
>
{applyUpdateMutation.isPending && (
<Spinner data-icon='inline-start' />
)}
立即升级
</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>升级到 {update.latest_version}?</AlertDialogTitle>
<AlertDialogTitle>
升级到 {update.latest_version}?
</AlertDialogTitle>
<AlertDialogDescription>
服务将下载并校验 {update.asset_name},随后替换当前二进制并重启。请确保安装目录可写,且服务允许原地重启。
服务将下载并校验 {update.asset_name}
,随后替换当前二进制并重启。请确保安装目录可写,且服务允许原地重启。
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>取消</AlertDialogCancel>
<AlertDialogAction onClick={() => applyUpdateMutation.mutate()}>
<AlertDialogAction
onClick={() => applyUpdateMutation.mutate()}
>
确认升级
</AlertDialogAction>
</AlertDialogFooter>
@@ -146,12 +190,12 @@ export function InfoTab() {
</div>
{!update.can_upgrade && (
<p className="text-xs text-muted-foreground">
{update.current_version === "dev"
? "开发构建没有可比较的 Release 版本,不能执行自动升级。"
<p className='text-xs text-muted-foreground'>
{update.current_version === 'dev'
? '开发构建没有可比较的 Release 版本,不能执行自动升级。'
: update.update_available
? "当前平台暂不支持自动替换二进制,请从 Release 页面手动升级。"
: "当前版本无需升级。"}
? '当前平台暂不支持自动替换二进制,请从 Release 页面手动升级。'
: '当前版本无需升级。'}
</p>
)}
</>
@@ -159,5 +203,5 @@ export function InfoTab() {
</CardContent>
</Card>
</div>
)
);
}
@@ -1,60 +1,74 @@
"use client"
'use client';
import Link from "next/link"
import { Bell } from "lucide-react"
import { Switch } from "@/components/ui/switch"
import { Label } from "@/components/ui/label"
import { useNotificationSettings } from "@/contexts/notification-settings-context"
import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator } from "@/components/ui/breadcrumb"
import Link from 'next/link';
import { Bell } from 'lucide-react';
import { Switch } from '@/components/ui/switch';
import { Label } from '@/components/ui/label';
import { useNotificationSettings } from '@/contexts/notification-settings-context';
import {
Breadcrumb,
BreadcrumbItem,
BreadcrumbLink,
BreadcrumbList,
BreadcrumbPage,
BreadcrumbSeparator,
} from '@/components/ui/breadcrumb';
export function NotificationsMain() {
const { showBell, setShowBell } = useNotificationSettings()
const { showBell, setShowBell } = useNotificationSettings();
return (
<div className="py-6 space-y-6">
<div className="font-semibold">
<div className='py-6 space-y-6'>
<div className='font-semibold'>
<Breadcrumb>
<BreadcrumbList>
<BreadcrumbItem>
<BreadcrumbLink asChild>
<Link href="/settings" className="text-base text-primary">设置</Link>
<Link href='/settings' className='text-base text-primary'>
设置
</Link>
</BreadcrumbLink>
</BreadcrumbItem>
<BreadcrumbSeparator />
<BreadcrumbItem>
<BreadcrumbPage className="text-base font-semibold">通知设置</BreadcrumbPage>
<BreadcrumbPage className='text-base font-semibold'>
通知设置
</BreadcrumbPage>
</BreadcrumbItem>
</BreadcrumbList>
</Breadcrumb>
</div>
<div className="space-y-6">
<div className='space-y-6'>
<div>
<h2 className="font-medium text-sm text-foreground">通知显示</h2>
<p className="text-xs text-muted-foreground">
<h2 className='font-medium text-sm text-foreground'>通知显示</h2>
<p className='text-xs text-muted-foreground'>
设置通知相关的显示选项
</p>
</div>
<div className="flex items-center justify-between rounded-lg border p-4">
<div className="flex items-center gap-3">
<Bell className="size-5 text-primary" />
<div className="space-y-0.5">
<Label htmlFor="show-bell" className="text-sm font-medium cursor-pointer">
<div className='flex items-center justify-between rounded-lg border p-4'>
<div className='flex items-center gap-3'>
<Bell className='size-5 text-primary' />
<div className='space-y-0.5'>
<Label
htmlFor='show-bell'
className='text-sm font-medium cursor-pointer'
>
显示通知铃铛
</Label>
<p className="text-xs text-muted-foreground">
<p className='text-xs text-muted-foreground'>
在顶部导航栏中显示通知铃铛图标
</p>
</div>
</div>
<Switch
id="show-bell"
id='show-bell'
checked={showBell}
onCheckedChange={setShowBell}
/>
</div>
</div>
</div>
)
);
}
@@ -1,158 +1,205 @@
"use client"
'use client';
import {useMemo} from "react"
import {useMutation, useQuery, useQueryClient, type UseQueryResult} from "@tanstack/react-query"
import {KeyRound, ShieldAlert, X} from "lucide-react"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Badge} from "@/components/ui/badge"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select"
import services from "@/lib/services"
import type {SystemConfig} from "@/lib/services/admin"
import {TemplatesManager} from "./templates"
import {toast} from "sonner"
import { useMemo } from 'react';
import {
useMutation,
useQuery,
useQueryClient,
type UseQueryResult,
} from '@tanstack/react-query';
import { KeyRound, ShieldAlert, X } from 'lucide-react';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import services from '@/lib/services';
import type { SystemConfig } from '@/lib/services/admin';
import { TemplatesManager } from './templates';
import { toast } from 'sonner';
interface OperationTabProps {
configs: Record<string, SystemConfig>
systemConfigsQuery: UseQueryResult<SystemConfig[], Error>
configs: Record<string, SystemConfig>;
systemConfigsQuery: UseQueryResult<SystemConfig[], Error>;
}
export function OperationTab({ configs, systemConfigsQuery }: OperationTabProps) {
const queryClient = useQueryClient()
export function OperationTab({
configs,
systemConfigsQuery,
}: OperationTabProps) {
const queryClient = useQueryClient();
const uploadTypesQuery = useQuery({
queryKey: ["admin", "upload-types"],
queryKey: ['admin', 'upload-types'],
queryFn: () => services.adminSystemConfig.listUploadTypes(),
})
});
const updateWhitelistMutation = useMutation({
mutationFn: async (newValue: string) => {
const config = configs["file_access_whitelist"]
const config = configs['file_access_whitelist'];
if (!config) {
throw new Error("缺少配置项: file_access_whitelist")
throw new Error('缺少配置项: file_access_whitelist');
}
await services.adminSystemConfig.updateSystemConfig("file_access_whitelist", {
await services.adminSystemConfig.updateSystemConfig(
'file_access_whitelist',
{
value: newValue,
description: config.description,
})
},
);
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] })
await queryClient.invalidateQueries({ queryKey: ["public-config"] })
toast.success("文件访问白名单已更新")
await queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
});
await queryClient.invalidateQueries({ queryKey: ['public-config'] });
toast.success('文件访问白名单已更新');
},
onError: (error: Error) => {
toast.error(error.message || "更新白名单失败")
toast.error(error.message || '更新白名单失败');
},
})
});
const whitelistConfig = configs["file_access_whitelist"]
const whitelistConfig = configs['file_access_whitelist'];
const currentWhitelist = useMemo<string[]>(() => {
if (!whitelistConfig?.value) return ["avatar"]
if (!whitelistConfig?.value) return ['avatar'];
try {
const parsed = JSON.parse(whitelistConfig.value)
if (Array.isArray(parsed)) return parsed
const parsed = JSON.parse(whitelistConfig.value);
if (Array.isArray(parsed)) return parsed;
} catch {
// 降级支持逗号分隔解析
return whitelistConfig.value.split(",").map(s => s.trim()).filter(Boolean)
return whitelistConfig.value
.split(',')
.map((s) => s.trim())
.filter(Boolean);
}
return ["avatar"]
}, [whitelistConfig?.value])
return ['avatar'];
}, [whitelistConfig?.value]);
const handleAddType = (type: string) => {
if (!type || currentWhitelist.includes(type)) return
const newWhitelist = [...currentWhitelist, type]
updateWhitelistMutation.mutate(JSON.stringify(newWhitelist))
}
if (!type || currentWhitelist.includes(type)) return;
const newWhitelist = [...currentWhitelist, type];
updateWhitelistMutation.mutate(JSON.stringify(newWhitelist));
};
const handleRemoveType = (typeToRemove: string) => {
const newWhitelist = currentWhitelist.filter(t => t !== typeToRemove)
updateWhitelistMutation.mutate(JSON.stringify(newWhitelist))
}
const newWhitelist = currentWhitelist.filter((t) => t !== typeToRemove);
updateWhitelistMutation.mutate(JSON.stringify(newWhitelist));
};
const availableTypes = useMemo(() => {
const types = uploadTypesQuery.data ?? []
return types.map(t => {
let label = t
if (t === "avatar") label = "头像 (avatar)"
else if (t === "attachment") label = "附件 (attachment)"
else if (t === "doc") label = "文档 (doc)"
else if (t === "generic") label = "通用 (generic)"
return { value: t, label }
})
}, [uploadTypesQuery.data])
const types = uploadTypesQuery.data ?? [];
return types.map((t) => {
let label = t;
if (t === 'avatar') label = '头像 (avatar)';
else if (t === 'attachment') label = '附件 (attachment)';
else if (t === 'doc') label = '文档 (doc)';
else if (t === 'generic') label = '通用 (generic)';
return { value: t, label };
});
}, [uploadTypesQuery.data]);
return (
<div className="space-y-6">
<div className='space-y-6'>
{/* 文件访问白名单设置 */}
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<div className="flex items-center gap-2">
<div className="p-1.5 rounded-lg bg-primary/10 text-primary">
<KeyRound className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<div className='flex items-center gap-2'>
<div className='p-1.5 rounded-lg bg-primary/10 text-primary'>
<KeyRound className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">文件访问权限控制</CardTitle>
<CardDescription className="text-xs">配置免登录直接访问的文件业务类型。不在白名单内的文件将要求登录鉴权。</CardDescription>
<CardTitle className='text-base font-semibold'>
文件访问权限控制
</CardTitle>
<CardDescription className='text-xs'>
配置免登录直接访问的文件业务类型。不在白名单内的文件将要求登录鉴权。
</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="pt-6 space-y-4">
<div className="flex flex-col gap-4">
<div className="flex items-center gap-3">
<span className="text-sm font-medium text-muted-foreground">添加免鉴权类型:</span>
<CardContent className='pt-6 space-y-4'>
<div className='flex flex-col gap-4'>
<div className='flex items-center gap-3'>
<span className='text-sm font-medium text-muted-foreground'>
添加免鉴权类型:
</span>
<Select
value=""
value=''
onValueChange={handleAddType}
disabled={updateWhitelistMutation.isPending || systemConfigsQuery.isPending || uploadTypesQuery.isPending}
disabled={
updateWhitelistMutation.isPending ||
systemConfigsQuery.isPending ||
uploadTypesQuery.isPending
}
>
<SelectTrigger className="w-[200px]" size="sm">
<SelectValue placeholder="选择业务类型..." />
<SelectTrigger className='w-[200px]' size='sm'>
<SelectValue placeholder='选择业务类型...' />
</SelectTrigger>
<SelectContent>
{availableTypes
.filter(t => !currentWhitelist.includes(t.value))
.map(t => (
.filter((t) => !currentWhitelist.includes(t.value))
.map((t) => (
<SelectItem key={t.value} value={t.value}>
{t.label}
</SelectItem>
))}
{availableTypes.filter(t => !currentWhitelist.includes(t.value)).length === 0 && (
<div className="text-xs text-muted-foreground p-2 text-center">所有类型已添加</div>
{availableTypes.filter(
(t) => !currentWhitelist.includes(t.value),
).length === 0 && (
<div className='text-xs text-muted-foreground p-2 text-center'>
所有类型已添加
</div>
)}
</SelectContent>
</Select>
</div>
{/* 当前白名单列表 */}
<div className="rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm space-y-3">
<div className="flex items-center gap-2">
<ShieldAlert className="size-4 text-primary" />
<span className="font-medium text-sm text-foreground">当前免鉴权列表</span>
<div className='rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm space-y-3'>
<div className='flex items-center gap-2'>
<ShieldAlert className='size-4 text-primary' />
<span className='font-medium text-sm text-foreground'>
当前免鉴权列表
</span>
</div>
{currentWhitelist.length > 0 ? (
<div className="flex flex-wrap gap-2">
{currentWhitelist.map(type => (
<div className='flex flex-wrap gap-2'>
{currentWhitelist.map((type) => (
<Badge
key={type}
variant="secondary"
className="px-2.5 py-1 text-xs gap-1.5 flex items-center bg-primary/10 text-primary dark:bg-primary/20 border border-primary/20"
variant='secondary'
className='px-2.5 py-1 text-xs gap-1.5 flex items-center bg-primary/10 text-primary dark:bg-primary/20 border border-primary/20'
>
{availableTypes.find(t => t.value === type)?.label || type}
{availableTypes.find((t) => t.value === type)?.label ||
type}
<button
type="button"
type='button'
onClick={() => handleRemoveType(type)}
disabled={updateWhitelistMutation.isPending || systemConfigsQuery.isPending}
className="rounded-full outline-hidden hover:bg-primary/20 p-0.5 text-primary cursor-pointer disabled:cursor-not-allowed"
disabled={
updateWhitelistMutation.isPending ||
systemConfigsQuery.isPending
}
className='rounded-full outline-hidden hover:bg-primary/20 p-0.5 text-primary cursor-pointer disabled:cursor-not-allowed'
>
<X className="size-3" />
<X className='size-3' />
</button>
</Badge>
))}
</div>
) : (
<p className="text-xs text-muted-foreground">
<p className='text-xs text-muted-foreground'>
白名单已空,所有类型文件的访问都将需要登录。
</p>
)}
@@ -164,5 +211,5 @@ export function OperationTab({ configs, systemConfigsQuery }: OperationTabProps)
{/* 通知模板管理 */}
<TemplatesManager />
</div>
)
);
}
+186 -91
View File
@@ -1,7 +1,7 @@
"use client"
'use client';
import {useMemo, ComponentType} from "react"
import {useMutation, useQueryClient} from "@tanstack/react-query"
import { ComponentType, useMemo } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import {
Bell,
Code,
@@ -16,170 +16,265 @@ import {
Settings,
ShieldCheck,
Terminal,
UserRound
} from "lucide-react"
UserRound,
} from 'lucide-react';
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Switch} from "@/components/ui/switch"
import services from "@/lib/services"
import type {SystemConfig} from "@/lib/services/admin"
import {toast} from "sonner"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Switch } from '@/components/ui/switch';
import services from '@/lib/services';
import type { SystemConfig } from '@/lib/services/admin';
import { toast } from 'sonner';
interface MenuItem {
path: string
label: string
description: string
icon: ComponentType<{ className?: string }>
readOnly?: boolean
path: string;
label: string;
description: string;
icon: ComponentType<{ className?: string }>;
readOnly?: boolean;
}
interface MenuGroup {
name: string
items: MenuItem[]
name: string;
items: MenuItem[];
}
const MENU_GROUPS: MenuGroup[] = [
{
name: "基础菜单",
name: '基础菜单',
items: [
{ path: "/home", label: "首页", description: "系统控制台/个人首页", icon: Home },
{ path: "/files", label: "我的文件", description: "用户个人文件管理与上传", icon: FolderOpen },
]
{
path: '/home',
label: '首页',
description: '系统控制台/个人首页',
icon: Home,
},
{
name: "管理菜单",
items: [
{ path: "/admin/users", label: "用户管理", description: "查看和管理系统用户列表及其状态", icon: UserRound },
{ path: "/admin/tasks", label: "任务管理", description: "查看和调度系统异步及定时任务", icon: Layers },
{ path: "/admin/files", label: "存储管理", description: "管理系统文件存储和清理无用文件", icon: FolderOpen },
{ path: "/admin/database", label: "数据管理", description: "监控物理数据库状态、分页浏览表数据及交互式 SQL 查询", icon: Database },
{ path: "/admin/push", label: "通知推送", description: "配置和发送系统通知及推送消息", icon: Bell },
{ path: "/admin/logs", label: "系统日志", description: "查看异步任务执行日志和系统运行情况", icon: Terminal },
{ path: "/admin/system", label: "系统配置", description: "管理和维护系统基础键值对配置", icon: ShieldCheck },
{ path: "/admin/settings", label: "系统设置", description: "配置安全验证、邮箱服务及目录显示", icon: Settings, readOnly: true },
]
path: '/files',
label: '我的文件',
description: '用户个人文件管理与上传',
icon: FolderOpen,
},
],
},
{
name: "文档菜单",
name: '管理菜单',
items: [
{ path: "/admin/demo", label: "规范示例", description: "内置 UI 组件与设计规范的展示、调试与参考", icon: Code },
{ path: "/docs/api", label: "接口文档", description: "系统 Swagger 交互式 API 接口文档", icon: CreditCard },
{ path: "/docs/how-to-use", label: "使用文档", description: "面向开发与运营的部署使用指南", icon: FileText },
]
}
]
{
path: '/admin/users',
label: '用户管理',
description: '查看和管理系统用户列表及其状态',
icon: UserRound,
},
{
path: '/admin/tasks',
label: '任务管理',
description: '查看和调度系统异步及定时任务',
icon: Layers,
},
{
path: '/admin/files',
label: '存储管理',
description: '管理系统文件存储和清理无用文件',
icon: FolderOpen,
},
{
path: '/admin/database',
label: '数据管理',
description: '监控物理数据库状态、分页浏览表数据及交互式 SQL 查询',
icon: Database,
},
{
path: '/admin/push',
label: '通知推送',
description: '配置和发送系统通知及推送消息',
icon: Bell,
},
{
path: '/admin/logs',
label: '系统日志',
description: '查看异步任务执行日志和系统运行情况',
icon: Terminal,
},
{
path: '/admin/system',
label: '系统配置',
description: '管理和维护系统基础键值对配置',
icon: ShieldCheck,
},
{
path: '/admin/settings',
label: '系统设置',
description: '配置安全验证、邮箱服务及目录显示',
icon: Settings,
readOnly: true,
},
],
},
{
name: '文档菜单',
items: [
{
path: '/admin/demo',
label: '规范示例',
description: '内置 UI 组件与设计规范的展示、调试与参考',
icon: Code,
},
{
path: '/docs/api',
label: '接口文档',
description: '系统 Swagger 交互式 API 接口文档',
icon: CreditCard,
},
{
path: '/docs/how-to-use',
label: '使用文档',
description: '面向开发与运营的部署使用指南',
icon: FileText,
},
],
},
];
interface OtherTabProps {
configs: Record<string, SystemConfig>
configs: Record<string, SystemConfig>;
}
export function OtherTab({ configs }: OtherTabProps) {
const queryClient = useQueryClient()
const queryClient = useQueryClient();
const menuDisplayConfig = useMemo(() => {
const raw = configs["menu_display_config"]?.value
if (!raw) return {} as Record<string, boolean>
const raw = configs['menu_display_config']?.value;
if (!raw) return {} as Record<string, boolean>;
try {
return JSON.parse(raw) as Record<string, boolean>
return JSON.parse(raw) as Record<string, boolean>;
} catch {
return {} as Record<string, boolean>
return {} as Record<string, boolean>;
}
}, [configs])
}, [configs]);
const updateMenuConfigMutation = useMutation({
mutationFn: async ({ path, enabled }: { path: string; enabled: boolean }) => {
const newConfig = { ...menuDisplayConfig, [path]: enabled }
const currentCfg = configs["menu_display_config"]
await services.adminSystemConfig.updateSystemConfig("menu_display_config", {
mutationFn: async ({
path,
enabled,
}: {
path: string;
enabled: boolean;
}) => {
const newConfig = { ...menuDisplayConfig, [path]: enabled };
const currentCfg = configs['menu_display_config'];
await services.adminSystemConfig.updateSystemConfig(
'menu_display_config',
{
value: JSON.stringify(newConfig),
description: currentCfg?.description || "目录显示配置(JSON 字符串,格式为 {url: enabled})",
})
description:
currentCfg?.description ||
'目录显示配置(JSON 字符串,格式为 {url: enabled})',
},
);
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] })
await queryClient.invalidateQueries({ queryKey: ["public-config"] })
toast.success("目录显示配置已更新")
await queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
});
await queryClient.invalidateQueries({ queryKey: ['public-config'] });
toast.success('目录显示配置已更新');
},
onError: (error: Error) => {
toast.error(error.message || "更新配置失败")
toast.error(error.message || '更新配置失败');
},
})
});
const handleMenuToggle = (path: string, checked: boolean) => {
updateMenuConfigMutation.mutate({ path, enabled: checked })
}
updateMenuConfigMutation.mutate({ path, enabled: checked });
};
return (
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<div className="flex items-center gap-2">
<div className="p-1.5 rounded-lg bg-primary/10 text-primary">
<LayoutList className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<div className='flex items-center gap-2'>
<div className='p-1.5 rounded-lg bg-primary/10 text-primary'>
<LayoutList className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">目录显示管理</CardTitle>
<CardDescription className="text-xs">
<CardTitle className='text-base font-semibold'>
目录显示管理
</CardTitle>
<CardDescription className='text-xs'>
配置系统左侧菜单的显示与隐藏状态,适用于所有登录用户。
</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="pt-6 space-y-6">
<CardContent className='pt-6 space-y-6'>
{MENU_GROUPS.map((group) => (
<div key={group.name} className="space-y-3">
<div className="flex items-center gap-2">
<span className="text-xs font-semibold text-muted-foreground tracking-wider uppercase">
<div key={group.name} className='space-y-3'>
<div className='flex items-center gap-2'>
<span className='text-xs font-semibold text-muted-foreground tracking-wider uppercase'>
{group.name}
</span>
<div className="h-px bg-border/40 flex-1" />
<div className='h-px bg-border/40 flex-1' />
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className='grid grid-cols-1 md:grid-cols-2 gap-4'>
{group.items.map((item) => {
const Icon = item.icon
const isReadOnly = !!item.readOnly
const checked = menuDisplayConfig[item.path] !== false
const Icon = item.icon;
const isReadOnly = !!item.readOnly;
const checked = menuDisplayConfig[item.path] !== false;
return (
<div
key={item.path}
className="flex items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm"
className='flex items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm'
>
<div className="space-y-1.5 flex-1 min-w-0 pr-2">
<div className="flex items-center gap-2">
{Icon && <Icon className="size-4 text-primary shrink-0" />}
<span className="font-medium text-sm text-foreground truncate">{item.label}</span>
<div className='space-y-1.5 flex-1 min-w-0 pr-2'>
<div className='flex items-center gap-2'>
{Icon && (
<Icon className='size-4 text-primary shrink-0' />
)}
<span className='font-medium text-sm text-foreground truncate'>
{item.label}
</span>
{isReadOnly && (
<span className="text-[9px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground border shrink-0">
<span className='text-[9px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground border shrink-0'>
不可隐藏
</span>
)}
</div>
<p className="text-xs text-muted-foreground leading-normal line-clamp-2">
<p className='text-xs text-muted-foreground leading-normal line-clamp-2'>
{item.description}
</p>
</div>
<div className="flex items-center">
<div className='flex items-center'>
<Switch
checked={checked}
disabled={isReadOnly || updateMenuConfigMutation.isPending}
onCheckedChange={(val) => handleMenuToggle(item.path, val)}
disabled={
isReadOnly || updateMenuConfigMutation.isPending
}
onCheckedChange={(val) =>
handleMenuToggle(item.path, val)
}
/>
</div>
</div>
)
);
})}
</div>
</div>
))}
<div className="p-3.5 rounded-lg border border-dashed border-primary/20 bg-primary/5 flex items-start gap-2.5">
<Info className="size-4 text-primary shrink-0 mt-0.5" />
<div className="text-xs text-muted-foreground leading-relaxed">
<span className="font-semibold text-foreground">安全提示:</span>
<div className='p-3.5 rounded-lg border border-dashed border-primary/20 bg-primary/5 flex items-start gap-2.5'>
<Info className='size-4 text-primary shrink-0 mt-0.5' />
<div className='text-xs text-muted-foreground leading-relaxed'>
<span className='font-semibold text-foreground'>安全提示:</span>
为了防止管理员在关闭“系统设置”后导致无法重新访问此配置页,系统限制了“系统设置”的关闭权限。其它所有菜单均可自由开关,隐藏后对应的分组标题在为空时也会自动隐藏。
</div>
</div>
</CardContent>
</Card>
)
);
}
File diff suppressed because it is too large Load Diff
@@ -1,7 +1,12 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {useMutation, useQuery, useQueryClient, type UseQueryResult} from "@tanstack/react-query"
import { useEffect, useState } from 'react';
import {
useMutation,
useQuery,
useQueryClient,
type UseQueryResult,
} from '@tanstack/react-query';
import {
Clock,
Fingerprint,
@@ -14,263 +19,309 @@ import {
Settings,
Shield,
Trash2,
UserPlus
} from "lucide-react"
UserPlus,
} from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Switch} from "@/components/ui/switch"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Select, SelectContent, SelectItem, SelectTrigger, SelectValue} from "@/components/ui/select"
import {AuthSourceModal} from "@/components/common/settings/auth-source-modal"
import services from "@/lib/services"
import type {AuthSource, SystemConfig} from "@/lib/services/admin"
import {toast} from "sonner"
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Switch } from '@/components/ui/switch';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { AuthSourceModal } from '@/components/common/settings/auth-source-modal';
import services from '@/lib/services';
import type { AuthSource, SystemConfig } from '@/lib/services/admin';
import { toast } from 'sonner';
const SECURITY_KEYS = [
{
key: "password_login_enabled",
title: "允许密码登录",
description: "关闭后仅保留第三方 OIDC 认证源进行系统登录。",
key: 'password_login_enabled',
title: '允许密码登录',
description: '关闭后仅保留第三方 OIDC 认证源进行系统登录。',
icon: Lock,
},
{
key: "registration_enabled",
title: "允许注册",
description: "关闭后系统将禁止新用户进行自主账号注册。",
key: 'registration_enabled',
title: '允许注册',
description: '关闭后系统将禁止新用户进行自主账号注册。',
icon: UserPlus,
},
{
key: "password_register_enabled",
title: "允许密码注册",
description: "关闭后只能通过管理员创建或第三方认证关联建号。",
key: 'password_register_enabled',
title: '允许密码注册',
description: '关闭后只能通过管理员创建或第三方认证关联建号。',
icon: Fingerprint,
},
{
key: "oidc_login_enabled",
title: "允许 OIDC 登录",
description: "关闭后所有的第三方 OIDC 认证登录入口都会被隐藏。",
key: 'oidc_login_enabled',
title: '允许 OIDC 登录',
description: '关闭后所有的第三方 OIDC 认证登录入口都会被隐藏。',
icon: Globe,
},
{
key: "email_login_verification_enabled",
title: "邮箱登录验证",
description: "开启后,使用账号密码登录时需要通过邮箱接收并验证 6 位验证码。",
key: 'email_login_verification_enabled',
title: '邮箱登录验证',
description:
'开启后,使用账号密码登录时需要通过邮箱接收并验证 6 位验证码。',
icon: Mail,
},
{
key: "email_register_verification_enabled",
title: "邮箱注册验证",
description: "开启后,用户注册账号时需要通过邮箱接收并验证 6 位验证码。",
key: 'email_register_verification_enabled',
title: '邮箱注册验证',
description: '开启后,用户注册账号时需要通过邮箱接收并验证 6 位验证码。',
icon: Mail,
},
] as const
] as const;
interface SecurityTabProps {
configs: Record<string, SystemConfig>
systemConfigsQuery: UseQueryResult<SystemConfig[], Error>
configs: Record<string, SystemConfig>;
systemConfigsQuery: UseQueryResult<SystemConfig[], Error>;
}
export function SecurityTab({ configs, systemConfigsQuery }: SecurityTabProps) {
const queryClient = useQueryClient()
const [authSourceModalOpen, setAuthSourceModalOpen] = useState(false)
const [selectedSource, setSelectedSource] = useState<AuthSource | null>(null)
const queryClient = useQueryClient();
const [authSourceModalOpen, setAuthSourceModalOpen] = useState(false);
const [selectedSource, setSelectedSource] = useState<AuthSource | null>(null);
const [capCount, setCapCount] = useState("")
const [capDifficulty, setCapDifficulty] = useState("")
const [capSize, setCapSize] = useState("")
const [capTTL, setCapTTL] = useState("")
const [capTokenTTL, setCapTokenTTL] = useState("")
const [capAutoSolve, setCapAutoSolve] = useState(true)
const [capCount, setCapCount] = useState('');
const [capDifficulty, setCapDifficulty] = useState('');
const [capSize, setCapSize] = useState('');
const [capTTL, setCapTTL] = useState('');
const [capTokenTTL, setCapTokenTTL] = useState('');
const [capAutoSolve, setCapAutoSolve] = useState(true);
const [sessionTTL, setSessionTTL] = useState("168")
const [customHours, setCustomHours] = useState("")
const [sessionTTL, setSessionTTL] = useState('168');
const [customHours, setCustomHours] = useState('');
const authSourcesQuery = useQuery({
queryKey: ["auth", "sources"],
queryKey: ['auth', 'sources'],
queryFn: () => services.adminAuthSource.listAuthSources(),
})
});
useEffect(() => {
if (systemConfigsQuery.data) {
const cfgMap = configs
setCapCount(cfgMap["cap_challenge_count"]?.value || "1")
setCapDifficulty(cfgMap["cap_challenge_difficulty"]?.value || "4")
setCapSize(cfgMap["cap_challenge_size"]?.value || "32")
setCapTTL(cfgMap["cap_challenge_ttl_seconds"]?.value || "600")
setCapTokenTTL(cfgMap["cap_token_ttl_seconds"]?.value || "1200")
setCapAutoSolve(cfgMap["cap_auto_solve"]?.value !== "false")
const cfgMap = configs;
setCapCount(cfgMap['cap_challenge_count']?.value || '1');
setCapDifficulty(cfgMap['cap_challenge_difficulty']?.value || '4');
setCapSize(cfgMap['cap_challenge_size']?.value || '32');
setCapTTL(cfgMap['cap_challenge_ttl_seconds']?.value || '600');
setCapTokenTTL(cfgMap['cap_token_ttl_seconds']?.value || '1200');
setCapAutoSolve(cfgMap['cap_auto_solve']?.value !== 'false');
// 初始化登录保持设置
const ttlVal = cfgMap["login_session_ttl_hours"]?.value || "0"
if (ttlVal === "0" || ttlVal === "168" || ttlVal === "720" || ttlVal === "-1") {
setSessionTTL(ttlVal)
setCustomHours("")
const ttlVal = cfgMap['login_session_ttl_hours']?.value || '0';
if (
ttlVal === '0' ||
ttlVal === '168' ||
ttlVal === '720' ||
ttlVal === '-1'
) {
setSessionTTL(ttlVal);
setCustomHours('');
} else {
setSessionTTL("custom")
setCustomHours(ttlVal)
setSessionTTL('custom');
setCustomHours(ttlVal);
}
}
}, [systemConfigsQuery.data, configs])
}, [systemConfigsQuery.data, configs]);
const updateTTLMutation = useMutation({
mutationFn: async (value: string) => {
const config = configs["login_session_ttl_hours"]
const config = configs['login_session_ttl_hours'];
if (!config) {
throw new Error("缺少配置项: login_session_ttl_hours")
throw new Error('缺少配置项: login_session_ttl_hours');
}
await services.adminSystemConfig.updateSystemConfig("login_session_ttl_hours", {
await services.adminSystemConfig.updateSystemConfig(
'login_session_ttl_hours',
{
value: value,
description: config.description,
})
},
);
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] })
toast.success("登录状态保持时间已更新")
await queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
});
toast.success('登录状态保持时间已更新');
},
onError: (error: Error) => {
toast.error(error.message || "更新配置失败")
toast.error(error.message || '更新配置失败');
},
})
});
const handleTTLChange = (val: string) => {
setSessionTTL(val)
if (val !== "custom") {
updateTTLMutation.mutate(val)
}
setSessionTTL(val);
if (val !== 'custom') {
updateTTLMutation.mutate(val);
}
};
const handleCustomBlur = () => {
const parsed = parseInt(customHours, 10)
const parsed = parseInt(customHours, 10);
if (isNaN(parsed) || parsed <= 0) {
toast.error("请输入有效的过期小时数(正整数)")
toast.error('请输入有效的过期小时数(正整数)');
// 重置为原本的值
const originalVal = configs["login_session_ttl_hours"]?.value || "0"
setCustomHours(originalVal === "custom" || ["0", "168", "720", "-1"].includes(originalVal) ? "" : originalVal)
return
}
updateTTLMutation.mutate(parsed.toString())
const originalVal = configs['login_session_ttl_hours']?.value || '0';
setCustomHours(
originalVal === 'custom' ||
['0', '168', '720', '-1'].includes(originalVal)
? ''
: originalVal,
);
return;
}
updateTTLMutation.mutate(parsed.toString());
};
const updateConfigMutation = useMutation({
mutationFn: async ({ key, value }: { key: string; value: boolean }) => {
const config = configs[key]
const config = configs[key];
if (!config) {
throw new Error(`缺少配置项: ${key}`)
throw new Error(`缺少配置项: ${key}`);
}
await services.adminSystemConfig.updateSystemConfig(key, {
value: value ? "true" : "false",
value: value ? 'true' : 'false',
description: config.description,
})
});
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] })
await queryClient.invalidateQueries({ queryKey: ["public-config"] })
toast.success("系统安全配置已更新")
await queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
});
await queryClient.invalidateQueries({ queryKey: ['public-config'] });
toast.success('系统安全配置已更新');
},
onError: (error: Error) => {
toast.error(error.message || "更新配置失败")
toast.error(error.message || '更新配置失败');
},
})
});
const toggleSourceMutation = useMutation({
mutationFn: async (source: AuthSource) => {
await services.adminAuthSource.toggleAuthSource(source.id, { is_active: !source.is_active })
await services.adminAuthSource.toggleAuthSource(source.id, {
is_active: !source.is_active,
});
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["auth", "sources"] })
await queryClient.invalidateQueries({ queryKey: ["auth", "public-sources"] })
toast.success("认证源状态已更新")
await queryClient.invalidateQueries({ queryKey: ['auth', 'sources'] });
await queryClient.invalidateQueries({
queryKey: ['auth', 'public-sources'],
});
toast.success('认证源状态已更新');
},
onError: (error: Error) => {
toast.error(error.message || "切换状态失败")
toast.error(error.message || '切换状态失败');
},
})
});
const deleteSourceMutation = useMutation({
mutationFn: async (sourceId: string) => {
await services.adminAuthSource.deleteAuthSource(sourceId)
await services.adminAuthSource.deleteAuthSource(sourceId);
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["auth", "sources"] })
await queryClient.invalidateQueries({ queryKey: ["auth", "public-sources"] })
toast.success("认证源已删除")
await queryClient.invalidateQueries({ queryKey: ['auth', 'sources'] });
await queryClient.invalidateQueries({
queryKey: ['auth', 'public-sources'],
});
toast.success('认证源已删除');
},
onError: (error: Error) => {
toast.error(error.message || "删除认证源失败")
toast.error(error.message || '删除认证源失败');
},
})
});
const handleToggle = (key: string, checked: boolean) => {
updateConfigMutation.mutate({ key, value: checked })
}
updateConfigMutation.mutate({ key, value: checked });
};
const saveCapMutation = useMutation({
mutationFn: async () => {
const updates = [
{ key: "cap_challenge_count", value: capCount },
{ key: "cap_challenge_difficulty", value: capDifficulty },
{ key: "cap_challenge_size", value: capSize },
{ key: "cap_challenge_ttl_seconds", value: capTTL },
{ key: "cap_token_ttl_seconds", value: capTokenTTL },
{ key: "cap_auto_solve", value: capAutoSolve ? "true" : "false" },
]
{ key: 'cap_challenge_count', value: capCount },
{ key: 'cap_challenge_difficulty', value: capDifficulty },
{ key: 'cap_challenge_size', value: capSize },
{ key: 'cap_challenge_ttl_seconds', value: capTTL },
{ key: 'cap_token_ttl_seconds', value: capTokenTTL },
{ key: 'cap_auto_solve', value: capAutoSolve ? 'true' : 'false' },
];
for (const update of updates) {
const currentCfg = configs[update.key]
const currentCfg = configs[update.key];
await services.adminSystemConfig.updateSystemConfig(update.key, {
value: update.value,
description: currentCfg?.description || "",
})
description: currentCfg?.description || '',
});
}
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] })
toast.success("人机验证配置已成功保存")
await queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
});
toast.success('人机验证配置已成功保存');
},
onError: (error: Error) => {
toast.error(error.message || "保存配置失败")
toast.error(error.message || '保存配置失败');
},
})
});
const handleCapSave = (e: React.FormEvent) => {
e.preventDefault()
saveCapMutation.mutate()
}
e.preventDefault();
saveCapMutation.mutate();
};
return (
<div className="space-y-6">
<div className='space-y-6'>
{/* 系统登录与注册控制 */}
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<div className="flex items-center gap-2">
<div className="p-1.5 rounded-lg bg-primary/10 text-primary">
<Settings className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<div className='flex items-center gap-2'>
<div className='p-1.5 rounded-lg bg-primary/10 text-primary'>
<Settings className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">系统登录/注册设置</CardTitle>
<CardDescription className="text-xs">配置系统的登录限制与用户自主注册权限</CardDescription>
<CardTitle className='text-base font-semibold'>
系统登录/注册设置
</CardTitle>
<CardDescription className='text-xs'>
配置系统的登录限制与用户自主注册权限
</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="pt-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<CardContent className='pt-6'>
<div className='grid grid-cols-1 md:grid-cols-2 gap-4'>
{SECURITY_KEYS.map((item) => {
const config = configs[item.key]
const checked = config ? config.value === "true" : false
const Icon = item.icon
const config = configs[item.key];
const checked = config ? config.value === 'true' : false;
const Icon = item.icon;
return (
<div
key={item.key}
className="flex items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm"
className='flex items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm'
>
<div className="space-y-1">
<div className="flex items-center gap-2">
{Icon && <Icon className="size-4 text-primary" />}
<span className="font-medium text-sm text-foreground">{item.title}</span>
<div className='space-y-1'>
<div className='flex items-center gap-2'>
{Icon && <Icon className='size-4 text-primary' />}
<span className='font-medium text-sm text-foreground'>
{item.title}
</span>
</div>
<p className="text-xs text-muted-foreground leading-relaxed pr-2">{item.description}</p>
<p className='text-xs text-muted-foreground leading-relaxed pr-2'>
{item.description}
</p>
</div>
<Switch
checked={checked}
@@ -278,56 +329,56 @@ export function SecurityTab({ configs, systemConfigsQuery }: SecurityTabProps) {
onCheckedChange={(value) => handleToggle(item.key, value)}
/>
</div>
)
);
})}
{/* 登录状态保持时间 (选择后立即更改) */}
<div
className="flex items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm md:col-span-2"
>
<div className="space-y-1 pr-4">
<div className="flex items-center gap-2">
<Clock className="size-4 text-primary" />
<span className="font-medium text-sm text-foreground">登录状态保持时间</span>
<div className='flex items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm md:col-span-2'>
<div className='space-y-1 pr-4'>
<div className='flex items-center gap-2'>
<Clock className='size-4 text-primary' />
<span className='font-medium text-sm text-foreground'>
登录状态保持时间
</span>
</div>
<p className="text-xs text-muted-foreground leading-relaxed pr-2">
<p className='text-xs text-muted-foreground leading-relaxed pr-2'>
配置用户登录会话在浏览器中的保持期限。设置为“关闭”则在浏览器关闭后自动退登。
</p>
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<div className='flex items-center gap-2 flex-shrink-0'>
<Select
value={sessionTTL}
disabled={updateTTLMutation.isPending}
onValueChange={handleTTLChange}
>
<SelectTrigger className="w-[180px] bg-card border-dashed text-xs h-8">
<SelectValue placeholder="选择保留时间" />
<SelectTrigger className='w-[180px] bg-card border-dashed text-xs h-8'>
<SelectValue placeholder='选择保留时间' />
</SelectTrigger>
<SelectContent>
<SelectItem value="0">关闭 (浏览器关闭自动退登)</SelectItem>
<SelectItem value="168">7 天</SelectItem>
<SelectItem value="720">30 天</SelectItem>
<SelectItem value="-1">永不过期</SelectItem>
<SelectItem value="custom">自定义时长</SelectItem>
<SelectItem value='0'>关闭 (浏览器关闭自动退登)</SelectItem>
<SelectItem value='168'>7 天</SelectItem>
<SelectItem value='720'>30 天</SelectItem>
<SelectItem value='-1'>永不过期</SelectItem>
<SelectItem value='custom'>自定义时长</SelectItem>
</SelectContent>
</Select>
{sessionTTL === "custom" && (
{sessionTTL === 'custom' && (
<Input
type="number"
type='number'
min={1}
value={customHours}
onChange={(e) => setCustomHours(e.target.value)}
onBlur={handleCustomBlur}
onKeyDown={(e) => {
if (e.key === "Enter") {
handleCustomBlur()
if (e.key === 'Enter') {
handleCustomBlur();
}
}}
placeholder="小时"
placeholder='小时'
disabled={updateTTLMutation.isPending}
className="w-20 bg-card border-dashed text-xs h-8 px-2"
className='w-20 bg-card border-dashed text-xs h-8 px-2'
/>
)}
</div>
@@ -337,116 +388,134 @@ export function SecurityTab({ configs, systemConfigsQuery }: SecurityTabProps) {
</Card>
{/* 认证源配置管理 */}
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4 flex flex-row items-center justify-between gap-4">
<div className="flex items-center gap-2">
<div className="p-1.5 rounded-lg bg-primary/10 text-primary">
<Globe className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4 flex flex-row items-center justify-between gap-4'>
<div className='flex items-center gap-2'>
<div className='p-1.5 rounded-lg bg-primary/10 text-primary'>
<Globe className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">认证源管理</CardTitle>
<CardDescription className="text-xs">添加、修改并启用系统自定义的 OIDC 认证源</CardDescription>
<CardTitle className='text-base font-semibold'>
认证源管理
</CardTitle>
<CardDescription className='text-xs'>
添加、修改并启用系统自定义的 OIDC 认证源
</CardDescription>
</div>
</div>
<Button
type="button"
size="sm"
type='button'
size='sm'
onClick={() => {
setSelectedSource(null)
setAuthSourceModalOpen(true)
setSelectedSource(null);
setAuthSourceModalOpen(true);
}}
variant="secondary"
variant='secondary'
>
<Plus className="mr-1.5 size-3.5" />
<Plus className='mr-1.5 size-3.5' />
新增认证源
</Button>
</CardHeader>
<CardContent className="pt-6 space-y-3">
<CardContent className='pt-6 space-y-3'>
{authSourcesQuery.isPending ? (
<div className="flex items-center justify-center p-8">
<Loader2 className="size-6 animate-spin text-muted-foreground/50" />
<div className='flex items-center justify-center p-8'>
<Loader2 className='size-6 animate-spin text-muted-foreground/50' />
</div>
) : (authSourcesQuery.data ?? []).length > 0 ? (
(authSourcesQuery.data ?? []).map((source) => (
<div
key={source.id}
className="flex items-center justify-between rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 transition-all duration-300 shadow-sm"
className='flex items-center justify-between rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 transition-all duration-300 shadow-sm'
>
<div className="space-y-1.5">
<div className="flex items-center gap-2">
<span className="font-semibold text-sm text-foreground">{source.display_name || source.name}</span>
<span className={`text-[10px] px-2 py-0.5 rounded-full border font-medium ${
<div className='space-y-1.5'>
<div className='flex items-center gap-2'>
<span className='font-semibold text-sm text-foreground'>
{source.display_name || source.name}
</span>
<span
className={`text-[10px] px-2 py-0.5 rounded-full border font-medium ${
source.is_active
? "bg-emerald-500/10 text-emerald-500 border-emerald-500/20"
: "bg-amber-500/10 text-amber-500 border-amber-500/20"
}`}>
{source.is_active ? "已启用" : "已禁用"}
? 'bg-emerald-500/10 text-emerald-500 border-emerald-500/20'
: 'bg-amber-500/10 text-amber-500 border-amber-500/20'
}`}
>
{source.is_active ? '已启用' : '已禁用'}
</span>
</div>
<div className="text-xs text-muted-foreground font-mono">
<div className='text-xs text-muted-foreground font-mono'>
标识符: {source.name} · 类型: {source.type.toUpperCase()}
</div>
</div>
<div className="flex items-center gap-4">
<span className={`text-xs px-2.5 py-1 rounded-lg border font-medium hidden sm:inline-block ${
<div className='flex items-center gap-4'>
<span
className={`text-xs px-2.5 py-1 rounded-lg border font-medium hidden sm:inline-block ${
source.client_secret_configured
? "bg-primary/5 text-primary border-primary/10"
: "bg-rose-500/5 text-rose-500 border-rose-500/10"
}`}>
{source.client_secret_configured ? "Secret 已配置" : "Secret 未配置"}
? 'bg-primary/5 text-primary border-primary/10'
: 'bg-rose-500/5 text-rose-500 border-rose-500/10'
}`}
>
{source.client_secret_configured
? 'Secret 已配置'
: 'Secret 未配置'}
</span>
<div className="flex items-center gap-2">
<div className='flex items-center gap-2'>
<Switch
checked={source.is_active}
disabled={toggleSourceMutation.isPending}
className="scale-90 mr-2"
onCheckedChange={() => toggleSourceMutation.mutate(source)}
className='scale-90 mr-2'
onCheckedChange={() =>
toggleSourceMutation.mutate(source)
}
/>
<Button
type="button"
variant="ghost"
size="icon"
className="size-8 text-muted-foreground hover:text-primary hover:bg-primary/10 rounded-lg transition-colors"
type='button'
variant='ghost'
size='icon'
className='size-8 text-muted-foreground hover:text-primary hover:bg-primary/10 rounded-lg transition-colors'
onClick={() => {
setSelectedSource(source)
setAuthSourceModalOpen(true)
setSelectedSource(source);
setAuthSourceModalOpen(true);
}}
>
<Pencil className="size-4" />
<Pencil className='size-4' />
</Button>
<Button
type="button"
variant="ghost"
size="icon"
className="size-8 text-muted-foreground hover:text-rose-500 hover:bg-rose-500/10 rounded-lg transition-colors"
type='button'
variant='ghost'
size='icon'
className='size-8 text-muted-foreground hover:text-rose-500 hover:bg-rose-500/10 rounded-lg transition-colors'
disabled={deleteSourceMutation.isPending}
onClick={() => {
if (window.confirm(`确定删除认证源「${source.display_name || source.name}」吗?`)) {
deleteSourceMutation.mutate(source.id)
if (
window.confirm(
`确定删除认证源「${source.display_name || source.name}」吗?`,
)
) {
deleteSourceMutation.mutate(source.id);
}
}}
>
<Trash2 className="size-4" />
<Trash2 className='size-4' />
</Button>
</div>
</div>
</div>
))
) : (
<div className="rounded-xl border border-dashed border-border/50 px-4 py-8 text-center text-xs text-muted-foreground bg-muted/5 flex flex-col items-center justify-center gap-3">
<div className='rounded-xl border border-dashed border-border/50 px-4 py-8 text-center text-xs text-muted-foreground bg-muted/5 flex flex-col items-center justify-center gap-3'>
<span>暂无配置的认证源,点击上方按钮新增</span>
<Button
type="button"
size="sm"
variant="outline"
type='button'
size='sm'
variant='outline'
onClick={() => {
setSelectedSource(null)
setAuthSourceModalOpen(true)
setSelectedSource(null);
setAuthSourceModalOpen(true);
}}
className="border-dashed"
className='border-dashed'
>
<Plus className="mr-1.5 size-3.5" />
<Plus className='mr-1.5 size-3.5' />
新增认证源
</Button>
</div>
@@ -455,123 +524,163 @@ export function SecurityTab({ configs, systemConfigsQuery }: SecurityTabProps) {
</Card>
{/* 人机验证配置 (Cap CAPTCHA) */}
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4 flex flex-row items-center justify-between gap-4">
<div className="flex items-center gap-2">
<div className="p-1.5 rounded-lg bg-primary/10 text-primary">
<Shield className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4 flex flex-row items-center justify-between gap-4'>
<div className='flex items-center gap-2'>
<div className='p-1.5 rounded-lg bg-primary/10 text-primary'>
<Shield className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">人机验证配置 (Cap CAPTCHA)</CardTitle>
<CardDescription className="text-xs">配置基于 Proof-of-Work (PoW) 的无感人机验证,保护系统登录免受暴力破解和撞库攻击</CardDescription>
<CardTitle className='text-base font-semibold'>
人机验证配置 (Cap CAPTCHA)
</CardTitle>
<CardDescription className='text-xs'>
配置基于 Proof-of-Work (PoW)
的无感人机验证,保护系统登录免受暴力破解和撞库攻击
</CardDescription>
</div>
</div>
<Switch
checked={configs["cap_login_enabled"]?.value === "true"}
checked={configs['cap_login_enabled']?.value === 'true'}
disabled={updateConfigMutation.isPending}
onCheckedChange={(checked) => handleToggle("cap_login_enabled", checked)}
onCheckedChange={(checked) =>
handleToggle('cap_login_enabled', checked)
}
/>
</CardHeader>
<CardContent className="pt-6">
<CardContent className='pt-6'>
{/* 自动开始计算 Switch */}
<div className="flex items-center justify-between rounded-xl border border-dashed p-4 bg-card mb-4">
<div className="space-y-0.5">
<p className="text-sm font-semibold">打开页面后自动开始计算</p>
<div className='flex items-center justify-between rounded-xl border border-dashed p-4 bg-card mb-4'>
<div className='space-y-0.5'>
<p className='text-sm font-semibold'>打开页面后自动开始计算</p>
</div>
<Switch
checked={capAutoSolve}
onCheckedChange={setCapAutoSolve}
/>
<Switch checked={capAutoSolve} onCheckedChange={setCapAutoSolve} />
</div>
<form onSubmit={handleCapSave} className="space-y-6">
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-1.5">
<Label htmlFor="cap_challenge_count" className="text-xs font-semibold">难题数量 (Count)</Label>
<form onSubmit={handleCapSave} className='space-y-6'>
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
<div className='space-y-1.5'>
<Label
htmlFor='cap_challenge_count'
className='text-xs font-semibold'
>
难题数量 (Count)
</Label>
<Input
id="cap_challenge_count"
type="number"
id='cap_challenge_count'
type='number'
min={1}
max={100}
value={capCount}
onChange={(e) => setCapCount(e.target.value)}
placeholder="50"
className="bg-card border-dashed text-xs"
placeholder='50'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">客户端需求解的难题总数。默认 1,推荐 1 至 5</p>
<p className='text-[10px] text-muted-foreground leading-normal'>
客户端需求解的难题总数。默认 1,推荐 1 至 5
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="cap_challenge_difficulty" className="text-xs font-semibold">验证难度 (Difficulty)</Label>
<div className='space-y-1.5'>
<Label
htmlFor='cap_challenge_difficulty'
className='text-xs font-semibold'
>
验证难度 (Difficulty)
</Label>
<Input
id="cap_challenge_difficulty"
type="number"
id='cap_challenge_difficulty'
type='number'
min={1}
max={10}
value={capDifficulty}
onChange={(e) => setCapDifficulty(e.target.value)}
placeholder="4"
className="bg-card border-dashed text-xs"
placeholder='4'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">PoW 前缀哈希位数,每加 1 计算时间翻倍。默认 4,推荐 4</p>
<p className='text-[10px] text-muted-foreground leading-normal'>
PoW 前缀哈希位数,每加 1 计算时间翻倍。默认 4,推荐 4
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="cap_challenge_size" className="text-xs font-semibold">盐值长度 (Size)</Label>
<div className='space-y-1.5'>
<Label
htmlFor='cap_challenge_size'
className='text-xs font-semibold'
>
盐值长度 (Size)
</Label>
<Input
id="cap_challenge_size"
type="number"
id='cap_challenge_size'
type='number'
min={8}
max={64}
value={capSize}
onChange={(e) => setCapSize(e.target.value)}
placeholder="32"
className="bg-card border-dashed text-xs"
placeholder='32'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">难题盐值混淆字符长度。默认 32</p>
<p className='text-[10px] text-muted-foreground leading-normal'>
难题盐值混淆字符长度。默认 32
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="cap_challenge_ttl" className="text-xs font-semibold">难题超时时长 (秒)</Label>
<div className='space-y-1.5'>
<Label
htmlFor='cap_challenge_ttl'
className='text-xs font-semibold'
>
难题超时时长 (秒)
</Label>
<Input
id="cap_challenge_ttl"
type="number"
id='cap_challenge_ttl'
type='number'
min={10}
value={capTTL}
onChange={(e) => setCapTTL(e.target.value)}
placeholder="600"
className="bg-card border-dashed text-xs"
placeholder='600'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">难题有效期限。默认 600 秒 (10 分钟)</p>
<p className='text-[10px] text-muted-foreground leading-normal'>
难题有效期限。默认 600 秒 (10 分钟)
</p>
</div>
<div className="space-y-1.5 sm:col-span-2">
<Label htmlFor="cap_token_ttl" className="text-xs font-semibold">验证凭证有效时长 (秒)</Label>
<div className='space-y-1.5 sm:col-span-2'>
<Label
htmlFor='cap_token_ttl'
className='text-xs font-semibold'
>
验证凭证有效时长 (秒)
</Label>
<Input
id="cap_token_ttl"
type="number"
id='cap_token_ttl'
type='number'
min={10}
value={capTokenTTL}
onChange={(e) => setCapTokenTTL(e.target.value)}
placeholder="1200"
className="bg-card border-dashed text-xs"
placeholder='1200'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">PoW 计算求解通过后,签发的登录凭证有效时长。默认 1200 秒 (20 分钟)</p>
<p className='text-[10px] text-muted-foreground leading-normal'>
PoW 计算求解通过后,签发的登录凭证有效时长。默认 1200 秒 (20
分钟)
</p>
</div>
</div>
<div className="flex justify-end pt-4 border-t border-dashed">
<div className='flex justify-end pt-4 border-t border-dashed'>
<Button
type="submit"
size="sm"
type='submit'
size='sm'
disabled={saveCapMutation.isPending}
>
{saveCapMutation.isPending ? (
<>
<Loader2 className="mr-1.5 size-3.5 animate-spin" />
<Loader2 className='mr-1.5 size-3.5 animate-spin' />
保存中...
</>
) : (
"保存配置"
'保存配置'
)}
</Button>
</div>
@@ -584,11 +693,15 @@ export function SecurityTab({ configs, systemConfigsQuery }: SecurityTabProps) {
source={selectedSource}
onClose={() => setAuthSourceModalOpen(false)}
onChanged={async () => {
await queryClient.invalidateQueries({ queryKey: ["auth", "sources"] })
await queryClient.invalidateQueries({ queryKey: ["auth", "public-sources"] })
await authSourcesQuery.refetch()
await queryClient.invalidateQueries({
queryKey: ['auth', 'sources'],
});
await queryClient.invalidateQueries({
queryKey: ['auth', 'public-sources'],
});
await authSourcesQuery.refetch();
}}
/>
</div>
)
);
}
+296 -227
View File
@@ -1,152 +1,178 @@
"use client"
'use client';
import {useEffect, useState} from "react"
import {useMutation, useQueryClient, type UseQueryResult} from "@tanstack/react-query"
import {Globe, Loader2, Mail, Search, Server, Sparkles} from "lucide-react"
import { useEffect, useState } from 'react';
import {
useMutation,
useQueryClient,
type UseQueryResult,
} from '@tanstack/react-query';
import { Globe, Loader2, Mail, Search, Server, Sparkles } from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Switch} from "@/components/ui/switch"
import {Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import {Badge} from "@/components/ui/badge"
import services from "@/lib/services"
import type {SystemConfig} from "@/lib/services/admin"
import {toast} from "sonner"
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import { Badge } from '@/components/ui/badge';
import services from '@/lib/services';
import type { SystemConfig } from '@/lib/services/admin';
import { toast } from 'sonner';
interface SystemTabProps {
configs: Record<string, SystemConfig>
systemConfigsQuery: UseQueryResult<SystemConfig[], Error>
configs: Record<string, SystemConfig>;
systemConfigsQuery: UseQueryResult<SystemConfig[], Error>;
}
export function SystemTab({ configs, systemConfigsQuery }: SystemTabProps) {
const queryClient = useQueryClient()
const [serverAddress, setServerAddress] = useState("")
const [siteName, setSiteName] = useState("")
const [smtpHost, setSmtpHost] = useState("")
const [smtpPort, setSmtpPort] = useState("")
const [smtpUsername, setSmtpUsername] = useState("")
const [smtpPassword, setSmtpPassword] = useState("")
const [smtpTestOpen, setSmtpTestOpen] = useState(false)
const [smtpTestTo, setSmtpTestTo] = useState("")
const [smtpTestLog, setSmtpTestLog] = useState("")
const [smtpTestSuccess, setSmtpTestSuccess] = useState<boolean | null>(null)
const [smtpTestError, setSmtpTestError] = useState("")
const queryClient = useQueryClient();
const [serverAddress, setServerAddress] = useState('');
const [siteName, setSiteName] = useState('');
const [smtpHost, setSmtpHost] = useState('');
const [smtpPort, setSmtpPort] = useState('');
const [smtpUsername, setSmtpUsername] = useState('');
const [smtpPassword, setSmtpPassword] = useState('');
const [smtpTestOpen, setSmtpTestOpen] = useState(false);
const [smtpTestTo, setSmtpTestTo] = useState('');
const [smtpTestLog, setSmtpTestLog] = useState('');
const [smtpTestSuccess, setSmtpTestSuccess] = useState<boolean | null>(null);
const [smtpTestError, setSmtpTestError] = useState('');
useEffect(() => {
if (systemConfigsQuery.data) {
setServerAddress(configs["server_address"]?.value || "")
setSiteName(configs["site_name"]?.value || "")
setSmtpHost(configs["smtp_host"]?.value || "")
setSmtpPort(configs["smtp_port"]?.value || "587")
setSmtpUsername(configs["smtp_username"]?.value || "")
setSmtpPassword(configs["smtp_password"]?.value || "")
setServerAddress(configs['server_address']?.value || '');
setSiteName(configs['site_name']?.value || '');
setSmtpHost(configs['smtp_host']?.value || '');
setSmtpPort(configs['smtp_port']?.value || '587');
setSmtpUsername(configs['smtp_username']?.value || '');
setSmtpPassword(configs['smtp_password']?.value || '');
}
}, [systemConfigsQuery.data, configs])
}, [systemConfigsQuery.data, configs]);
const handleDetectAddress = () => {
if (typeof window !== "undefined") {
setServerAddress(window.location.origin)
toast.success("已自动获取当前域名并填充")
}
if (typeof window !== 'undefined') {
setServerAddress(window.location.origin);
toast.success('已自动获取当前域名并填充');
}
};
const updateConfigMutation = useMutation({
mutationFn: async ({ key, value }: { key: string; value: boolean }) => {
const config = configs[key]
const config = configs[key];
if (!config) {
throw new Error(`缺少配置项: ${key}`)
throw new Error(`缺少配置项: ${key}`);
}
await services.adminSystemConfig.updateSystemConfig(key, {
value: value ? "true" : "false",
value: value ? 'true' : 'false',
description: config.description,
})
});
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] })
await queryClient.invalidateQueries({ queryKey: ["public-config"] })
toast.success("配置已更新")
await queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
});
await queryClient.invalidateQueries({ queryKey: ['public-config'] });
toast.success('配置已更新');
},
onError: (error: Error) => {
toast.error(error.message || "更新配置失败")
toast.error(error.message || '更新配置失败');
},
})
});
const saveSystemMutation = useMutation({
mutationFn: async () => {
const currentAddrCfg = configs["server_address"]
const currentSiteCfg = configs["site_name"]
const currentAddrCfg = configs['server_address'];
const currentSiteCfg = configs['site_name'];
await Promise.all([
services.adminSystemConfig.updateSystemConfig("server_address", {
services.adminSystemConfig.updateSystemConfig('server_address', {
value: serverAddress,
description: currentAddrCfg?.description || "服务器地址",
description: currentAddrCfg?.description || '服务器地址',
}),
services.adminSystemConfig.updateSystemConfig("site_name", {
services.adminSystemConfig.updateSystemConfig('site_name', {
value: siteName,
description: currentSiteCfg?.description || "系统平台的展示名称",
description: currentSiteCfg?.description || '系统平台的展示名称',
}),
])
]);
},
onSuccess: async () => {
await Promise.all([
queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] }),
queryClient.invalidateQueries({ queryKey: ["public-config"] }),
])
toast.success("通用配置已成功保存")
queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
}),
queryClient.invalidateQueries({ queryKey: ['public-config'] }),
]);
toast.success('通用配置已成功保存');
},
onError: (error: Error) => {
toast.error(error.message || "保存配置失败")
toast.error(error.message || '保存配置失败');
},
})
});
const handleSystemSave = (e: React.FormEvent) => {
e.preventDefault()
saveSystemMutation.mutate()
}
e.preventDefault();
saveSystemMutation.mutate();
};
const saveSmtpMutation = useMutation({
mutationFn: async () => {
const updates = [
{ key: "smtp_host", value: smtpHost },
{ key: "smtp_port", value: smtpPort },
{ key: "smtp_username", value: smtpUsername },
{ key: "smtp_password", value: smtpPassword },
]
{ key: 'smtp_host', value: smtpHost },
{ key: 'smtp_port', value: smtpPort },
{ key: 'smtp_username', value: smtpUsername },
{ key: 'smtp_password', value: smtpPassword },
];
for (const update of updates) {
const currentCfg = configs[update.key]
if (update.key === "smtp_password" && (update.value === "" || update.value === "******")) {
const currentCfg = configs[update.key];
if (
update.key === 'smtp_password' &&
(update.value === '' || update.value === '******')
) {
// If already configured and sent empty or mask, skip updating it (keep existing)
if (currentCfg && currentCfg.value === "******") {
continue
if (currentCfg && currentCfg.value === '******') {
continue;
}
}
await services.adminSystemConfig.updateSystemConfig(update.key, {
value: update.value,
description: currentCfg?.description || "",
})
description: currentCfg?.description || '',
});
}
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "system-configs"] })
toast.success("SMTP 邮件配置已成功保存")
await queryClient.invalidateQueries({
queryKey: ['admin', 'system-configs'],
});
toast.success('SMTP 邮件配置已成功保存');
},
onError: (error: Error) => {
toast.error(error.message || "保存配置失败")
toast.error(error.message || '保存配置失败');
},
})
});
const handleSmtpSave = (e: React.FormEvent) => {
e.preventDefault()
saveSmtpMutation.mutate()
}
e.preventDefault();
saveSmtpMutation.mutate();
};
const testSmtpMutation = useMutation({
mutationFn: async () => {
setSmtpTestLog("正在发起连接测试...\n")
setSmtpTestSuccess(null)
setSmtpTestError("")
setSmtpTestLog('正在发起连接测试...\n');
setSmtpTestSuccess(null);
setSmtpTestError('');
const res = await services.adminSystemConfig.testSMTP({
smtp_host: smtpHost,
@@ -154,151 +180,167 @@ export function SystemTab({ configs, systemConfigsQuery }: SystemTabProps) {
smtp_username: smtpUsername,
smtp_password: smtpPassword,
to: smtpTestTo,
})
return res
});
return res;
},
onSuccess: (data) => {
setSmtpTestLog(data.log)
setSmtpTestLog(data.log);
if (data.success) {
setSmtpTestSuccess(true)
toast.success("测试邮件发送成功")
setSmtpTestSuccess(true);
toast.success('测试邮件发送成功');
} else {
setSmtpTestSuccess(false)
setSmtpTestError(data.error || "发送失败,请检查配置和日志。")
toast.error("测试邮件发送失败")
setSmtpTestSuccess(false);
setSmtpTestError(data.error || '发送失败,请检查配置和日志。');
toast.error('测试邮件发送失败');
}
},
onError: (error: Error) => {
setSmtpTestSuccess(false)
setSmtpTestError(error.message || "请求发送失败")
setSmtpTestLog((prev) => prev + `\n[请求错误] ${error.message}\n`)
toast.error(error.message || "测试请求发送失败")
setSmtpTestSuccess(false);
setSmtpTestError(error.message || '请求发送失败');
setSmtpTestLog((prev) => prev + `\n[请求错误] ${error.message}\n`);
toast.error(error.message || '测试请求发送失败');
},
})
});
const handleSmtpTestSubmit = (e: React.FormEvent) => {
e.preventDefault()
e.preventDefault();
if (!smtpTestTo) {
toast.error("请输入目标邮箱地址")
return
}
testSmtpMutation.mutate()
toast.error('请输入目标邮箱地址');
return;
}
testSmtpMutation.mutate();
};
const indexingEnabled = configs["search_engine_indexing_enabled"]?.value === "true"
const indexingEnabled =
configs['search_engine_indexing_enabled']?.value === 'true';
return (
<div className="space-y-8">
<div className='space-y-8'>
{/* 通用设置 */}
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<div className="flex items-center gap-2">
<div className="rounded-lg bg-primary/10 p-1.5 text-primary">
<Server className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<div className='flex items-center gap-2'>
<div className='rounded-lg bg-primary/10 p-1.5 text-primary'>
<Server className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">通用设置</CardTitle>
<CardDescription className="text-xs">
<CardTitle className='text-base font-semibold'>
通用设置
</CardTitle>
<CardDescription className='text-xs'>
配置站点标识、服务地址与搜索引擎收录策略
</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="pt-6">
<form onSubmit={handleSystemSave} className="flex flex-col gap-6">
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
<div className="flex flex-col gap-1.5">
<Label htmlFor="site_name" className="text-xs font-semibold">
<CardContent className='pt-6'>
<form onSubmit={handleSystemSave} className='flex flex-col gap-6'>
<div className='grid grid-cols-1 gap-4 lg:grid-cols-2'>
<div className='flex flex-col gap-1.5'>
<Label htmlFor='site_name' className='text-xs font-semibold'>
站点名称
</Label>
<Input
id="site_name"
type="text"
id='site_name'
type='text'
value={siteName}
onChange={(e) => setSiteName(e.target.value)}
placeholder="例如: Wavelet"
className="border-dashed bg-card text-xs"
placeholder='例如: Wavelet'
className='border-dashed bg-card text-xs'
/>
<p className="text-[11px] leading-relaxed text-muted-foreground">
<p className='text-[11px] leading-relaxed text-muted-foreground'>
用于网页标题、登录注册页面、管理后台与系统邮件。
</p>
</div>
<div className="flex flex-col gap-1.5">
<div className="flex items-center justify-between gap-3">
<Label htmlFor="server_address" className="text-xs font-semibold">
<div className='flex flex-col gap-1.5'>
<div className='flex items-center justify-between gap-3'>
<Label
htmlFor='server_address'
className='text-xs font-semibold'
>
服务器访问地址
</Label>
<Button
type="button"
variant="link"
size="sm"
type='button'
variant='link'
size='sm'
onClick={handleDetectAddress}
className="h-auto px-0 text-xs"
className='h-auto px-0 text-xs'
>
<Sparkles data-icon="inline-start" />
<Sparkles data-icon='inline-start' />
使用当前域名
</Button>
</div>
<Input
id="server_address"
type="url"
id='server_address'
type='url'
value={serverAddress}
onChange={(e) => setServerAddress(e.target.value)}
placeholder="例如: https://example.com"
className="border-dashed bg-card text-xs"
placeholder='例如: https://example.com'
className='border-dashed bg-card text-xs'
/>
<p className="text-[11px] leading-relaxed text-muted-foreground">
<p className='text-[11px] leading-relaxed text-muted-foreground'>
限定 API 的允许来源;留空将开放任意来源访问。
</p>
</div>
</div>
<div className="flex flex-col gap-3 rounded-lg border border-dashed p-3 sm:flex-row sm:items-center sm:justify-between">
<div className="flex min-w-0 items-start gap-3">
<div className="rounded-md bg-muted p-1.5 text-muted-foreground">
<Search className="size-4" />
<div className='flex flex-col gap-3 rounded-lg border border-dashed p-3 sm:flex-row sm:items-center sm:justify-between'>
<div className='flex min-w-0 items-start gap-3'>
<div className='rounded-md bg-muted p-1.5 text-muted-foreground'>
<Search className='size-4' />
</div>
<div className="flex min-w-0 flex-col gap-1">
<div className="flex flex-wrap items-center gap-2">
<span className="text-xs font-semibold">搜索引擎抓取与收录</span>
<Badge variant={indexingEnabled ? "default" : "secondary"}>
{indexingEnabled ? "允许收录" : "禁止收录"}
<div className='flex min-w-0 flex-col gap-1'>
<div className='flex flex-wrap items-center gap-2'>
<span className='text-xs font-semibold'>
搜索引擎抓取与收录
</span>
<Badge variant={indexingEnabled ? 'default' : 'secondary'}>
{indexingEnabled ? '允许收录' : '禁止收录'}
</Badge>
</div>
<p className="text-[11px] leading-relaxed text-muted-foreground">
<p className='text-[11px] leading-relaxed text-muted-foreground'>
控制 Google、Baidu、Bing 等搜索引擎是否可以索引公开页面。
</p>
</div>
</div>
<Switch
aria-label="允许搜索引擎抓取与收录"
aria-label='允许搜索引擎抓取与收录'
checked={indexingEnabled}
disabled={updateConfigMutation.isPending || systemConfigsQuery.isPending}
disabled={
updateConfigMutation.isPending || systemConfigsQuery.isPending
}
onCheckedChange={(checked) =>
updateConfigMutation.mutate({ key: "search_engine_indexing_enabled", value: checked })
updateConfigMutation.mutate({
key: 'search_engine_indexing_enabled',
value: checked,
})
}
/>
</div>
<div className="flex flex-col-reverse gap-2 border-t border-dashed pt-4 sm:flex-row sm:items-center sm:justify-between">
<div className="flex items-center gap-2 text-[11px] text-muted-foreground">
<Globe className="size-3.5" />
<div className='flex flex-col-reverse gap-2 border-t border-dashed pt-4 sm:flex-row sm:items-center sm:justify-between'>
<div className='flex items-center gap-2 text-[11px] text-muted-foreground'>
<Globe className='size-3.5' />
地址变更会影响跨域访问策略,请填写完整协议与域名。
</div>
<Button
type="submit"
size="sm"
type='submit'
size='sm'
disabled={saveSystemMutation.isPending}
>
{saveSystemMutation.isPending ? (
<>
<Loader2 data-icon="inline-start" className="animate-spin" />
<Loader2
data-icon='inline-start'
className='animate-spin'
/>
保存中...
</>
) : (
"保存配置"
'保存配置'
)}
</Button>
</div>
@@ -307,98 +349,120 @@ export function SystemTab({ configs, systemConfigsQuery }: SystemTabProps) {
</Card>
{/* SMTP 邮件设置 */}
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4">
<div className="flex items-center gap-2">
<div className="p-1.5 rounded-lg bg-primary/10 text-primary">
<Mail className="size-4" />
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4'>
<div className='flex items-center gap-2'>
<div className='p-1.5 rounded-lg bg-primary/10 text-primary'>
<Mail className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">SMTP 邮件设置</CardTitle>
<CardDescription className="text-xs">配置系统的邮件发送服务 (SMTP)</CardDescription>
<CardTitle className='text-base font-semibold'>
SMTP 邮件设置
</CardTitle>
<CardDescription className='text-xs'>
配置系统的邮件发送服务 (SMTP)
</CardDescription>
</div>
</div>
</CardHeader>
<CardContent className="pt-6">
<form onSubmit={handleSmtpSave} className="space-y-6">
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-1.5">
<Label htmlFor="smtp_host" className="text-xs font-semibold">SMTP 服务器地址</Label>
<CardContent className='pt-6'>
<form onSubmit={handleSmtpSave} className='space-y-6'>
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
<div className='space-y-1.5'>
<Label htmlFor='smtp_host' className='text-xs font-semibold'>
SMTP 服务器地址
</Label>
<Input
id="smtp_host"
type="text"
id='smtp_host'
type='text'
value={smtpHost}
onChange={(e) => setSmtpHost(e.target.value)}
placeholder="例如: smtp.example.com"
className="bg-card border-dashed text-xs"
placeholder='例如: smtp.example.com'
className='bg-card border-dashed text-xs'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="smtp_port" className="text-xs font-semibold">SMTP 端口</Label>
<div className='space-y-1.5'>
<Label htmlFor='smtp_port' className='text-xs font-semibold'>
SMTP 端口
</Label>
<Input
id="smtp_port"
type="number"
id='smtp_port'
type='number'
value={smtpPort}
onChange={(e) => setSmtpPort(e.target.value)}
placeholder="例如: 587 或 465"
className="bg-card border-dashed text-xs"
placeholder='例如: 587 或 465'
className='bg-card border-dashed text-xs'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="smtp_username" className="text-xs font-semibold">SMTP 账户</Label>
<div className='space-y-1.5'>
<Label
htmlFor='smtp_username'
className='text-xs font-semibold'
>
SMTP 账户
</Label>
<Input
id="smtp_username"
type="text"
id='smtp_username'
type='text'
value={smtpUsername}
onChange={(e) => setSmtpUsername(e.target.value)}
placeholder="例如: sender@example.com"
className="bg-card border-dashed text-xs"
placeholder='例如: sender@example.com'
className='bg-card border-dashed text-xs'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="smtp_password" className="text-xs font-semibold">SMTP 访问凭证</Label>
<div className='space-y-1.5'>
<Label
htmlFor='smtp_password'
className='text-xs font-semibold'
>
SMTP 访问凭证
</Label>
<Input
id="smtp_password"
type="password"
id='smtp_password'
type='password'
value={smtpPassword}
onChange={(e) => setSmtpPassword(e.target.value)}
placeholder={configs["smtp_password"]?.value === "******" ? "•••••• (已配置,留空或输入新值)" : "输入凭证密码"}
className="bg-card border-dashed text-xs"
placeholder={
configs['smtp_password']?.value === '******'
? '•••••• (已配置,留空或输入新值)'
: '输入凭证密码'
}
className='bg-card border-dashed text-xs'
/>
</div>
</div>
<div className="flex justify-end gap-2 pt-4 border-t border-dashed">
<div className='flex justify-end gap-2 pt-4 border-t border-dashed'>
<Button
type="button"
variant="outline"
size="sm"
type='button'
variant='outline'
size='sm'
onClick={() => {
setSmtpTestOpen(true)
setSmtpTestTo("")
setSmtpTestLog("")
setSmtpTestSuccess(null)
setSmtpTestError("")
setSmtpTestOpen(true);
setSmtpTestTo('');
setSmtpTestLog('');
setSmtpTestSuccess(null);
setSmtpTestError('');
}}
disabled={saveSmtpMutation.isPending}
>
测试发件
</Button>
<Button
type="submit"
size="sm"
type='submit'
size='sm'
disabled={saveSmtpMutation.isPending}
>
{saveSmtpMutation.isPending ? (
<>
<Loader2 className="mr-1.5 size-3.5 animate-spin" />
<Loader2 className='mr-1.5 size-3.5 animate-spin' />
保存中...
</>
) : (
"保存配置"
'保存配置'
)}
</Button>
</div>
@@ -407,72 +471,77 @@ export function SystemTab({ configs, systemConfigsQuery }: SystemTabProps) {
</Card>
<Dialog open={smtpTestOpen} onOpenChange={setSmtpTestOpen}>
<DialogContent className="max-w-lg border border-dashed">
<DialogContent className='max-w-lg border border-dashed'>
<DialogHeader>
<DialogTitle className="text-base font-semibold">SMTP 发件测试</DialogTitle>
<DialogDescription className="text-xs">
输入接收测试邮件的邮箱地址。系统将使用您在表单中当前填写的 SMTP 配置进行发件测试。
<DialogTitle className='text-base font-semibold'>
SMTP 发件测试
</DialogTitle>
<DialogDescription className='text-xs'>
输入接收测试邮件的邮箱地址。系统将使用您在表单中当前填写的 SMTP
配置进行发件测试。
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSmtpTestSubmit} className="space-y-4">
<div className="space-y-1.5">
<Label htmlFor="smtp_test_to" className="text-xs font-semibold">目标邮箱地址</Label>
<form onSubmit={handleSmtpTestSubmit} className='space-y-4'>
<div className='space-y-1.5'>
<Label htmlFor='smtp_test_to' className='text-xs font-semibold'>
目标邮箱地址
</Label>
<Input
id="smtp_test_to"
type="email"
id='smtp_test_to'
type='email'
required
value={smtpTestTo}
onChange={(e) => setSmtpTestTo(e.target.value)}
placeholder="例如: receiver@example.com"
className="bg-card border-dashed text-xs"
placeholder='例如: receiver@example.com'
className='bg-card border-dashed text-xs'
disabled={testSmtpMutation.isPending}
/>
</div>
{smtpTestLog && (
<div className="space-y-1.5">
<Label className="text-xs font-semibold">连接与传输日志</Label>
<pre className="bg-zinc-950 text-zinc-50 font-mono p-4 rounded-lg text-[10px] h-60 overflow-y-auto whitespace-pre-wrap border border-dashed border-zinc-800 leading-relaxed">
<div className='space-y-1.5'>
<Label className='text-xs font-semibold'>连接与传输日志</Label>
<pre className='bg-zinc-950 text-zinc-50 font-mono p-4 rounded-lg text-[10px] h-60 overflow-y-auto whitespace-pre-wrap border border-dashed border-zinc-800 leading-relaxed'>
{smtpTestLog}
</pre>
</div>
)}
{smtpTestSuccess === true && (
<div className="p-3 rounded-lg border border-dashed border-emerald-500/30 bg-emerald-500/5 text-emerald-500 text-xs">
<div className='p-3 rounded-lg border border-dashed border-emerald-500/30 bg-emerald-500/5 text-emerald-500 text-xs'>
测试成功!邮件已顺利发出。
</div>
)}
{smtpTestSuccess === false && (
<div className="p-3 rounded-lg border border-dashed border-rose-500/30 bg-rose-500/5 text-rose-500 text-xs break-all">
<div className='p-3 rounded-lg border border-dashed border-rose-500/30 bg-rose-500/5 text-rose-500 text-xs break-all'>
测试失败:{smtpTestError}
</div>
)}
<DialogFooter className="gap-2 sm:gap-0 border-t border-dashed pt-4">
<DialogFooter className='gap-2 sm:gap-0 border-t border-dashed pt-4'>
<Button
type="button"
variant="ghost"
size="sm"
type='button'
variant='ghost'
size='sm'
onClick={() => setSmtpTestOpen(false)}
disabled={testSmtpMutation.isPending}
>
关闭
</Button>
<Button
type="submit"
size="sm"
type='submit'
size='sm'
disabled={testSmtpMutation.isPending}
>
{testSmtpMutation.isPending ? (
<>
<Loader2 className="mr-1.5 size-3.5 animate-spin" />
<Loader2 className='mr-1.5 size-3.5 animate-spin' />
测试中...
</>
) : (
"开始测试"
'开始测试'
)}
</Button>
</DialogFooter>
@@ -480,5 +549,5 @@ export function SystemTab({ configs, systemConfigsQuery }: SystemTabProps) {
</DialogContent>
</Dialog>
</div>
)
);
}
+201 -172
View File
@@ -1,36 +1,51 @@
"use client"
'use client';
import {useState} from "react"
import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"
import {FileText, Loader2, Pencil, Plus, Trash2} from "lucide-react"
import { useState } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { FileText, Loader2, Pencil, Plus, Trash2 } from 'lucide-react';
import {Button} from "@/components/ui/button"
import {Card, CardContent, CardDescription, CardHeader, CardTitle} from "@/components/ui/card"
import {Input} from "@/components/ui/input"
import {Label} from "@/components/ui/label"
import {Textarea} from "@/components/ui/textarea"
import {Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle} from "@/components/ui/dialog"
import services from "@/lib/services"
import type {Template} from "@/lib/services/admin/types"
import {toast} from "sonner"
import { Button } from '@/components/ui/button';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import services from '@/lib/services';
import type { Template } from '@/lib/services/admin/types';
import { toast } from 'sonner';
export function TemplatesManager() {
const queryClient = useQueryClient()
const [modalOpen, setModalOpen] = useState(false)
const [selectedTemplate, setSelectedTemplate] = useState<Template | null>(null)
const queryClient = useQueryClient();
const [modalOpen, setModalOpen] = useState(false);
const [selectedTemplate, setSelectedTemplate] = useState<Template | null>(
null,
);
// Form states
const [key, setKey] = useState("")
const [name, setName] = useState("")
const [type, setType] = useState("email")
const [subject, setSubject] = useState("")
const [content, setContent] = useState("")
const [description, setDescription] = useState("")
const [key, setKey] = useState('');
const [name, setName] = useState('');
const [type, setType] = useState('email');
const [subject, setSubject] = useState('');
const [content, setContent] = useState('');
const [description, setDescription] = useState('');
const templatesQuery = useQuery({
queryKey: ["admin", "templates"],
queryKey: ['admin', 'templates'],
queryFn: () => services.adminTemplate.listTemplates(),
})
});
const createTemplateMutation = useMutation({
mutationFn: async () => {
@@ -41,17 +56,17 @@ export function TemplatesManager() {
subject,
content,
description,
})
});
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "templates"] })
toast.success("通知模板已成功创建")
setModalOpen(false)
await queryClient.invalidateQueries({ queryKey: ['admin', 'templates'] });
toast.success('通知模板已成功创建');
setModalOpen(false);
},
onError: (error: Error) => {
toast.error(error.message || "创建模板失败")
toast.error(error.message || '创建模板失败');
},
})
});
const updateTemplateMutation = useMutation({
mutationFn: async (key: string) => {
@@ -61,164 +76,178 @@ export function TemplatesManager() {
subject,
content,
description,
})
});
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "templates"] })
toast.success("通知模板已成功保存")
setModalOpen(false)
await queryClient.invalidateQueries({ queryKey: ['admin', 'templates'] });
toast.success('通知模板已成功保存');
setModalOpen(false);
},
onError: (error: Error) => {
toast.error(error.message || "修改模板失败")
toast.error(error.message || '修改模板失败');
},
})
});
const deleteTemplateMutation = useMutation({
mutationFn: async (key: string) => {
await services.adminTemplate.deleteTemplate(key)
await services.adminTemplate.deleteTemplate(key);
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["admin", "templates"] })
toast.success("通知模板已删除")
await queryClient.invalidateQueries({ queryKey: ['admin', 'templates'] });
toast.success('通知模板已删除');
},
onError: (error: Error) => {
toast.error(error.message || "删除模板失败")
toast.error(error.message || '删除模板失败');
},
})
});
const handleOpenCreate = () => {
setSelectedTemplate(null)
setKey("")
setName("")
setType("email")
setSubject("")
setContent("")
setDescription("")
setModalOpen(true)
}
setSelectedTemplate(null);
setKey('');
setName('');
setType('email');
setSubject('');
setContent('');
setDescription('');
setModalOpen(true);
};
const handleOpenEdit = (tmpl: Template) => {
setSelectedTemplate(tmpl)
setKey(tmpl.key)
setName(tmpl.name)
setType(tmpl.type)
setSubject(tmpl.subject || "")
setContent(tmpl.content)
setDescription(tmpl.description || "")
setModalOpen(true)
}
setSelectedTemplate(tmpl);
setKey(tmpl.key);
setName(tmpl.name);
setType(tmpl.type);
setSubject(tmpl.subject || '');
setContent(tmpl.content);
setDescription(tmpl.description || '');
setModalOpen(true);
};
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault()
e.preventDefault();
if (selectedTemplate) {
updateTemplateMutation.mutate(selectedTemplate.key)
updateTemplateMutation.mutate(selectedTemplate.key);
} else {
createTemplateMutation.mutate()
}
createTemplateMutation.mutate();
}
};
const isPending = createTemplateMutation.isPending || updateTemplateMutation.isPending
const isPending =
createTemplateMutation.isPending || updateTemplateMutation.isPending;
return (
<div className="space-y-6">
<Card className="border border-dashed shadow-sm">
<CardHeader className="border-b border-dashed pb-4 flex flex-row items-center justify-between gap-4">
<div className="flex items-center gap-2">
<div className="p-1.5 rounded-lg bg-primary/10 text-primary">
<FileText className="size-4" />
<div className='space-y-6'>
<Card className='border border-dashed shadow-sm'>
<CardHeader className='border-b border-dashed pb-4 flex flex-row items-center justify-between gap-4'>
<div className='flex items-center gap-2'>
<div className='p-1.5 rounded-lg bg-primary/10 text-primary'>
<FileText className='size-4' />
</div>
<div>
<CardTitle className="text-base font-semibold">通知模板管理</CardTitle>
<CardDescription className="text-xs">
<CardTitle className='text-base font-semibold'>
通知模板管理
</CardTitle>
<CardDescription className='text-xs'>
配置与编辑系统各类场景的通知邮件/短信模板,支持动态变量渲染
</CardDescription>
</div>
</div>
<Button
type="button"
size="sm"
type='button'
size='sm'
onClick={handleOpenCreate}
variant="secondary"
variant='secondary'
>
<Plus className="mr-1.5 size-3.5" />
<Plus className='mr-1.5 size-3.5' />
新增模板
</Button>
</CardHeader>
<CardContent className="pt-6 space-y-4">
<CardContent className='pt-6 space-y-4'>
{templatesQuery.isPending ? (
<div className="flex items-center justify-center p-8">
<Loader2 className="size-6 animate-spin text-muted-foreground/50" />
<div className='flex items-center justify-center p-8'>
<Loader2 className='size-6 animate-spin text-muted-foreground/50' />
</div>
) : (templatesQuery.data ?? []).length > 0 ? (
<div className="grid grid-cols-1 gap-4">
<div className='grid grid-cols-1 gap-4'>
{(templatesQuery.data ?? []).map((tmpl) => (
<div
key={tmpl.id}
className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm"
className='flex flex-col sm:flex-row sm:items-center justify-between gap-4 rounded-xl border border-dashed p-4 bg-card hover:bg-muted/10 hover:border-primary/30 transition-all duration-300 shadow-sm'
>
<div className="space-y-1.5">
<div className="flex items-center gap-2 flex-wrap">
<span className="font-semibold text-sm text-foreground">{tmpl.name}</span>
<span className={`text-[10px] px-2 py-0.5 rounded-full border font-medium ${
tmpl.is_system
? "bg-primary/10 text-primary border-primary/20"
: "bg-amber-500/10 text-amber-500 border-amber-500/20"
}`}>
{tmpl.is_system ? "系统内置" : "自定义"}
<div className='space-y-1.5'>
<div className='flex items-center gap-2 flex-wrap'>
<span className='font-semibold text-sm text-foreground'>
{tmpl.name}
</span>
<span className="text-[10px] px-2 py-0.5 rounded-full border border-border/50 bg-muted/50 text-muted-foreground font-mono">
<span
className={`text-[10px] px-2 py-0.5 rounded-full border font-medium ${
tmpl.is_system
? 'bg-primary/10 text-primary border-primary/20'
: 'bg-amber-500/10 text-amber-500 border-amber-500/20'
}`}
>
{tmpl.is_system ? '系统内置' : '自定义'}
</span>
<span className='text-[10px] px-2 py-0.5 rounded-full border border-border/50 bg-muted/50 text-muted-foreground font-mono'>
{tmpl.type.toUpperCase()}
</span>
</div>
<div className="text-xs text-muted-foreground">
标识符: <span className="font-mono text-primary bg-primary/5 px-1.5 py-0.5 rounded">{tmpl.key}</span>
<div className='text-xs text-muted-foreground'>
标识符:{' '}
<span className='font-mono text-primary bg-primary/5 px-1.5 py-0.5 rounded'>
{tmpl.key}
</span>
{tmpl.subject && ` · 主题: ${tmpl.subject}`}
</div>
{tmpl.description && (
<p className="text-xs text-muted-foreground/80 leading-relaxed max-w-xl">
<p className='text-xs text-muted-foreground/80 leading-relaxed max-w-xl'>
{tmpl.description}
</p>
)}
</div>
<div className="flex items-center justify-end gap-2 shrink-0">
<div className='flex items-center justify-end gap-2 shrink-0'>
<Button
type="button"
variant="ghost"
size="icon"
className="size-8 text-muted-foreground hover:text-primary hover:bg-primary/10 rounded-lg transition-colors"
type='button'
variant='ghost'
size='icon'
className='size-8 text-muted-foreground hover:text-primary hover:bg-primary/10 rounded-lg transition-colors'
onClick={() => handleOpenEdit(tmpl)}
>
<Pencil className="size-4" />
<Pencil className='size-4' />
</Button>
<Button
type="button"
variant="ghost"
size="icon"
className="size-8 text-muted-foreground hover:text-rose-500 hover:bg-rose-500/10 rounded-lg transition-colors"
disabled={tmpl.is_system || deleteTemplateMutation.isPending}
type='button'
variant='ghost'
size='icon'
className='size-8 text-muted-foreground hover:text-rose-500 hover:bg-rose-500/10 rounded-lg transition-colors'
disabled={
tmpl.is_system || deleteTemplateMutation.isPending
}
onClick={() => {
if (window.confirm(`确定删除模板「${tmpl.name}」吗?`)) {
deleteTemplateMutation.mutate(tmpl.key)
if (
window.confirm(`确定删除模板「${tmpl.name}」吗?`)
) {
deleteTemplateMutation.mutate(tmpl.key);
}
}}
>
<Trash2 className="size-4" />
<Trash2 className='size-4' />
</Button>
</div>
</div>
))}
</div>
) : (
<div className="rounded-xl border border-dashed border-border/50 px-4 py-8 text-center text-xs text-muted-foreground bg-muted/5 flex flex-col items-center justify-center gap-3">
<div className='rounded-xl border border-dashed border-border/50 px-4 py-8 text-center text-xs text-muted-foreground bg-muted/5 flex flex-col items-center justify-center gap-3'>
<span>暂无配置的通知模板,点击上方按钮新增</span>
<Button
type="button"
size="sm"
variant="outline"
type='button'
size='sm'
variant='outline'
onClick={handleOpenCreate}
className="border-dashed"
className='border-dashed'
>
<Plus className="mr-1.5 size-3.5" />
<Plus className='mr-1.5 size-3.5' />
新增模板
</Button>
</div>
@@ -227,142 +256,142 @@ export function TemplatesManager() {
</Card>
<Dialog open={modalOpen} onOpenChange={setModalOpen}>
<DialogContent className="max-w-xl border border-dashed">
<DialogContent className='max-w-xl border border-dashed'>
<DialogHeader>
<DialogTitle className="text-base font-semibold">
{selectedTemplate ? "编辑通知模板" : "新增通知模板"}
<DialogTitle className='text-base font-semibold'>
{selectedTemplate ? '编辑通知模板' : '新增通知模板'}
</DialogTitle>
<DialogDescription className="text-xs">
配置模板渲染逻辑,支持 Go `text/template` 语法(例如 `{"{{.Code}}"}` 表示验证码变量)。
<DialogDescription className='text-xs'>
配置模板渲染逻辑,支持 Go `text/template` 语法(例如 `
{'{{.Code}}'}` 表示验证码变量)。
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-1.5">
<Label htmlFor="tmpl_key" className="text-xs font-semibold">
<form onSubmit={handleSubmit} className='space-y-4'>
<div className='grid grid-cols-1 sm:grid-cols-2 gap-4'>
<div className='space-y-1.5'>
<Label htmlFor='tmpl_key' className='text-xs font-semibold'>
模板标识符 (Key)
</Label>
<Input
id="tmpl_key"
type="text"
id='tmpl_key'
type='text'
required
value={key}
onChange={(e) => setKey(e.target.value)}
placeholder="例如: login_email"
className="bg-card border-dashed text-xs"
placeholder='例如: login_email'
className='bg-card border-dashed text-xs'
disabled={!!selectedTemplate}
/>
<p className="text-[10px] text-muted-foreground leading-normal">
<p className='text-[10px] text-muted-foreground leading-normal'>
模板的唯一标识,在代码中通过此 Key 调用。
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="tmpl_name" className="text-xs font-semibold">
<div className='space-y-1.5'>
<Label htmlFor='tmpl_name' className='text-xs font-semibold'>
模板名称
</Label>
<Input
id="tmpl_name"
type="text"
id='tmpl_name'
type='text'
required
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="例如: 登录验证码邮件"
className="bg-card border-dashed text-xs"
placeholder='例如: 登录验证码邮件'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">
<p className='text-[10px] text-muted-foreground leading-normal'>
用于后台识别该模板的可读名称。
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="tmpl_type" className="text-xs font-semibold">
<div className='space-y-1.5'>
<Label htmlFor='tmpl_type' className='text-xs font-semibold'>
模板类型
</Label>
<Input
id="tmpl_type"
type="text"
id='tmpl_type'
type='text'
required
value={type}
onChange={(e) => setType(e.target.value)}
placeholder="email"
className="bg-card border-dashed text-xs"
placeholder='email'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">
<p className='text-[10px] text-muted-foreground leading-normal'>
模板分类标识,目前支持 `email`。
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="tmpl_subject" className="text-xs font-semibold">
<div className='space-y-1.5'>
<Label htmlFor='tmpl_subject' className='text-xs font-semibold'>
模板主题 (Subject)
</Label>
<Input
id="tmpl_subject"
type="text"
id='tmpl_subject'
type='text'
value={subject}
onChange={(e) => setSubject(e.target.value)}
placeholder="例如: Wavelet 登录验证码"
className="bg-card border-dashed text-xs"
placeholder='例如: Wavelet 登录验证码'
className='bg-card border-dashed text-xs'
/>
<p className="text-[10px] text-muted-foreground leading-normal">
<p className='text-[10px] text-muted-foreground leading-normal'>
邮件标题(类型为 email 时生效)。
</p>
</div>
</div>
<div className="space-y-1.5">
<Label htmlFor="tmpl_description" className="text-xs font-semibold">
<div className='space-y-1.5'>
<Label
htmlFor='tmpl_description'
className='text-xs font-semibold'
>
模板说明与变量描述
</Label>
<Input
id="tmpl_description"
type="text"
id='tmpl_description'
type='text'
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="例如: 包含变量:{{.Code}},5分钟内有效"
className="bg-card border-dashed text-xs"
placeholder='例如: 包含变量:{{.Code}},5分钟内有效'
className='bg-card border-dashed text-xs'
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="tmpl_content" className="text-xs font-semibold">
<div className='space-y-1.5'>
<Label htmlFor='tmpl_content' className='text-xs font-semibold'>
模板正文内容 (Content)
</Label>
<Textarea
id="tmpl_content"
id='tmpl_content'
required
value={content}
onChange={(e) => setContent(e.target.value)}
placeholder="<h3>正文标题</h3><p>内容段落,可用变量 {{.Code}}</p>"
placeholder='<h3>正文标题</h3><p>内容段落,可用变量 {{.Code}}</p>'
rows={8}
className="bg-card border-dashed text-xs font-mono"
className='bg-card border-dashed text-xs font-mono'
/>
</div>
<DialogFooter className="gap-2 sm:gap-0 border-t border-dashed pt-4">
<DialogFooter className='gap-2 sm:gap-0 border-t border-dashed pt-4'>
<Button
type="button"
variant="ghost"
size="sm"
type='button'
variant='ghost'
size='sm'
onClick={() => setModalOpen(false)}
disabled={isPending}
>
取消
</Button>
<Button
type="submit"
size="sm"
disabled={isPending}
>
<Button type='submit' size='sm' disabled={isPending}>
{isPending ? (
<>
<Loader2 className="mr-1.5 size-3.5 animate-spin" />
<Loader2 className='mr-1.5 size-3.5 animate-spin' />
保存中...
</>
) : (
"保存配置"
'保存配置'
)}
</Button>
</DialogFooter>
@@ -370,5 +399,5 @@ export function TemplatesManager() {
</DialogContent>
</Dialog>
</div>
)
);
}

Some files were not shown because too many files have changed in this diff Show More