Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: keep sidebar conversations loaded by scrolling in the conversati…
…ons store
  • Loading branch information
burrows99 committed Nov 15, 2026
commit 979b96c8ae41afcb84c6d0bba23f13b4e3fbb3a2
10 changes: 8 additions & 2 deletions src/lib/components/NavMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import { browser } from "$app/environment";
import { usePublicConfig } from "$lib/utils/PublicConfig.svelte";
import { useAPIClient, handleResponse } from "$lib/APIClient";
import { useConversationsStore } from "$lib/stores/conversations.svelte";
import { requireAuthUser } from "$lib/utils/auth";
import { enabledServersCount } from "$lib/stores/mcpServers";
import { isPro } from "$lib/stores/isPro";
Expand All @@ -35,6 +36,7 @@

const publicConfig = usePublicConfig();
const client = useAPIClient();
const convsStore = useConversationsStore();

interface Props {
conversations: ConvSidebar[];
Expand All @@ -45,7 +47,7 @@
}

let {
conversations = $bindable(),
conversations,
user,
p = $bindable(0),
ondeleteConversation,
Expand Down Expand Up @@ -103,14 +105,18 @@
hasMore = false;
}

conversations = [...conversations, ...newConvs];
// Older pages go into the shared store, not a local copy of the prop: a local
// copy is dropped as soon as the store list is replaced (delete, new chat) and
// never sees a rename.
convsStore.append(newConvs);
}

$effect(() => {
if (conversations.length <= CONV_NUM_PER_PAGE) {
// reset p to 0 if there's only one page of content
// that would be caused by a data loading invalidation
p = 0;
hasMore = true;
}
});

Expand Down
27 changes: 27 additions & 0 deletions src/lib/stores/conversations.svelte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import type { ConvSidebar } from "$lib/types/ConvSidebar";
import { ConversationsStore } from "./conversations.svelte";

const conv = (id: string): ConvSidebar => ({ id, title: `Chat ${id}`, updatedAt: new Date(0) });
const titles = (store: ConversationsStore) => store.list.map((c) => c.title);

describe("ConversationsStore.append", () => {
it("keeps older pages in the list so rename and delete apply to them", () => {
const store = new ConversationsStore();
store.init([conv("3"), conv("2")]);
store.append([conv("1"), conv("0")]);

store.update("0", { title: "Renamed" });
store.remove("3");

expect(titles(store)).toEqual(["Chat 2", "Chat 1", "Renamed"]);
});

it("skips conversations that are already listed", () => {
const store = new ConversationsStore();
store.init([conv("3"), conv("2")]);
store.append([conv("2"), conv("1")]);

expect(titles(store)).toEqual(["Chat 3", "Chat 2", "Chat 1"]);
});
});
13 changes: 12 additions & 1 deletion src/lib/stores/conversations.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface ConversationListItem {
mlAssistant?: boolean;
}

class ConversationsStore {
export class ConversationsStore {
#list = $state<ConvSidebar[]>([]);

get list(): ConvSidebar[] {
Expand Down Expand Up @@ -67,6 +67,17 @@ class ConversationsStore {
this.#list = [conv, ...this.#list];
}

/**
* Append a page of older conversations loaded by the sidebar's infinite scroll.
* Skips ids already in the list: pages shift when a conversation is created or
* deleted, and the mobile and desktop sidebars paginate independently.
*/
append(conversations: ConvSidebar[]): void {
const known = new Set(this.#list.map((c) => String(c.id)));
const older = conversations.filter((c) => !known.has(String(c.id)));
Comment thread
burrows99 marked this conversation as resolved.
Resolved
if (older.length) this.#list = [...this.#list, ...older];
}

/**
* Re-fetch GET /api/v2/conversations?p=0 and reconcile with the local list.
* Performs a last-write-wins replace so updatedAt ordering reflects the server.
Expand Down