-
Notifications
You must be signed in to change notification settings - Fork 103
/
active-accounts.store.ts
41 lines (35 loc) · 1.6 KB
/
active-accounts.store.ts
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
import type { IAccountState } from '@core/account'
import { derived, Readable, writable } from 'svelte/store'
import { activeProfile } from './active-profile.store'
export const activeAccounts = writable<IAccountState[]>([])
export function removeAccountFromActiveAccounts(index: number): void {
activeAccounts?.update((state) => state.filter((account) => account.index !== index))
}
export function addAccountToActiveAccounts(account: IAccountState): void {
activeAccounts?.update((state) => [...state, account])
}
export function updateActiveAccount(index: number, partialAccount: Partial<IAccountState>): void {
activeAccounts.update((state) => [
...state.map((account) => (account.index === index ? { ...account, ...partialAccount } : account)),
])
}
export const nonHiddenActiveAccounts: Readable<IAccountState[]> = derived([activeAccounts], ([$activeAccounts]) => {
if (!$activeAccounts) {
return []
}
const unsortedNonHiddenAccounts = $activeAccounts?.filter((account) => !account?.hidden)
return unsortedNonHiddenAccounts.sort((a, b) => a.index - b.index)
})
export const visibleActiveAccounts: Readable<IAccountState[]> = derived(
[activeAccounts, activeProfile],
([$activeAccounts, $activeProfile]) => {
if (!$activeAccounts || !$activeProfile) {
return []
}
const unsortedVisibleAccounts =
$activeProfile?.showHiddenAccounts ?? false
? $activeAccounts
: $activeAccounts?.filter((account) => !account?.hidden)
return unsortedVisibleAccounts.sort((a, b) => a.index - b.index)
}
)