From 72bf6fb6f0e81f0509b7be39e15255aeccf367b2 Mon Sep 17 00:00:00 2001 From: Stanislas Date: Sun, 29 Jan 2023 16:58:32 +0100 Subject: [PATCH] fix: handle account switching when accounts have the same id (#1510) --- components/user/UserPicker.vue | 4 ++-- composables/users.ts | 35 ++++++++++++++-------------------- constants/index.ts | 1 - 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/components/user/UserPicker.vue b/components/user/UserPicker.vue index 23ea40ae..f3c3d486 100644 --- a/components/user/UserPicker.vue +++ b/components/user/UserPicker.vue @@ -5,7 +5,7 @@ const all = useUsers() const router = useRouter() const clickUser = (user: UserLogin) => { - if (user.account.id === currentUser.value?.account.id) + if (user.account.acct === currentUser.value?.account.acct) router.push(getAccountRoute(user.account)) else switchUser(user) @@ -21,7 +21,7 @@ const clickUser = (user: UserLogin) => { flex rounded cursor-pointer aria-label="Switch user" - :class="user.account.id === currentUser?.account.id ? '' : 'op25 grayscale'" + :class="user.account.acct === currentUser?.account.acct ? '' : 'op25 grayscale'" hover="filter-none op100" @click="clickUser(user)" > diff --git a/composables/users.ts b/composables/users.ts index f5925e7a..e0ff87b1 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -7,7 +7,6 @@ import type { UserLogin } from '~/types' import type { Overwrite } from '~/types/utils' import { DEFAULT_POST_CHARS_LIMIT, - STORAGE_KEY_CURRENT_USER, STORAGE_KEY_CURRENT_USER_HANDLE, STORAGE_KEY_NODES, STORAGE_KEY_NOTIFICATION, @@ -46,7 +45,7 @@ const initializeUsers = async (): Promise | RemovableRef>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true }) export const nodes = useLocalStorage>(STORAGE_KEY_NODES, {}, { deep: true }) -const currentUserId = useLocalStorage(STORAGE_KEY_CURRENT_USER, mock ? mock.user.account.id : '') +const currentUserHandle = useLocalStorage(STORAGE_KEY_CURRENT_USER_HANDLE, mock ? mock.user.account.id : '') export type ElkInstance = Partial & { uri: string @@ -56,8 +55,8 @@ export type ElkInstance = Partial & { export const getInstanceCache = (server: string): mastodon.v1.Instance | undefined => instances.value[server] export const currentUser = computed(() => { - if (currentUserId.value) { - const user = users.value.find(user => user.account?.id === currentUserId.value) + if (currentUserHandle.value) { + const user = users.value.find(user => user.account?.acct === currentUserHandle.value) if (user) return user } @@ -84,12 +83,12 @@ if (process.client) { const windowReload = () => { document.visibilityState === 'visible' && window.location.reload() } - watch(currentUserId, async (id, oldId) => { + watch(currentUserHandle, async (handle, oldHandle) => { // when sign in or switch account - if (id) { - if (id === currentUser.value?.account?.id) { + if (handle) { + if (handle === currentUser.value?.account?.acct) { // when sign in, the other tab will not have the user, idb is not reactive - const newUser = users.value.find(user => user.account?.id === id) + const newUser = users.value.find(user => user.account?.acct === handle) // if the user is there, then we are switching account if (newUser) { // check if the change is on current tab: if so, don't reload @@ -101,19 +100,13 @@ if (process.client) { window.addEventListener('visibilitychange', windowReload, { capture: true }) } // when sign out - else if (oldId) { - const oldUser = users.value.find(user => user.account?.id === oldId) + else if (oldHandle) { + const oldUser = users.value.find(user => user.account?.acct === oldHandle) // when sign out, the other tab will not have the user, idb is not reactive if (oldUser) window.addEventListener('visibilitychange', windowReload, { capture: true }) } }, { immediate: true, flush: 'post' }) - - // for injected script to read - const currentUserHandle = computed(() => currentUser.value?.account.acct || '') - watchEffect(() => { - localStorage.setItem(STORAGE_KEY_CURRENT_USER_HANDLE, currentUserHandle.value) - }) } export const useUsers = () => users @@ -144,7 +137,7 @@ export async function loginTo(masto: ElkMasto, user: Overwrite