Files
syops/frontend/src/features/chat/components/ChatQueryPreviewDialog.vue
T

154 lines
6.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* features/chat/components/ChatQueryPreviewDialog.vue
* Text2SQL 查询结果全量预览弹窗 UI。
*/
<script setup lang="ts">
import { computed } from 'vue';
import { Loader2, X } from 'lucide-vue-next';
import type { QueryPreviewState } from '../composables/useChatQueryPreview';
const open = defineModel<boolean>('open', { required: true });
const props = defineProps<{
loading: boolean;
error: string;
state: QueryPreviewState;
}>();
const emit = defineEmits<{
close: [];
'change-page': [delta: number];
'open-asset-history': [payload: { assetId: string; assetName: string }];
'open-asset-image': [payload: { assetId: string; assetName: string }];
}>();
const totalPages = computed(() =>
Math.max(1, Math.ceil((props.state.total || 0) / (props.state.pageSize || 100))),
);
const assetIdColumnIndex = computed(() => props.state.columns.findIndex((column) => column === '资产编号'));
const assetNameColumnIndex = computed(() => props.state.columns.findIndex((column) => column === '资产名称'));
const imageColumnIndex = computed(() => props.state.columns.findIndex((column) => column === '图片'));
function isHighlightColumn(column: string): boolean {
return column === '资产编号' || column === '图片';
}
function normalizeCellText(cell: unknown): string {
return String(cell ?? '').trim();
}
function canOpenAssetHistory(row: unknown[], cellIndex: number): boolean {
if (cellIndex === assetIdColumnIndex.value) {
return !!normalizeCellText(row[cellIndex]);
}
if (cellIndex === imageColumnIndex.value) {
return !!normalizeCellText(row[assetIdColumnIndex.value]);
}
return false;
}
function emitAssetAction(row: unknown[], cellIndex: number): void {
const assetId = normalizeCellText(row[assetIdColumnIndex.value]);
const assetName = assetNameColumnIndex.value >= 0 ? normalizeCellText(row[assetNameColumnIndex.value]) : '';
if (!assetId) return;
if (cellIndex === imageColumnIndex.value) {
emit('open-asset-image', { assetId, assetName });
return;
}
emit('open-asset-history', { assetId, assetName });
}
</script>
<template>
<div
v-if="open"
class="dialog-overlay z-[120] items-center"
@click.self="emit('close')"
>
<div class="dialog-panel flex max-h-[88vh] w-[min(96vw,1280px)] max-w-[1280px] flex-col">
<div class="dialog-header">
<div>
<h3 class="section-title">查询结果全量预览</h3>
<p class="section-desc mt-1">{{ state.query || state.title }}</p>
</div>
<button type="button" class="btn-icon-ds" aria-label="关闭" @click="emit('close')">
<X class="h-5 w-5" />
</button>
</div>
<div class="dialog-body flex min-h-0 flex-1 flex-col gap-4 border-t-0 pt-4">
<div class="flex flex-wrap items-center justify-between gap-3 rounded-xl border border-default bg-surface-soft px-4 py-3 text-sm text-secondary">
<div>共 {{ state.total || 0 }} 条,当前第 {{ state.page }} / {{ totalPages }} 页</div>
<div class="flex items-center gap-2">
<button
type="button"
class="rounded-lg border border-default px-3 py-1.5 text-xs transition-colors hover:bg-surface disabled:cursor-not-allowed disabled:opacity-50"
:disabled="loading || state.page <= 1"
@click="emit('change-page', -1)"
>
上一页
</button>
<button
type="button"
class="rounded-lg border border-default px-3 py-1.5 text-xs transition-colors hover:bg-surface disabled:cursor-not-allowed disabled:opacity-50"
:disabled="loading || state.page >= totalPages"
@click="emit('change-page', 1)"
>
下一页
</button>
</div>
</div>
<div v-if="error" class="rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700">
{{ error }}
</div>
<div class="min-h-0 flex-1 overflow-auto rounded-2xl border border-default bg-surface">
<div v-if="loading" class="flex h-56 items-center justify-center">
<Loader2 class="h-6 w-6 animate-spin text-brand-500" />
</div>
<div v-else-if="!state.columns.length" class="flex h-56 items-center justify-center text-sm text-muted">
暂无可预览数据
</div>
<table v-else class="min-w-full border-collapse text-sm">
<thead class="sticky top-0 z-10 bg-slate-50">
<tr>
<th
v-for="column in state.columns"
:key="`query-preview-col-${column}`"
:class="[
'border border-slate-200 px-3 py-2 text-left font-semibold',
isHighlightColumn(column) ? 'text-blue-800' : 'text-primary',
]"
>
{{ column }}
</th>
</tr>
</thead>
<tbody>
<tr
v-for="(row, rowIndex) in state.rows"
:key="`query-preview-row-${rowIndex}`"
class="odd:bg-white even:bg-slate-50/50"
>
<td
v-for="(cell, cellIndex) in row"
:key="`query-preview-cell-${rowIndex}-${cellIndex}`"
class="max-w-[320px] border border-slate-200 px-3 py-2 align-top text-secondary"
>
<button
v-if="canOpenAssetHistory(row, cellIndex)"
type="button"
class="whitespace-pre-wrap break-all text-left font-semibold text-blue-700 underline-offset-2 transition hover:text-blue-800 hover:underline"
@click="emitAssetAction(row, cellIndex)"
>
{{ cell ?? '-' }}
</button>
<div v-else class="whitespace-pre-wrap break-all">{{ cell ?? '-' }}</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</template>