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
<template>
  <div
    class="chat-item"
    :class="{ active: active, indented: indented }"
    :title="fullTime(chat.updatedAt)"
    @click="$emit('select', chat.id)"
  >
    <div class="chat-item-main">
      <!-- 双击进入就地重命名,符合桌面端文件管理的直觉 -->
      <n-input
        v-if="editing"
        ref="renameInputRef"
        v-model:value="editingTitle"
        size="tiny"
        @blur="commitRename"
        @keyup.enter="commitRename"
        @keyup.esc="cancelRename"
        @click.stop
      />
      <div v-else class="chat-title" @dblclick.stop="startRename">
        <span v-if="chat.pinned" class="pin-icon" title="已置顶">📌</span>
        {{ chat.title || '新对话' }}
      </div>
 
      <!-- 命中消息内容时展示片段(搜索结果用),方便确认是不是这次对话 -->
      <div v-if="chat.matchSnippet" class="chat-snippet">{{ chat.matchSnippet }}</div>
 
      <div class="chat-meta">
        <span class="chat-date">{{ time(chat.updatedAt) }}</span>
        <span class="chat-dot">·</span>
        <span>{{ chat.messageCount || 0 }} 条消息</span>
        <span v-if="chat.collectionNames?.length" class="chat-rag-tag">
          · {{ chat.collectionNames.length }} 个知识库
        </span>
        <span v-if="chat.matchType" class="match-badge">{{ matchLabel }}</span>
      </div>
    </div>
 
    <n-dropdown
      trigger="click"
      :options="options"
      placement="bottom-end"
      @select="(key) => $emit('menu', key, chat)"
    >
      <n-button text class="chat-more" @click.stop>⋯</n-button>
    </n-dropdown>
  </div>
</template>
 
<script setup>
import { computed, nextTick, ref } from 'vue'
import { NButton, NDropdown, NInput, useMessage } from 'naive-ui'
import { useChatStore } from '@/stores/chatStore'
import { formatChatTime, formatFullTime } from '@/utils/chatTime'
 
/**
 * 侧边栏里的一条会话。
 *
 * 抽成组件是因为同一个条目现在要出现在三个地方:搜索结果、时间分组、
 * 自定义分组。复制三份模板意味着以后改一处样式要记得改三处
 * (这个项目的图片显示 bug 就是这么来的)。
 */
const props = defineProps({
  chat: { type: Object, required: true },
  active: { type: Boolean, default: false },
  /** 分组内的条目缩进一层,体现层级 */
  indented: { type: Boolean, default: false },
  /** 菜单项由父组件提供:不同位置(搜索态/分组内)可用操作不同 */
  options: { type: Array, required: true }
})
 
const emit = defineEmits(['select', 'menu'])
 
const chatStore = useChatStore()
const toast = useMessage()
 
// 就地重命名:条目自己管理编辑态,父组件不必为「哪一条在编辑」维护状态
const editing = ref(false)
const editingTitle = ref('')
const renameInputRef = ref(null)
 
function time(value) {
  return formatChatTime(value)
}
 
function fullTime(value) {
  return formatFullTime(value)
}
 
const matchLabel = computed(() => {
  const type = props.chat.matchType
  if (type === 'title') return '标题命中'
  if (type === 'both') return '标题+内容'
  return '内容命中'
})
 
function startRename() {
  if (editing.value) return
  editing.value = true
  editingTitle.value = props.chat.title || ''
  nextTick(() => renameInputRef.value?.focus?.())
}
 
function cancelRename() {
  editing.value = false
  editingTitle.value = ''
}
 
async function commitRename() {
  const title = editingTitle.value.trim()
  // 没改动或为空则放弃,避免产生空标题
  if (!title || title === props.chat.title) {
    cancelRename()
    return
  }
  try {
    await chatStore.renameChat(props.chat.id, title)
    toast.success('已重命名')
  } catch (error) {
    toast.error(error.message || '重命名失败')
  } finally {
    cancelRename()
  }
}
 
defineExpose({ startRename })
</script>
 
<style scoped>
.chat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  transition: background 0.12s ease;
}
 
.chat-item:hover {
  background: var(--bg-hover);
}
 
.chat-item.active {
  background: #e3e5e8;
}
 
/* 分组内的条目缩进,体现「它在这个文件夹里」 */
.chat-item.indented {
  padding-left: 20px;
}
 
.chat-item-main {
  flex: 1;
  min-width: 0;
}
 
.chat-title {
  font-size: 13.5px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
 
.pin-icon {
  font-size: 11px;
  margin-right: 2px;
}
 
.chat-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;
  line-height: 1.5;
}
 
.chat-meta {
  font-size: 11.5px;
  color: var(--text-muted);
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
 
.chat-date {
  /* 等宽数字:多行日期在视觉上对齐,扫视时不会参差 */
  font-variant-numeric: tabular-nums;
}
 
.chat-dot {
  margin: 0 4px;
}
 
.chat-rag-tag {
  color: #6b7280;
}
 
.match-badge {
  color: #0f766e;
  margin-left: 6px;
}
 
.chat-more {
  opacity: 0;
  font-size: 16px;
  line-height: 1;
  padding: 0 4px;
  color: var(--text-secondary);
  transition: opacity 0.12s ease;
}
 
.chat-item:hover .chat-more,
.chat-item.active .chat-more {
  opacity: 1;
}
</style>