diff --git a/src/lib/components/chat/Placeholder/FolderPlaceholder.svelte b/src/lib/components/chat/Placeholder/FolderPlaceholder.svelte index 81fbee931f..e87af59ec6 100644 --- a/src/lib/components/chat/Placeholder/FolderPlaceholder.svelte +++ b/src/lib/components/chat/Placeholder/FolderPlaceholder.svelte @@ -2,7 +2,19 @@ import { getContext, onMount, setContext, tick } from 'svelte'; import { writable } from 'svelte/store'; import { toast } from 'svelte-sonner'; - import { config, machines, showFileNavPath, socket, selectedFolder, user } from '$lib/stores'; + // @ts-expect-error The installed SortableJS package has no type declarations. + import Sortable from 'sortablejs'; + import { + config, + machines, + mobile, + settings, + showFileNavPath, + socket, + selectedFolder, + user + } from '$lib/stores'; + import { updateUserSettings } from '$lib/apis/users'; import type { AppLayoutContext } from '$lib/types'; import { resolveMachineTarget } from '$lib/apis/machines'; import { machineIdOf } from '$lib/utils/machines'; @@ -197,18 +209,63 @@ $: canUploadKnowledge = $user?.role === 'admin' || ($user?.permissions?.chat?.file_upload ?? true); - // Literal $i18n.t calls, so i18n:parse keeps the keys. - let sectionTabs: [FolderSection, string][]; - $: sectionTabs = [ - ['overview', $i18n.t('Overview')], - ...(filesTarget ? ([['files', $i18n.t('Files')]] as [FolderSection, string][]) : []), - ['chats', $i18n.t('Chats')], - ...(canUseNotes ? ([['notes', $i18n.t('Notes')]] as [FolderSection, string][]) : []), - ...(canUseAutomations - ? ([['automations', $i18n.t('Automations')]] as [FolderSection, string][]) - : []), - ['knowledge', $i18n.t('Knowledge')] + const FOLDER_TABS: FolderSection[] = [ + 'overview', + 'files', + 'chats', + 'notes', + 'automations', + 'knowledge' ]; + // A saved order wins; a tab it doesn't know yet takes its default place. + $: tabOrder = FOLDER_TABS.reduce( + (order, id, index) => + order.includes(id) ? order : [...order.slice(0, index), id, ...order.slice(index)], + (($settings?.folderTabOrder ?? []) as FolderSection[]).filter((id) => FOLDER_TABS.includes(id)) + ); + // Literal $i18n.t calls, so i18n:parse keeps the keys. + $: tabLabels = { + overview: $i18n.t('Overview'), + files: $i18n.t('Files'), + chats: $i18n.t('Chats'), + notes: $i18n.t('Notes'), + automations: $i18n.t('Automations'), + knowledge: $i18n.t('Knowledge') + } satisfies Record; + let sectionTabs: [FolderSection, string][]; + $: sectionTabs = tabOrder + .filter( + (id) => + (id !== 'files' || filesTarget) && + (id !== 'notes' || canUseNotes) && + (id !== 'automations' || canUseAutomations) + ) + .map((id) => [id, tabLabels[id]]); + + const sortableTabs = (node: HTMLElement) => { + if ($mobile) return; + const sortable = new Sortable(node, { + // As the chat tab bar: the browser's own drag image, neighbours sliding aside. + animation: 150, + direction: 'horizontal', + ghostClass: 'opacity-40', + draggable: '[data-tab-id]', + dataIdAttr: 'data-tab-id', + onEnd: async ({ item, from, oldIndex, newIndex }: any) => { + if (oldIndex === newIndex) return; + const visibleOrder: FolderSection[] = 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 tabs keep their slots; visible ones fill the rest in the new order. + const queue = [...visibleOrder]; + const next = tabOrder.map((id) => (visibleOrder.includes(id) ? queue.shift()! : id)); + settings.set({ ...$settings, folderTabOrder: next }); + await updateUserSettings(localStorage.token, { ui: { folderTabOrder: next } }); + } + }); + return { destroy: () => sortable.destroy() }; + }; function showSection(section: FolderSection, save = true) { folderSection = section; @@ -1309,12 +1366,14 @@
{#each sectionTabs as [key, label] (key)}