Sharkey/packages/frontend/src/pages/settings/accounts.vue

114 lines
3 KiB
Vue
Raw Normal View History

2021-04-23 07:01:52 +03:00
<template>
2023-01-08 06:10:01 +02:00
<div class="">
2021-04-23 07:01:52 +03:00
<FormSuspense :p="init">
2023-01-08 06:10:01 +02:00
<div class="_gaps">
<div class="_buttons">
<MkButton primary @click="addAccount"><i class="ti ti-plus"></i> {{ i18n.ts.addAccount }}</MkButton>
<MkButton @click="init"><i class="ti ti-refresh"></i> {{ i18n.ts.reloadAccountsList }}</MkButton>
2021-04-23 07:01:52 +03:00
</div>
<MkUserCardMini v-for="user in accounts" :key="user.id" :user="user" :class="$style.user" @click.prevent="menu(user, $event)"/>
2021-04-23 07:01:52 +03:00
</div>
</FormSuspense>
2022-01-04 11:21:00 +02:00
</div>
2021-04-23 07:01:52 +03:00
</template>
<script lang="ts" setup>
import { defineAsyncComponent, ref } from 'vue';
2023-05-19 14:52:15 +03:00
import type * as Misskey from 'misskey-js';
2022-01-04 11:21:00 +02:00
import FormSuspense from '@/components/form/suspense.vue';
2023-01-06 02:41:14 +02:00
import MkButton from '@/components/MkButton.vue';
2021-11-11 19:02:25 +02:00
import * as os from '@/os';
import { getAccounts, addAccount as addAccounts, removeAccount as _removeAccount, login, $i } from '@/account';
import { i18n } from '@/i18n';
import { definePageMetadata } from '@/scripts/page-metadata';
import MkUserCardMini from '@/components/MkUserCardMini.vue';
2021-04-23 07:01:52 +03:00
const storedAccounts = ref<any>(null);
const accounts = ref<Misskey.entities.UserDetailed[]>([]);
2021-04-23 07:01:52 +03:00
const init = async () => {
getAccounts().then(accounts => {
storedAccounts.value = accounts.filter(x => x.id !== $i!.id);
2021-04-23 07:01:52 +03:00
return os.api('users/show', {
userIds: storedAccounts.value.map(x => x.id),
});
}).then(response => {
accounts.value = response;
});
};
function menu(account, ev) {
os.popupMenu([{
text: i18n.ts.switch,
2022-12-20 01:35:49 +02:00
icon: 'ti ti-switch-horizontal',
action: () => switchAccount(account),
}, {
text: i18n.ts.logout,
icon: 'ti ti-trash',
danger: true,
action: () => removeAccount(account),
}], ev.currentTarget ?? ev.target);
}
function addAccount(ev) {
os.popupMenu([{
text: i18n.ts.existingAccount,
action: () => { addExistingAccount(); },
}, {
text: i18n.ts.createAccount,
action: () => { createAccount(); },
}], ev.currentTarget ?? ev.target);
}
async function removeAccount(account) {
await _removeAccount(account.id);
accounts.value = accounts.value.filter(x => x.id !== account.id);
}
function addExistingAccount() {
os.popup(defineAsyncComponent(() => import('@/components/MkSigninDialog.vue')), {}, {
done: async res => {
await addAccounts(res.id, res.i);
os.success();
init();
2021-04-23 07:01:52 +03:00
},
}, 'closed');
}
2021-04-23 07:01:52 +03:00
function createAccount() {
os.popup(defineAsyncComponent(() => import('@/components/MkSignupDialog.vue')), {}, {
done: async res => {
await addAccounts(res.id, res.i);
switchAccountWithToken(res.i);
2021-04-23 07:01:52 +03:00
},
}, 'closed');
}
async function switchAccount(account: any) {
const fetchedAccounts: any[] = await getAccounts();
const token = fetchedAccounts.find(x => x.id === account.id).token;
switchAccountWithToken(token);
}
function switchAccountWithToken(token: string) {
login(token);
}
const headerActions = $computed(() => []);
const headerTabs = $computed(() => []);
definePageMetadata({
title: i18n.ts.accounts,
icon: 'ti ti-users',
2021-04-23 07:01:52 +03:00
});
</script>
<style lang="scss" module>
.user {
cursor: pointer;
2021-04-23 07:01:52 +03:00
}
</style>