Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: infinite scrolling list of conversations #1564

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 47 additions & 0 deletions src/lib/components/InfiniteScroll.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script lang="ts">
import { onMount } from "svelte";
import { createEventDispatcher } from "svelte";
Comment on lines +2 to +3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { onMount } from "svelte";
import { createEventDispatcher } from "svelte";
import { onMount, createEventDispatcher } from "svelte";


const dispatch = createEventDispatcher();
let loader: HTMLDivElement;
let observer: IntersectionObserver;
let intervalId: ReturnType<typeof setInterval> | undefined;

onMount(() => {
observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
// Clear any existing interval
if (intervalId) {
clearInterval(intervalId);
}
// Start new interval that dispatches every 250ms
intervalId = setInterval(() => {
dispatch("visible");
}, 250);
} else {
// Clear interval when not intersecting
if (intervalId) {
clearInterval(intervalId);
intervalId = undefined;
}
}
});
});

observer.observe(loader);

return () => {
observer.disconnect();
if (intervalId) {
clearInterval(intervalId);
}
};
});
</script>

<div bind:this={loader} class="flex animate-pulse flex-col gap-4">
<div class="ml-2 h-5 w-4/5 gap-5 rounded bg-gray-200 dark:bg-gray-700" />
<div class="ml-2 h-5 w-4/5 gap-5 rounded bg-gray-200 dark:bg-gray-700" />
<div class="ml-2 h-5 w-4/5 gap-5 rounded bg-gray-200 dark:bg-gray-700" />
</div>
33 changes: 33 additions & 0 deletions src/lib/components/NavMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
import type { ConvSidebar } from "$lib/types/ConvSidebar";
import type { Model } from "$lib/types/Model";
import { page } from "$app/stores";
import InfiniteScroll from "./InfiniteScroll.svelte";
import type { Conversation } from "$lib/types/Conversation";

export let conversations: ConvSidebar[];
export let canLogin: boolean;
export let user: LayoutData["user"];

export let p = 0;

let hasMore = true;

let scrollContainer: HTMLDivElement;

function handleNewChatClick() {
isAborted.set(true);
}
Expand Down Expand Up @@ -44,6 +52,27 @@
} as const;

const nModels: number = $page.data.models.filter((el: Model) => !el.unlisted).length;

async function handleVisible() {
p++;
const newConvs = await fetch(`${base}/api/conversations?p=${p}`)
.then((res) => res.json())
.then((convs) =>
convs.map(
(conv: Pick<Conversation, "_id" | "title" | "updatedAt" | "model" | "assistantId">) => ({
...conv,
updatedAt: new Date(conv.updatedAt),
})
)
)
.catch(() => []);

if (newConvs.length === 0) {
hasMore = false;
}

conversations = [...conversations, ...newConvs];
}
</script>

<div class="sticky top-0 flex flex-none items-center justify-between px-3 py-3.5 max-sm:pt-0">
Expand All @@ -63,6 +92,7 @@
</a>
</div>
<div
bind:this={scrollContainer}
class="scrollbar-custom flex flex-col gap-1 overflow-y-auto rounded-r-xl from-gray-50 px-3 pb-3 pt-2 text-[.9rem] dark:from-gray-800/30 max-sm:bg-gradient-to-t md:bg-gradient-to-l"
>
{#await groupedConversations}
Expand All @@ -89,6 +119,9 @@
{/if}
{/each}
</div>
{#if hasMore}
<InfiniteScroll on:visible={handleVisible} />
{/if}
{/await}
</div>
<div
Expand Down
31 changes: 12 additions & 19 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { MetricsServer } from "$lib/server/metrics";
import type { ToolFront, ToolInputFile } from "$lib/types/Tool";
import { ReviewStatus } from "$lib/types/Review";

export const load: LayoutServerLoad = async ({ locals, depends }) => {
export const load: LayoutServerLoad = async ({ locals, depends, fetch }) => {
depends(UrlDependency.ConversationList);

const settings = await collections.settings.findOne(authCondition(locals));
Expand Down Expand Up @@ -56,24 +56,17 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
const conversations =
nConversations === 0
? Promise.resolve([])
: collections.conversations
.find(authCondition(locals))
.sort({ updatedAt: -1 })
.project<
Pick<
Conversation,
"title" | "model" | "_id" | "updatedAt" | "createdAt" | "assistantId"
>
>({
title: 1,
model: 1,
_id: 1,
updatedAt: 1,
createdAt: 1,
assistantId: 1,
})
.limit(300)
.toArray();
: fetch(`${env.APP_BASE}/api/conversations`)
.then((res) => res.json())
.then(
(
convs: Pick<Conversation, "_id" | "title" | "updatedAt" | "model" | "assistantId">[]
) =>
convs.map((conv) => ({
...conv,
updatedAt: new Date(conv.updatedAt),
}))
);

const userAssistants = settings?.assistants?.map((assistantId) => assistantId.toString()) ?? [];
const userAssistantsSet = new Set(userAssistants);
Expand Down
13 changes: 9 additions & 4 deletions src/routes/api/conversations/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { models } from "$lib/server/models";
import { authCondition } from "$lib/server/auth";
import type { Conversation } from "$lib/types/Conversation";

const NUM_PER_PAGE = 300;
const NUM_PER_PAGE = 5;

export async function GET({ locals, url }) {
const p = parseInt(url.searchParams.get("p") ?? "0");
Expand All @@ -24,15 +24,20 @@ export async function GET({ locals, url }) {
.limit(NUM_PER_PAGE)
.toArray();

if (convs.length === 0) {
return Response.json([]);
}

const res = convs.map((conv) => ({
id: conv._id,
_id: conv._id,
id: conv._id, // legacy param iOS
title: conv.title,
updatedAt: conv.updatedAt,
modelId: conv.model,
model: conv.model,
modelId: conv.model, // legacy param iOS
assistantId: conv.assistantId,
modelTools: models.find((m) => m.id == conv.model)?.tools ?? false,
}));

return Response.json(res);
} else {
return Response.json({ message: "Must have session cookie" }, { status: 401 });
Expand Down
Loading