|
|
@@ -14,6 +14,7 @@ import {
|
|
|
X,
|
|
|
} from 'lucide-react';
|
|
|
import { toast } from 'react-hot-toast';
|
|
|
+import { retroConfirm } from '@/lib/dialog';
|
|
|
import api from '@/lib/api';
|
|
|
import ConfigurationTable from '@/components/admin/configurations/ConfigurationTable';
|
|
|
import ConfigurationModal from '@/components/admin/configurations/ConfigurationModal';
|
|
|
@@ -23,15 +24,27 @@ import ConfigurationImportModal from '@/components/admin/configurations/Configur
|
|
|
import { DynamicConfiguration } from '@/types';
|
|
|
|
|
|
function normalizeConfig(item: Partial<DynamicConfiguration>): DynamicConfiguration {
|
|
|
+ let val = '';
|
|
|
+ if (item.config_value !== null && item.config_value !== undefined) {
|
|
|
+ if (typeof item.config_value === 'string') {
|
|
|
+ val = item.config_value;
|
|
|
+ } else if (typeof item.config_value === 'object') {
|
|
|
+ try {
|
|
|
+ val = JSON.stringify(item.config_value, null, 2);
|
|
|
+ } catch {
|
|
|
+ val = String(item.config_value);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ val = String(item.config_value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
return {
|
|
|
id: item.id,
|
|
|
- config_key: item.config_key || '',
|
|
|
- config_value:
|
|
|
- typeof item.config_value === 'string'
|
|
|
- ? item.config_value
|
|
|
- : JSON.stringify(item.config_value ?? ''),
|
|
|
- description: item.description || '',
|
|
|
- type: item.type || '',
|
|
|
+ config_key: String(item.config_key || ''),
|
|
|
+ config_value: val,
|
|
|
+ description: String(item.description || ''),
|
|
|
+ type: String(item.type || ''),
|
|
|
created_at: item.created_at,
|
|
|
updated_at: item.updated_at,
|
|
|
};
|
|
|
@@ -104,16 +117,22 @@ export default function AdminConfigurationsPage() {
|
|
|
};
|
|
|
|
|
|
const filteredConfigs = useMemo(() => {
|
|
|
+ const q = keyword.trim().toLowerCase();
|
|
|
return configs.filter((c) => {
|
|
|
- const q = keyword.trim().toLowerCase();
|
|
|
+ const configKey = String(c.config_key || '').toLowerCase();
|
|
|
+ const desc = String(c.description || '').toLowerCase();
|
|
|
+ const configVal = String(c.config_value || '').toLowerCase();
|
|
|
+ const configType = String(c.type || '').toLowerCase();
|
|
|
+
|
|
|
const matchKeyword =
|
|
|
!q ||
|
|
|
- c.config_key.toLowerCase().includes(q) ||
|
|
|
- (c.description && c.description.toLowerCase().includes(q)) ||
|
|
|
- c.config_value.toLowerCase().includes(q);
|
|
|
+ configKey.includes(q) ||
|
|
|
+ desc.includes(q) ||
|
|
|
+ configVal.includes(q) ||
|
|
|
+ configType.includes(q);
|
|
|
|
|
|
- const isJson =
|
|
|
- c.config_value?.trim().startsWith('{') || c.config_value?.trim().startsWith('[');
|
|
|
+ const valTrimmed = String(c.config_value || '').trim();
|
|
|
+ const isJson = valTrimmed.startsWith('{') || valTrimmed.startsWith('[');
|
|
|
const computedType = (c.type || (isJson ? 'json' : 'string')).toLowerCase();
|
|
|
|
|
|
const matchType =
|
|
|
@@ -133,9 +152,9 @@ export default function AdminConfigurationsPage() {
|
|
|
let stringCount = 0;
|
|
|
|
|
|
configs.forEach((c) => {
|
|
|
- const isJson =
|
|
|
- c.config_value?.trim().startsWith('{') || c.config_value?.trim().startsWith('[');
|
|
|
- if (isJson || c.type?.toLowerCase() === 'json') {
|
|
|
+ const valTrimmed = String(c.config_value || '').trim();
|
|
|
+ const isJson = valTrimmed.startsWith('{') || valTrimmed.startsWith('[');
|
|
|
+ if (isJson || (c.type && c.type.toLowerCase() === 'json')) {
|
|
|
jsonCount++;
|
|
|
} else {
|
|
|
stringCount++;
|
|
|
@@ -160,6 +179,24 @@ export default function AdminConfigurationsPage() {
|
|
|
setDrawerOpen(true);
|
|
|
};
|
|
|
|
|
|
+ const handleDelete = async (config: DynamicConfiguration) => {
|
|
|
+ const ok = await retroConfirm({
|
|
|
+ title: '[ CONFIRM // 永久删除配置项 ]',
|
|
|
+ message: `警告:确定要永久删除配置项 "${config.config_key}" 吗?此操作不可撤销!`,
|
|
|
+ type: 'danger',
|
|
|
+ confirmText: '[ 确认删除 ]',
|
|
|
+ });
|
|
|
+ if (!ok) return;
|
|
|
+
|
|
|
+ try {
|
|
|
+ await api.delete(`/api/dynamic-configurations/key/${encodeURIComponent(config.config_key)}`);
|
|
|
+ toast.success(`配置 "${config.config_key}" 已删除`);
|
|
|
+ fetchConfigs();
|
|
|
+ } catch (e: any) {
|
|
|
+ toast.error('删除失败: ' + (e.response?.data?.message || e.message || '未知错误'));
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
return (
|
|
|
<div className="space-y-6 font-mono">
|
|
|
{/* 顶部标头 */}
|
|
|
@@ -246,19 +283,27 @@ export default function AdminConfigurationsPage() {
|
|
|
<input
|
|
|
type="text"
|
|
|
placeholder="搜索配置 Key / 作用描述 / Value 内容..."
|
|
|
- className="w-full pl-9 pr-8 py-2 border-2 border-black bg-neutral-50 text-xs font-mono focus:bg-white outline-none shadow-[2px_2px_0px_#000] text-black"
|
|
|
+ className="w-full pl-9 pr-24 py-2 border-2 border-black bg-neutral-50 text-xs font-mono focus:bg-white outline-none shadow-[2px_2px_0px_#000] text-black"
|
|
|
value={keyword}
|
|
|
onChange={(e) => setKeyword(e.target.value)}
|
|
|
/>
|
|
|
<Search size={14} className="absolute left-3 top-2.5 text-black pointer-events-none" />
|
|
|
- {keyword && (
|
|
|
- <button
|
|
|
- onClick={() => setKeyword('')}
|
|
|
- className="absolute right-2.5 top-2 text-black hover:text-rose-600 font-bold"
|
|
|
- >
|
|
|
- ✕
|
|
|
- </button>
|
|
|
- )}
|
|
|
+ <div className="absolute right-2.5 top-2 flex items-center gap-1.5">
|
|
|
+ {keyword && (
|
|
|
+ <button
|
|
|
+ onClick={() => setKeyword('')}
|
|
|
+ className="text-black hover:text-rose-600 font-bold px-1 text-xs"
|
|
|
+ title="清空输入"
|
|
|
+ >
|
|
|
+ ✕
|
|
|
+ </button>
|
|
|
+ )}
|
|
|
+ {keyword && (
|
|
|
+ <span className="text-[10px] font-mono font-bold bg-amber-300 px-1 border border-black text-black">
|
|
|
+ {filteredConfigs.length} 项
|
|
|
+ </span>
|
|
|
+ )}
|
|
|
+ </div>
|
|
|
</div>
|
|
|
|
|
|
<div className="flex items-center gap-2 w-full md:w-auto">
|
|
|
@@ -310,6 +355,7 @@ export default function AdminConfigurationsPage() {
|
|
|
loading={loading}
|
|
|
onEdit={handleOpenEdit}
|
|
|
onViewDetails={handleOpenDetails}
|
|
|
+ onDelete={handleDelete}
|
|
|
/>
|
|
|
|
|
|
{/* 5. 新建 / 编辑弹窗 */}
|
|
|
@@ -329,6 +375,7 @@ export default function AdminConfigurationsPage() {
|
|
|
setDrawerOpen(false);
|
|
|
handleOpenEdit(cfg);
|
|
|
}}
|
|
|
+ onDelete={handleDelete}
|
|
|
/>
|
|
|
|
|
|
{/* 7. 导出配置弹窗 */}
|