From 419f847cfd3866c39e55d5e3115ffbba8fb49f26 Mon Sep 17 00:00:00 2001 From: codewithcheese Date: Tue, 30 Jul 2024 20:26:40 +1000 Subject: [PATCH] Get latest revision version if no version specified --- src/routes/(app)/chat/[id]/$data.ts | 8 ++++++-- src/routes/(app)/chat/[id]/+layout.ts | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/routes/(app)/chat/[id]/$data.ts b/src/routes/(app)/chat/[id]/$data.ts index 5b998c6..9304fb8 100644 --- a/src/routes/(app)/chat/[id]/$data.ts +++ b/src/routes/(app)/chat/[id]/$data.ts @@ -56,9 +56,12 @@ export function toChatMessage(message: RevisionView["messages"][number]): ChatMe }; } -export function getRevision(chatId: string, version: number) { +export function getRevision(chatId: string, version: number | null) { return useDb().query.revisionTable.findFirst({ - where: and(eq(revisionTable.chatId, chatId), eq(revisionTable.version, version)), + where: + version !== null + ? and(eq(revisionTable.chatId, chatId), eq(revisionTable.version, version)) + : eq(revisionTable.chatId, chatId), with: { messages: { with: { @@ -70,6 +73,7 @@ export function getRevision(chatId: string, version: number) { }, }, }, + orderBy: [desc(revisionTable.version)], }); } diff --git a/src/routes/(app)/chat/[id]/+layout.ts b/src/routes/(app)/chat/[id]/+layout.ts index 48c5970..84ecf63 100644 --- a/src/routes/(app)/chat/[id]/+layout.ts +++ b/src/routes/(app)/chat/[id]/+layout.ts @@ -47,8 +47,8 @@ export async function load({ route, url, params, depends }) { const services = await loadServices(); registerModel(serviceTable, services, depends); - const version = url.searchParams.get("version") || "1"; - const revision = await getRevision(params.id, parseInt(version)); + const version = Number(url.searchParams.get("version")) || null; + const revision = await getRevision(params.id, version); if (!revision) { return error(404, `Revision ${version} not found`); }