Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(app): reorder saved accounts in the account switcher

Hampus2b700baf3a8a
diff --git a/fluxer_app/src/features/app/components/layout/DndContext.tsx b/fluxer_app/src/features/app/components/layout/DndContext.tsx
index 3b834031f..13c4e5499 100644
--- a/fluxer_app/src/features/app/components/layout/DndContext.tsx
+++ b/fluxer_app/src/features/app/components/layout/DndContext.tsx
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import {setKeyboardDragActive} from '@app/features/app/components/layout/dnd/KeyboardDragMode';
import type React from 'react';
import {DndProvider} from 'react-dnd';
import KeyboardBackend, {isKeyboardDragTrigger} from 'react-dnd-accessible-backend';
@@ -22,6 +23,7 @@ const DND_OPTIONS = {
id: 'keyboard',
backend: KeyboardBackend,
context: {window, document},
+ options: {onDndModeChanged: setKeyboardDragActive},
preview: true,
transition: KeyboardTransition,
},
diff --git a/fluxer_app/src/features/app/components/layout/dnd/KeyboardDragMode.ts b/fluxer_app/src/features/app/components/layout/dnd/KeyboardDragMode.ts
new file mode 100644
index 000000000..31158dda4
--- /dev/null
+++ b/fluxer_app/src/features/app/components/layout/dnd/KeyboardDragMode.ts
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+let keyboardDragActive = false;
+
+export function setKeyboardDragActive(active: boolean): void {
+ keyboardDragActive = active;
+}
+
+export function isKeyboardDragActive(): boolean {
+ return keyboardDragActive;
+}
diff --git a/fluxer_app/src/features/app/components/layout/types/DndTypes.ts b/fluxer_app/src/features/app/components/layout/types/DndTypes.ts
index 522c9a104..ace93499d 100644
--- a/fluxer_app/src/features/app/components/layout/types/DndTypes.ts
+++ b/fluxer_app/src/features/app/components/layout/types/DndTypes.ts
@@ -12,6 +12,7 @@ export const DragItemType = Object.freeze({
CONNECTION: 'connection',
FAVORITES_CHANNEL: 'favorites-channel',
FAVORITES_CATEGORY: 'favorites-category',
+ ACCOUNT_TILE: 'account-tile',
} as const);
export type DragItemType = (typeof DragItemType)[keyof typeof DragItemType];
@@ -73,3 +74,8 @@ export interface ConnectionDragItem {
id: string;
index: number;
}
+
+export interface AccountTileDragItem {
+ type: typeof DragItemType.ACCOUNT_TILE;
+ accountKey: string;
+}
diff --git a/fluxer_app/src/features/platform/state/AuthSession.ts b/fluxer_app/src/features/platform/state/AuthSession.ts
index e373eab91..41fabd3b1 100644
--- a/fluxer_app/src/features/platform/state/AuthSession.ts
+++ b/fluxer_app/src/features/platform/state/AuthSession.ts
@@ -201,6 +201,18 @@ export class AuthSessionManager {
return selectAuthSessionAccountKey(this._snapshot);
}
+ get restoringAccountKey(): string | null {
+ return this.currentCredentialMirror().storageKey;
+ }
+
+ async restoreAccountOnNextStart(accountKey: string): Promise<void> {
+ const resolved = this.accountCatalog.resolve(this.accounts, accountKey);
+ if (resolved === null) {
+ throw new Error(`No stored account found for ${accountKey}`);
+ }
+ await this.deps.credentialMirror.persist(this.accountPersistence.pointer(resolved.account));
+ }
+
get currentAccount(): Account | null {
const accountKey = this.currentAccountKey;
if (accountKey === null) return null;
@@ -455,6 +467,32 @@ export class AuthSessionManager {
});
}
+ async addStoredAccount({
+ userId,
+ token,
+ userData,
+ instance,
+ }: {
+ userId: string;
+ token: string;
+ userData?: UserData;
+ instance: RuntimeConfigSnapshot;
+ }): Promise<void> {
+ await this.initialize();
+ const account = {
+ storageKey: getAccountKey({userId, instance}),
+ userId,
+ token,
+ userData,
+ presenceIntent: null,
+ lastActive: this.deps.now(),
+ instance,
+ isValid: true,
+ } satisfies PersistableSessionAccount;
+ await this.accountPersistence.stash(account);
+ this.send({type: 'account.upsert', account});
+ }
+
async validateToken(
token: string,
userId: string,
@@ -517,6 +555,30 @@ export class AuthSessionManager {
return prepared;
}
+ async reloadStoredAccount(accountKey: string): Promise<void> {
+ await this.initialize();
+ const resolved = this.accountCatalog.resolve(this.accounts, accountKey);
+ if (resolved === null || resolved.accountKey === this.currentAccountKey) {
+ return;
+ }
+ await this.adoptStoredAccountToken(resolved.account);
+ }
+
+ async adoptRotatedAccountToken(accountKey: string, expectedToken: string, token: string): Promise<void> {
+ await this.initialize();
+ const resolved = this.accountCatalog.resolve(this.accounts, accountKey);
+ if (
+ resolved === null ||
+ resolved.accountKey === this.currentAccountKey ||
+ resolved.account.token !== expectedToken ||
+ token.length === 0
+ ) {
+ return;
+ }
+ await this.deps.accountStorage.updateAccountToken(resolved.accountKey, expectedToken, token);
+ await this.adoptStoredAccountToken(resolved.account);
+ }
+
private async adoptStoredAccountToken(account: Account): Promise<Account> {
if (account.storageKey === this.currentAccountKey) {
return account;
@@ -552,9 +614,9 @@ export class AuthSessionManager {
await this.lifecycle.switchAccountWithinAccountActivation(accountKey, options, suspension);
}
- async logout(): Promise<void> {
+ async logout(options: {notifyServer?: boolean} = {}): Promise<void> {
await this.initialize();
- await this.lifecycle.logout();
+ await this.lifecycle.logout(options);
}
async removeAccount(accountKey: string): Promise<void> {
@@ -643,6 +705,21 @@ export class AuthSessionManager {
this.send({type: 'account.userDataUpdated', accountKey: resolved.accountKey, userData});
}
+ async reorderAccounts(orderedAccountKeys: ReadonlyArray<string>): Promise<void> {
+ const positions = new Map<string, number>();
+ for (const accountKey of orderedAccountKeys) {
+ const resolved = this.accountCatalog.resolve(this.accounts, accountKey);
+ if (resolved !== null && !positions.has(resolved.accountKey)) {
+ positions.set(resolved.accountKey, positions.size);
+ }
+ }
+ if (positions.size === 0) {
+ return;
+ }
+ this.send({type: 'accounts.reordered', positions});
+ await this.deps.accountStorage.updateAccountSwitcherPositions(positions);
+ }
+
async reset(): Promise<void> {
await this.deps.deactivateRuntime();
this.send({type: 'reset'});
diff --git a/fluxer_app/src/features/platform/state/auth_session/AuthSessionAccountCatalog.ts b/fluxer_app/src/features/platform/state/auth_session/AuthSessionAccountCatalog.ts
index 4aced7641..ad7e415bc 100644
--- a/fluxer_app/src/features/platform/state/auth_session/AuthSessionAccountCatalog.ts
+++ b/fluxer_app/src/features/platform/state/auth_session/AuthSessionAccountCatalog.ts
@@ -43,7 +43,7 @@ export interface ResolvedAccountRestoration {
readonly usedMirroredCredential: boolean;
}
-export interface UnresolvedAccountRestoration {
+interface UnresolvedAccountRestoration {
readonly kind: 'unresolved';
readonly reason: 'missing-account';
}
@@ -172,6 +172,7 @@ export class AuthSessionAccountCatalog {
presenceIntent: data.presenceIntent,
lastActive: data.lastActive,
isValid: data.isValid ?? true,
+ ...(data.switcherPosition === undefined ? {} : {switcherPosition: data.switcherPosition}),
};
}
diff --git a/fluxer_app/src/features/platform/state/auth_session/AuthSessionDependencies.ts b/fluxer_app/src/features/platform/state/auth_session/AuthSessionDependencies.ts
index 984c3d630..209416437 100644
--- a/fluxer_app/src/features/platform/state/auth_session/AuthSessionDependencies.ts
+++ b/fluxer_app/src/features/platform/state/auth_session/AuthSessionDependencies.ts
@@ -58,9 +58,11 @@ export interface AuthSessionAccountStorage {
restoreAccountData(accountKey: string): Promise<StoredAccount | null>;
deleteAccount(accountKey: string): Promise<void>;
updateAccountValidity(accountKey: string, isValid: boolean, expectedToken?: string): Promise<void>;
+ updateAccountToken(accountKey: string, expectedToken: string, token: string): Promise<void>;
+ updateAccountSwitcherPositions(positions: ReadonlyMap<string, number>): Promise<void>;
}
-export interface AuthSessionHttpOptions {
+interface AuthSessionHttpOptions {
readonly headers?: Record<string, string>;
readonly auth?: 'session' | 'none';
readonly target: InstanceHTTPTarget;
@@ -68,7 +70,7 @@ export interface AuthSessionHttpOptions {
readonly retries?: number;
}
-export interface AuthSessionHttp {
+interface AuthSessionHttp {
get<T>(url: string, options: AuthSessionHttpOptions): Promise<{body: T}>;
}
diff --git a/fluxer_app/src/features/platform/state/auth_session/AuthSessionLifecycle.ts b/fluxer_app/src/features/platform/state/auth_session/AuthSessionLifecycle.ts
index ff91554e9..8d7029834 100644
--- a/fluxer_app/src/features/platform/state/auth_session/AuthSessionLifecycle.ts
+++ b/fluxer_app/src/features/platform/state/auth_session/AuthSessionLifecycle.ts
@@ -51,7 +51,7 @@ export interface AuthSessionValidatedSwitchOptions {
readonly validatedToken: string;
}
-export class AccountSwitchPreparedCredentialChangedError extends Error {
+class AccountSwitchPreparedCredentialChangedError extends Error {
constructor(accountKey: string) {
super(`Prepared credentials changed before ${accountKey} could be activated`);
this.name = 'AccountSwitchPreparedCredentialChangedError';
@@ -368,7 +368,7 @@ export class AuthSessionLifecycle {
});
}
- async logout(): Promise<void> {
+ async logout({notifyServer = true}: {notifyServer?: boolean} = {}): Promise<void> {
return await this.transitionGate.runAccountTransition(async () => {
const state = this.state.getState();
if (
@@ -382,7 +382,7 @@ export class AuthSessionLifecycle {
}
const reason = AccountScopedWorkTransitionReason.LOGOUT;
return await this.transitionGate.runAccountScoped(reason, async (transition) => {
- await this.removeCurrentSession(this.state.getCurrentAccountKey(), reason, true, transition);
+ await this.removeCurrentSession(this.state.getCurrentAccountKey(), reason, notifyServer, transition);
});
});
}
diff --git a/fluxer_app/src/features/platform/state/auth_session/AuthSessionStateMachine.ts b/fluxer_app/src/features/platform/state/auth_session/AuthSessionStateMachine.ts
index 2116d40d7..5439e39b8 100644
--- a/fluxer_app/src/features/platform/state/auth_session/AuthSessionStateMachine.ts
+++ b/fluxer_app/src/features/platform/state/auth_session/AuthSessionStateMachine.ts
@@ -27,6 +27,7 @@ export interface Account {
lastActive: number;
instance?: RuntimeConfigSnapshot;
isValid: boolean;
+ switcherPosition?: number;
}
interface AuthSessionMachineContext {
@@ -52,6 +53,7 @@ export type AuthSessionMachineEvent =
| {type: 'account.markInvalid'; accountKey: string}
| {type: 'account.remove'; accountKey: string}
| {type: 'account.userDataUpdated'; accountKey: string; userData: UserData}
+ | {type: 'accounts.reordered'; positions: ReadonlyMap<string, number>}
| {type: 'token.set'; token: string | null}
| {type: 'userId.set'; userId: string | null}
| {type: 'connection.start'}
@@ -84,7 +86,8 @@ function accountMapFromAccounts(accounts: ReadonlyArray<Account>): Map<string, A
function upsertAccount(accounts: Map<string, Account>, account: Account): Map<string, Account> {
const nextAccounts = cloneAccounts(accounts);
- nextAccounts.set(account.storageKey, account);
+ const switcherPosition = account.switcherPosition ?? accounts.get(account.storageKey)?.switcherPosition;
+ nextAccounts.set(account.storageKey, switcherPosition === undefined ? account : {...account, switcherPosition});
return nextAccounts;
}
@@ -196,6 +199,17 @@ const authSessionStateMachine = setup({
})),
};
}),
+ reorderAccounts: assign(({context, event}) => {
+ if (event.type !== 'accounts.reordered') return {};
+ const nextAccounts = cloneAccounts(context.accounts);
+ for (const [accountKey, switcherPosition] of event.positions) {
+ const account = nextAccounts.get(accountKey);
+ if (account !== undefined) {
+ nextAccounts.set(accountKey, {...account, switcherPosition});
+ }
+ }
+ return {accounts: nextAccounts};
+ }),
setToken: assign(({event}) => {
if (event.type !== 'token.set') return {};
return {token: event.token};
@@ -223,6 +237,7 @@ const authSessionStateMachine = setup({
'account.markInvalid': {actions: 'markInvalid'},
'account.remove': {actions: 'removeAccount'},
'account.userDataUpdated': {actions: 'updateAccountUserData'},
+ 'accounts.reordered': {actions: 'reorderAccounts'},
'token.set': {actions: 'setToken'},
'userId.set': {actions: 'setUserId'},
reset: {target: '.idle', actions: 'reset'},