This commit is contained in:
Timothy Jaeryang Baek committed 2026-07-14 01:39:40 -04:00
1 parent 34813573fa
commit 6e14d0d627
22 files changed
+249 -243

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
import { get, readonly, writable } from 'svelte/store';
import { getChatList, getPinnedChatList } from '$lib/apis/chats';
const chatsStore = writable(null);
const pinnedChatsStore = writable([]);
export const chats = readonly(chatsStore);
export const pinnedChats = readonly(pinnedChatsStore);
let currentPage = 1;
let paginationReady = false;
let requestGeneration = 0;
let allLoaded = false;
let loadingNextPage = false;
type RefreshChatListOptions = {
refreshPinned?: boolean;
clearPinned?: boolean;
};
type ChatListResult = {
accepted: boolean;
allLoaded: boolean;
};
export const refreshChatList = async (
token: string = '',
options: RefreshChatListOptions = {}
): Promise<ChatListResult> => {
const generation = ++requestGeneration;
paginationReady = false;
loadingNextPage = false;
const [nextChats, nextPinnedChats] = await Promise.all([
getChatList(token, 1),
options.refreshPinned && !options.clearPinned
? getPinnedChatList(token)
: Promise.resolve(undefined)
]);
if (generation !== requestGeneration) {
return { accepted: false, allLoaded };
}
chatsStore.set(nextChats);
currentPage = 1;
allLoaded = nextChats.length === 0;
if (options.clearPinned) {
pinnedChatsStore.set([]);
} else if (options.refreshPinned) {
pinnedChatsStore.set(nextPinnedChats ?? []);
}
paginationReady = true;
return { accepted: true, allLoaded };
};
export const loadNextChatListPage = async (token: string = ''): Promise<ChatListResult> => {
if (!paginationReady || allLoaded || loadingNextPage) {
return { accepted: false, allLoaded };
}
const generation = requestGeneration;
const nextPage = currentPage + 1;
loadingNextPage = true;
try {
const nextChats = await getChatList(token, nextPage);
if (generation !== requestGeneration) {
return { accepted: false, allLoaded };
}
allLoaded = nextChats.length === 0;
currentPage = nextPage;
const existingIds = new Set((get(chatsStore) ?? []).map((chat) => chat.id));
const uniqueChats = nextChats.filter((chat) => !existingIds.has(chat.id));
chatsStore.set([...(get(chatsStore) ?? []), ...uniqueChats]);
return { accepted: true, allLoaded };
} finally {
loadingNextPage = false;
}
};
export const resetChatListState = () => {
requestGeneration += 1;
currentPage = 1;
paginationReady = false;
allLoaded = false;
loadingNextPage = false;
chatsStore.set(null);
pinnedChatsStore.set([]);
};
+1 -4
View File
@@ -58,8 +58,7 @@ export const chatTitle = writable('');
export const channels = writable([]);
export const channelId = writable(null);
export const chats = writable(null);
export const pinnedChats = writable([]);
export { chats, pinnedChats } from './chat-list';
export const pinnedNotes = writable([]);
export const tags = writable([]);
export const folders = writable([]);
@@ -122,8 +121,6 @@ export type DesktopEvent = {
data?: any;
};
export const desktopEvent: Writable<DesktopEvent | null> = writable(null);
export const scrollPaginationEnabled = writable(false);
export const currentChatPage = writable(1);
export const isLastActiveTab = writable(true);
export const playingNotificationSound = writable(false);