<template>
|
<n-drawer v-model:show="visible" :width="460" placement="right">
|
<n-drawer-content closable>
|
<template #header>
|
<div class="drawer-header">
|
<span>归档对话</span>
|
<span class="drawer-count">{{ total }} 个</span>
|
</div>
|
</template>
|
|
<div class="archive-search">
|
<n-input
|
v-model:value="keyword"
|
size="small"
|
clearable
|
placeholder="在归档的对话里搜索"
|
@input="handleInput"
|
/>
|
</div>
|
|
<n-spin v-if="loading || searching" size="small" class="archive-loading" />
|
|
<div v-else-if="visibleChats.length === 0" class="empty-hint archive-empty">
|
<template v-if="isSearching">
|
没有找到匹配「{{ keyword }}」的归档对话
|
</template>
|
<template v-else>
|
还没有归档的对话<br />
|
在对话的「⋯」菜单里选「归档」,聊完的对话就会收进这里
|
</template>
|
</div>
|
|
<div
|
v-for="chat in visibleChats"
|
v-else
|
:key="chat.id"
|
class="archive-item"
|
:class="{ active: chat.id === activeChatId }"
|
@click="$emit('select', chat.id)"
|
>
|
<div class="archive-item-main">
|
<div class="archive-title">{{ chat.title || '新对话' }}</div>
|
<div v-if="chat.matchSnippet" class="archive-snippet">{{ chat.matchSnippet }}</div>
|
<div class="archive-meta">
|
<span>归档于 {{ relative(chat.archivedAt) }}</span>
|
<span class="dot">·</span>
|
<span>{{ chat.messageCount || 0 }} 条消息</span>
|
</div>
|
</div>
|
|
<div class="archive-actions" @click.stop>
|
<n-button text size="tiny" @click="restore(chat)">恢复</n-button>
|
<n-dropdown
|
trigger="click"
|
:options="downloadOptions"
|
placement="bottom-end"
|
@select="(key) => download(chat, key)"
|
>
|
<n-button text size="tiny">下载</n-button>
|
</n-dropdown>
|
<n-button text size="tiny" class="danger" @click="confirmDelete(chat)">删除</n-button>
|
</div>
|
</div>
|
</n-drawer-content>
|
</n-drawer>
|
</template>
|
|
<script setup>
|
import { computed, ref, watch } from 'vue'
|
import {
|
NButton,
|
NDrawer,
|
NDrawerContent,
|
NDropdown,
|
NInput,
|
NSpin,
|
useDialog,
|
useMessage
|
} from 'naive-ui'
|
import { useChatStore } from '@/stores/chatStore'
|
import { formatRelative } from '@/utils/chatTime'
|
|
const props = defineProps({
|
show: { type: Boolean, default: false }
|
})
|
const emit = defineEmits(['update:show', 'select'])
|
|
const chatStore = useChatStore()
|
const message = useMessage()
|
const dialog = useDialog()
|
|
const loading = ref(false)
|
const keyword = ref('')
|
|
/** 抽屉开关:本地代理 v-model:show */
|
const visible = computed({
|
get: () => props.show,
|
set: (value) => emit('update:show', value)
|
})
|
|
const activeChatId = computed(() => chatStore.activeChatId)
|
const total = computed(() => chatStore.archivedChats.length)
|
const isSearching = computed(() => keyword.value.trim().length > 0)
|
const searching = computed(() => chatStore.archiveSearching)
|
|
/**
|
* 归档列表的加载时机。
|
*
|
* 抽屉每次打开都重新拉一次,而不是依赖挂载时的一次加载:
|
* 归档动作可能发生在这个组件之外(侧边栏菜单),
|
* 每次打开都刷新是最省心也最不容易出现「归档了但列表里没有」的做法。
|
*/
|
watch(visible, async (open) => {
|
if (!open) return
|
keyword.value = ''
|
chatStore.clearArchiveSearch()
|
loading.value = true
|
try {
|
await chatStore.loadArchivedChats()
|
} catch (error) {
|
message.error(error.message || '加载归档对话失败')
|
} finally {
|
loading.value = false
|
}
|
})
|
|
/** 搜索态显示结果,否则显示全部归档会话 */
|
const visibleChats = computed(() =>
|
isSearching.value ? chatStore.archiveResults : chatStore.archivedChats
|
)
|
|
let timer = null
|
function handleInput() {
|
if (timer) clearTimeout(timer)
|
const value = keyword.value
|
if (!value.trim()) {
|
chatStore.clearArchiveSearch()
|
return
|
}
|
timer = setTimeout(async () => {
|
try {
|
await chatStore.searchArchive(value)
|
} catch (error) {
|
message.error(error.message || '搜索失败')
|
}
|
}, 250)
|
}
|
|
function relative(value) {
|
return formatRelative(value)
|
}
|
|
const downloadOptions = [
|
{ label: 'JSON(含全部分支)', key: 'json' },
|
{ label: 'TXT(纯文本)', key: 'txt' },
|
{ label: 'PDF(带页码排版)', key: 'pdf' }
|
]
|
|
async function restore(chat) {
|
try {
|
await chatStore.setArchived(chat.id, false)
|
message.success('已恢复到对话列表')
|
} catch (error) {
|
message.error(error.message || '恢复失败')
|
}
|
}
|
|
async function download(chat, format) {
|
try {
|
const filename = await chatStore.exportChat(chat.id, format)
|
message.success(`已导出 ${filename}`)
|
} catch (error) {
|
message.error(error.message || '导出失败')
|
}
|
}
|
|
function confirmDelete(chat) {
|
dialog.warning({
|
title: '删除对话',
|
content: `确定删除「${chat.title || '新对话'}」吗?归档只是收起,删除是**不可恢复**的,`
|
+ '该会话的所有消息都会被一并删除。',
|
positiveText: '删除',
|
negativeText: '取消',
|
onPositiveClick: async () => {
|
try {
|
await chatStore.removeChat(chat.id)
|
message.success('已删除')
|
} catch (error) {
|
message.error(error.message || '删除失败')
|
return false
|
}
|
return true
|
}
|
})
|
}
|
</script>
|
|
<style scoped>
|
.drawer-header {
|
display: flex;
|
align-items: baseline;
|
gap: 8px;
|
}
|
|
.drawer-count {
|
font-size: 12px;
|
color: var(--text-muted);
|
font-weight: 400;
|
}
|
|
.archive-search {
|
margin-bottom: 10px;
|
}
|
|
.archive-loading {
|
display: flex;
|
justify-content: center;
|
padding: 24px 0;
|
}
|
|
.archive-empty {
|
padding: 32px 8px;
|
line-height: 1.9;
|
text-align: center;
|
}
|
|
.archive-item {
|
display: flex;
|
align-items: center;
|
gap: 10px;
|
padding: 10px;
|
border-radius: 8px;
|
cursor: pointer;
|
border: 1px solid transparent;
|
}
|
|
.archive-item:hover {
|
background: var(--bg-hover);
|
}
|
|
.archive-item.active {
|
background: #eef2f7;
|
border-color: #cbd5e1;
|
}
|
|
.archive-item-main {
|
flex: 1;
|
min-width: 0;
|
}
|
|
.archive-title {
|
font-size: 13.5px;
|
font-weight: 500;
|
overflow: hidden;
|
text-overflow: ellipsis;
|
white-space: nowrap;
|
}
|
|
.archive-snippet {
|
font-size: 11.5px;
|
color: var(--text-secondary);
|
margin-top: 3px;
|
display: -webkit-box;
|
-webkit-line-clamp: 2;
|
-webkit-box-orient: vertical;
|
overflow: hidden;
|
}
|
|
.archive-meta {
|
font-size: 11.5px;
|
color: var(--text-muted);
|
margin-top: 3px;
|
}
|
|
.dot {
|
margin: 0 4px;
|
}
|
|
.archive-actions {
|
display: flex;
|
align-items: center;
|
gap: 6px;
|
flex-shrink: 0;
|
opacity: 0;
|
transition: opacity 0.12s ease;
|
}
|
|
.archive-item:hover .archive-actions {
|
opacity: 1;
|
}
|
|
.danger {
|
color: #d03050;
|
}
|
</style>
|