使用 i18n.t() 全局方式(避免 hook 定位问题): - 288 目标文件中 135 个成功转换 - 2228 处中文替换为 i18n.t() 调用 - 1006 个新翻译 key 添加到 zh-CN.json + en-US.json - 词典总量: 3554 keys (22 namespaces) - tsc 零错误 + Vite 构建成功 - 6 个含代码示例/数据字符串的文件安全回退
90 lines
2.9 KiB
TypeScript
90 lines
2.9 KiB
TypeScript
// 审计策略配置 — 保留天数与应用后立即清理 // Logan.Li
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
import { toast } from 'sonner';
|
|
import { auditSettingsApi, type AuditSettings } from '@/api';
|
|
import i18n from '@/i18n';
|
|
|
|
const RETENTION_OPTIONS = [7, 15, 30, 90, 180, 365] as const;
|
|
|
|
export function useAuditSettings() {
|
|
const [settings, setSettings] = useState<AuditSettings | null>(null);
|
|
const [retentionDays, setRetentionDays] = useState('180');
|
|
const [recordAll, setRecordAll] = useState(true);
|
|
const [loading, setLoading] = useState(true);
|
|
const [applying, setApplying] = useState(false);
|
|
|
|
const load = useCallback(async () => {
|
|
setLoading(true);
|
|
try {
|
|
const data = await auditSettingsApi.get();
|
|
setSettings(data);
|
|
setRetentionDays(String(data.retention_days || 180));
|
|
setRecordAll(data.record_all !== false);
|
|
} catch {
|
|
toast.error(i18n.t('system.useauditsetting.s32e8a76e'));
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
load();
|
|
}, [load]);
|
|
|
|
const handleApply = async () => {
|
|
const days = Number(retentionDays);
|
|
if (!RETENTION_OPTIONS.includes(days as (typeof RETENTION_OPTIONS)[number])) {
|
|
toast.error(i18n.t('system.useauditsetting.s51680af0'));
|
|
return;
|
|
}
|
|
setApplying(true);
|
|
try {
|
|
const res = await auditSettingsApi.apply({ retention_days: days, record_all: recordAll });
|
|
setSettings({
|
|
retention_days: res.retention_days ?? days,
|
|
record_all: res.record_all ?? recordAll,
|
|
last_cleanup_at: res.last_cleanup_at,
|
|
last_cleanup_summary: res.last_cleanup_summary,
|
|
});
|
|
const total = res.cleanup?.total ?? 0;
|
|
toast.success(
|
|
total > 0
|
|
? `已应用 ${days} 天保留策略,立即清理 ${total} 条超期记录`
|
|
: `已应用 ${days} 天保留策略,暂无超期记录需清理`,
|
|
);
|
|
} catch (e: unknown) {
|
|
toast.error(e instanceof Error ? e.message : i18n.t('kubernetes.msg.applyFailed'));
|
|
} finally {
|
|
setApplying(false);
|
|
}
|
|
};
|
|
|
|
const lastCleanupText = (() => {
|
|
if (!settings?.last_cleanup_at) return null;
|
|
const t = new Date(settings.last_cleanup_at).toLocaleString('zh-CN', { hour12: false });
|
|
let detail = '';
|
|
if (settings.last_cleanup_summary) {
|
|
try {
|
|
const s = JSON.parse(settings.last_cleanup_summary) as { total?: number };
|
|
if (typeof s.total === 'number') {
|
|
detail = s.total > 0 ? `,清理 ${s.total} 条` : i18n.t('system.useauditsetting.s7d55bd57');
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
return `上次清理:${t}${detail}`;
|
|
})();
|
|
|
|
return {
|
|
retentionDays,
|
|
setRetentionDays,
|
|
recordAll,
|
|
setRecordAll,
|
|
loading,
|
|
applying,
|
|
handleApply,
|
|
lastCleanupText,
|
|
retentionOptions: settings?.allowed_retention_days ?? [...RETENTION_OPTIONS],
|
|
};
|
|
} |