From 0a17ea2ac6b12c3241b72b743964edb303051374 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Fri, 6 May 2022 01:55:21 +0300 Subject: [PATCH] frontend: save selected channel --- frontend/src/main.js | 3 +-- frontend/src/storage.js | 7 +++++++ frontend/src/stores.js | 17 ++++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/frontend/src/main.js b/frontend/src/main.js index c59bd50..e8d6bb4 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -1,10 +1,9 @@ import Main from './components/Main.svelte'; -import { getItem, init } from './storage'; +import { getItem } from './storage'; import { authWithToken, useAuthHandlers } from './auth'; import { initResponsiveHandlers } from './responsive'; import { useDebuggingApi } from './debuggingapi'; -init(); useDebuggingApi(); initResponsiveHandlers(); useAuthHandlers(); diff --git a/frontend/src/storage.js b/frontend/src/storage.js index 388dde7..f0c5e74 100644 --- a/frontend/src/storage.js +++ b/frontend/src/storage.js @@ -3,11 +3,13 @@ const defaults = { "server:gatewayBase": `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`, "auth:token": "", "app:behavior:doAnimations": true, + "app:cache:openChannelId": -1, "loggingSink:Gateway": false, "loggingSink:Store": false }; const store = new Map(Object.entries(defaults)); const persistentProvider = localStorage; +let didCacheProvider = false; export function setItem(key, value) { store.set(key, value); @@ -17,6 +19,9 @@ export function setItem(key, value) { } export function getItem(key) { + if (!didCacheProvider) { + init(); + } return store.get(key); } @@ -42,6 +47,8 @@ export function init() { } } }); + + didCacheProvider = true; } export function apiRoute(fragment) { diff --git a/frontend/src/stores.js b/frontend/src/stores.js index 45c2fa4..9cba74d 100644 --- a/frontend/src/stores.js +++ b/frontend/src/stores.js @@ -1,7 +1,7 @@ import gateway, { GatewayEventType } from "./gateway"; import logger from "./logging"; import request from "./request"; -import { apiRoute } from "./storage"; +import { apiRoute, getItem, setItem } from "./storage"; const storeLog = logger("Store"); @@ -42,7 +42,14 @@ class ChannelsStore extends Store { gateway.subscribe(GatewayEventType.Ready, ({ channels }) => { this.value = channels; if (channels.length >= 1) { - selectedChannel.set(channels[0]); + if (!selectedChannel.value || selectedChannel.value.id === -1) { + selectedChannel.set(channels[0]); + } else { + // if a channel id is already selected, we'll populate it with the data we just got from the gateway + const index = this.value.findIndex(e => e.id === selectedChannel.value.id); + if (index !== -1) + selectedChannel.set(this.value[index]); + } } this.updated(); }); @@ -286,7 +293,7 @@ class OverlayStore extends Store { } } -export const selectedChannel = new Store({ id: -1, name: "none", creator_id: -1 }, "selectedChannel"); +export const selectedChannel = new Store({ id: getItem("app:cache:openChannelId"), name: "none", creator_id: -1 }, "selectedChannel"); export const showSidebar = new Store(false, "showSidebar"); export const showChannelView = new Store(true, "showChannelView"); export const smallViewport = new Store(false, "smallViewport"); @@ -306,3 +313,7 @@ export const allStores = { userInfoStore, overlayStore, }; + +selectedChannel.subscribe((newSelectedChannel) => { + setItem("app:cache:openChannelId", newSelectedChannel.id); +});