feat(frontend): main menu lists accounts with their avatars, switching opens the last chat of that account
This commit is contained in:
1 parent
3460246749
commit
a1d3552519
10 files changed
+338
-55
No files matched your search
@@ -5,8 +5,21 @@ import {
|
||||
} from "#lib/api/endpoints.js";
|
||||
import type { Account } from "#lib/api/types.js";
|
||||
import { browser } from "$app/env";
|
||||
import { goto } from "$app/navigation";
|
||||
|
||||
const STORAGE_KEY = "bg.account";
|
||||
const LAST_CHAT_KEY = "bg.lastChat";
|
||||
|
||||
function readLastChats(): Record<string, number> {
|
||||
if (!browser) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(LAST_CHAT_KEY) ?? "{}");
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function readSelected(): number | null {
|
||||
if (!browser) {
|
||||
@@ -20,6 +33,7 @@ function createAccounts() {
|
||||
let list = $state<Account[]>([]);
|
||||
let selectedId = $state<number | null>(readSelected());
|
||||
let loaded = $state(false);
|
||||
const lastChats = readLastChats();
|
||||
|
||||
const selected = $derived(
|
||||
list.find((account) => account.account_id === selectedId) ?? null
|
||||
@@ -67,6 +81,17 @@ function createAccounts() {
|
||||
}
|
||||
await load();
|
||||
},
|
||||
rememberChat(chatId: number | null) {
|
||||
if (selectedId === null || !browser) {
|
||||
return;
|
||||
}
|
||||
if (chatId === null) {
|
||||
delete lastChats[selectedId];
|
||||
} else {
|
||||
lastChats[selectedId] = chatId;
|
||||
}
|
||||
localStorage.setItem(LAST_CHAT_KEY, JSON.stringify(lastChats));
|
||||
},
|
||||
async renameDevice(id: number, deviceModel: string) {
|
||||
const updated = await renameAccountDevice(id, deviceModel);
|
||||
list = list.map((account) =>
|
||||
@@ -80,6 +105,14 @@ function createAccounts() {
|
||||
get selectedId() {
|
||||
return selectedId;
|
||||
},
|
||||
switchTo(id: number) {
|
||||
if (id === selectedId) {
|
||||
return;
|
||||
}
|
||||
select(id);
|
||||
const chatId = lastChats[id];
|
||||
goto(chatId === undefined ? "/app" : `/app/${chatId}`);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user