mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-05 11:07:55 -05:00
refac
This commit is contained in:
1 parent
7eb87a08d6
commit
f612c4db83
2 files changed
+73
-12
No files matched your search
@@ -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<FolderSection, string>;
|
||||
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 @@
|
||||
<div
|
||||
role="tablist"
|
||||
aria-label={$i18n.t('Folder')}
|
||||
use:sortableTabs
|
||||
class="flex min-w-0 items-center gap-0.5 overflow-x-auto py-1 text-sm font-normal scrollbar-none select-none md:gap-1"
|
||||
>
|
||||
{#each sectionTabs as [key, label] (key)}
|
||||
<button
|
||||
type="button"
|
||||
role="tab"
|
||||
data-tab-id={key}
|
||||
aria-selected={folderSection === key}
|
||||
class="inline-flex min-w-fit items-center px-1 transition {folderSection === key
|
||||
? 'text-base-900 dark:text-base-100'
|
||||
|
||||
@@ -399,6 +399,8 @@ type Settings = {
|
||||
pinnedNotesOrder?: string[];
|
||||
/** Sidebar section ids ('models', 'notes', 'channels', 'folders', 'chats') in display order. */
|
||||
sidebarSectionOrder?: string[];
|
||||
/** Folder page tab ids ('overview', 'files', 'chats', ...) in display order. */
|
||||
folderTabOrder?: string[];
|
||||
/** Folder ids in display order, by parent folder id ('root' for top-level folders and folderSections). */
|
||||
folderOrder?: Record<string, string[]>;
|
||||
/** Whose top-level folders the sidebar lists. */
|
||||
|
||||
Reference in new issue
Block a user