32 lines
691 B
JavaScript
32 lines
691 B
JavaScript
|
|
import { chatStore } from "./chatStore.svelte.js";
|
||
|
|
|
||
|
|
// keyed by chat_id → chatStore instance
|
||
|
|
const cache = $state({});
|
||
|
|
|
||
|
|
// which chat is on screen right now
|
||
|
|
export const activeChatId = $state(null);
|
||
|
|
|
||
|
|
export function getStore(chatId) {
|
||
|
|
if (!cache[chatId]) {
|
||
|
|
cache[chatId] = chatStore(chatId);
|
||
|
|
}
|
||
|
|
return cache[chatId];
|
||
|
|
}
|
||
|
|
|
||
|
|
export function switchChat(chatId) {
|
||
|
|
activeChatId = chatId;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function newChat() {
|
||
|
|
const id = "chat_" + crypto.randomUUID();
|
||
|
|
switchChat(id);
|
||
|
|
return id;
|
||
|
|
}
|
||
|
|
|
||
|
|
// restore last opened chat (or create first one)
|
||
|
|
(() => {
|
||
|
|
const ids = JSON.parse(localStorage.getItem("chat_ids") || "[]");
|
||
|
|
if (ids.length) switchChat(ids[0]);
|
||
|
|
else newChat();
|
||
|
|
})();
|