- 品牌: Go 模块/数据文件/二进制/构建部署资产全部 opsx→lokistack;前端 localStorage 命名空间迁移(读旧写新,含按用户 id 前缀迁移);修复 nginx upstream 与 Makefile 引用已改名服务的断裂 - i18n: react-i18next 落地,common + 11 个页面域命名空间全站中英(菜单/顶栏/登录/列表页/表单/确认弹窗/状态徽章);设置页语言即时切换;修复词典自注册导致的循环导入白屏 - 用户管理: 批量删除(POST /users/batch-delete,不删自己/最后管理员,部分失败不阻断)+ 行复选/全选/批量工具条 - 主题: 浅色侧栏(中性灰激活态)、四态模式(浅色/深色/跟随系统/定时切换,按时区判定)、顶栏太阳月亮二态、设置页紧凑分段选择器 - UI: 登录记住用户名、CardActionButton 统一五页卡片操作钮、shadcn Badge 替换手写徽章、语义色修正 - 清理: 移除主机 Web 终端(前后端)、k8s-helm/Helm 遗留代码与字段、README 重写精简 - 修复: 侧栏与日志告警 order 重复、路由标题 i18n key 未翻译、词典双逗号等
183 lines
6.1 KiB
TypeScript
183 lines
6.1 KiB
TypeScript
import { memo, useCallback, useEffect, useRef, useState } from 'react'
|
|
import { Server, Plus } from 'lucide-react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { Button } from '@/components/ui/button'
|
|
import { clusterApi, deploymentApi, collectorApi } from '@/api'
|
|
import { PageContainer } from '@/components/PageContainer'
|
|
import { LoadingSpinner } from '@/components/LoadingSpinner'
|
|
import { EmptyState } from '@/components/EmptyState'
|
|
import { ErrorState } from '@/components/ErrorState'
|
|
import { PaginationBar } from '@/components/PaginationBar'
|
|
import { ConfirmDialog } from '@/components/ConfirmDialog'
|
|
import { toast } from 'sonner'
|
|
import { useListQuery } from '@/hooks/useListQuery'
|
|
import type { Cluster } from '@/types'
|
|
import i18n from '@/i18n'
|
|
import zhClusters from '@/i18n/locales/zh/clusters'
|
|
import enClusters from '@/i18n/locales/en/clusters'
|
|
import { ClusterCard } from './components/ClusterCard'
|
|
import { ClusterFormDialog } from './components/ClusterFormDialog'
|
|
import { ClusterDetailDialog } from './components/ClusterDetailDialog'
|
|
|
|
// 注册本域命名空间(i18n/index.ts 只负责 common,域词典就近注册;页面为懒加载,渲染前必然已注册)
|
|
i18n.addResourceBundle('zh', 'clusters', zhClusters)
|
|
i18n.addResourceBundle('en', 'clusters', enClusters)
|
|
|
|
/**
|
|
* 集群管理页 — 只做编排:数据走 useListQuery(取消+版本号),
|
|
* 卡片/表单/详情/删除确认均为域内子组件。成功操作静默(列表刷新即反馈)。
|
|
*/
|
|
const ClustersPage = memo(function ClustersPage() {
|
|
const { t } = useTranslation('clusters')
|
|
const query = useListQuery<Cluster>((params, signal) =>
|
|
clusterApi.list(params, { signal })
|
|
)
|
|
|
|
const [formOpen, setFormOpen] = useState(false)
|
|
const [detailCluster, setDetailCluster] = useState<Cluster | null>(null)
|
|
const [deleteTarget, setDeleteTarget] = useState<Cluster | null>(null)
|
|
const [editingTarget, setEditingTarget] = useState<Cluster | null>(null)
|
|
const [deleting, setDeleting] = useState(false)
|
|
const [validating, setValidating] = useState<number | null>(null)
|
|
|
|
// 验证后延时刷新列表的定时器(卸载清理)
|
|
const timeoutsRef = useRef<ReturnType<typeof setTimeout>[]>([])
|
|
useEffect(() => {
|
|
const timers = timeoutsRef.current
|
|
return () => {
|
|
timers.forEach((id) => clearTimeout(id))
|
|
}
|
|
}, [])
|
|
|
|
// 删除保护:引用检查(部署/采集器仍指向该集群时警告)
|
|
const [refCount, setRefCount] = useState<{ dep: number; col: number }>({ dep: 0, col: 0 })
|
|
useEffect(() => {
|
|
if (!deleteTarget) return
|
|
Promise.all([
|
|
deploymentApi.list({ page: 1, pageSize: 100 }),
|
|
collectorApi.list({ page: 1, pageSize: 100 }),
|
|
]).then(([d, c]) => {
|
|
setRefCount({
|
|
dep: d.list.filter((x) => x.clusterID === deleteTarget.id).length,
|
|
col: c.list.filter((x) => x.clusterID === deleteTarget.id).length,
|
|
})
|
|
}).catch(() => setRefCount({ dep: 0, col: 0 }))
|
|
}, [deleteTarget])
|
|
|
|
const handleDelete = useCallback(async () => {
|
|
const id = deleteTarget?.id
|
|
if (!id) return
|
|
setDeleting(true)
|
|
try {
|
|
await clusterApi.remove(id)
|
|
setDeleteTarget(null)
|
|
query.refresh()
|
|
} catch (err) {
|
|
toast.error(err instanceof Error ? err.message : t('toast.deleteFailed'))
|
|
} finally {
|
|
setDeleting(false)
|
|
}
|
|
}, [deleteTarget, query, t])
|
|
|
|
const handleValidate = useCallback(
|
|
async (id: number) => {
|
|
setValidating(id)
|
|
try {
|
|
await clusterApi.validate(id)
|
|
timeoutsRef.current.push(setTimeout(() => query.refresh(), 3000))
|
|
} catch (err) {
|
|
toast.error(err instanceof Error ? err.message : t('toast.validateFailed'))
|
|
} finally {
|
|
timeoutsRef.current.push(setTimeout(() => setValidating(null), 3000))
|
|
}
|
|
},
|
|
[query, t]
|
|
)
|
|
|
|
return (
|
|
<PageContainer
|
|
title={t('title')}
|
|
description={t('description')}
|
|
action={
|
|
<Button onClick={() => setFormOpen(true)}>
|
|
<Plus className="w-4 h-4 mr-1" />
|
|
{t('actions.add')}
|
|
</Button>
|
|
}
|
|
>
|
|
{query.loading ? (
|
|
<LoadingSpinner className="h-64" />
|
|
) : query.error ? (
|
|
<ErrorState message={query.error} onRetry={query.refresh} />
|
|
) : query.list.length === 0 ? (
|
|
<EmptyState
|
|
icon={Server}
|
|
title={t('empty.title')}
|
|
description={t('empty.description')}
|
|
/>
|
|
) : (
|
|
<>
|
|
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
|
|
{query.list.map((cluster) => (
|
|
<ClusterCard
|
|
key={cluster.id}
|
|
cluster={cluster}
|
|
validating={validating}
|
|
onViewInfo={setDetailCluster}
|
|
onValidate={handleValidate}
|
|
onDelete={setDeleteTarget}
|
|
onEdit={setEditingTarget}
|
|
/>
|
|
))}
|
|
</div>
|
|
<PaginationBar
|
|
page={query.page}
|
|
pageSize={query.pageSize}
|
|
total={query.total}
|
|
onPageChange={query.setPage}
|
|
/>
|
|
</>
|
|
)}
|
|
|
|
<ClusterFormDialog
|
|
open={formOpen || editingTarget !== null}
|
|
onOpenChange={(o) => {
|
|
if (!o) {
|
|
setFormOpen(false)
|
|
setEditingTarget(null)
|
|
}
|
|
}}
|
|
onCreated={() => (editingTarget ? query.refresh() : query.reload())}
|
|
editing={editingTarget}
|
|
/>
|
|
|
|
<ClusterDetailDialog
|
|
key={detailCluster?.id ?? 'none'}
|
|
cluster={detailCluster}
|
|
onClose={() => setDetailCluster(null)}
|
|
/>
|
|
|
|
<ConfirmDialog
|
|
open={deleteTarget !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) setDeleteTarget(null)
|
|
}}
|
|
name={deleteTarget?.name}
|
|
loading={deleting}
|
|
description={
|
|
refCount.dep + refCount.col > 0
|
|
? t('delete.warning', {
|
|
name: deleteTarget?.name ?? '',
|
|
dep: refCount.dep,
|
|
col: refCount.col,
|
|
})
|
|
: undefined
|
|
}
|
|
onConfirm={handleDelete}
|
|
/>
|
|
</PageContainer>
|
|
)
|
|
})
|
|
|
|
export default ClustersPage
|