Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(mentions): rank users/groups first on mobile mention menu (#3158)
gbirmand62c8f16ec1c
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/MentionsMenu.tsx b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/MentionsMenu.tsxindex fdba2ee2e2..fc255baece 100644--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/MentionsMenu.tsx+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/MentionsMenu.tsx@@ -9,7 +9,6 @@ import { isMobile } from '@core/mobile/isMobile';import type { ChannelWithParticipants, IUser } from '@core/user';import { useDateSearch } from '@core/util/dateSearch/useDateSearch';import { debouncedDependent } from '@core/util/debounce';-import { createFreshSearch } from '@core/util/freshSort';import { useIsKeyPressActive } from '@core/util/useIsKeyPressActive';import type { EmailEntity } from '@entity';import type { HistoryItem as Item } from '@queries/history/history';@@ -50,24 +49,7 @@ import { useUsersMention } from './hooks/useUsersMention';import type { BucketConfig, MentionBucketId } from './MentionsMenuController';import { useMentionsMenuController } from './MentionsMenuController';import { createItemHandler } from './utils/mentionHandlers';--const mobileAllSearch = createFreshSearch<MentionItem>({- config: {- useViewedAt: true,- fuzzyWeight: 0.4,- timeWeight: 0.6,- brevityWeight: 0,- },- getName: (item) => {- if (item.kind === 'date') return item.data.displayText;- if (item.kind === 'group') return item.data.groupAlias;- return item.searchText;- },- getTimestamp: (item) => {- if (item.kind === 'date' || item.kind === 'group') return {};- return item.timestamps;- },-});+import { sortMobileMentions } from './utils/mobileSort';const MAX_ITEMS = 8;const VIRTUAL_ITEM_HEIGHT = 36;@@ -227,8 +209,6 @@ function MentionsMenuInner(props: MentionsMenuProps) {const [mountSelection, setMountSelection] = createSignal<Selection | null>();- // On mobile, combine all sources into a single list interleaved by- // freshness instead of grouped by category.const mobileAllItems = createLazyMemo((): MentionItem[] => {const combined: MentionItem[] = [...(usersAndGroups() ?? []),@@ -237,7 +217,7 @@ function MentionsMenuInner(props: MentionsMenuProps) {...(emails() ?? []),...(dates() ?? []),];- return mobileAllSearch(combined, searchTerm()).map(({ item }) => item);+ return sortMobileMentions(combined, searchTerm());});const bucketConfigs = createLazyMemo((): BucketConfig[] => {diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/utils/mobileSort.test.ts b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/utils/mobileSort.test.tsnew file mode 100644index 0000000000..e3b67258e3--- /dev/null+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/utils/mobileSort.test.ts@@ -0,0 +1,231 @@+import { describe, expect, test } from 'vitest';+import type {+ DateMentionItem,+ GroupMentionItem,+ MentionItem,+} from '../../../../utils/mentionsUtils';+import { sortMobileMentions } from './mobileSort';++const HOUR = 60 * 60 * 1000;++function userItem(+ id: string,+ name: string,+ viewedAt: Date = new Date()+): MentionItem {+ return {+ kind: 'user',+ bucket: 'person',+ id,+ searchText: name,+ sortTimestamp: viewedAt.getTime(),+ timestamps: { viewedAt, updatedAt: viewedAt },+ data: {+ id,+ name,+ email: `${name}@example.com`,+ } as MentionItem extends { kind: 'user'; data: infer D } ? D : never,+ } as MentionItem;+}++function docItem(+ id: string,+ name: string,+ viewedAt: Date = new Date()+): MentionItem {+ return {+ kind: 'entity',+ bucket: 'document',+ id,+ searchText: name,+ sortTimestamp: viewedAt.getTime(),+ timestamps: { viewedAt, updatedAt: viewedAt },+ data: {+ id,+ name,+ type: 'document',+ } as MentionItem extends { kind: 'entity'; data: infer D } ? D : never,+ } as MentionItem;+}++function channelItem(+ id: string,+ name: string,+ viewedAt: Date = new Date()+): MentionItem {+ return {+ kind: 'entity',+ bucket: 'channel',+ id,+ searchText: name,+ sortTimestamp: viewedAt.getTime(),+ timestamps: { viewedAt, updatedAt: viewedAt },+ data: {+ id,+ name,+ type: 'channel',+ } as MentionItem extends { kind: 'entity'; data: infer D } ? D : never,+ } as MentionItem;+}++function dmItem(+ id: string,+ name: string,+ viewedAt: Date = new Date()+): MentionItem {+ return {+ kind: 'entity',+ bucket: 'dm',+ id,+ searchText: name,+ sortTimestamp: viewedAt.getTime(),+ timestamps: { viewedAt, updatedAt: viewedAt },+ data: {+ id,+ name,+ type: 'channel',+ } as MentionItem extends { kind: 'entity'; data: infer D } ? D : never,+ } as MentionItem;+}++function groupItem(alias: string): GroupMentionItem {+ return {+ kind: 'group',+ id: alias,+ data: { id: alias, groupAlias: alias },+ };+}++function dateItem(id: string, displayText: string): DateMentionItem {+ return {+ kind: 'date',+ id: `date-${id}`,+ data: {+ id,+ displayText,+ date: new Date(),+ type: 'natural',+ },+ };+}++describe('sortMobileMentions', () => {+ test('boosts users above an equally-fresh doc when no query is present', () => {+ const now = new Date();+ const user = userItem('u1', 'Alice', now);+ const doc = docItem('d1', 'Recent Doc', now);++ const result = sortMobileMentions([doc, user], '');++ expect(result[0].id).toBe('u1');+ });++ test('does not pin stale users above much fresher docs', () => {+ const now = new Date();+ const veryStaleUser = userItem(+ 'u1',+ 'Old User',+ new Date(now.getTime() - 14 * 24 * HOUR)+ );+ const freshDoc = docItem('d1', 'Just Viewed', now);++ const result = sortMobileMentions([veryStaleUser, freshDoc], '');++ expect(result[0].id).toBe('d1');+ });++ test('orders users by freshness within the user kind', () => {+ const now = new Date();+ const olderUser = userItem(+ 'u-old',+ 'OldUser',+ new Date(now.getTime() - 6 * HOUR)+ );+ const newerUser = userItem('u-new', 'NewUser', now);++ const result = sortMobileMentions([olderUser, newerUser], '');++ expect(result.indexOf(newerUser)).toBeLessThan(result.indexOf(olderUser));+ });++ test('boosts DMs over similarly-fresh channels', () => {+ const now = new Date();+ const dm = dmItem('dm1', 'alice-bob', now);+ const channel = channelItem('c1', 'general', now);++ const result = sortMobileMentions([channel, dm], '');++ expect(result[0].id).toBe('dm1');+ });++ test('with a query, fuzzy match drives ordering across kinds', () => {+ const now = new Date();+ const matchingDoc = docItem('d1', 'project alpha', now);+ const unrelatedUser = userItem('u1', 'Bob', now);++ const result = sortMobileMentions([unrelatedUser, matchingDoc], 'project');++ expect(result[0].id).toBe('d1');+ expect(result.find((i) => i.id === 'u1')).toBeUndefined();+ });++ test('with a query, a matching user still beats a matching doc on equal freshness', () => {+ const now = new Date();+ const matchingUser = userItem('u1', 'project lead', now);+ const matchingDoc = docItem('d1', 'project plan', now);++ const result = sortMobileMentions([matchingDoc, matchingUser], 'project');++ expect(result[0].id).toBe('u1');+ });++ test('typing a name surfaces the user above group DMs that merely contain that name', () => {+ const now = new Date();+ const platyUser = userItem('u1', 'Platymantis lawtoni', now);+ const groupDmA = dmItem(+ 'dm1',+ 'Platymantis lawtoni, test me name, Gabriel Birman',+ now+ );+ const groupDmB = dmItem(+ 'dm2',+ 'Platymantis lawtoni, Gabriel Birman, teo+12312312',+ now+ );++ const result = sortMobileMentions([groupDmA, groupDmB, platyUser], 'platy');++ expect(result[0].id).toBe('u1');+ });++ test('penalizes users whose match starts mid-string vs docs that match at start', () => {+ const now = new Date();+ const lateMatchUser = userItem('u1', '[email protected]', now);+ const startMatchDoc = docItem('d1', 'test lexical api', now);++ const result = sortMobileMentions([lateMatchUser, startMatchDoc], 'test');++ expect(result[0].id).toBe('d1');+ });++ test('groups appear when matched by query but stay below users with stronger boost', () => {+ const now = new Date();+ const here = groupItem('here');+ const user = userItem('u1', 'here-user', now);++ const result = sortMobileMentions([here, user], 'here');++ expect(result.indexOf(user)).toBeLessThan(result.indexOf(here));+ });++ test('items with no timestamp (groups, dates) sink without a query', () => {+ const now = new Date();+ const tomorrow = dateItem('tomorrow', 'tomorrow');+ const here = groupItem('here');+ const doc = docItem('d1', 'Doc', now);++ const result = sortMobileMentions([tomorrow, here, doc], '');++ expect(result[0].id).toBe('d1');+ });+});diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/utils/mobileSort.ts b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/utils/mobileSort.tsnew file mode 100644index 0000000000..be80d201bb--- /dev/null+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/utils/mobileSort.ts@@ -0,0 +1,63 @@+import { createFreshSearch, type FreshSortConfig } from '@core/util/freshSort';+import type { MentionItem } from '../../../../utils/mentionsUtils';++function getMentionName(item: MentionItem): string {+ if (item.kind === 'date') return item.data.displayText;+ if (item.kind === 'group') return item.data.groupAlias;+ return item.searchText;+}++function getMentionTimestamps(item: MentionItem) {+ if (item.kind === 'date' || item.kind === 'group') return {};+ return item.timestamps;+}++function isDmItem(item: MentionItem): boolean {+ return item.kind === 'entity' && item.bucket === 'dm';+}++/**+ * Per-kind boost. With a query, the user is usually targeting a specific+ * person, so users beat group DMs/channels that merely contain that name —+ * the command menu sidesteps this by excluding persons from its "all" view,+ * but the mention menu must include them. Without a query we keep the boost+ * small so freshness dominates.+ */+function mentionBoost(hasQuery: boolean) {+ return (item: MentionItem): number => {+ if (item.kind === 'user') return hasQuery ? 0.4 : 0.2;+ if (item.kind === 'group') return hasQuery ? 0.2 : 0.1;+ return 0;+ };+}++function createMobileSearchConfig(+ hasQuery: boolean+): FreshSortConfig<MentionItem> {+ return {+ useViewedAt: true,+ fuzzyWeight: hasQuery ? 0.5 : 0,+ timeWeight: hasQuery ? 0.4 : 0.9,+ brevityWeight: hasQuery ? 0.1 : 0,+ minFuzzyThreshold: hasQuery ? 0.1 : 0,+ dmBoost: hasQuery ? 1.4 : 1.2,+ commaSeparatedChannelMatch: true,+ gapPenaltyWeight: hasQuery ? 0.4 : 0,+ startBonusDecay: hasQuery ? 0.4 : 0,+ boostFn: mentionBoost(hasQuery),+ };+}++export function sortMobileMentions(+ items: MentionItem[],+ query: string+): MentionItem[] {+ const hasQuery = query.trim().length > 0;+ const search = createFreshSearch<MentionItem>({+ config: createMobileSearchConfig(hasQuery),+ getName: getMentionName,+ isDmItem,+ getTimestamp: getMentionTimestamps,+ });+ return search(items, query).map(({ item }) => item);+}