Skip to content

Commit

Permalink
Get latest revision version if no version specified
Browse files Browse the repository at this point in the history
  • Loading branch information
codewithcheese committed Jul 30, 2024
1 parent cfab4e7 commit 419f847
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/routes/(app)/chat/[id]/$data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -70,6 +73,7 @@ export function getRevision(chatId: string, version: number) {
},
},
},
orderBy: [desc(revisionTable.version)],
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/(app)/chat/[id]/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down

0 comments on commit 419f847

Please sign in to comment.