Skip to content

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

TypeScript fluxerfluxerapp

feat(app): search other accounts in the quick switcher

Hampus81aa77a23256
diff --git a/fluxer_app/src/features/search/state/QuickSwitcher.tsx b/fluxer_app/src/features/search/state/QuickSwitcher.tsx
index 88b094a4f..95049b84b 100644
--- a/fluxer_app/src/features/search/state/QuickSwitcher.tsx
+++ b/fluxer_app/src/features/search/state/QuickSwitcher.tsx
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
+import AccountEngine from '@app/features/account_engine/state/AccountEngine';
import Guilds from '@app/features/guild/state/Guilds';
import {OPEN_LINK_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';
import {onLocaleChange} from '@app/features/i18n/utils/LocaleChangeListener';
@@ -12,7 +13,16 @@ import {isAppDeepLink, parseChannelUrl} from '@app/features/navigation/utils/Dee
import {AccountScopedWork} from '@app/features/platform/state/AccountScopedWork';
import {Logger} from '@app/features/platform/utils/AppLogger';
import {loadLazyModule} from '@app/features/platform/utils/LazyModuleLoader';
+import {
+ generateOtherAccountSections,
+ listOtherAccountResults,
+ mergeWithOtherAccountResults,
+ OTHER_ACCOUNT_ACTIVITY_CHANNEL_LIMIT,
+ type OtherAccountsContext,
+ searchOtherAccounts,
+} from '@app/features/search/state/QuickSwitcherAccountResults';
import {buildCandidateSets} from '@app/features/search/state/QuickSwitcherCandidateBuilder';
+import {buildOtherAccountsContext} from '@app/features/search/state/QuickSwitcherOtherAccounts';
import {getFirstSelectableIndex} from '@app/features/search/state/QuickSwitcherResultConverters';
import {
generateDefaultResults,
@@ -30,6 +40,7 @@ import type {
QuickSwitcherExecutableResult,
QuickSwitcherQueryMode,
QuickSwitcherResult,
+ QuickSwitcherScope,
} from '@app/features/search/state/QuickSwitcherTypes';
import {MEMBER_SEARCH_LIMIT, QUICK_SWITCHER_OVERLAY_ID} from '@app/features/search/state/QuickSwitcherTypes';
import {
@@ -41,9 +52,10 @@ import * as ModalCommands from '@app/features/ui/commands/ModalCommands';
import {modal} from '@app/features/ui/commands/ModalCommands';
import MobileLayout from '@app/features/ui/state/MobileLayout';
import {QuickSwitcherResultTypes} from '@fluxer/constants/src/QuickSwitcherConstants';
+import type {AccountEngineAccountActivity} from '@fluxer/desktop_ipc/src/AccountEngineContract';
import type {I18n} from '@lingui/core';
import {msg} from '@lingui/core/macro';
-import {makeAutoObservable, runInAction} from 'mobx';
+import {type IReactionDisposer, makeAutoObservable, reaction, runInAction} from 'mobx';
const GO_TO_MESSAGE_DESCRIPTOR = msg({
message: 'Go to message',
@@ -57,6 +69,8 @@ const PREFIX_MODES: ReadonlyMap<string, QuickSwitcherQueryMode> = new Map([
]);
const ALL_USERS_PREFIX = '@@';
const NO_CONFUSABLES: ReadonlyMap<string, string> = new Map();
+const NO_ACCOUNT_ACTIVITY: ReadonlyMap<string, AccountEngineAccountActivity> = new Map();
+const ACCOUNT_ACTIVITY_RELOAD_DELAY_MS = 1_000;
const QUICK_SWITCHER_I18N_MISSING_ERROR = 'QuickSwitcher i18n has not been set';
interface ParsedPrefix {
@@ -88,7 +102,11 @@ class QuickSwitcher {
private defaultResults: Array<QuickSwitcherResult> | null = null;
private modalPreloadPromise: Promise<QuickSwitcherModalModule> | null = null;
private modalPreloadCancel: (() => void) | null = null;
+ private accountActivity: ReadonlyMap<string, AccountEngineAccountActivity> = NO_ACCOUNT_ACTIVITY;
+ private accountActivityGeneration = 0;
+ private accountActivityReaction: IReactionDisposer | null = null;
isOpen = false;
+ scope: QuickSwitcherScope = 'all';
query = '';
prefixMode: QuickSwitcherQueryMode | null = null;
results: Array<QuickSwitcherResult> = [];
@@ -110,6 +128,9 @@ class QuickSwitcher {
| 'logger'
| 'modalPreloadCancel'
| 'modalPreloadPromise'
+ | 'accountActivity'
+ | 'accountActivityGeneration'
+ | 'accountActivityReaction'
>(
this,
{
@@ -121,6 +142,9 @@ class QuickSwitcher {
logger: false,
modalPreloadCancel: false,
modalPreloadPromise: false,
+ accountActivity: false,
+ accountActivityGeneration: false,
+ accountActivityReaction: false,
},
{autoBind: true},
);
@@ -194,17 +218,27 @@ class QuickSwitcher {
return this.selectedIndex;
}
- show(): void {
- if (this.isOpen) return;
+ show(options: {scope?: QuickSwitcherScope} = {}): void {
+ const scope = options.scope ?? 'all';
+ if (this.isOpen) {
+ if (this.scope !== scope) {
+ this.scope = scope;
+ this.defaultResults = null;
+ this.search('');
+ }
+ return;
+ }
this.cancelCandidateWarmup();
this.candidateSets = null;
this.defaultResults = null;
this.isOpen = true;
+ this.scope = scope;
this.query = '';
this.prefixMode = null;
if (!MobileLayout.isMobileLayout()) {
void this.pushModal();
}
+ this.watchAccountActivity();
try {
const {results, selectedIndex} = this.computeResultsForQuery('');
this.results = results;
@@ -268,7 +302,9 @@ class QuickSwitcher {
return;
}
this.cancelCandidateWarmup();
+ this.stopWatchingAccountActivity();
this.isOpen = false;
+ this.scope = 'all';
this.candidateSets = null;
this.searchSources = null;
this.defaultResults = null;
@@ -291,6 +327,44 @@ class QuickSwitcher {
}
}
+ private watchAccountActivity(): void {
+ this.stopWatchingAccountActivity();
+ if (!AccountEngine.available) return;
+ this.accountActivityReaction = reaction(
+ () =>
+ Array.from(
+ AccountEngine.summaries.values(),
+ (summary) => `${summary.accountKey}:${summary.mentionCount}:${summary.unreadChannelCount}:${summary.state}`,
+ ).join('|'),
+ () => this.loadAccountActivity(),
+ {fireImmediately: true, delay: ACCOUNT_ACTIVITY_RELOAD_DELAY_MS},
+ );
+ }
+
+ private stopWatchingAccountActivity(): void {
+ this.accountActivityReaction?.();
+ this.accountActivityReaction = null;
+ this.accountActivityGeneration += 1;
+ this.accountActivity = NO_ACCOUNT_ACTIVITY;
+ }
+
+ private loadAccountActivity(): void {
+ const generation = ++this.accountActivityGeneration;
+ AccountEngine.loadActivity({channelLimit: OTHER_ACCOUNT_ACTIVITY_CHANNEL_LIMIT, mentionLimit: 0}).then(
+ (activity) => {
+ if (!this.isOpen || generation !== this.accountActivityGeneration) return;
+ this.accountActivity = new Map(activity.map((entry) => [entry.accountKey, entry]));
+ this.defaultResults = null;
+ this.recomputeIfOpen({force: true, invalidateCandidates: false});
+ },
+ (error: unknown) => this.logger.warn('Quick switcher failed to load other account activity', error),
+ );
+ }
+
+ private getOtherAccountsContext(i18n: I18n): OtherAccountsContext {
+ return buildOtherAccountsContext(this.accountActivity, i18n);
+ }
+
private invalidateCandidateSets(): void {
this.cancelCandidateWarmup();
this.candidateSets = null;
@@ -460,13 +534,31 @@ class QuickSwitcher {
private getDefaultResults(i18n: I18n): Array<QuickSwitcherResult> {
if (this.defaultResults == null || this.defaultResults.length === 0) {
- this.defaultResults = generateDefaultResults(i18n);
+ this.defaultResults = [
+ ...generateDefaultResults(i18n),
+ ...generateOtherAccountSections(this.getOtherAccountsContext(i18n), i18n),
+ ];
}
return this.defaultResults;
}
+ private computeOtherAccountResults(query: string, i18n: I18n): ComputeResultsForQueryResult {
+ const context = this.getOtherAccountsContext(i18n);
+ let results: Array<QuickSwitcherResult>;
+ if (query.trim().length === 0) {
+ const sections = generateOtherAccountSections(context, i18n);
+ results = sections.length > 0 ? sections : listOtherAccountResults(context, i18n);
+ } else {
+ results = mergeWithOtherAccountResults([], searchOtherAccounts(query, context, i18n));
+ }
+ return {prefixMode: null, results, selectedIndex: getFirstSelectableIndex(results)};
+ }
+
private computeResultsForQuery(query: string): ComputeResultsForQueryResult {
const i18n = this.getI18n();
+ if (this.scope === 'other_accounts') {
+ return this.computeOtherAccountResults(query, i18n);
+ }
const channelPath = parseChannelUrl(query);
const linkResults: Array<QuickSwitcherResult> = [];
if (channelPath) {
@@ -502,7 +594,13 @@ class QuickSwitcher {
} else if (search.length === 0) {
results = this.getDefaultResults(i18n);
} else {
- results = [...linkResults, ...generateGeneralResults(search, this.getSearchContext(i18n), i18n)];
+ results = [
+ ...linkResults,
+ ...mergeWithOtherAccountResults(
+ generateGeneralResults(search, this.getSearchContext(i18n), i18n),
+ searchOtherAccounts(search, this.getOtherAccountsContext(i18n), i18n),
+ ),
+ ];
}
return {
prefixMode: mode,
diff --git a/fluxer_app/src/features/search/state/QuickSwitcherAccountResults.ts b/fluxer_app/src/features/search/state/QuickSwitcherAccountResults.ts
new file mode 100644
index 000000000..359bf18b8
--- /dev/null
+++ b/fluxer_app/src/features/search/state/QuickSwitcherAccountResults.ts
@@ -0,0 +1,289 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {createHeaderResult} from '@app/features/search/state/QuickSwitcherResultConverters';
+import type {
+ AccountChannelResult,
+ AccountResult,
+ QuickSwitcherResult,
+} from '@app/features/search/state/QuickSwitcherTypes';
+import {createSearchTerm, scoreSearchTerm} from '@app/features/search/utils/SearchTextMatching';
+import {compareStrings} from '@app/lib/list/CompareStrings';
+import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
+import {QuickSwitcherResultTypes} from '@fluxer/constants/src/QuickSwitcherConstants';
+import type {
+ AccountEngineAccountActivity,
+ AccountEngineChannelActivity,
+ AccountEngineUser,
+} from '@fluxer/desktop_ipc/src/AccountEngineContract';
+import type {I18n} from '@lingui/core';
+import {msg} from '@lingui/core/macro';
+
+const ACCOUNT_ON_HOST_DESCRIPTOR = msg({
+ message: '{accountName} on {host}',
+ comment:
+ 'Quick switcher section header and row label for another signed-in account when accounts span several instances. accountName is the account display name and host is the instance host name.',
+});
+const MORE_IN_ACCOUNT_DESCRIPTOR = msg({
+ message: '{count, plural, one {# more in {accountName}} other {# more in {accountName}}}',
+ comment:
+ 'Last quick switcher row in another account section. count is how many more unread or mentioned channels that account has. accountName is the account display name.',
+});
+const COMMUNITY_ON_ACCOUNT_DESCRIPTOR = msg({
+ message: '{communityName} · {accountName}',
+ comment:
+ 'Quick switcher row subtitle for a channel on another account. communityName is the community name and accountName is the account display name. Keep the middle dot between the two names.',
+});
+const ACCOUNT_CHANNEL_LABEL_DESCRIPTOR = msg({
+ message: '{channelName} in {communityName} on {accountName}',
+ comment:
+ 'Accessible name for a quick switcher row that opens a channel on another account. channelName, communityName and accountName are names.',
+});
+const ACCOUNT_DM_LABEL_DESCRIPTOR = msg({
+ message: '{channelName} on {accountName}',
+ comment:
+ 'Accessible name for a quick switcher row that opens a direct message on another account. channelName is the conversation name and accountName is the account display name.',
+});
+const UNNAMED_CHANNEL_DESCRIPTOR = msg({
+ message: 'Unnamed channel',
+ comment: 'Fallback name for a channel on another account when its name is not known yet.',
+});
+const SESSION_EXPIRED_DESCRIPTOR = msg({
+ message: 'Session expired',
+ comment: 'Quick switcher row subtitle for another account whose session expired and needs signing in again.',
+});
+const SWITCH_TO_ACCOUNT_DESCRIPTOR = msg({
+ message: 'Switch to this account',
+ comment: 'Quick switcher row subtitle for a row that switches to another signed-in account.',
+});
+
+const OTHER_ACCOUNT_SECTION_ROW_LIMIT = 5;
+export const OTHER_ACCOUNT_ACTIVITY_CHANNEL_LIMIT = OTHER_ACCOUNT_SECTION_ROW_LIMIT + 1;
+const ACCOUNT_PRIORITY_SCORE = 7;
+const QUERY_ACCOUNT_CHANNEL_LIMIT = 20;
+const TITLE_MATCH_WEIGHT = 2;
+
+export interface OtherAccountInfo {
+ readonly accountKey: string;
+ readonly displayName: string;
+ readonly username: string | null;
+ readonly host: string | null;
+ readonly isExpired: boolean;
+ readonly mentionCount: number;
+ readonly hasUnread: boolean;
+ readonly lastActivityAt: number | null;
+}
+
+export interface OtherAccountsContext {
+ readonly accounts: ReadonlyArray<OtherAccountInfo>;
+ readonly activity: ReadonlyMap<string, AccountEngineAccountActivity>;
+ readonly showHost: boolean;
+}
+
+export interface OtherAccountQueryResults {
+ readonly leading: Array<QuickSwitcherResult>;
+ readonly trailing: Array<QuickSwitcherResult>;
+}
+
+function accountLabel(account: OtherAccountInfo, showHost: boolean, i18n: I18n): string {
+ if (!showHost || account.host == null) return account.displayName;
+ return i18n._(ACCOUNT_ON_HOST_DESCRIPTOR, {accountName: account.displayName, host: account.host});
+}
+
+function userName(user: AccountEngineUser): string {
+ return user.globalName != null && user.globalName.length > 0 ? user.globalName : user.username;
+}
+
+function isDirectChannel(channel: AccountEngineChannelActivity): boolean {
+ return channel.type === ChannelTypes.DM || channel.type === ChannelTypes.GROUP_DM;
+}
+
+export function accountChannelTitle(channel: AccountEngineChannelActivity, i18n: I18n): string {
+ if (channel.name != null && channel.name.length > 0) return channel.name;
+ if (channel.recipients.length > 0) return channel.recipients.map(userName).join(', ');
+ return i18n._(UNNAMED_CHANNEL_DESCRIPTOR);
+}
+
+export function accountChannelAccessibleName(result: AccountChannelResult, i18n: I18n): string {
+ if (result.guildName == null) {
+ return i18n._(ACCOUNT_DM_LABEL_DESCRIPTOR, {channelName: result.title, accountName: result.accountName});
+ }
+ return i18n._(ACCOUNT_CHANNEL_LABEL_DESCRIPTOR, {
+ channelName: result.title,
+ communityName: result.guildName,
+ accountName: result.accountName,
+ });
+}
+
+function accountChannelSubtitle(
+ account: OtherAccountInfo,
+ guildName: string | null,
+ underAccountHeader: boolean,
+ i18n: I18n,
+): string | undefined {
+ if (underAccountHeader) return guildName ?? undefined;
+ if (guildName == null) return account.displayName;
+ return i18n._(COMMUNITY_ON_ACCOUNT_DESCRIPTOR, {communityName: guildName, accountName: account.displayName});
+}
+
+function toAccountChannelResult(
+ account: OtherAccountInfo,
+ channel: AccountEngineChannelActivity,
+ underAccountHeader: boolean,
+ i18n: I18n,
+): AccountChannelResult {
+ const guildName = isDirectChannel(channel) ? null : channel.guildName;
+ return {
+ type: QuickSwitcherResultTypes.ACCOUNT_CHANNEL,
+ id: `${account.accountKey}|${channel.channelId}`,
+ title: accountChannelTitle(channel, i18n),
+ subtitle: accountChannelSubtitle(account, guildName, underAccountHeader, i18n),
+ accountKey: account.accountKey,
+ accountName: account.displayName,
+ channelId: channel.channelId,
+ guildId: channel.guildId,
+ guildName,
+ channelType: channel.type,
+ mentionCount: channel.mentionCount,
+ unread: channel.unread || channel.mentionCount > 0,
+ };
+}
+
+function toAccountResult(
+ account: OtherAccountInfo,
+ showHost: boolean,
+ i18n: I18n,
+ moreCount: number | null = null,
+): AccountResult {
+ const title =
+ moreCount == null
+ ? accountLabel(account, showHost, i18n)
+ : i18n._(MORE_IN_ACCOUNT_DESCRIPTOR, {count: moreCount, accountName: account.displayName});
+ let subtitle: string | undefined;
+ if (moreCount == null) {
+ subtitle = account.isExpired
+ ? i18n._(SESSION_EXPIRED_DESCRIPTOR)
+ : (account.username ?? i18n._(SWITCH_TO_ACCOUNT_DESCRIPTOR));
+ }
+ return {
+ type: QuickSwitcherResultTypes.ACCOUNT,
+ id: moreCount == null ? account.accountKey : `${account.accountKey}|more`,
+ title,
+ subtitle,
+ accountKey: account.accountKey,
+ accountName: account.displayName,
+ mentionCount: account.mentionCount,
+ hasUnread: account.hasUnread,
+ moreCount,
+ };
+}
+
+function sortChannelsForSection(
+ channels: ReadonlyArray<AccountEngineChannelActivity>,
+): Array<AccountEngineChannelActivity> {
+ const mentioned = channels.filter((channel) => channel.mentionCount > 0);
+ const unread = channels.filter((channel) => channel.mentionCount === 0 && channel.unread);
+ return [...mentioned, ...unread];
+}
+
+function newestActivityFirst(left: OtherAccountInfo, right: OtherAccountInfo): number {
+ const leftAt = left.lastActivityAt ?? 0;
+ const rightAt = right.lastActivityAt ?? 0;
+ if (leftAt !== rightAt) return rightAt - leftAt;
+ if (left.mentionCount !== right.mentionCount) return right.mentionCount - left.mentionCount;
+ return compareStrings(left.accountKey, right.accountKey);
+}
+
+export function generateOtherAccountSections(context: OtherAccountsContext, i18n: I18n): Array<QuickSwitcherResult> {
+ const results: Array<QuickSwitcherResult> = [];
+ for (const account of [...context.accounts].sort(newestActivityFirst)) {
+ const activity = context.activity.get(account.accountKey);
+ if (activity == null) continue;
+ const channels = sortChannelsForSection(activity.channels);
+ if (channels.length === 0) continue;
+ const shown = channels.slice(0, OTHER_ACCOUNT_SECTION_ROW_LIMIT);
+ results.push(
+ createHeaderResult(`account-${account.accountKey}`, accountLabel(account, context.showHost, i18n), {
+ accountKey: account.accountKey,
+ }),
+ );
+ for (const channel of shown) results.push(toAccountChannelResult(account, channel, true, i18n));
+ const total = Math.max(activity.totalChannelCount, channels.length);
+ const moreCount = total - shown.length;
+ if (moreCount > 0) results.push(toAccountResult(account, context.showHost, i18n, moreCount));
+ }
+ return results;
+}
+
+function bestScore(values: ReadonlyArray<string | null>, query: string): number {
+ const term = createSearchTerm(query.toLocaleLowerCase());
+ let best = 0;
+ for (const value of values) {
+ if (value == null || value.length === 0) continue;
+ best = Math.max(best, scoreSearchTerm(value, term, false));
+ }
+ return best;
+}
+
+export function searchOtherAccounts(
+ query: string,
+ context: OtherAccountsContext,
+ i18n: I18n,
+): OtherAccountQueryResults {
+ const trimmed = query.trim();
+ if (trimmed.length === 0) return {leading: [], trailing: []};
+ const leading: Array<{result: AccountResult; score: number}> = [];
+ const trailingAccounts: Array<{result: AccountResult; score: number}> = [];
+ const channelMatches: Array<{result: AccountChannelResult; score: number}> = [];
+ for (const account of context.accounts) {
+ const nameScore = bestScore([account.displayName, account.username], trimmed);
+ const score = Math.max(nameScore, bestScore([account.host], trimmed));
+ if (score > 0) {
+ const entry = {result: toAccountResult(account, context.showHost, i18n), score};
+ if (nameScore >= ACCOUNT_PRIORITY_SCORE) leading.push(entry);
+ else trailingAccounts.push(entry);
+ }
+ const activity = context.activity.get(account.accountKey);
+ if (activity == null) continue;
+ for (const channel of activity.channels) {
+ const title = accountChannelTitle(channel, i18n);
+ const channelScore = Math.max(
+ bestScore([title], trimmed) * TITLE_MATCH_WEIGHT,
+ bestScore(
+ [channel.guildName, ...channel.recipients.flatMap((user) => [user.globalName, user.username])],
+ trimmed,
+ ),
+ );
+ if (channelScore > 0)
+ channelMatches.push({result: toAccountChannelResult(account, channel, false, i18n), score: channelScore});
+ }
+ }
+ const byScore = <T extends {result: {mentionCount: number; title: string}; score: number}>(left: T, right: T) => {
+ if (left.score !== right.score) return right.score - left.score;
+ if (left.result.mentionCount !== right.result.mentionCount)
+ return right.result.mentionCount - left.result.mentionCount;
+ return left.result.title.localeCompare(right.result.title);
+ };
+ return {
+ leading: leading.sort(byScore).map((entry) => entry.result),
+ trailing: [
+ ...trailingAccounts.sort(byScore).map((entry) => entry.result),
+ ...channelMatches
+ .sort(byScore)
+ .slice(0, QUERY_ACCOUNT_CHANNEL_LIMIT)
+ .map((entry) => entry.result),
+ ],
+ };
+}
+
+export function listOtherAccountResults(context: OtherAccountsContext, i18n: I18n): Array<AccountResult> {
+ return [...context.accounts]
+ .sort(newestActivityFirst)
+ .map((account) => toAccountResult(account, context.showHost, i18n));
+}
+
+export function mergeWithOtherAccountResults(
+ currentAccountResults: ReadonlyArray<QuickSwitcherResult>,
+ otherAccountResults: OtherAccountQueryResults,
+): Array<QuickSwitcherResult> {
+ return [...otherAccountResults.leading, ...currentAccountResults, ...otherAccountResults.trailing];
+}
diff --git a/fluxer_app/src/features/search/state/QuickSwitcherOtherAccounts.ts b/fluxer_app/src/features/search/state/QuickSwitcherOtherAccounts.ts
new file mode 100644
index 000000000..4303781fe
--- /dev/null
+++ b/fluxer_app/src/features/search/state/QuickSwitcherOtherAccounts.ts
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import AccountEngine from '@app/features/account_engine/state/AccountEngine';
+import {
+ ACCOUNT_DETAILS_UNAVAILABLE_DESCRIPTOR,
+ getAccountDisplayLabels,
+ resolveAccountInstanceKey,
+ resolveAccountInstanceLabel,
+} from '@app/features/auth/AccountDisplayUtils';
+import {getAccountKey} from '@app/features/auth/state/AccountStorageKey';
+import {switcherAccounts} from '@app/features/auth/state/AccountSwitcherAccounts';
+import Accounts from '@app/features/auth/state/Accounts';
+import type {Account} from '@app/features/platform/state/AuthSession';
+import type {OtherAccountInfo, OtherAccountsContext} from '@app/features/search/state/QuickSwitcherAccountResults';
+import type {AccountEngineAccountActivity} from '@fluxer/desktop_ipc/src/AccountEngineContract';
+import type {I18n} from '@lingui/core';
+
+export function accountsSpanSeveralInstances(accounts: ReadonlyArray<Account>): boolean {
+ const instanceKeys = new Set<string>();
+ for (const account of accounts) {
+ const instanceKey = resolveAccountInstanceKey(account);
+ if (instanceKey != null) instanceKeys.add(instanceKey);
+ if (instanceKeys.size > 1) return true;
+ }
+ return false;
+}
+
+export function otherAccountDisplayName(account: Account, i18n: I18n): string {
+ const labels = getAccountDisplayLabels(account);
+ if (labels.available) return labels.displayLabel;
+ const user = AccountEngine.getSummary(getAccountKey(account))?.user ?? null;
+ if (user != null) return user.globalName ?? user.username;
+ return i18n._(ACCOUNT_DETAILS_UNAVAILABLE_DESCRIPTOR);
+}
+
+function toOtherAccountInfo(account: Account, i18n: I18n): OtherAccountInfo {
+ const accountKey = getAccountKey(account);
+ const labels = getAccountDisplayLabels(account);
+ const summary = AccountEngine.getSummary(accountKey);
+ return {
+ accountKey,
+ displayName: otherAccountDisplayName(account, i18n),
+ username: labels.available ? labels.usernameLabel : (summary?.user?.username ?? null),
+ host: resolveAccountInstanceLabel(account),
+ isExpired: account.isValid === false,
+ mentionCount: AccountEngine.getMentionCount(accountKey),
+ hasUnread: AccountEngine.hasUnread(accountKey),
+ lastActivityAt: summary?.lastMessageAt ?? null,
+ };
+}
+
+export function buildOtherAccountsContext(
+ activity: ReadonlyMap<string, AccountEngineAccountActivity>,
+ i18n: I18n,
+): OtherAccountsContext {
+ const accounts = switcherAccounts();
+ const currentAccountKey = Accounts.currentAccountKey;
+ return {
+ accounts: accounts
+ .filter((account) => getAccountKey(account) !== currentAccountKey)
+ .map((account) => toOtherAccountInfo(account, i18n)),
+ activity,
+ showHost: accountsSpanSeveralInstances(accounts),
+ };
+}
diff --git a/fluxer_app/src/features/search/state/QuickSwitcherResultConverters.ts b/fluxer_app/src/features/search/state/QuickSwitcherResultConverters.ts
index 4c1080d69..657ed221a 100644
--- a/fluxer_app/src/features/search/state/QuickSwitcherResultConverters.ts
+++ b/fluxer_app/src/features/search/state/QuickSwitcherResultConverters.ts
@@ -72,8 +72,9 @@ export function candidateToResult(candidate: Candidate): QuickSwitcherExecutable
}
}
-export function createHeaderResult(id: string, title: string): HeaderResult {
- return {type: QuickSwitcherResultTypes.HEADER, id, title};
+export function createHeaderResult(id: string, title: string, options: {accountKey?: string} = {}): HeaderResult {
+ if (options.accountKey == null) return {type: QuickSwitcherResultTypes.HEADER, id, title};
+ return {type: QuickSwitcherResultTypes.HEADER, id, title, accountKey: options.accountKey};
}
export function getFirstSelectableIndex(results: ReadonlyArray<QuickSwitcherResult>): number {
diff --git a/fluxer_app/src/features/search/state/QuickSwitcherTypes.ts b/fluxer_app/src/features/search/state/QuickSwitcherTypes.ts
index d74183f97..94f3c2bb3 100644
--- a/fluxer_app/src/features/search/state/QuickSwitcherTypes.ts
+++ b/fluxer_app/src/features/search/state/QuickSwitcherTypes.ts
@@ -26,6 +26,7 @@ export interface HeaderResult {
type: typeof QuickSwitcherResultTypes.HEADER;
id: string;
title: string;
+ accountKey?: string;
}
export interface UserResult {
@@ -96,6 +97,35 @@ export interface LinkResult {
path: string;
}
+export interface AccountResult {
+ type: typeof QuickSwitcherResultTypes.ACCOUNT;
+ id: string;
+ title: string;
+ subtitle?: string;
+ accountKey: string;
+ accountName: string;
+ mentionCount: number;
+ hasUnread: boolean;
+ moreCount: number | null;
+}
+
+export interface AccountChannelResult {
+ type: typeof QuickSwitcherResultTypes.ACCOUNT_CHANNEL;
+ id: string;
+ title: string;
+ subtitle?: string;
+ accountKey: string;
+ accountName: string;
+ channelId: string;
+ guildId: string | null;
+ guildName: string | null;
+ channelType: number;
+ mentionCount: number;
+ unread: boolean;
+}
+
+export type QuickSwitcherScope = 'all' | 'other_accounts';
+
export type QuickSwitcherResult =
| HeaderResult
| UserResult
@@ -105,7 +135,9 @@ export type QuickSwitcherResult =
| GuildResult
| VirtualGuildResult
| SettingsResult
- | LinkResult;
+ | LinkResult
+ | AccountResult
+ | AccountChannelResult;
export type QuickSwitcherExecutableResult = Exclude<QuickSwitcherResult, HeaderResult>;
interface CandidateBase<T extends QuickSwitcherResultType> {
diff --git a/packages/constants/src/QuickSwitcherConstants.ts b/packages/constants/src/QuickSwitcherConstants.ts
index 2ea0ae1d2..1b8dd1909 100644
--- a/packages/constants/src/QuickSwitcherConstants.ts
+++ b/packages/constants/src/QuickSwitcherConstants.ts
@@ -12,6 +12,8 @@ export const QuickSwitcherResultTypes = {
VIRTUAL_GUILD: 'virtual_guild',
SETTINGS: 'settings',
LINK: 'link',
+ ACCOUNT: 'account',
+ ACCOUNT_CHANNEL: 'account_channel',
} as const;
export type QuickSwitcherResultType = ValueOf<typeof QuickSwitcherResultTypes>;