From 285bf234bd50ce54fcae7bb777f8fa579b91266e Mon Sep 17 00:00:00 2001
From: kongdeqiang <123456>
Date: 星期一, 12 十月 2026 09:53:45 +0800
Subject: [PATCH] fix: 新增分组

---
 src/components/Sidebar.vue |  893 +++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 657 insertions(+), 236 deletions(-)

diff --git a/src/components/Sidebar.vue b/src/components/Sidebar.vue
index 3c549fc..b6ab2d9 100644
--- a/src/components/Sidebar.vue
+++ b/src/components/Sidebar.vue
@@ -25,84 +25,138 @@
     </div>
 
     <div class="sidebar-list">
-      <!-- 鎼滅储鎬侊細鏄剧ず缁撴灉锛屽懡涓唴瀹规椂缁欏嚭涓婁笅鏂囩墖娈� -->
+      <!-- 鎼滅储鎬侊細璺ㄥ叏閮ㄤ細璇濇悳锛堝惈鍒嗙粍閲岀殑锛夛紝鍛戒腑鍐呭鏃剁粰鍑轰笂涓嬫枃鐗囨 -->
       <template v-if="isSearching">
         <n-spin v-if="chatStore.searching" size="small" class="sidebar-loading" />
 
-        <div v-else-if="visibleChats.length === 0" class="empty-hint sidebar-empty">
+        <div v-else-if="chatStore.searchResults.length === 0" class="empty-hint sidebar-empty">
           娌℃湁鎵惧埌鍖归厤銆寋{ chatStore.searchKeyword }}銆嶇殑瀵硅瘽<br />
           璇曡瘯鍏朵粬鍏抽敭璇�
         </div>
 
-        <div
-          v-for="chat in visibleChats"
+        <ChatListItem
+          v-for="chat in chatStore.searchResults"
           v-else
           :key="chat.id"
-          class="chat-item"
-          :class="{ active: chat.id === activeChatId }"
-          @click="$emit('select', chat.id)"
-        >
-          <div class="chat-item-main">
-            <div class="chat-title">{{ chat.title || '鏂板璇�' }}</div>
-            <!-- 鍛戒腑娑堟伅鍐呭鏃跺睍绀虹墖娈碉紝鏂逛究纭鏄笉鏄繖娆″璇� -->
-            <div v-if="chat.matchSnippet" class="chat-snippet">
-              {{ chat.matchSnippet }}
-            </div>
-            <div class="chat-meta">
-              <span>{{ chat.messageCount || 0 }} 鏉℃秷鎭�</span>
-              <span class="match-badge">{{ matchLabel(chat.matchType) }}</span>
-            </div>
-          </div>
-        </div>
+          :chat="chat"
+          :active="chat.id === activeChatId"
+          :options="menuFor(chat)"
+          @select="(id) => $emit('select', id)"
+          @menu="handleMenu"
+        />
       </template>
 
       <!-- 鏅�氭�� -->
       <template v-else>
         <n-spin v-if="loading" size="small" class="sidebar-loading" />
 
+        <!-- ============ 鍒嗙粍鍖哄潡 ============ -->
+        <div class="section">
+          <div class="section-head" @click="toggleGroupSection()">
+            <span class="chevron" :class="{ collapsed: groupSectionCollapsed }">鈻�</span>
+            <span class="section-title">鍒嗙粍</span>
+            <span class="section-count">{{ sections.groups.length || '' }}</span>
+            <n-button
+              text
+              class="section-add"
+              title="鏂板缓鍒嗙粍"
+              @click.stop="openCreateGroup"
+            >
+              +
+            </n-button>
+          </div>
+
+          <template v-if="!groupSectionCollapsed">
+            <div v-if="sections.groups.length === 0" class="section-empty">
+              杩樻病鏈夊垎缁勶紝鐐瑰彸涓婅銆�+銆嶆柊寤�
+            </div>
+
+            <div v-for="group in sections.groups" :key="group.id" class="group-block">
+              <div class="group-head" @click="toggleGroup(group.id)">
+                <span class="chevron small" :class="{ collapsed: isGroupCollapsed(group.id) }">鈻�</span>
+                <span class="group-name" :title="group.name">{{ group.name }}</span>
+                <span class="group-count">{{ group.chats.length }}</span>
+
+                <n-dropdown
+                  trigger="click"
+                  :options="groupMenuOptions"
+                  placement="bottom-end"
+                  @select="(key) => handleGroupMenu(key, group)"
+                >
+                  <n-button text class="group-more" @click.stop>鈰�</n-button>
+                </n-dropdown>
+              </div>
+
+              <template v-if="!isGroupCollapsed(group.id)">
+                <div v-if="group.chats.length === 0" class="group-empty">
+                  绌哄垎缁� 鈥斺�� 鐢ㄥ璇濆彸涓婅鐨勩�屸嫰 鈫� 绉诲姩鍒板垎缁勩�嶆斁杩涙潵
+                </div>
+                <ChatListItem
+                  v-for="chat in group.chats"
+                  v-else
+                  :key="chat.id"
+                  :chat="chat"
+                  :active="chat.id === activeChatId"
+                  indented
+                  :options="menuFor(chat)"
+                  @select="(id) => $emit('select', id)"
+                  @menu="handleMenu"
+                />
+              </template>
+            </div>
+          </template>
+        </div>
+
+        <!-- ============ 缃《锛堟湭鍒嗙粍鐨勭疆椤朵細璇濓級============ -->
+        <div v-if="sections.pinned.length" class="section">
+          <div class="section-head static">
+            <span class="chevron">鈻�</span>
+            <span class="section-title">缃《</span>
+          </div>
+          <ChatListItem
+            v-for="chat in sections.pinned"
+            :key="chat.id"
+            :chat="chat"
+            :active="chat.id === activeChatId"
+            :options="menuFor(chat)"
+            @select="(id) => $emit('select', id)"
+            @menu="handleMenu"
+          />
+        </div>
+
+        <!-- ============ 鏈垎缁勭殑浼氳瘽锛屾寜鏃堕棿褰掔被 ============ -->
         <div v-if="!loading && chats.length === 0" class="empty-hint sidebar-empty">
           杩樻病鏈夊璇�<br />鐐瑰嚮涓婃柟銆屾柊瀵硅瘽銆嶅紑濮�
         </div>
 
-        <div
-          v-for="chat in chats"
-          :key="chat.id"
-          class="chat-item"
-          :class="{ active: chat.id === activeChatId }"
-          @click="$emit('select', chat.id)"
-        >
-        <div class="chat-item-main">
-          <!-- 鍙屽嚮杩涘叆閲嶅懡鍚嶏紝绗﹀悎妗岄潰绔枃浠剁鐞嗙殑鐩磋 -->
-          <n-input
-            v-if="editingId === chat.id"
-            ref="renameInputRef"
-            v-model:value="editingTitle"
-            size="tiny"
-            @blur="commitRename(chat)"
-            @keyup.enter="commitRename(chat)"
-            @keyup.esc="cancelRename"
-            @click.stop
+        <div v-for="group in sections.timeGroups" :key="group.key" class="chat-group">
+          <div class="chat-group-title">{{ group.label }}</div>
+          <ChatListItem
+            v-for="chat in group.chats"
+            :key="chat.id"
+            :chat="chat"
+            :active="chat.id === activeChatId"
+            :options="menuFor(chat)"
+            @select="(id) => $emit('select', id)"
+            @menu="handleMenu"
           />
-          <div v-else class="chat-title" @dblclick.stop="startRename(chat)">
-            {{ chat.title || '鏂板璇�' }}
-          </div>
-          <div class="chat-meta">
-            <span>{{ chat.messageCount || 0 }} 鏉℃秷鎭�</span>
-            <span v-if="chat.collectionNames?.length" class="chat-rag-tag">
-              路 {{ chat.collectionNames.length }} 涓煡璇嗗簱
-            </span>
-          </div>
-        </div>
-
-          <n-dropdown
-            trigger="click"
-            :options="menuOptions"
-            @select="(key) => handleMenu(key, chat)"
-          >
-            <n-button text class="chat-more" @click.stop>鈰�</n-button>
-          </n-dropdown>
         </div>
       </template>
+    </div>
+
+    <!-- 褰掓。鍏ュ彛 -->
+    <div class="sidebar-archive">
+      <div class="archive-entry" @click="archiveVisible = true">
+        <span class="archive-icon">馃梽</span>
+        <span class="archive-text">褰掓。瀵硅瘽</span>
+        <n-badge
+          v-if="archivedCount > 0"
+          :value="archivedCount"
+          :max="99"
+          type="info"
+          class="archive-badge"
+        />
+      </div>
     </div>
 
     <div class="sidebar-footer">
@@ -117,14 +171,80 @@
         </div>
       </n-dropdown>
     </div>
+
+    <!-- 鏂板缓 / 閲嶅懡鍚嶅垎缁勫叡鐢ㄥ悓涓�涓脊绐� -->
+    <n-modal
+      v-model:show="groupModal.show"
+      preset="card"
+      :title="groupModal.id ? '閲嶅懡鍚嶅垎缁�' : '鏂板缓鍒嗙粍'"
+      class="small-modal"
+      :bordered="false"
+    >
+      <n-input
+        v-model:value="groupModal.name"
+        placeholder="鍒嗙粍鍚嶇О锛屼緥濡傘�屽伐浣溿�嶃�屽涔犺祫鏂欍��"
+        maxlength="64"
+        show-count
+        @keyup.enter="confirmGroupModal"
+      />
+      <template #footer>
+        <div class="modal-footer">
+          <n-button size="small" @click="groupModal.show = false">鍙栨秷</n-button>
+          <n-button
+            size="small"
+            type="primary"
+            :loading="groupModal.saving"
+            @click="confirmGroupModal"
+          >
+            淇濆瓨
+          </n-button>
+        </div>
+      </template>
+    </n-modal>
+
+    <!-- 閲嶅懡鍚嶅璇� -->
+    <n-modal
+      v-model:show="renameModal.show"
+      preset="card"
+      title="閲嶅懡鍚嶅璇�"
+      class="small-modal"
+      :bordered="false"
+    >
+      <n-input
+        v-model:value="renameModal.title"
+        placeholder="杈撳叆鏂扮殑瀵硅瘽鍚嶇О"
+        maxlength="255"
+        show-count
+        @keyup.enter="confirmRenameModal"
+      />
+      <template #footer>
+        <div class="modal-footer">
+          <n-button size="small" @click="renameModal.show = false">鍙栨秷</n-button>
+          <n-button
+            size="small"
+            type="primary"
+            :loading="renameModal.saving"
+            @click="confirmRenameModal"
+          >
+            淇濆瓨
+          </n-button>
+        </div>
+      </template>
+    </n-modal>
+
+    <ArchivedChatDrawer v-model:show="archiveVisible" @select="handleArchivedSelect" />
   </div>
 </template>
 
 <script setup>
-import { computed, nextTick, ref } from 'vue'
-import { NButton, NDropdown, NInput, NSpin, useDialog, useMessage } from 'naive-ui'
+import { computed, ref } from 'vue'
+import { NBadge, NButton, NDropdown, NInput, NModal, NSpin, useDialog, useMessage } from 'naive-ui'
+import ArchivedChatDrawer from '@/components/ArchivedChatDrawer.vue'
+import ChatListItem from '@/components/ChatListItem.vue'
 import { useAuthStore } from '@/stores/authStore'
 import { useChatStore } from '@/stores/chatStore'
+import { buildSidebarSections } from '@/utils/sidebarGroups'
+import { usePersistentFlag, usePersistentMap } from '@/utils/uiPrefs'
 
 const emit = defineEmits(['select', 'created', 'deleted', 'open-files', 'open-admin-knowledge'])
 
@@ -134,31 +254,311 @@
 const dialog = useDialog()
 
 const loading = ref(false)
-const editingId = ref(null)
-const editingTitle = ref('')
-const renameInputRef = ref(null)
+const archiveVisible = ref(false)
 
 /** 鎼滅储妗嗙殑鏈湴杈撳叆鍊硷紙涓� store 閲岀殑宸叉彁浜ゅ叧閿瘝鍖哄垎寮�锛� */
 const keyword = ref('')
 
+/**
+ * 鎶樺彔鐘舵�侊細绾鍥惧亸濂斤紝瀛� localStorage锛屼笉鎵撴壈鍚庣銆�
+ *
+ * 銆屾暣涓垎缁勫尯鍧椼�嶇殑寮�鍏宠В鏋勬垚椤跺眰 ref 鈥斺�� Vue 鍙細鑷姩瑙e寘
+ * setup 椤跺眰杩斿洖鐨� ref锛屽鏋滄妸瀹冪暀鍦ㄥ璞¢噷锛坒lag.value锛夛紝
+ * 妯℃澘閲屽氨寰楀啓 flag.value.value锛屾瀬鏄撳啓閿欍��
+ */
+const {
+  collapsed: groupSectionCollapsed,
+  toggle: toggleGroupSection
+} = usePersistentFlag('group_section_collapsed', false)
+const groupCollapse = usePersistentMap('group_collapsed')
+
 const chats = computed(() => chatStore.chats)
 const activeChatId = computed(() => chatStore.activeChatId)
-
-/** 鏄惁澶勪簬鎼滅储鎬侊細鍙鍏抽敭璇嶉潪绌哄氨鏄剧ず鎼滅储缁撴灉鑰岄潪瀹屾暣鍒楄〃 */
+const archivedCount = computed(() => chatStore.archivedChats.length)
 const isSearching = computed(() => chatStore.searchKeyword.length > 0)
 
-/** 鎼滅储鎬佹樉绀虹粨鏋滃垪琛紝鏅�氭�佹樉绀哄叏閮ㄤ細璇� */
-const visibleChats = computed(() =>
-  isSearching.value ? chatStore.searchResults : chatStore.chats
+/**
+ * 渚ц竟鏍忕粨鏋勶細鍒嗙粍 / 缃《 / 浠婂ぉ / 鏄ㄥぉ / 鏇存棭銆�
+ *
+ * 绾绠楀睘鎬� 鈥斺�� 缂栨帓瑙勫垯閮藉湪 buildSidebarSections 閲岋紝
+ * 缁勪欢鍙礋璐f覆鏌擄紝瑙勫垯涔熻兘鍗曠嫭娴嬨��
+ */
+const sections = computed(() =>
+  buildSidebarSections(chats.value, chatStore.chatGroups)
 )
 
+function isGroupCollapsed(id) {
+  return groupCollapse.isCollapsed(id)
+}
+
+function toggleGroup(id) {
+  groupCollapse.toggle(id)
+}
+
+const avatarText = computed(() => (auth.displayName || '?').charAt(0).toUpperCase())
+
+// ---------------- 寮圭獥鐘舵�� ----------------
+
+const groupModal = ref({ show: false, id: null, name: '', saving: false })
+const renameModal = ref({ show: false, id: null, title: '', saving: false })
+
+function openCreateGroup() {
+  groupModal.value = { show: true, id: null, name: '', saving: false }
+}
+
+function openRenameGroup(group) {
+  groupModal.value = { show: true, id: group.id, name: group.name, saving: false }
+}
+
+async function confirmGroupModal() {
+  const name = groupModal.value.name.trim()
+  if (!name) {
+    message.warning('鍒嗙粍鍚嶄笉鑳戒负绌�')
+    return
+  }
+  groupModal.value.saving = true
+  try {
+    if (groupModal.value.id) {
+      await chatStore.renameChatGroup(groupModal.value.id, name)
+      message.success('鍒嗙粍宸查噸鍛藉悕')
+    } else {
+      await chatStore.createChatGroup(name)
+      message.success('鍒嗙粍宸插垱寤�')
+    }
+    groupModal.value.show = false
+  } catch (error) {
+    // 閲嶅悕绛変笟鍔¢敊璇敱鍚庣缁欏嚭浜鸿瘽鎻愮ず锛岃繖閲岀洿鎺ュ睍绀�
+    message.error(error.message || '鎿嶄綔澶辫触')
+  } finally {
+    groupModal.value.saving = false
+  }
+}
+
+// ---------------- 鑿滃崟 ----------------
+
 /**
- * 杈撳叆闃叉姈銆�
+ * 姣忔潯浼氳瘽鐨勮彍鍗曘��
  *
- * 銆岃緭鍏ュ嵆鏌ャ�嶈嫢涓嶉槻鎶栵紝姣忔暡涓�涓瓧閮戒細鎵撲竴娆℃帴鍙o紝
- * 鑰屼笖涓枃杈撳叆娉曞湪鎷奸煶闃舵涔熶細棰戠箒瑙﹀彂 input 浜嬩欢銆�
- * 250ms 鏄墦瀛楀仠椤夸笌鍝嶅簲閫熷害涔嬮棿鐨勫父瑙佹姌涓��
+ * 銆岀Щ鍔ㄥ埌鍒嗙粍銆嶇殑瀛愯彍鍗曞垪鍑恒�屾湭鍒嗙粍 + 鎵�鏈夊垎缁勩�嶏紝褰撳墠鎵�鍦ㄧ殑閭d竴椤规墦鍕� 鈥斺��
+ * 鐢ㄦ埛涓嶅繀鍏堣浣忓畠鍦ㄥ摢涓垎缁勯噷銆�
  */
+function menuFor(chat) {
+  const moveChildren = [
+    {
+      label: chat.groupId == null ? '鉁� 鏈垎缁�' : '鏈垎缁�',
+      key: 'move-none'
+    },
+    ...chatStore.chatGroups.map((group) => ({
+      label: (chat.groupId === group.id ? '鉁� ' : '') + group.name,
+      key: 'move-' + group.id
+    }))
+  ]
+
+  return [
+    { label: chat.pinned ? '鍙栨秷缃《' : '缃《', key: 'pin' },
+    { label: '閲嶅懡鍚�', key: 'rename' },
+    { label: '绉诲姩鍒板垎缁�', key: 'move', children: moveChildren },
+    {
+      label: '涓嬭浇',
+      key: 'download',
+      children: [
+        { label: 'JSON锛堝惈鍏ㄩ儴鍒嗘敮锛�', key: 'download-json' },
+        { label: 'TXT锛堢函鏂囨湰锛�', key: 'download-txt' },
+        { label: 'PDF锛堝甫椤电爜鎺掔増锛�', key: 'download-pdf' }
+      ]
+    },
+    { label: '褰掓。', key: 'archive' },
+    { type: 'divider', key: 'd1' },
+    { label: '鐭ヨ瘑搴撹缃�', key: 'rag' },
+    { label: '鍒犻櫎瀵硅瘽', key: 'delete', props: { style: 'color: #d03050;' } }
+  ]
+}
+
+const groupMenuOptions = [
+  { label: '閲嶅懡鍚嶅垎缁�', key: 'rename' },
+  { label: '鍒犻櫎鍒嗙粍', key: 'delete', props: { style: 'color: #d03050;' } }
+]
+
+const userMenuOptions = computed(() => {
+  const options = [{ label: '鎴戠殑鐭ヨ瘑搴�', key: 'files' }]
+  // 绠$悊鍛橀澶栨彁渚涘叏灞�鐭ヨ瘑搴撶淮鎶ゅ叆鍙c��
+  // 鍚庣瀵� /api/files/global/upload 绛夌鐐瑰彟鏈� 403 鏍¢獙锛�
+  // 杩欓噷鐨勯殣钘忓彧鏄伩鍏嶆櫘閫氱敤鎴风湅鍒版棤鏉冮檺鐨勮彍鍗曢」銆�
+  if (auth.isAdmin) {
+    options.push({ label: '鍏ㄥ眬鐭ヨ瘑搴擄紙绠$悊鍛橈級', key: 'admin-knowledge' })
+  }
+  options.push({ type: 'divider', key: 'd1' })
+  options.push({ label: '閫�鍑虹櫥褰�', key: 'logout' })
+  return options
+})
+
+async function handleMenu(key, chat) {
+  if (key.startsWith('move-')) {
+    const raw = key.slice('move-'.length)
+    const groupId = raw === 'none' ? null : Number(raw)
+    await moveChat(chat, groupId)
+    return
+  }
+
+  switch (key) {
+    case 'pin':
+      await togglePin(chat)
+      break
+    case 'rename':
+      renameModal.value = { show: true, id: chat.id, title: chat.title || '', saving: false }
+      break
+    case 'download-json':
+      await download(chat, 'json')
+      break
+    case 'download-txt':
+      await download(chat, 'txt')
+      break
+    case 'download-pdf':
+      await download(chat, 'pdf')
+      break
+    case 'archive':
+      archive(chat)
+      break
+    case 'rag':
+      emit('select', chat.id)
+      emit('open-files')
+      break
+    case 'delete':
+      confirmDelete(chat)
+      break
+    default:
+      break
+  }
+}
+
+async function handleGroupMenu(key, group) {
+  if (key === 'rename') {
+    openRenameGroup(group)
+    return
+  }
+  if (key === 'delete') {
+    dialog.warning({
+      title: '鍒犻櫎鍒嗙粍',
+      content: `纭畾鍒犻櫎鍒嗙粍銆�${group.name}銆嶅悧锛熺粍鍐呯殑 ${group.chats.length} 鏉″璇漙
+        + '**涓嶄細琚垹闄�**锛屽畠浠細鍥炲埌銆屾湭鍒嗙粍銆嶏紝缁х画鎸夋椂闂村綊绫汇��',
+      positiveText: '鍒犻櫎鍒嗙粍',
+      negativeText: '鍙栨秷',
+      onPositiveClick: async () => {
+        try {
+          const moved = await chatStore.deleteChatGroup(group.id)
+          message.success(moved > 0
+            ? `鍒嗙粍宸插垹闄わ紝${moved} 鏉″璇濆凡鍥炲埌鏈垎缁刞
+            : '鍒嗙粍宸插垹闄�')
+        } catch (error) {
+          message.error(error.message || '鍒犻櫎鍒嗙粍澶辫触')
+          return false
+        }
+        return true
+      }
+    })
+  }
+}
+
+async function moveChat(chat, groupId) {
+  if ((chat.groupId ?? null) === groupId) {
+    return
+  }
+  try {
+    await chatStore.moveChatToGroup(chat.id, groupId)
+    const name = groupId == null
+      ? '鏈垎缁�'
+      : chatStore.chatGroups.find((g) => g.id === groupId)?.name || '鍒嗙粍'
+    message.success(`宸茬Щ鍔ㄥ埌銆�${name}銆峘)
+  } catch (error) {
+    message.error(error.message || '绉诲姩澶辫触')
+  }
+}
+
+async function togglePin(chat) {
+  try {
+    await chatStore.setPinned(chat.id, !chat.pinned)
+    message.success(chat.pinned ? '宸插彇娑堢疆椤�' : '宸茬疆椤�')
+  } catch (error) {
+    message.error(error.message || '鎿嶄綔澶辫触')
+  }
+}
+
+const downloading = ref(false)
+
+async function download(chat, format) {
+  if (downloading.value) return
+  downloading.value = true
+  message.loading(`姝e湪瀵煎嚭 ${format.toUpperCase()}鈥, { duration: 1200 })
+  try {
+    const filename = await chatStore.exportChat(chat.id, format)
+    message.success(`宸插鍑� ${filename}`)
+  } catch (error) {
+    message.error(error.message || '瀵煎嚭澶辫触')
+  } finally {
+    downloading.value = false
+  }
+}
+
+function archive(chat) {
+  dialog.create({
+    title: '褰掓。瀵硅瘽',
+    content: `銆�${chat.title || '鏂板璇�'}銆嶅皢浠庡垪琛ㄦ敹璧枫�傛秷鎭笌鐭ヨ瘑搴撶粦瀹氶兘浼氫繚鐣欙紝`
+      + '涔嬪悗鍙互鍦ㄣ�屽綊妗e璇濄�嶉噷鎵惧埌骞舵仮澶嶃��',
+    positiveText: '褰掓。',
+    negativeText: '鍙栨秷',
+    onPositiveClick: async () => {
+      try {
+        await chatStore.setArchived(chat.id, true)
+        message.success('宸插綊妗o紝鍙湪銆屽綊妗e璇濄�嶄腑鏌ョ湅')
+      } catch (error) {
+        message.error(error.message || '褰掓。澶辫触')
+        return false
+      }
+      return true
+    }
+  })
+}
+
+function confirmDelete(chat) {
+  dialog.warning({
+    title: '鍒犻櫎瀵硅瘽',
+    content: `纭畾鍒犻櫎銆�${chat.title}銆嶅悧锛熻浼氳瘽鐨勬墍鏈夋秷鎭兘浼氳涓�骞跺垹闄わ紝涓旀棤娉曟仮澶嶃�俙,
+    positiveText: '鍒犻櫎',
+    negativeText: '鍙栨秷',
+    onPositiveClick: async () => {
+      try {
+        await chatStore.removeChat(chat.id)
+        message.success('宸插垹闄�')
+        emit('deleted', chat.id)
+      } catch (error) {
+        message.error(error.message || '鍒犻櫎澶辫触')
+        return false
+      }
+      return true
+    }
+  })
+}
+
+async function confirmRenameModal() {
+  const title = renameModal.value.title.trim()
+  if (!title) {
+    message.warning('鍚嶇О涓嶈兘涓虹┖')
+    return
+  }
+  renameModal.value.saving = true
+  try {
+    await chatStore.renameChat(renameModal.value.id, title)
+    message.success('宸查噸鍛藉悕')
+    renameModal.value.show = false
+  } catch (error) {
+    message.error(error.message || '閲嶅懡鍚嶅け璐�')
+  } finally {
+    renameModal.value.saving = false
+  }
+}
+
+// ---------------- 鎼滅储 ----------------
+
 let searchTimer = null
 const SEARCH_DEBOUNCE_MS = 250
 
@@ -169,7 +569,6 @@
   const value = keyword.value
 
   if (!value || !value.trim()) {
-    // 娓呯┖鏃剁珛鍗虫仮澶嶅畬鏁村垪琛紝涓嶅繀绛夐槻鎶�
     if (searchTimer) clearTimeout(searchTimer)
     chatStore.clearSearch()
     return
@@ -190,47 +589,9 @@
   chatStore.clearSearch()
 }
 
-/** 鍛戒腑绫诲瀷鏂囨 */
-function matchLabel(type) {
-  if (type === 'title') return '鏍囬鍛戒腑'
-  if (type === 'both') return '鏍囬+鍐呭'
-  return '鍐呭鍛戒腑'
-}
-
-const avatarText = computed(() => (auth.displayName || '?').charAt(0).toUpperCase())
-
-const menuOptions = [
-  { label: '閲嶅懡鍚�', key: 'rename' },
-  { label: '鐭ヨ瘑搴撹缃�', key: 'rag' },
-  { type: 'divider', key: 'd1' },
-  { label: '鍒犻櫎瀵硅瘽', key: 'delete' }
-]
-
-const userMenuOptions = computed(() => {
-  const options = [{ label: '鎴戠殑鐭ヨ瘑搴�', key: 'files' }]
-  // 绠$悊鍛橀澶栨彁渚涘叏灞�鐭ヨ瘑搴撶淮鎶ゅ叆鍙c��
-  // 鍚庣瀵� /api/files/global/upload 绛夌鐐瑰彟鏈� 403 鏍¢獙锛�
-  // 杩欓噷鐨勯殣钘忓彧鏄伩鍏嶆櫘閫氱敤鎴风湅鍒版棤鏉冮檺鐨勮彍鍗曢」銆�
-  if (auth.isAdmin) {
-    options.push({ label: '鍏ㄥ眬鐭ヨ瘑搴擄紙绠$悊鍛橈級', key: 'admin-knowledge' })
-  }
-  options.push({ type: 'divider', key: 'd1' })
-  options.push({ label: '閫�鍑虹櫥褰�', key: 'logout' })
-  return options
-})
-
-/** 鐢辩埗缁勪欢鍦ㄦ寕杞芥椂璋冪敤锛岄伩鍏嶇粍浠惰嚜宸辫Е鍙戣姹傞�犳垚鏃跺簭涓嶇‘瀹� */
-async function refresh() {
-  loading.value = true
-  try {
-    // 鍒锋柊浼氳瘽鍒楄〃鏃堕��鍑烘悳绱㈡�侊紝閬垮厤鍋滅暀鍦ㄦ棫缁撴灉涓�
-    handleClearSearch()
-    await chatStore.loadChats()
-  } catch (error) {
-    message.error(error.message || '鍔犺浇浼氳瘽鍒楄〃澶辫触')
-  } finally {
-    loading.value = false
-  }
+function handleArchivedSelect(id) {
+  archiveVisible.value = false
+  emit('select', id)
 }
 
 async function handleNewChat() {
@@ -242,65 +603,6 @@
   }
 }
 
-function startRename(chat) {
-  editingId.value = chat.id
-  editingTitle.value = chat.title
-  nextTick(() => {
-    // ref 鍦� v-for 涓細鏄暟缁�
-    const input = Array.isArray(renameInputRef.value)
-      ? renameInputRef.value[0]
-      : renameInputRef.value
-    input?.focus?.()
-  })
-}
-
-function cancelRename() {
-  editingId.value = null
-  editingTitle.value = ''
-}
-
-async function commitRename(chat) {
-  const title = editingTitle.value.trim()
-  // 娌℃敼鍔ㄦ垨涓虹┖鍒欐斁寮冿紝閬垮厤浜х敓绌烘爣棰�
-  if (!title || title === chat.title) {
-    cancelRename()
-    return
-  }
-  try {
-    await chatStore.renameChat(chat.id, title)
-    message.success('宸查噸鍛藉悕')
-  } catch (error) {
-    message.error(error.message || '閲嶅懡鍚嶅け璐�')
-  } finally {
-    cancelRename()
-  }
-}
-
-function handleMenu(key, chat) {
-  if (key === 'rename') {
-    startRename(chat)
-  } else if (key === 'rag') {
-    emit('select', chat.id)
-    emit('open-files')
-  } else if (key === 'delete') {
-    dialog.warning({
-      title: '鍒犻櫎瀵硅瘽',
-      content: `纭畾鍒犻櫎銆�${chat.title}銆嶅悧锛熻浼氳瘽鐨勬墍鏈夋秷鎭兘浼氳涓�骞跺垹闄わ紝涓旀棤娉曟仮澶嶃�俙,
-      positiveText: '鍒犻櫎',
-      negativeText: '鍙栨秷',
-      onPositiveClick: async () => {
-        try {
-          await chatStore.removeChat(chat.id)
-          message.success('宸插垹闄�')
-          emit('deleted', chat.id)
-        } catch (error) {
-          message.error(error.message || '鍒犻櫎澶辫触')
-        }
-      }
-    })
-  }
-}
-
 function handleUserMenu(key) {
   if (key === 'files') {
     emit('open-files')
@@ -309,6 +611,23 @@
   } else if (key === 'logout') {
     auth.logout()
     emit('deleted', null)
+  }
+}
+
+/** 鐢辩埗缁勪欢鍦ㄦ寕杞芥椂璋冪敤锛岄伩鍏嶇粍浠惰嚜宸辫Е鍙戣姹傞�犳垚鏃跺簭涓嶇‘瀹� */
+async function refresh() {
+  loading.value = true
+  try {
+    handleClearSearch()
+    await Promise.all([
+      chatStore.loadChats(),
+      chatStore.loadArchivedChats(),
+      chatStore.loadChatGroups()
+    ])
+  } catch (error) {
+    message.error(error.message || '鍔犺浇浼氳瘽鍒楄〃澶辫触')
+  } finally {
+    loading.value = false
   }
 }
 
@@ -349,27 +668,147 @@
   color: var(--text-muted);
 }
 
-.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;
-}
-
-.match-badge {
-  color: #0f766e;
-  margin-left: 6px;
-}
-
 .sidebar-list {
   flex: 1;
   overflow-y: auto;
   padding: 0 8px 8px;
+}
+
+/* ---------- 鍒嗙粍鍖哄潡 ---------- */
+
+.section {
+  margin-bottom: 6px;
+}
+
+.section-head {
+  display: flex;
+  align-items: center;
+  gap: 4px;
+  padding: 6px 10px;
+  border-radius: 6px;
+  cursor: pointer;
+  user-select: none;
+}
+
+.section-head:hover {
+  background: var(--bg-hover);
+}
+
+.section-head.static {
+  cursor: default;
+}
+
+.section-head.static:hover {
+  background: transparent;
+}
+
+.section-title {
+  font-size: 11.5px;
+  font-weight: 600;
+  letter-spacing: 0.3px;
+  color: var(--text-muted);
+}
+
+.section-count {
+  font-size: 11px;
+  color: var(--text-muted);
+}
+
+.section-add {
+  margin-left: auto;
+  font-size: 15px;
+  line-height: 1;
+  padding: 0 4px;
+  color: var(--text-secondary);
+}
+
+.section-add:hover {
+  color: #2080f0;
+}
+
+.section-empty,
+.group-empty {
+  padding: 6px 10px 8px 26px;
+  font-size: 11.5px;
+  color: var(--text-muted);
+  line-height: 1.7;
+}
+
+/* 鎶樺彔绠ご锛氶粯璁ゆ湞涓嬶紝鎶樺彔鏃惰浆 -90 搴� */
+.chevron {
+  display: inline-block;
+  width: 12px;
+  font-size: 10px;
+  color: var(--text-muted);
+  transition: transform 0.15s ease;
+}
+
+.chevron.collapsed {
+  transform: rotate(-90deg);
+}
+
+.chevron.small {
+  font-size: 9px;
+}
+
+.group-block {
+  margin-bottom: 2px;
+}
+
+.group-head {
+  display: flex;
+  align-items: center;
+  gap: 4px;
+  padding: 6px 10px 6px 14px;
+  border-radius: 6px;
+  cursor: pointer;
+  user-select: none;
+}
+
+.group-head:hover {
+  background: var(--bg-hover);
+}
+
+.group-name {
+  font-size: 12.5px;
+  font-weight: 500;
+  color: var(--text-primary);
+  overflow: hidden;
+  text-overflow: ellipsis;
+  white-space: nowrap;
+}
+
+.group-count {
+  font-size: 11px;
+  color: var(--text-muted);
+}
+
+.group-more {
+  margin-left: auto;
+  font-size: 14px;
+  line-height: 1;
+  padding: 0 4px;
+  color: var(--text-secondary);
+  opacity: 0;
+  transition: opacity 0.12s ease;
+}
+
+.group-head:hover .group-more {
+  opacity: 1;
+}
+
+/* ---------- 鏃堕棿鍒嗙粍 ---------- */
+
+.chat-group-title {
+  position: sticky;
+  top: 0;
+  z-index: 1;
+  padding: 8px 10px 4px;
+  font-size: 11px;
+  font-weight: 600;
+  letter-spacing: 0.3px;
+  color: var(--text-muted);
+  background: var(--bg-sidebar);
 }
 
 .sidebar-loading {
@@ -383,64 +822,36 @@
   line-height: 1.9;
 }
 
-.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;
+/* ---------- 褰掓。鍏ュ彛 ---------- */
+
+.sidebar-archive {
+  padding: 4px 8px 0;
 }
 
-.chat-item:hover {
+.archive-entry {
+  display: flex;
+  align-items: center;
+  gap: 8px;
+  padding: 8px 10px;
+  border-radius: 8px;
+  cursor: pointer;
+  font-size: 12.5px;
+  color: var(--text-secondary);
+}
+
+.archive-entry:hover {
   background: var(--bg-hover);
 }
 
-.chat-item.active {
-  background: #e3e5e8;
+.archive-icon {
+  font-size: 13px;
 }
 
-.chat-item-main {
+.archive-text {
   flex: 1;
-  min-width: 0;
 }
 
-.chat-title {
-  font-size: 13.5px;
-  font-weight: 500;
-  /* 鍗曡鎴柇锛岄伩鍏嶉暱鏍囬鎶婁晶杈规爮鎾戝紑 */
-  overflow: hidden;
-  text-overflow: ellipsis;
-  white-space: nowrap;
-}
-
-.chat-meta {
-  font-size: 11.5px;
-  color: var(--text-muted);
-  margin-top: 3px;
-  overflow: hidden;
-  text-overflow: ellipsis;
-  white-space: nowrap;
-}
-
-.chat-rag-tag {
-  color: #6b7280;
-}
-
-.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 {
-  opacity: 1;
-}
+/* ---------- 搴曢儴鐢ㄦ埛鍖� ---------- */
 
 .sidebar-footer {
   border-top: 1px solid var(--border-color);
@@ -495,4 +906,14 @@
 .user-more {
   color: var(--text-muted);
 }
+
+.small-modal {
+  width: 420px;
+}
+
+.modal-footer {
+  display: flex;
+  justify-content: flex-end;
+  gap: 8px;
+}
 </style>

--
Gitblit v1.9.1