From dea90aacd56860f040698524c1d9f2417861fedd Mon Sep 17 00:00:00 2001 From: codewithcheese Date: Tue, 30 Jul 2024 15:56:36 +1000 Subject: [PATCH] Use editor event to update content instead of bind --- src/database/seed.ts | 13 ++------ src/routes/(app)/$data.ts | 1 - src/routes/(app)/+layout.ts | 31 ++++++++++--------- src/routes/(app)/Menu.svelte | 3 +- src/routes/(app)/chat/[id]/+layout.ts | 18 +++-------- src/routes/(app)/chat/[id]/ChatPage.svelte | 1 + src/routes/(app)/chat/[id]/MessageCard.svelte | 13 +++++--- .../(app)/chat/[id]/MessageEditor.svelte | 11 +++---- .../(app)/chat/[id]/revise/RevisePage.svelte | 5 ++- 9 files changed, 43 insertions(+), 53 deletions(-) diff --git a/src/database/seed.ts b/src/database/seed.ts index ef6367b..2473404 100644 --- a/src/database/seed.ts +++ b/src/database/seed.ts @@ -1,15 +1,6 @@ -import { chatTable } from "@/database/schema"; -import { useDb } from "@/database/client"; -import { nanoid } from "nanoid"; +import { newChat } from "../routes/(app)/$data"; export function seed() { // create first chat - return useDb().transaction(async (tx) => { - await tx.insert(chatTable).values({ - id: nanoid(10), - name: "Untitled", - prompt: "", - createdAt: new Date().toISOString(), - }); - }); + return newChat(); } diff --git a/src/routes/(app)/$data.ts b/src/routes/(app)/$data.ts index 4bc90f0..d06adb1 100644 --- a/src/routes/(app)/$data.ts +++ b/src/routes/(app)/$data.ts @@ -18,7 +18,6 @@ export async function newChat() { chatId: id, }); }); - await invalidate("view:chats"); return id; } diff --git a/src/routes/(app)/+layout.ts b/src/routes/(app)/+layout.ts index 1b6cf36..90d8de3 100644 --- a/src/routes/(app)/+layout.ts +++ b/src/routes/(app)/+layout.ts @@ -9,27 +9,30 @@ let migrated = false; export async function load({ depends }) { exposeDb(); - try { - // only run migrations once - if (!migrated) { - const { runMigrations } = await import("@/database/migrator"); - console.log("Migrating database"); - await runMigrations(); - await useDb().run(sql.raw("PRAGMA foreign_keys=on;")); - console.log("Migration complete"); - migrated = true; - } - } catch (err) { - console.error(err); - } + await runMigrations(); const chats = await useDb().query.chatTable.findMany({ // limit: 10, orderBy: [desc(chatTable.createdAt)], }); - console.log("chats", chats); registerModel(chatTable, chats, depends); depends("view:chats"); return { chats, }; } + +async function runMigrations() { + if (migrated) { + return; + } + try { + const { runMigrations } = await import("@/database/migrator"); + console.log("Migrating database"); + await runMigrations(); + await useDb().run(sql.raw("PRAGMA foreign_keys=on;")); + console.log("Migration complete"); + migrated = true; + } catch (err) { + console.error(err); + } +} diff --git a/src/routes/(app)/Menu.svelte b/src/routes/(app)/Menu.svelte index 247a6da..6bafddc 100644 --- a/src/routes/(app)/Menu.svelte +++ b/src/routes/(app)/Menu.svelte @@ -5,7 +5,7 @@ import * as Dialog from "@/components/ui/dialog/index"; import { page } from "$app/stores"; import { cn } from "$lib/cn"; - import { goto } from "$app/navigation"; + import { goto, invalidate } from "$app/navigation"; import DeleteDialog from "@/components/DeleteDialog.svelte"; import PersistenceAlert from "@/components/PersistenceAlert.svelte"; import { type Chat } from "@/database"; @@ -36,6 +36,7 @@ variant="outline" onclick={async () => { const id = await newChat(); + await invalidate("view:chats"); await goto(route(`/chat/[id]`, { id })); }} > diff --git a/src/routes/(app)/chat/[id]/+layout.ts b/src/routes/(app)/chat/[id]/+layout.ts index 5544876..48c5970 100644 --- a/src/routes/(app)/chat/[id]/+layout.ts +++ b/src/routes/(app)/chat/[id]/+layout.ts @@ -47,20 +47,12 @@ export async function load({ route, url, params, depends }) { const services = await loadServices(); registerModel(serviceTable, services, depends); - const version = url.searchParams.get("version"); - let revision: RevisionView | undefined; - if (version) { - const result = await getRevision(params.id, parseInt(version)); - if (!result) { - return error(404, `Revision ${version} not found`); - } - revision = result; - } else { - revision = await getLatestRevision(params.id); - } - if (revision) { - registerModel(revisionTable, revision, depends); + const version = url.searchParams.get("version") || "1"; + const revision = await getRevision(params.id, parseInt(version)); + if (!revision) { + return error(404, `Revision ${version} not found`); } + registerModel(revisionTable, revision, depends); depends("view:messages"); depends("view:chat"); return { chat, services, tab, revision, version }; diff --git a/src/routes/(app)/chat/[id]/ChatPage.svelte b/src/routes/(app)/chat/[id]/ChatPage.svelte index 0cbb9c8..1ccc9b5 100644 --- a/src/routes/(app)/chat/[id]/ChatPage.svelte +++ b/src/routes/(app)/chat/[id]/ChatPage.svelte @@ -24,6 +24,7 @@ revision: RevisionView; }; let { chat, revision }: Props = $props(); + console.log("ChatPage", chat, revision); let autoScroller = new AutoScroller(); let saveLength = $state(revision.messages.length); diff --git a/src/routes/(app)/chat/[id]/MessageCard.svelte b/src/routes/(app)/chat/[id]/MessageCard.svelte index 50037e9..ffbdaee 100644 --- a/src/routes/(app)/chat/[id]/MessageCard.svelte +++ b/src/routes/(app)/chat/[id]/MessageCard.svelte @@ -1,15 +1,13 @@