use a route signal so that we dont double-connect
This commit is contained in:
parent
323802e740
commit
43f44fcab4
1 changed files with 14 additions and 12 deletions
|
@ -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;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue