353 lines
16 KiB
Vue
353 lines
16 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* features/kb/components/KbAdminPanel.vue
|
|
* 知识库配置面板(治理面:分组、Dify)。
|
|
* 依赖:kb.api、DifySettingsDialog、DifyHealthDialog、DeleteConfirmDialog、Vue I18n。
|
|
* 逻辑:管理控制台加载知识库分组;分组列表在主表格过滤排序,新增/编辑通过右侧抽屉提交。
|
|
*/
|
|
|
|
import { computed, onMounted, ref, watch } from 'vue';
|
|
import { useRouter } from 'vue-router';
|
|
import { useI18n } from 'vue-i18n';
|
|
import {
|
|
Activity,
|
|
Pencil,
|
|
Plus,
|
|
RefreshCw,
|
|
Search,
|
|
Settings,
|
|
Trash2,
|
|
Wrench,
|
|
} from 'lucide-vue-next';
|
|
import KbGroupDrawer, { type KbGroupFormValue } from './KbGroupDrawer.vue';
|
|
import DifyHealthDialog from './DifyHealthDialog.vue';
|
|
import DifySettingsDialog from './DifySettingsDialog.vue';
|
|
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
|
|
import PaginationBar from '@/shared/components/PaginationBar.vue';
|
|
import { WORKSPACE_PATHS } from '@/features/workspace/constants/routes';
|
|
import {
|
|
createKbGroup,
|
|
deleteKbGroup,
|
|
getKbGroupsSafe,
|
|
updateKbGroup,
|
|
type KbGroup,
|
|
} from '../api/kb.api';
|
|
import { formatDateTime } from '../utils/kb-display.utils';
|
|
|
|
type GroupFilterMode = 'all' | 'withDocs' | 'empty';
|
|
type GroupSortMode = 'updatedDesc' | 'updatedAsc' | 'docsDesc' | 'docsAsc' | 'nameAsc';
|
|
|
|
const router = useRouter();
|
|
const { t } = useI18n();
|
|
|
|
const groups = ref<KbGroup[]>([]);
|
|
const isSubmitting = ref(false);
|
|
const isDeleting = ref(false);
|
|
const isRefreshing = ref(false);
|
|
const editingGroupId = ref<number | null>(null);
|
|
const form = ref<KbGroupFormValue>({ name: '', description: '' });
|
|
const feedback = ref('');
|
|
const feedbackType = ref<'success' | 'error' | ''>('');
|
|
const settingsDialogOpen = ref(false);
|
|
const healthDialogOpen = ref(false);
|
|
const deleteDialogOpen = ref(false);
|
|
const drawerOpen = ref(false);
|
|
const deletingGroup = ref<KbGroup | null>(null);
|
|
const searchKeyword = ref('');
|
|
const filterMode = ref<GroupFilterMode>('all');
|
|
const sortMode = ref<GroupSortMode>('updatedDesc');
|
|
const currentPage = ref(1);
|
|
const pageSize = ref(20);
|
|
|
|
const filterOptions: GroupFilterMode[] = ['all', 'withDocs', 'empty'];
|
|
const sortOptions: GroupSortMode[] = ['updatedDesc', 'updatedAsc', 'docsDesc', 'docsAsc', 'nameAsc'];
|
|
const pageSizeOptions = [10, 20, 50, 100];
|
|
|
|
const totalDocuments = computed(() => groups.value.reduce((total, group) => total + (group.document_count ?? 0), 0));
|
|
const filteredGroups = computed(() => {
|
|
const keyword = searchKeyword.value.trim().toLowerCase();
|
|
const matched = groups.value.filter((group) => {
|
|
const documentCount = group.document_count ?? 0;
|
|
const matchesKeyword = !keyword
|
|
|| group.name.toLowerCase().includes(keyword)
|
|
|| (group.description || '').toLowerCase().includes(keyword);
|
|
const matchesFilter = filterMode.value === 'all'
|
|
|| (filterMode.value === 'withDocs' && documentCount > 0)
|
|
|| (filterMode.value === 'empty' && documentCount === 0);
|
|
return matchesKeyword && matchesFilter;
|
|
});
|
|
|
|
return [...matched].sort((a, b) => {
|
|
const docsA = a.document_count ?? 0;
|
|
const docsB = b.document_count ?? 0;
|
|
const updatedA = Date.parse(a.updated_at || '') || 0;
|
|
const updatedB = Date.parse(b.updated_at || '') || 0;
|
|
if (sortMode.value === 'updatedAsc') return updatedA - updatedB;
|
|
if (sortMode.value === 'docsDesc') return docsB - docsA;
|
|
if (sortMode.value === 'docsAsc') return docsA - docsB;
|
|
if (sortMode.value === 'nameAsc') return a.name.localeCompare(b.name, undefined, { numeric: true });
|
|
return updatedB - updatedA;
|
|
});
|
|
});
|
|
const pagedGroups = computed(() => {
|
|
const start = (currentPage.value - 1) * pageSize.value;
|
|
return filteredGroups.value.slice(start, start + pageSize.value);
|
|
});
|
|
|
|
/** 格式化分组更新时间,避免管理台表格直接暴露后端 ISO 时间字符串。 */
|
|
const formatGroupUpdatedAt = (value?: string | null) => formatDateTime(value);
|
|
|
|
/** 加载知识库治理页的分组数据,供页面初始渲染和操作后刷新复用。 */
|
|
const loadData = async () => {
|
|
isRefreshing.value = true;
|
|
const groupsResult = await getKbGroupsSafe();
|
|
if (groupsResult.ok) groups.value = groupsResult.data;
|
|
else {
|
|
feedback.value = groupsResult.error || t('kbAdmin.msg.loadGroupsFailed');
|
|
feedbackType.value = 'error';
|
|
}
|
|
isRefreshing.value = false;
|
|
};
|
|
|
|
/** 重置分组表单到新建状态,关闭抽屉或切换新建时清空当前编辑对象。 */
|
|
const resetForm = () => {
|
|
editingGroupId.value = null;
|
|
form.value = { name: '', description: '' };
|
|
};
|
|
|
|
/** 打开新建抽屉,保持主列表当前位置和筛选条件不变。 */
|
|
const openCreateDrawer = () => {
|
|
resetForm();
|
|
feedback.value = '';
|
|
feedbackType.value = '';
|
|
drawerOpen.value = true;
|
|
};
|
|
|
|
/** 将列表中的分组带入右侧抽屉,避免大列表编辑时跳转到页面顶部。 */
|
|
const openEditDrawer = (group: KbGroup) => {
|
|
editingGroupId.value = group.id;
|
|
form.value = { name: group.name, description: group.description || '' };
|
|
feedback.value = '';
|
|
feedbackType.value = '';
|
|
drawerOpen.value = true;
|
|
};
|
|
|
|
/** 关闭分组抽屉并恢复表单,列表筛选和分页状态保留。 */
|
|
const closeDrawer = () => {
|
|
drawerOpen.value = false;
|
|
resetForm();
|
|
};
|
|
|
|
/** 提交分组新建或编辑;根据 editingGroupId 自动选择创建或更新接口。 */
|
|
const handleSubmitGroup = async () => {
|
|
if (!form.value.name.trim()) {
|
|
feedback.value = t('kbAdmin.msg.nameRequired');
|
|
feedbackType.value = 'error';
|
|
return;
|
|
}
|
|
isSubmitting.value = true;
|
|
const payload = { name: form.value.name.trim(), description: form.value.description.trim() };
|
|
const result = editingGroupId.value ? await updateKbGroup(editingGroupId.value, payload) : await createKbGroup(payload);
|
|
isSubmitting.value = false;
|
|
|
|
if (!result.ok) {
|
|
feedback.value = result.error || t('kbAdmin.msg.createFailed');
|
|
feedbackType.value = 'error';
|
|
return;
|
|
}
|
|
|
|
feedback.value = editingGroupId.value ? t('kbAdmin.msg.updated') : t('kbAdmin.msg.created');
|
|
feedbackType.value = 'success';
|
|
closeDrawer();
|
|
await loadData();
|
|
};
|
|
|
|
/** 打开分组删除确认弹窗,确认前不直接调用删除接口。 */
|
|
const handleDeleteGroup = (group: KbGroup) => {
|
|
deletingGroup.value = group;
|
|
deleteDialogOpen.value = true;
|
|
};
|
|
|
|
/** 确认删除当前选中的分组,并在成功后同步刷新列表与编辑表单状态。 */
|
|
const handleDeleteGroupConfirm = async () => {
|
|
if (!deletingGroup.value) return;
|
|
isDeleting.value = true;
|
|
const result = await deleteKbGroup(deletingGroup.value.id);
|
|
isDeleting.value = false;
|
|
if (!result.ok) {
|
|
feedback.value = result.error || t('kbAdmin.msg.deleteFailed');
|
|
feedbackType.value = 'error';
|
|
return;
|
|
}
|
|
|
|
if (editingGroupId.value === deletingGroup.value.id) closeDrawer();
|
|
feedback.value = t('kbAdmin.msg.deleted');
|
|
feedbackType.value = 'success';
|
|
deleteDialogOpen.value = false;
|
|
deletingGroup.value = null;
|
|
await loadData();
|
|
};
|
|
|
|
watch([searchKeyword, filterMode, sortMode, pageSize], () => {
|
|
currentPage.value = 1;
|
|
});
|
|
|
|
onMounted(async () => {
|
|
await loadData();
|
|
});
|
|
|
|
defineExpose({ loadData });
|
|
</script>
|
|
|
|
<template>
|
|
<div class="animate-fade-in-up">
|
|
<div class="kb-admin-header">
|
|
<div class="min-w-0 flex-1">
|
|
<h2 class="text-xl font-bold text-primary">{{ t('kbAdmin.groupsTitle') }}</h2>
|
|
<p class="section-desc mt-1">{{ t('kbAdmin.groupManageSubtitle') }}</p>
|
|
</div>
|
|
<div class="flex w-full shrink-0 flex-col gap-2 sm:w-auto sm:flex-row sm:items-center sm:justify-end">
|
|
<div class="kb-admin-toolbar" role="toolbar" :aria-label="t('kbAdmin.toolbar')">
|
|
<button type="button" class="kb-admin-toolbar-btn" @click="settingsDialogOpen = true" :title="t('kbAdmin.difySettings')">
|
|
<Settings class="h-4 w-4 shrink-0" />
|
|
<span class="whitespace-nowrap">{{ t('kbAdmin.difySettings') }}</span>
|
|
</button>
|
|
<button type="button" class="kb-admin-toolbar-btn" @click="healthDialogOpen = true" :title="t('kbAdmin.healthCheck')">
|
|
<Activity class="h-4 w-4 shrink-0" />
|
|
<span class="whitespace-nowrap">{{ t('kbAdmin.healthCheck') }}</span>
|
|
</button>
|
|
<button type="button" class="kb-admin-toolbar-btn" @click="loadData" :title="t('common.refresh')">
|
|
<RefreshCw class="h-4 w-4 shrink-0" :class="{ 'animate-spin': isRefreshing }" />
|
|
<span class="whitespace-nowrap">{{ t('common.refresh') }}</span>
|
|
</button>
|
|
</div>
|
|
<button type="button" class="btn-secondary inline-flex w-full items-center justify-center gap-2 px-4 py-2.5 text-sm sm:w-auto" @click="router.push(WORKSPACE_PATHS.knowledgeBase)">
|
|
<Wrench class="h-4 w-4 shrink-0" />
|
|
<span class="whitespace-nowrap">{{ t('kbAdmin.goWorkspace') }}</span>
|
|
</button>
|
|
<button type="button" class="btn-primary inline-flex w-full items-center justify-center gap-2 px-4 py-2.5 text-sm sm:w-auto" @click="openCreateDrawer">
|
|
<Plus class="h-4 w-4 shrink-0" />
|
|
<span class="whitespace-nowrap">{{ t('kbAdmin.form.create') }}</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="kb-admin-body">
|
|
<section class="admin-table-section">
|
|
<div class="admin-table-filters flex flex-col gap-4">
|
|
<div class="grid grid-cols-1 gap-3 lg:grid-cols-[minmax(16rem,1fr)_auto_minmax(11rem,14rem)] lg:items-center">
|
|
<label class="relative min-w-0">
|
|
<Search class="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted" />
|
|
<input v-model="searchKeyword" class="input-field w-full pl-9" :placeholder="t('kbAdmin.filters.searchPlaceholder')" />
|
|
</label>
|
|
<div class="inline-flex w-full overflow-hidden rounded-xl border border-default bg-surface lg:w-auto">
|
|
<button
|
|
v-for="option in filterOptions"
|
|
:key="option"
|
|
type="button"
|
|
class="min-w-0 flex-1 whitespace-nowrap px-4 py-2.5 text-sm font-medium text-secondary transition-colors hover:bg-surface-soft hover:text-primary lg:flex-none"
|
|
:class="{ 'bg-brand-50 text-brand-700': filterMode === option }"
|
|
@click="filterMode = option"
|
|
>
|
|
{{ t(`kbAdmin.filters.${option}`) }}
|
|
</button>
|
|
</div>
|
|
<select v-model="sortMode" class="select-field w-full">
|
|
<option v-for="option in sortOptions" :key="option" :value="option">
|
|
{{ t(`kbAdmin.sort.${option}`) }}
|
|
</option>
|
|
</select>
|
|
</div>
|
|
<div class="flex flex-wrap items-center gap-3 text-xs text-muted">
|
|
<span>{{ t('kbAdmin.stats.groups', { count: groups.length }) }}</span>
|
|
<span>{{ t('kbAdmin.stats.documents', { count: totalDocuments }) }}</span>
|
|
<span>{{ t('kbAdmin.stats.visible', { count: filteredGroups.length }) }}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<p v-if="feedback" class="mx-6 mt-4 rounded-xl border px-4 py-3 text-sm" :class="feedbackType === 'error' ? 'border-rose-200 bg-rose-50 text-rose-600' : 'border-emerald-200 bg-emerald-50 text-emerald-700'">
|
|
{{ feedback }}
|
|
</p>
|
|
|
|
<div v-if="groups.length === 0" class="admin-table-empty">{{ t('kbAdmin.groupsEmpty') }}</div>
|
|
<div v-else-if="filteredGroups.length === 0" class="admin-table-empty">{{ t('kbAdmin.filters.emptyResult') }}</div>
|
|
<div v-else>
|
|
<div class="admin-table-wrap hidden md:block">
|
|
<table class="admin-table table-fixed">
|
|
<thead>
|
|
<tr>
|
|
<th class="w-[20%] px-6 py-3 text-xs">{{ t('kbAdmin.table.name') }}</th>
|
|
<th class="w-[30%] px-6 py-3 text-xs">{{ t('kbAdmin.table.description') }}</th>
|
|
<th class="w-[12%] px-6 py-3 text-xs">{{ t('kbAdmin.table.documents') }}</th>
|
|
<th class="w-[26%] px-6 py-3 text-xs">{{ t('kbAdmin.table.updatedAt') }}</th>
|
|
<th class="w-[12%] px-6 py-3 text-right text-xs">{{ t('kbAdmin.table.actions') }}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="group in pagedGroups" :key="group.id">
|
|
<td class="px-6 py-4 align-middle"><div class="truncate font-semibold text-primary" :title="group.name">{{ group.name }}</div></td>
|
|
<td class="px-6 py-4 align-middle text-sm text-muted">
|
|
<div class="truncate" :title="group.description || t('kbAdmin.noDescription')">
|
|
{{ group.description || t('kbAdmin.noDescription') }}
|
|
</div>
|
|
</td>
|
|
<td class="px-6 py-4 align-middle"><span class="kb-admin-badge whitespace-nowrap">{{ group.document_count ?? 0 }}</span></td>
|
|
<td class="px-6 py-4 align-middle text-sm text-muted"><div class="truncate" :title="formatGroupUpdatedAt(group.updated_at)">{{ formatGroupUpdatedAt(group.updated_at) }}</div></td>
|
|
<td class="px-6 py-4 align-middle">
|
|
<div class="flex justify-end gap-1.5">
|
|
<button type="button" class="btn-icon-ds border border-default" @click="openEditDrawer(group)" :title="t('common.edit')"><Pencil class="h-4 w-4" /></button>
|
|
<button type="button" class="btn-icon-ds border border-rose-200 text-rose-600 hover:bg-rose-50" @click="handleDeleteGroup(group)" :disabled="isDeleting" :title="t('common.delete')"><Trash2 class="h-4 w-4" /></button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="space-y-3 p-4 md:hidden">
|
|
<article v-for="group in pagedGroups" :key="group.id" class="kb-admin-card">
|
|
<div class="flex items-start justify-between gap-3">
|
|
<div class="min-w-0">
|
|
<h3 class="font-semibold text-primary">{{ group.name }}</h3>
|
|
<p class="mt-1 line-clamp-2 text-sm text-muted" :title="group.description || t('kbAdmin.noDescription')">{{ group.description || t('kbAdmin.noDescription') }}</p>
|
|
</div>
|
|
<span class="kb-admin-badge">{{ group.document_count ?? 0 }}</span>
|
|
</div>
|
|
<div class="mt-3 flex items-center justify-between gap-3">
|
|
<span class="text-xs text-muted">{{ formatGroupUpdatedAt(group.updated_at) }}</span>
|
|
<div class="flex gap-1.5">
|
|
<button type="button" class="btn-icon-ds border border-default" @click="openEditDrawer(group)" :title="t('common.edit')"><Pencil class="h-4 w-4" /></button>
|
|
<button type="button" class="btn-icon-ds border border-rose-200 text-rose-600" @click="handleDeleteGroup(group)" :title="t('common.delete')"><Trash2 class="h-4 w-4" /></button>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
</div>
|
|
|
|
<div class="border-t border-default px-6 py-4">
|
|
<PaginationBar
|
|
v-model:page="currentPage"
|
|
v-model:page-size="pageSize"
|
|
:total="filteredGroups.length"
|
|
:page-size-options="pageSizeOptions"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<KbGroupDrawer
|
|
v-model:form="form"
|
|
:open="drawerOpen"
|
|
:mode="editingGroupId ? 'edit' : 'create'"
|
|
:submitting="isSubmitting"
|
|
@submit="handleSubmitGroup"
|
|
@close="closeDrawer"
|
|
/>
|
|
|
|
<DifySettingsDialog :open="settingsDialogOpen" @close="settingsDialogOpen = false" @saved="(message) => { feedback = message; feedbackType = 'success'; void loadData(); }" @error="(message) => { feedback = message; feedbackType = 'error'; }" />
|
|
<DifyHealthDialog :open="healthDialogOpen" @close="healthDialogOpen = false" @error="(message) => { feedback = message; feedbackType = 'error'; }" />
|
|
<DeleteConfirmDialog v-model="deleteDialogOpen" :title="t('kbAdmin.deleteDialog.title')" :message="t('kbAdmin.deleteDialog.message', { name: deletingGroup?.name || '' })" :confirm-loading="isDeleting" @confirm="handleDeleteGroupConfirm" @cancel="() => { deleteDialogOpen = false; deletingGroup = null; }" />
|
|
</template>
|