refac
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
import { WEBUI_API_BASE_URL } from '$lib/constants';
|
||||
|
||||
export const checkActiveChats = async (token: string, chatIds: string[]) => {
|
||||
const res = await fetch(`${WEBUI_API_BASE_URL}/tasks/active/chats`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({ chat_ids: chatIds })
|
||||
});
|
||||
if (!res.ok) throw await res.json();
|
||||
return res.json();
|
||||
};
|
||||
@@ -25,10 +25,9 @@
|
||||
models,
|
||||
selectedFolder,
|
||||
WEBUI_NAME,
|
||||
sidebarWidth,
|
||||
activeChatIds
|
||||
sidebarWidth
|
||||
} from '$lib/stores';
|
||||
import { loadNextChatListPage, refreshChatList } from '$lib/stores/chatList';
|
||||
import { loadNextChatListPage, refreshChatList, setChatActive } from '$lib/stores/chatList';
|
||||
import { onMount, getContext, tick, onDestroy } from 'svelte';
|
||||
|
||||
const i18n = getContext('i18n');
|
||||
@@ -52,7 +51,6 @@
|
||||
} from '$lib/apis/folders';
|
||||
import { createNewNote, getPinnedNoteList, toggleNotePinnedStatusById } from '$lib/apis/notes';
|
||||
import { updateUserSettings } from '$lib/apis/users';
|
||||
import { checkActiveChats } from '$lib/apis/tasks';
|
||||
import { createNoteHandler } from '$lib/components/notes/utils';
|
||||
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
|
||||
|
||||
@@ -110,7 +108,7 @@
|
||||
let showSharedFolders = false;
|
||||
|
||||
let folders = {};
|
||||
let folderRegistry = {};
|
||||
let folderRegistry: Record<string, { setFolderItems?: () => unknown }> = {};
|
||||
|
||||
let newFolderId = null;
|
||||
|
||||
@@ -366,18 +364,20 @@
|
||||
})(),
|
||||
(async () => {
|
||||
console.log('Init chat list');
|
||||
const result = await refreshChatList(localStorage.token, { refreshPinned: true });
|
||||
if (result.accepted) {
|
||||
await Promise.all(
|
||||
Object.values(folderRegistry).map((folder: any) => folder?.setFolderItems?.())
|
||||
);
|
||||
allChatsLoaded = result.allLoaded;
|
||||
chatListReady = true;
|
||||
}
|
||||
await refreshChatRows();
|
||||
})()
|
||||
]);
|
||||
};
|
||||
|
||||
const refreshChatRows = async () => {
|
||||
const result = await refreshChatList(localStorage.token, { refreshPinned: true });
|
||||
if (result.accepted) {
|
||||
await Promise.all(Object.values(folderRegistry).map((folder) => folder?.setFolderItems?.()));
|
||||
allChatsLoaded = result.allLoaded;
|
||||
chatListReady = true;
|
||||
}
|
||||
};
|
||||
|
||||
const loadMoreChats = async () => {
|
||||
chatListLoading = true;
|
||||
|
||||
@@ -610,17 +610,6 @@
|
||||
await initChannels();
|
||||
}
|
||||
await initChatList();
|
||||
|
||||
// Check which chats have active tasks
|
||||
const allChatIds = [...$chats.map((c) => c.id), ...$pinnedChats.map((c) => c.id)];
|
||||
if (allChatIds.length > 0) {
|
||||
try {
|
||||
const res = await checkActiveChats(localStorage.token, allChatIds);
|
||||
activeChatIds.set(new Set(res.active_chat_ids || []));
|
||||
} catch (e) {
|
||||
console.debug('Failed to check active chats:', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
settings.subscribe((value) => {
|
||||
@@ -649,6 +638,7 @@
|
||||
|
||||
const socketInstance = $socket;
|
||||
socketInstance?.on('events', chatActiveEventHandler);
|
||||
socketInstance?.on('connect', refreshChatRows);
|
||||
|
||||
await tick();
|
||||
initPinnedMenuSortable();
|
||||
@@ -672,28 +662,24 @@
|
||||
}
|
||||
|
||||
socketInstance?.off('events', chatActiveEventHandler);
|
||||
socketInstance?.off('connect', refreshChatRows);
|
||||
};
|
||||
});
|
||||
|
||||
// Handler for chat events (defined outside onMount for proper cleanup)
|
||||
const chatActiveEventHandler = (event: {
|
||||
const chatActiveEventHandler = async (event: {
|
||||
chat_id: string;
|
||||
message_id: string;
|
||||
data: { type: string; data: any };
|
||||
data: { type: string; data: { active?: boolean } };
|
||||
}) => {
|
||||
if (event.data?.type === 'chat:active') {
|
||||
const { active } = event.data.data;
|
||||
activeChatIds.update((ids) => {
|
||||
const newSet = new Set(ids);
|
||||
if (active) {
|
||||
newSet.add(event.chat_id);
|
||||
} else {
|
||||
newSet.delete(event.chat_id);
|
||||
}
|
||||
return newSet;
|
||||
});
|
||||
const active = event.data.data.active ?? false;
|
||||
const found = setChatActive(event.chat_id, active);
|
||||
if (!found && active) {
|
||||
await refreshChatRows();
|
||||
}
|
||||
} else if (event.data?.type === 'chat:list') {
|
||||
initChatList();
|
||||
refreshChatRows();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1416,6 +1402,7 @@
|
||||
createdAt={chat.created_at}
|
||||
updatedAt={chat.updated_at}
|
||||
lastReadAt={chat.last_read_at}
|
||||
active={chat.active ?? false}
|
||||
{shiftKey}
|
||||
selected={selectedChatId === chat.id}
|
||||
on:select={() => {
|
||||
@@ -1479,6 +1466,7 @@
|
||||
createdAt={chat.created_at}
|
||||
updatedAt={chat.updated_at}
|
||||
lastReadAt={chat.last_read_at}
|
||||
active={chat.active ?? false}
|
||||
{shiftKey}
|
||||
selected={selectedChatId === chat.id}
|
||||
on:select={() => {
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
showSidebar,
|
||||
tags,
|
||||
selectedFolder,
|
||||
activeChatIds,
|
||||
settings,
|
||||
user
|
||||
} from '$lib/stores';
|
||||
@@ -58,6 +57,7 @@
|
||||
export let createdAt: number | null = null;
|
||||
export let updatedAt: number | null = null;
|
||||
export let lastReadAt: number | null = null;
|
||||
export let active = false;
|
||||
|
||||
export let selected = false;
|
||||
export let shiftKey = false;
|
||||
@@ -104,7 +104,7 @@
|
||||
|
||||
$: unread =
|
||||
id !== $chatId &&
|
||||
!$activeChatIds.has(id) &&
|
||||
!active &&
|
||||
(effectiveReadAt === null || (updatedAt !== null && updatedAt > effectiveReadAt));
|
||||
$: showInlineActions = id === $chatId || confirmEdit || mouseOver || selected;
|
||||
|
||||
@@ -545,7 +545,7 @@
|
||||
{/if}
|
||||
|
||||
<!-- Loading spinner for active chat (left side) -->
|
||||
{#if $activeChatIds.has(id)}
|
||||
{#if active}
|
||||
<div class="shrink-0 self-center pr-2">
|
||||
<Spinner className="size-3" />
|
||||
</div>
|
||||
|
||||
@@ -721,6 +721,7 @@
|
||||
createdAt={chat.created_at}
|
||||
updatedAt={chat.updated_at}
|
||||
lastReadAt={chat.last_read_at}
|
||||
active={chat.active ?? false}
|
||||
ownerName={folders[folderId]?.shared ? (chat.owner_name ?? null) : null}
|
||||
ownerUserId={folders[folderId]?.shared && chat.owner_name ? chat.user_id : null}
|
||||
readonly={chat.user_id !== $user?.id}
|
||||
|
||||
@@ -104,6 +104,7 @@
|
||||
title={chat.title}
|
||||
createdAt={chat.created_at}
|
||||
updatedAt={chat.updated_at}
|
||||
active={chat.active ?? false}
|
||||
ownerName={chat.owner_name}
|
||||
ownerUserId={chat.user_id}
|
||||
readonly={chat.readonly ?? !isWritable}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getChatList, getPinnedChatList } from '$lib/apis/chats';
|
||||
|
||||
type ChatListItem = {
|
||||
id: string;
|
||||
[key: string]: any;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
const chatsStore = writable<ChatListItem[] | null>(null);
|
||||
@@ -90,6 +90,21 @@ export const loadNextChatListPage = async (token: string = ''): Promise<ChatList
|
||||
}
|
||||
};
|
||||
|
||||
export const setChatActive = (chatId: string, active: boolean): boolean => {
|
||||
let found = false;
|
||||
const updateChat = (chat: ChatListItem) => {
|
||||
if (chat.id !== chatId) {
|
||||
return chat;
|
||||
}
|
||||
found = true;
|
||||
return { ...chat, active };
|
||||
};
|
||||
|
||||
chatsStore.update((items) => (items ? items.map(updateChat) : items));
|
||||
pinnedChatsStore.update((items) => items.map(updateChat));
|
||||
return found;
|
||||
};
|
||||
|
||||
export const resetChatListState = () => {
|
||||
requestGeneration += 1;
|
||||
currentPage = 1;
|
||||
|
||||
@@ -31,7 +31,6 @@ export const mobile = writable(false);
|
||||
export const socket: Writable<null | Socket> = writable(null);
|
||||
export const socketConnected: Writable<boolean> = writable(true);
|
||||
export const activeUserIds: Writable<null | string[]> = writable(null);
|
||||
export const activeChatIds: Writable<Set<string>> = writable(new Set());
|
||||
export const USAGE_POOL: Writable<null | string[]> = writable(null);
|
||||
|
||||
export const theme = writable('system');
|
||||
|
||||
Reference in New Issue
Block a user