fixes icon and get parameter logic

This commit is contained in:
Sebarocks 2025-07-31 18:30:25 -04:00
parent 16f084bfcd
commit 66eaa56429
7 changed files with 53 additions and 23 deletions

View file

@ -18,7 +18,7 @@
{#each chatStore.history as c}
<li>
<a
href="/{c.id}"
href="?chat={c.id}"
class={chatStore.chatId === c.id ? "active" : ""}
onclick={(e) => {
e.preventDefault();

View file

@ -40,7 +40,14 @@ export const chatStore = (() => {
messages = stored?.messages || [];
loading = true;
loading = false;
window.history.replaceState({}, "", `/${id}`);
// Update URL with GET parameter
const url = new URL(window.location.href);
if (id) {
url.searchParams.set('chat', id);
} else {
url.searchParams.delete('chat');
}
window.history.replaceState({}, "", url);
}
async function createAndSelect() {
@ -101,13 +108,14 @@ export const chatStore = (() => {
}
}
// initial route handling
const path = window.location.pathname.slice(1);
// initial route handling - use GET parameter instead of path
const params = new URLSearchParams(window.location.search);
const chatIdFromUrl = params.get('chat');
const storedHistory = loadHistory();
if (path && !storedHistory.find((c) => c.id === path)) {
if (chatIdFromUrl && !storedHistory.find((c) => c.id === chatIdFromUrl)) {
createAndSelect();
} else if (path) {
selectChat(path);
} else if (chatIdFromUrl) {
selectChat(chatIdFromUrl);
}
// Load models on initialization

View file

@ -1,20 +1,26 @@
import { chatStore } from "./chatStore.svelte.js";
// Parse chat ID from GET parameter
export function getChatIdFromUrl() {
const params = new URLSearchParams(window.location.search);
return params.get('chat');
}
// keyed by chat_id → chatStore instance
const cache = $state({});
// Update URL with GET parameter
export function updateUrlWithChatId(chatId) {
const url = new URL(window.location.href);
if (chatId) {
url.searchParams.set('chat', chatId);
} else {
url.searchParams.delete('chat');
}
window.history.replaceState({}, "", url);
}
// 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 let activeChatId = $state(null);
export function switchChat(chatId) {
activeChatId = chatId;
updateUrlWithChatId(chatId);
}
export function newChat() {
@ -26,6 +32,13 @@ export function newChat() {
// restore last opened chat (or create first one)
(() => {
const ids = JSON.parse(localStorage.getItem("chat_ids") || "[]");
if (ids.length) switchChat(ids[0]);
else newChat();
const urlChatId = getChatIdFromUrl();
if (urlChatId) {
switchChat(urlChatId);
} else if (ids.length) {
switchChat(ids[0]);
} else {
newChat();
}
})();