<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>
|