From 4f133aabc06e1e2f38a03506c2db4cbeb8b67b28 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 25 Nov 2022 14:21:07 +0000 Subject: [PATCH] fix: store info for all servers --- composables/users.ts | 9 +++++---- constants/index.ts | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/composables/users.ts b/composables/users.ts index a35d060f..ad75d7ff 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -1,9 +1,10 @@ import { login as loginMasto } from 'masto' import type { AccountCredentials, Instance } from 'masto' import type { UserLogin } from '~/types' -import { DEFAULT_POST_CHARS_LIMIT, DEFAULT_SERVER, STORAGE_KEY_CURRENT_USER, STORAGE_KEY_SERVER, STORAGE_KEY_USERS } from '~/constants' +import { DEFAULT_POST_CHARS_LIMIT, DEFAULT_SERVER, STORAGE_KEY_CURRENT_USER, STORAGE_KEY_SERVERS, STORAGE_KEY_USERS } from '~/constants' const users = useLocalStorage(STORAGE_KEY_USERS, [], { deep: true }) +const servers = useLocalStorage>(STORAGE_KEY_SERVERS, {}, { deep: true }) const currentUserId = useLocalStorage(STORAGE_KEY_CURRENT_USER, '') export const currentUser = computed(() => { @@ -21,9 +22,9 @@ export const currentServer = computed(() => currentUser.value?.server || export const useUsers = () => users -export const currentInstance = useLocalStorage>(STORAGE_KEY_SERVER, {}, { deep: true }) +export const currentInstance = computed(() => currentUserId.value ? servers.value[currentUserId.value] ?? null : null) -export const characterLimit = computed(() => currentInstance.value.configuration?.statuses.maxCharacters ?? DEFAULT_POST_CHARS_LIMIT) +export const characterLimit = computed(() => currentInstance.value?.configuration.statuses.maxCharacters ?? DEFAULT_POST_CHARS_LIMIT) export async function loginTo(user: UserLogin & { account?: AccountCredentials }) { const existing = users.value.findIndex(u => u.server === user.server && u.token === user.token) @@ -41,10 +42,10 @@ export async function loginTo(user: UserLogin & { account?: AccountCredentials } }) const me = await masto.accounts.verifyCredentials() user.account = me - currentInstance.value = await masto.instances.fetch() users.value.push(user) currentUserId.value = me.id + servers.value[me.id] = await masto.instances.fetch() await reloadPage() return true } diff --git a/constants/index.ts b/constants/index.ts index af94e552..fc06e91e 100644 --- a/constants/index.ts +++ b/constants/index.ts @@ -7,9 +7,9 @@ export const HOST_DOMAIN = process.dev export const DEFAULT_POST_CHARS_LIMIT = 500 export const DEFAULT_SERVER = 'mas.to' -export const STORAGE_KEY_SERVER = 'elk-current-server' export const STORAGE_KEY_DRAFTS = 'elk-drafts' export const STORAGE_KEY_USERS = 'elk-users' +export const STORAGE_KEY_SERVERS = 'elk-servers' export const STORAGE_KEY_CURRENT_USER = 'elk-current-user' export const STORAGE_KEY_NOTIFY_TAB = 'elk-notify-tab' export const STORAGE_KEY_FIRST_VISIT = 'elk-first-visit'