use a route signal so that we dont double-connect

This commit is contained in:
Charlotte Som 2025-02-26 16:34:58 +00:00
parent 323802e740
commit 43f44fcab4

View file

@ -1,6 +1,9 @@
import { Signal } from "@char/aftercare";
import { ChatResponse } from "./response.tsx";
const route = new Signal<string | undefined>(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 type="button">{conversation.name}</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;
});