feat: rename accounts to user

This commit is contained in:
Anthony Fu 2022-11-23 12:25:48 +08:00
parent 8dc1fb6493
commit ed5b8091d3
10 changed files with 32 additions and 45 deletions

View file

@ -1,12 +1,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { isAccountSwitcherOpen, isSigninDialogOpen } from '~/composables/dialog' import { isSigninDialogOpen, isUserSwitcherOpen } from '~/composables/dialog'
</script> </script>
<template> <template>
<ModalDrawer v-model="isAccountSwitcherOpen"> <ModalDrawer v-model="isUserSwitcherOpen">
<AccountSwitcher /> <UserSwitcher />
</ModalDrawer> </ModalDrawer>
<ModalDialog v-model="isSigninDialogOpen"> <ModalDialog v-model="isSigninDialogOpen">
<AccountSignIn m6 /> <UserSignIn m6 />
</ModalDialog> </ModalDialog>
</template> </template>

View file

@ -1,10 +1,10 @@
<script setup lang="ts"> <script setup lang="ts">
const allAccounts = useAccounts() const all = useUsers()
const sortedAccounts = computed(() => { const sorted = computed(() => {
return [ return [
currentUser.value!, currentUser.value!,
...allAccounts.value.filter(account => account.token !== currentUser.value?.token), ...all.value.filter(account => account.token !== currentUser.value?.token),
].filter(Boolean) ].filter(Boolean)
}) })
</script> </script>
@ -15,16 +15,16 @@ const sortedAccounts = computed(() => {
Switch Account Switch Account
</h1> </h1>
<div mx--2> <div mx--2>
<template v-for="acc of sortedAccounts" :key="acc.id"> <template v-for="user of sorted" :key="user.id">
<AccountInfo <AccountInfo
:account="acc.account" :account="user.account"
:link="false" :link="false"
:full-server="true" :full-server="true"
rounded p2 rounded p2
hover:bg-active cursor-pointer hover:bg-active cursor-pointer
@click="loginTo(acc)" @click="loginTo(user)"
> >
<template v-if="acc.token === currentUser?.token"> <template v-if="user.token === currentUser?.token">
<div flex-auto /> <div flex-auto />
<div i-ri:check-line text-primary mya text-2xl /> <div i-ri:check-line text-primary mya text-2xl />
</template> </template>

View file

@ -1,5 +1,5 @@
import { login } from 'masto' import { login } from 'masto'
import { currentUser } from './accounts' import { currentUser } from './users'
import { DEFAULT_SERVER } from '~/constants' import { DEFAULT_SERVER } from '~/constants'
// TODO: improve upsteam to make this synchronous (delayed auth) // TODO: improve upsteam to make this synchronous (delayed auth)

View file

@ -1,11 +1,11 @@
export const isAccountSwitcherOpen = ref(false) export const isUserSwitcherOpen = ref(false)
export const isSigninDialogOpen = ref(false) export const isSigninDialogOpen = ref(false)
export function openAccountSwitcher() { export function openUserSwitcher() {
isAccountSwitcherOpen.value = true isUserSwitcherOpen.value = true
} }
export function openSigninDialog() { export function openSigninDialog() {
isSigninDialogOpen.value = true isSigninDialogOpen.value = true
isAccountSwitcherOpen.value = false isUserSwitcherOpen.value = false
} }

View file

@ -2,30 +2,30 @@ import { login as loginMasto } from 'masto'
import type { UserLogin } from '~/types' import type { UserLogin } from '~/types'
import { DEFAULT_SERVER } from '~/constants' import { DEFAULT_SERVER } from '~/constants'
const accounts = useLocalStorage<UserLogin[]>('nuxtodon-accounts', [], { deep: true }) const users = useLocalStorage<UserLogin[]>('nuxtodon-accounts', [], { deep: true })
const currentId = useLocalStorage<string>('nuxtodon-current-user', '') const currentUserId = useLocalStorage<string>('nuxtodon-current-user', '')
export const currentUser = computed<UserLogin | undefined>(() => { export const currentUser = computed<UserLogin | undefined>(() => {
let user: UserLogin | undefined let user: UserLogin | undefined
if (currentId.value) { if (currentUserId.value) {
user = accounts.value.find(user => user.account?.id === currentId.value) user = users.value.find(user => user.account?.id === currentUserId.value)
if (user) if (user)
return user return user
} }
// Fallback to the first account // Fallback to the first account
return accounts.value[0] return users.value[0]
}) })
export const currentServer = computed<string>(() => currentUser.value?.server || DEFAULT_SERVER) export const currentServer = computed<string>(() => currentUser.value?.server || DEFAULT_SERVER)
export const useAccounts = () => accounts export const useUsers = () => users
export async function loginTo(user: UserLogin) { export async function loginTo(user: UserLogin) {
const existing = accounts.value.findIndex(u => u.server === user.server && u.token === user.token) const existing = users.value.findIndex(u => u.server === user.server && u.token === user.token)
if (existing !== -1) { if (existing !== -1) {
if (currentId.value === accounts.value[existing].account?.id) if (currentUserId.value === users.value[existing].account?.id)
return null return null
currentId.value = user.account?.id currentUserId.value = user.account?.id
await reloadPage() await reloadPage()
return true return true
} }
@ -37,8 +37,8 @@ export async function loginTo(user: UserLogin) {
const me = await masto.accounts.verifyCredentials() const me = await masto.accounts.verifyCredentials()
user.account = me user.account = me
accounts.value.push(user) users.value.push(user)
currentId.value = me.id currentUserId.value = me.id
await reloadPage() await reloadPage()
return true return true
} }
@ -48,12 +48,12 @@ export async function signout() {
if (!currentUser.value) if (!currentUser.value)
return return
const index = accounts.value.findIndex(u => u.account?.id === currentUser.value?.account?.id) const index = users.value.findIndex(u => u.account?.id === currentUser.value?.account?.id)
if (index === -1) if (index === -1)
return return
accounts.value.splice(index, 1) users.value.splice(index, 1)
currentId.value = accounts.value[0]?.account?.id currentUserId.value = users.value[0]?.account?.id
await reloadPage() await reloadPage()
} }

View file

@ -7,14 +7,14 @@
<template v-if="currentUser"> <template v-if="currentUser">
<PublishWidget px4 draft-key="home" /> <PublishWidget px4 draft-key="home" />
</template> </template>
<AccountSignInEntry v-else /> <UserSignInEntry v-else />
<div flex-auto /> <div flex-auto />
<AccountInfo <AccountInfo
v-if="currentUser" v-if="currentUser"
p4 p4
:account="currentUser?.account" :account="currentUser?.account"
:link="false" :link="false"
@click="openAccountSwitcher" @click="openUserSwitcher"
/> />
</slot> </slot>
</div> </div>

View file

@ -1,8 +1,4 @@
<script setup lang="ts"> <script setup lang="ts">
const props = defineProps<{
modelValue?: boolean
}>()
const params = useRoute().params const params = useRoute().params
const user = $computed(() => params.user as string) const user = $computed(() => params.user as string)

View file

@ -1,9 +0,0 @@
<script setup lang="ts">
definePageMeta({
layout: 'none',
})
</script>
<template>
<AccountSignIn />
</template>