summaryrefslogtreecommitdiff
path: root/packages/frontend/src/pages/settings/accounts.vue
blob: c2588736b35a3a26edb157db82fb0ff59b401fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!--
SPDX-FileCopyrightText: syuilo and misskey-project
SPDX-License-Identifier: AGPL-3.0-only
-->

<template>
<div class="">
	<FormSuspense :p="init">
		<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>
			</div>

			<template v-for="[id, user] in accounts">
				<MkUserCardMini v-if="user != null" :key="user.id" :user="user" :class="$style.user" @click.prevent="menu(user, $event)"/>
				<button v-else v-panel class="_button" :class="$style.unknownUser" @click="menu(id, $event)">
					<div :class="$style.unknownUserAvatarMock"><i class="ti ti-user-question"></i></div>
					<div>
						<div :class="$style.unknownUserTitle">{{ i18n.ts.unknown }}</div>
						<div :class="$style.unknownUserSub">ID: <span class="_monospace">{{ id }}</span></div>
					</div>
				</button>
			</template>
		</div>
	</FormSuspense>
</div>
</template>

<script lang="ts" setup>
import { ref, computed } from 'vue';
import type * as Misskey from 'misskey-js';
import FormSuspense from '@/components/form/suspense.vue';
import MkButton from '@/components/MkButton.vue';
import * as os from '@/os.js';
import { misskeyApi } from '@/scripts/misskey-api.js';
import { getAccounts, removeAccount as _removeAccount, login, $i, getAccountWithSigninDialog, getAccountWithSignupDialog } from '@/account.js';
import { i18n } from '@/i18n.js';
import { definePageMetadata } from '@/scripts/page-metadata.js';
import MkUserCardMini from '@/components/MkUserCardMini.vue';
import { MenuItem } from '@/types/menu';

const storedAccounts = ref<{ id: string, token: string }[] | null>(null);
const accounts = ref(new Map<string, Misskey.entities.UserDetailed | null>());

const init = async () => {
	getAccounts().then(accounts => {
		storedAccounts.value = accounts.filter(x => x.id !== $i!.id);

		return misskeyApi('users/show', {
			userIds: storedAccounts.value.map(x => x.id),
		});
	}).then(response => {
		if (storedAccounts.value == null) return;
		accounts.value = new Map(storedAccounts.value.map(x => [x.id, response.find((y: Misskey.entities.UserDetailed) => y.id === x.id) ?? null]));
	});
};

function menu(account: Misskey.entities.UserDetailed | string, ev: MouseEvent) {
	let menu: MenuItem[];

	if (typeof account === 'string') {
		menu = [{
			text: i18n.ts.logout,
			icon: 'ti ti-trash',
			danger: true,
			action: () => removeAccount(account),
		}];
	} else {
		menu = [{
			text: i18n.ts.switch,
			icon: 'ti ti-switch-horizontal',
			action: () => switchAccount(account.id),
		}, {
			text: i18n.ts.logout,
			icon: 'ti ti-trash',
			danger: true,
			action: () => removeAccount(account.id),
		}];
	}

	os.popupMenu(menu, ev.currentTarget ?? ev.target);
}

function addAccount(ev: MouseEvent) {
	os.popupMenu([{
		text: i18n.ts.existingAccount,
		action: () => { addExistingAccount(); },
	}, {
		text: i18n.ts.createAccount,
		action: () => { createAccount(); },
	}], ev.currentTarget ?? ev.target);
}

async function removeAccount(id: string) {
	await _removeAccount(id);
	accounts.value.delete(id);
}

function addExistingAccount() {
	getAccountWithSigninDialog().then((res) => {
		if (res != null) {
			os.success();
			init();
		}
	});
}

function createAccount() {
	getAccountWithSignupDialog().then((res) => {
		if (res != null) {
			switchAccountWithToken(res.token);
		}
	});
}

async function switchAccount(id: string) {
	const fetchedAccounts = await getAccounts();
	const token = fetchedAccounts.find(x => x.id === 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',
}));
</script>

<style lang="scss" module>
.user {
	cursor: pointer;
}

.unknownUser {
	display: flex;
	align-items: center;
	text-align: start;
	padding: 16px;
	background: var(--MI_THEME-panel);
	border-radius: 8px;
	font-size: 0.9em;
}

.unknownUserAvatarMock {
	display: block;
	width: 34px;
	height: 34px;
	line-height: 34px;
	text-align: center;
	font-size: 16px;
	margin-right: 12px;
	background-color: color-mix(in srgb, var(--MI_THEME-fg), transparent 85%);
	color: color-mix(in srgb, var(--MI_THEME-fg), transparent 25%);
	border-radius: 50%;
}

.unknownUserTitle {
	display: block;
	width: 100%;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	line-height: 18px;
}

.unknownUserSub {
	display: block;
	width: 100%;
	font-size: 95%;
	opacity: 0.7;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	line-height: 16px;
}
</style>