1
0

fix: store info for all servers

This commit is contained in:
Daniel Roe 2022-11-25 14:21:07 +00:00
parent aac689a2f5
commit 4f133aabc0
No known key found for this signature in database
GPG Key ID: 22D5008E4F5D9B55
2 changed files with 6 additions and 5 deletions

View File

@ -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<UserLogin[]>(STORAGE_KEY_USERS, [], { deep: true })
const servers = useLocalStorage<Record<string, Instance>>(STORAGE_KEY_SERVERS, {}, { deep: true })
const currentUserId = useLocalStorage<string>(STORAGE_KEY_CURRENT_USER, '')
export const currentUser = computed<UserLogin | undefined>(() => {
@ -21,9 +22,9 @@ export const currentServer = computed<string>(() => currentUser.value?.server ||
export const useUsers = () => users
export const currentInstance = useLocalStorage<Partial<Instance>>(STORAGE_KEY_SERVER, {}, { deep: true })
export const currentInstance = computed<null | Instance>(() => 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
}

View File

@ -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'