diff --git a/client/main.tsx b/client/main.tsx index 35bbf7f..7397c57 100644 --- a/client/main.tsx +++ b/client/main.tsx @@ -1,6 +1,9 @@ import { Signal } from "@char/aftercare"; import { ChatResponse } from "./response.tsx"; +const route = new Signal(window.location.hash.substring(1) || undefined); +let lastRoute: string | undefined = ""; + const main = document.querySelector("main")!; async function nav() { @@ -11,9 +14,7 @@ async function nav() { const button = ; button.addEventListener("click", e => { e.preventDefault(); - - main.append(conversationUI(conversation.id)); - nav.remove(); + route.set(conversation.id); }); nav.append(button); @@ -25,8 +26,7 @@ async function nav() { _tap={b => b.addEventListener("click", e => { e.preventDefault(); - main.append(conversationUI("new")); - nav.remove(); + route.set("new"); }) } > @@ -131,17 +131,19 @@ function conversationUI(id: string) { ); } -const showUI = async () => { +window.addEventListener("hashchange", () => { + route.set(window.location.hash.substring(1)); +}); +route.subscribeImmediate(async r => { + if (r === lastRoute) return; + main.innerHTML = ""; - if (window.location.hash) { - main.append(conversationUI(window.location.hash.substring(1))); + if (r) { + main.append(conversationUI(r)); } else { main.append(await nav()); } -}; -await showUI(); -window.addEventListener("hashchange", async () => { - await showUI(); + lastRoute = r; });