kongdeqiang
2026-10-12 285bf234bd50ce54fcae7bb777f8fa579b91266e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<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>