ai-chat-ui/src/components/sidebar/ConversationItem.vue

351 lines
6.5 KiB
Vue

<template>
<div
class="conversation-item group"
:class="{
active: isActive,
pinned: conversation.pinned,
selected: isSelected,
'select-mode': isSelectMode,
}"
@click="handleClick"
@dblclick="handleRename"
>
<!-- 选择模式复选框 -->
<div v-if="isSelectMode" class="item-checkbox" @click.stop="handleToggleSelect">
<div class="checkbox" :class="{ checked: isSelected }">
<Check v-if="isSelected" :size="14" />
</div>
</div>
<!-- 图标 -->
<div v-if="!isSelectMode" class="item-icon">
<MessageSquare :size="18" />
</div>
<!-- 内容 -->
<div class="item-content">
<div v-if="!isEditing" class="item-title">
{{ conversation.title }}
</div>
<input
v-else
ref="inputRef"
v-model="editTitle"
class="item-title-input"
@blur="handleSaveRename"
@keydown.enter="handleSaveRename"
@keydown.escape="handleCancelRename"
@click.stop
/>
<div class="item-meta">
<Clock :size="12" />
<span>{{ formattedTime }}</span>
</div>
</div>
<!-- 置顶标识 -->
<div v-if="conversation.pinned && !isSelectMode" class="pin-indicator">
<Pin :size="12" />
</div>
<!-- 操作按钮 (非选择模式显示) -->
<div v-if="!isSelectMode" class="item-actions" @click.stop>
<button
class="action-btn"
:title="conversation.pinned ? '取消置顶' : '置顶'"
@click="handleTogglePin"
>
<PinOff v-if="conversation.pinned" :size="14" />
<Pin v-else :size="14" />
</button>
<button class="action-btn" title="重命名" @click="handleRename">
<Edit3 :size="14" />
</button>
<button class="action-btn delete" title="删除" @click="handleDelete">
<Trash2 :size="14" />
</button>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, nextTick } from "vue";
import {
MessageSquare,
Pin,
PinOff,
Edit3,
Trash2,
Clock,
Check,
} from "@/components/icons";
import { formatTimestamp } from "@/utils/helpers";
import type { Conversation } from "@/types/chat";
const props = defineProps<{
conversation: Conversation;
isActive: boolean;
isSelectMode?: boolean;
isSelected?: boolean;
}>();
const emit = defineEmits<{
select: [id: string];
delete: [id: string];
rename: [id: string, title: string];
togglePin: [id: string];
toggleSelect: [id: string];
}>();
const isEditing = ref(false);
const editTitle = ref("");
const inputRef = ref<HTMLInputElement | null>(null);
const formattedTime = computed(() => {
return formatTimestamp(props.conversation.updatedAt);
});
function handleClick() {
if (props.isSelectMode) {
emit("toggleSelect", props.conversation.id);
} else if (!isEditing.value) {
emit("select", props.conversation.id);
}
}
function handleToggleSelect() {
emit("toggleSelect", props.conversation.id);
}
function handleTogglePin() {
emit("togglePin", props.conversation.id);
}
function handleRename() {
if (props.isSelectMode) return;
isEditing.value = true;
editTitle.value = props.conversation.title;
nextTick(() => {
inputRef.value?.focus();
inputRef.value?.select();
});
}
function handleSaveRename() {
if (editTitle.value.trim()) {
emit("rename", props.conversation.id, editTitle.value.trim());
}
isEditing.value = false;
}
function handleCancelRename() {
isEditing.value = false;
editTitle.value = "";
}
function handleDelete() {
if (confirm("确定要删除这个对话吗?")) {
emit("delete", props.conversation.id);
}
}
</script>
<style lang="scss" scoped>
.conversation-item {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 12px;
margin: 2px 8px;
border-radius: 10px;
cursor: pointer;
transition: all 0.2s ease;
position: relative;
&:hover {
background: rgba(0, 0, 0, 0.05);
.dark & {
background: rgba(255, 255, 255, 0.05);
}
.item-actions {
opacity: 1;
pointer-events: auto;
}
.pin-indicator {
opacity: 0;
}
}
&.active {
background: rgba(59, 130, 246, 0.1);
.dark & {
background: rgba(59, 130, 246, 0.2);
}
.item-icon {
color: #3b82f6;
}
}
}
.item-icon {
flex-shrink: 0;
color: #6b7280;
.dark & {
color: #9ca3af;
}
}
.item-content {
flex: 1;
min-width: 0;
overflow: hidden;
}
.item-title {
font-size: 14px;
font-weight: 500;
color: #1f2937;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
.dark & {
color: #f3f4f6;
}
}
.item-title-input {
width: 100%;
font-size: 14px;
font-weight: 500;
color: #1f2937;
background: white;
border: 1px solid #3b82f6;
border-radius: 4px;
padding: 2px 6px;
outline: none;
.dark & {
color: #f3f4f6;
background: #374151;
}
}
.item-meta {
display: flex;
align-items: center;
gap: 4px;
margin-top: 2px;
font-size: 11px;
color: #9ca3af;
.dark & {
color: #6b7280;
}
}
.pin-indicator {
position: absolute;
right: 12px;
color: #f59e0b;
transition: opacity 0.2s ease;
}
.item-actions {
display: flex;
align-items: center;
gap: 2px;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s ease;
}
.action-btn {
display: flex;
align-items: center;
justify-content: center;
width: 26px;
height: 26px;
border: none;
border-radius: 6px;
background: transparent;
color: #6b7280;
cursor: pointer;
transition: all 0.15s ease;
&:hover {
background: rgba(0, 0, 0, 0.1);
color: #374151;
.dark & {
background: rgba(255, 255, 255, 0.1);
color: #e5e7eb;
}
}
&.delete:hover {
background: rgba(239, 68, 68, 0.1);
color: #ef4444;
}
}
// 选择模式样式
.item-checkbox {
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
padding-right: 4px;
}
.checkbox {
width: 18px;
height: 18px;
border: 2px solid #d1d5db;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.15s ease;
cursor: pointer;
.dark & {
border-color: #4b5563;
}
&:hover {
border-color: #3b82f6;
}
&.checked {
background: #3b82f6;
border-color: #3b82f6;
color: white;
}
}
.conversation-item.select-mode {
&:hover {
background: rgba(59, 130, 246, 0.05);
.dark & {
background: rgba(59, 130, 246, 0.1);
}
}
&.selected {
background: rgba(59, 130, 246, 0.1);
.dark & {
background: rgba(59, 130, 246, 0.15);
}
}
}
</style>