From e07a4b2bc6d818507e8aa49e13f47fe6f007a1ed Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 27 Nov 2026 15:31:53 +0400 Subject: [PATCH] refac --- src/lib/components/layout/Sidebar.svelte | 917 ++++++++++-------- .../components/layout/Sidebar/Section.svelte | 5 +- src/lib/stores/index.ts | 2 + 3 files changed, 499 insertions(+), 425 deletions(-) diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 4d833e4770..b1bc8e5d99 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -137,6 +137,7 @@ import MobileSwipePanel from '../common/MobileSwipePanel.svelte'; const DEFAULT_PINNED_ITEMS = ['notes', 'workspace']; + const SIDEBAR_SECTIONS = ['models', 'notes', 'channels', 'folders', 'chats']; let scrollTop = 0; @@ -418,6 +419,62 @@ } }; + $: sectionOrder = [ + ...($settings?.sidebarSectionOrder ?? []).filter((id) => SIDEBAR_SECTIONS.includes(id)), + ...SIDEBAR_SECTIONS.filter((id) => !($settings?.sidebarSectionOrder ?? []).includes(id)) + ]; + + $: visibleSections = sectionOrder.filter((id) => { + switch (id) { + case 'models': + return $visiblePinnedModels.length > 0; + case 'notes': + return ( + ($config?.features?.enable_notes ?? false) && + ($user?.role === 'admin' || ($user?.permissions?.features?.notes ?? true)) && + $pinnedNotes.length > 0 + ); + case 'channels': + return ( + $config?.features?.enable_channels && + ($user?.role === 'admin' || ($user?.permissions?.features?.channels ?? true)) + ); + case 'folders': + return ( + $config?.features?.enable_folders && + ($user?.role === 'admin' || ($user?.permissions?.features?.folders ?? true)) + ); + default: + return true; + } + }); + + const sortableSections = (node: HTMLElement) => { + if ($mobile) return; + const sortable = new Sortable(node, { + forceFallback: true, + fallbackOnBody: true, + fallbackTolerance: 4, + ghostClass: 'opacity-40', + draggable: '[data-section-id]', + dataIdAttr: 'data-section-id', + handle: '[data-section-handle]', + onEnd: async ({ item, from, oldIndex, newIndex }) => { + if (oldIndex === newIndex) return; + const visibleOrder = sortable.toArray(); + // Put the node back so Svelte's keyed each does the move. + from.insertBefore(item, from.children[oldIndex + (oldIndex < newIndex ? 0 : 1)] ?? null); + + // Hidden sections keep their slots; visible ones fill the rest in the new order. + const queue = [...visibleOrder]; + const next = sectionOrder.map((id) => (visibleOrder.includes(id) ? queue.shift() : id)); + settings.set({ ...$settings, sidebarSectionOrder: next }); + await updateUserSettings(localStorage.token, { ui: { sidebarSectionOrder: next } }); + } + }); + return { destroy: () => sortable.destroy() }; + }; + $: initSelectedFolderChats($selectedFolder as SelectedSidebarFolder); const initFolders = async () => { @@ -1608,322 +1665,168 @@ - {#if $visiblePinnedModels.length > 0} - - - - {/if} - - {#if ($config?.features?.enable_notes ?? false) && ($user?.role === 'admin' || ($user?.permissions?.features?.notes ?? true)) && $pinnedNotes.length > 0} - { - const note = await createNoteHandler('New Note'); - if (note) { - if (!openSidebarPage(`/notes/${note.id}`, note.title)) goto(`/notes/${note.id}`); - } - }} - onAddLabel={$i18n.t('New Note')} - > - - - {/if} - - {#if $config?.features?.enable_channels && ($user?.role === 'admin' || ($user?.permissions?.features?.channels ?? true))} - { - await tick(); - - setTimeout(() => { - showCreateChannel = true; - }, 0); - } - : null} - onAddLabel={$i18n.t('Create Channel')} - > - {#each channelGroups as group, groupIdx} - {#if groupIdx > 0} -
- {/if} - {#each group as channel (channel.id)} - { - await initChannels(); - }} - /> - {/each} - {/each} -
- {/if} - - {#if $config?.features?.enable_folders && ($user?.role === 'admin' || ($user?.permissions?.features?.folders ?? true))} - { - showCreateFolderModal = true; - }} - onAddLabel={$i18n.t('New Folder')} - on:drop={async (e) => { - const { type, id, item } = e.detail; - - if (type === 'folder') { - if (folders[id].parent_id === null) { - return; - } - - const res = await updateFolderParentIdById(localStorage.token, id, null).catch( - (error) => { - toast.error(`${error}`); - return null; - } - ); - - if (res) { - updateFolderDetails(id, res); - await initFolders(); - } - } - }} - > - - {#if directoryMachines.length} - - - - - -
- - - - -
-
- {/if} -
- { - for (const childId of Object.keys(folders)) { - let parentId = childId; - while (parentId && parentId !== folderId) - parentId = folders[parentId]?.parent_id; - if (parentId) removeFolderState(childId); - } - let id = $selectedFolder?.id; - while (id && id !== folderId) id = folders[id]?.parent_id; - if (id) selectedFolder.set(null); - initChatList(); - }} - on:update={() => { - initChatList(); - }} - on:import={(e) => { - const { folderId, items } = e.detail; - importChatHandler(items, false, folderId); - }} - on:change={async () => { - initChatList(); - }} - /> -
- {/if} - - { - selectedFolder.set(null); - }} - on:import={(e) => { - importChatHandler(e.detail); - }} - on:drop={async (e) => { - const { type, id, item } = e.detail; - - if (type === 'chat') { - let chat = await getChatById(localStorage.token, id).catch((error) => { - return null; - }); - if (!chat && item) { - if (!canImportChats) { - toast.error($i18n.t('Access prohibited')); - return; - } - - chat = await importChats(localStorage.token, [ - { - chat: item.chat, - meta: item?.meta ?? {}, - pinned: false, - folder_id: null, - created_at: item?.created_at ?? null, - updated_at: item?.updated_at ?? null - } - ]); - } - - if (chat) { - console.log(chat); - if (!chat.folder_id && !chat.pinned) { - return; - } - - if (chat.folder_id) { - const res = await updateChatFolderIdById( - localStorage.token, - chat.id, - null - ).catch((error) => { - toast.error(`${error}`); - return null; - }); - - folderRegistry[chat.folder_id]?.setFolderItems(); - - if (res) { - updateFolderChatLists(res, null); - chat = res; - } - } - - if (chat.pinned) { - const res = await toggleChatPinnedStatusById(localStorage.token, chat.id); - } - - await initChatList(); - void refreshFolderChatLists(); - } - } else if (type === 'folder') { - if (folders[id].parent_id === null) { - return; - } - - const res = await updateFolderParentIdById(localStorage.token, id, null).catch( - (error) => { - toast.error(`${error}`); - return null; - } - ); - - if (res) { - await initFolders(); - } - } - }} - > - - - - - + + + {:else if sectionId === 'notes'} + { + const note = await createNoteHandler('New Note'); + if (note) { + if (!openSidebarPage(`/notes/${note.id}`, note.title)) + goto(`/notes/${note.id}`); + } + }} + onAddLabel={$i18n.t('New Note')} + > + + + {:else if sectionId === 'channels'} + { + await tick(); -
- - {#each chatListMenus as menu (menu.key)} - {@const current = menu.choices.find( - (choice) => choice.value === $chatListView[menu.key] - )} - - - {#each menu.choices as choice (choice.value)} - - {/each} - + setTimeout(() => { + showCreateChannel = true; + }, 0); + } + : null} + onAddLabel={$i18n.t('Create Channel')} + > + {#each channelGroups as group, groupIdx} + {#if groupIdx > 0} +
+ {/if} + {#each group as channel (channel.id)} + { + await initChannels(); + }} + /> + {/each} {/each} -
- -
-
- - +
+ {:else if sectionId === 'folders'} + { + showCreateFolderModal = true; + }} + onAddLabel={$i18n.t('New Folder')} + on:drop={async (e) => { + const { type, id, item } = e.detail; - {#if $pinnedChats.length > 0} -
-
- { + toast.error(`${error}`); + return null; + }); + + if (res) { + updateFolderDetails(id, res); + await initFolders(); + } + } + }} + > + + {#if directoryMachines.length} + + + + + +
+ + + + +
+
+ {/if} +
+ { + for (const childId of Object.keys(folders)) { + let parentId = childId; + while (parentId && parentId !== folderId) + parentId = folders[parentId]?.parent_id; + if (parentId) removeFolderState(childId); + } + let id = $selectedFolder?.id; + while (id && id !== folderId) id = folders[id]?.parent_id; + if (id) selectedFolder.set(null); + initChatList(); + }} + on:update={() => { + initChatList(); + }} + on:import={(e) => { + const { folderId, items } = e.detail; + importChatHandler(items, false, folderId); + }} + on:change={async () => { + initChatList(); + }} + /> + + {:else} + { + selectedFolder.set(null); + }} on:import={(e) => { - importChatHandler(e.detail, true); + importChatHandler(e.detail); }} on:drop={async (e) => { const { type, id, item } = e.detail; @@ -1952,6 +1855,10 @@ if (chat) { console.log(chat); + if (!chat.folder_id && !chat.pinned) { + return; + } + if (chat.folder_id) { const res = await updateChatFolderIdById( localStorage.token, @@ -1962,13 +1869,15 @@ return null; }); + folderRegistry[chat.folder_id]?.setFolderItems(); + if (res) { updateFolderChatLists(res, null); chat = res; } } - if (!chat.pinned) { + if (chat.pinned) { const res = await toggleChatPinnedStatusById( localStorage.token, chat.id @@ -1978,65 +1887,221 @@ await initChatList(); void refreshFolderChatLists(); } + } else if (type === 'folder') { + if (folders[id].parent_id === null) { + return; + } + + const res = await updateFolderParentIdById( + localStorage.token, + id, + null + ).catch((error) => { + toast.error(`${error}`); + return null; + }); + + if (res) { + await initFolders(); + } } }} - name={$i18n.t('Pinned')} > -
- {#each $pinnedChats as chat, idx (`pinned-chat-${chat?.id ?? idx}`)} -
- { - selectedChatId = chat.id; - }} - on:unselect={() => { - selectedChatId = null; - }} - on:change={async () => { - initChatList(); - }} - onReadStateChange={applyChatReadState} - on:tag={(e) => { - const { type, name } = e.detail; - tagEventHandler(type, name, chat.id); - }} - /> -
- {/each} -
-
-
-
- {/if} - -
-
- {#if $chats} -
- {#each $chats as chat, idx (`chat-${chat?.id ?? idx}`)} -
- {#if idx === 0 || (idx > 0 && chat.time_range !== $chats[idx - 1].time_range)} -

+ + +

+ + {/if} + + { + selectedChatId = chat.id; + }} + on:unselect={() => { + selectedChatId = null; + }} + on:change={async () => { + initChatList(); + }} + onReadStateChange={applyChatReadState} + on:tag={(e) => { + const { type, name } = e.detail; + tagEventHandler(type, name, chat.id); + }} + /> +
+ {/each} +
+ + {#if chatListReady && !allChatsLoaded} + { + if (!chatListLoading) { + loadMoreChats(); + } + }} + > +
+ +
{$i18n.t('Loading...')}
+
+
+ {/if} + {:else} +
+ +
{$i18n.t('Loading...')}
+
{/if} - - { - selectedChatId = chat.id; - }} - on:unselect={() => { - selectedChatId = null; - }} - on:change={async () => { - initChatList(); - }} - onReadStateChange={applyChatReadState} - on:tag={(e) => { - const { type, name } = e.detail; - tagEventHandler(type, name, chat.id); - }} - />
- {/each} -
- - {#if chatListReady && !allChatsLoaded} - { - if (!chatListLoading) { - loadMoreChats(); - } - }} - > -
- -
{$i18n.t('Loading...')}
-
-
- {/if} - {:else} -
- -
{$i18n.t('Loading...')}
-
+ +
{/if} - - + {/each} +