Skip to content

The open alternative to Salesforce, designed for AI.

feat(ai-chat): one @ menu for teammates and records, grouped by type (#27258)

Félix Malfait20591013980f
diff --git a/packages/twenty-front/src/modules/advanced-text-editor/components/__stories__/AdvancedTextEditor.stories.tsx b/packages/twenty-front/src/modules/advanced-text-editor/components/__stories__/AdvancedTextEditor.stories.tsx
index dd7979e16f8..2a6f3b804ef 100644
--- a/packages/twenty-front/src/modules/advanced-text-editor/components/__stories__/AdvancedTextEditor.stories.tsx
+++ b/packages/twenty-front/src/modules/advanced-text-editor/components/__stories__/AdvancedTextEditor.stories.tsx
@@ -306,22 +306,19 @@ export const Empty: Story = {
export const MinimalDocument: Story = {
args: {
extensionSet: 'minimal',
- placeholder: 'Ask anything, # a record, @ a teammate or / a skill...',
+ placeholder: 'Ask anything, @ a teammate or record, / a skill...',
},
play: async ({ canvasElement, step }) => {
- await step(
- 'Verify placeholder hints at #, @ and / references',
- async () => {
- await waitFor(() =>
- expect(
- canvasElement.querySelector('[data-placeholder]'),
- ).toHaveAttribute(
- 'data-placeholder',
- 'Ask anything, # a record, @ a teammate or / a skill...',
- ),
- );
- },
- );
+ await step('Verify placeholder hints at @ and / references', async () => {
+ await waitFor(() =>
+ expect(
+ canvasElement.querySelector('[data-placeholder]'),
+ ).toHaveAttribute(
+ 'data-placeholder',
+ 'Ask anything, @ a teammate or record, / a skill...',
+ ),
+ );
+ });
},
};
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatAddMenu.tsx b/packages/twenty-front/src/modules/ai/components/AiChatAddMenu.tsx
index c43af3c7863..e5e6d57bfa3 100644
--- a/packages/twenty-front/src/modules/ai/components/AiChatAddMenu.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AiChatAddMenu.tsx
@@ -72,7 +72,7 @@ export const AiChatAddMenu = ({ editor }: AiChatAddMenuProps) => {
</Dropdown.ActionItem>
<Dropdown.ActionItem
startIcon={<IconAddressBook />}
- shortcut={['#']}
+ shortcut={['@']}
page={AI_CHAT_ADD_MENU_PAGE.RECORDS}
>
{t`Records`}
diff --git a/packages/twenty-front/src/modules/ai/constants/AiChatEditorProfile.ts b/packages/twenty-front/src/modules/ai/constants/AiChatEditorProfile.ts
index b662dba62fe..c35c763bc73 100644
--- a/packages/twenty-front/src/modules/ai/constants/AiChatEditorProfile.ts
+++ b/packages/twenty-front/src/modules/ai/constants/AiChatEditorProfile.ts
@@ -1,7 +1,6 @@
import { type AdvancedTextEditorProfile } from '@/advanced-text-editor/types/AdvancedTextEditorProfile';
import { MentionSuggestion } from '@/mention/extensions/MentionSuggestion';
import { MentionTag } from '@/mention/extensions/MentionTag';
-import { WorkspaceMemberMentionSuggestion } from '@/mention/extensions/WorkspaceMemberMentionSuggestion';
import { SkillSuggestion } from '@/skill-suggestion/extensions/SkillSuggestion';
import { SkillTag } from '@/skill-suggestion/extensions/SkillTag';
@@ -12,7 +11,6 @@ export const AI_CHAT_EDITOR_PROFILE = {
buildExtensions: () => [
MentionTag,
MentionSuggestion,
- WorkspaceMemberMentionSuggestion,
SkillTag,
SkillSuggestion,
],
diff --git a/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.ts b/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.ts
index cd1eb368f2a..49f52f3bd54 100644
--- a/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.ts
+++ b/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.ts
@@ -17,7 +17,6 @@ import { AGENT_CHAT_ENSURE_THREAD_FOR_DRAFT_EVENT_NAME } from '@/ai/constants/Ag
import { AGENT_CHAT_SEND_MESSAGE_EVENT_NAME } from '@/ai/constants/AgentChatSendMessageEventName';
import { dispatchBrowserEvent } from '@/browser-event/utils/dispatchBrowserEvent';
import { MENTION_SUGGESTION_PLUGIN_KEY } from '@/mention/constants/MentionSuggestionPluginKey';
-import { WORKSPACE_MEMBER_MENTION_SUGGESTION_PLUGIN_KEY } from '@/mention/constants/WorkspaceMemberMentionSuggestionPluginKey';
import { useMentionSearch } from '@/mention/hooks/useMentionSearch';
import { useWorkspaceMemberMentionSearch } from '@/mention/hooks/useWorkspaceMemberMentionSearch';
import { SKILL_SUGGESTION_PLUGIN_KEY } from '@/skill-suggestion/constants/SkillSuggestionPluginKey';
@@ -45,7 +44,7 @@ export const useAiChatEditor = () => {
const initialDraft = agentChatDraftsByThreadId[draftKey] ?? '';
const editor = useAdvancedTextEditor({
profile: AI_CHAT_EDITOR_PROFILE,
- placeholder: t`Ask anything, # a record, @ a teammate or / a skill...`,
+ placeholder: t`Ask anything, @ a teammate or record, / a skill...`,
readonly: false,
defaultValue: initialDraft,
editorProps: {
@@ -53,7 +52,6 @@ export const useAiChatEditor = () => {
if (event.key === 'Enter' && !event.shiftKey) {
const isSuggestionMenuOpen = [
MENTION_SUGGESTION_PLUGIN_KEY,
- WORKSPACE_MEMBER_MENTION_SUGGESTION_PLUGIN_KEY,
SKILL_SUGGESTION_PLUGIN_KEY,
].some(
(pluginKey) => pluginKey.getState(view.state)?.active === true,
@@ -112,16 +110,10 @@ export const useAiChatEditor = () => {
>;
const mentionStorage = storage['mention-suggestion'] as {
searchMentionRecords: typeof searchMentionRecords;
- };
- mentionStorage.searchMentionRecords = searchMentionRecords;
-
- const workspaceMemberMentionStorage = storage[
- 'workspace-member-mention-suggestion'
- ] as {
searchWorkspaceMembers: typeof searchWorkspaceMembers;
};
- workspaceMemberMentionStorage.searchWorkspaceMembers =
- searchWorkspaceMembers;
+ mentionStorage.searchMentionRecords = searchMentionRecords;
+ mentionStorage.searchWorkspaceMembers = searchWorkspaceMembers;
const skillStorage = storage['skill-suggestion'] as {
searchSkills: typeof searchSkills;
diff --git a/packages/twenty-front/src/modules/mention/components/MentionSuggestionMenu.tsx b/packages/twenty-front/src/modules/mention/components/MentionSuggestionMenu.tsx
index ed090371772..084c424be63 100644
--- a/packages/twenty-front/src/modules/mention/components/MentionSuggestionMenu.tsx
+++ b/packages/twenty-front/src/modules/mention/components/MentionSuggestionMenu.tsx
@@ -1,15 +1,21 @@
-import { AvatarOrIcon } from '@/ui/field/display/components/internal/AvatarOrIcon/AvatarOrIcon';
import { forwardRef } from 'react';
import { MenuItemSuggestion } from 'twenty-ui/components';
import type { MentionSearchResult } from '@/mention/types/MentionSearchResult';
import type { MentionSuggestionMenuProps } from '@/mention/types/MentionSuggestionMenuProps';
+import { groupMentionSearchResultsBySection } from '@/mention/utils/groupMentionSearchResultsBySection';
+import { AvatarOrIcon } from '@/ui/field/display/components/internal/AvatarOrIcon/AvatarOrIcon';
import { SuggestionMenu } from '@/ui/suggestion/components/SuggestionMenu';
import { getAbsoluteImageUrl } from '~/utils/image/getAbsoluteImageUrl';
const getItemKey = (item: MentionSearchResult) =>
`${item.objectNameSingular}-${item.recordId}`;
+const getItemSection = (item: MentionSearchResult) => ({
+ key: item.objectNameSingular,
+ label: item.objectLabelPlural,
+});
+
const renderItem = (
item: MentionSearchResult,
isSelected: boolean,
@@ -42,11 +48,12 @@ export const MentionSuggestionMenu = forwardRef<
return (
<SuggestionMenu
ref={ref}
- items={items}
+ items={groupMentionSearchResultsBySection(items)}
onSelect={onSelect}
editor={editor}
range={range}
getItemKey={getItemKey}
+ getItemSection={getItemSection}
renderItem={(item, isSelected) => renderItem(item, isSelected, onSelect)}
/>
);
diff --git a/packages/twenty-front/src/modules/mention/components/__tests__/MentionSuggestionMenu.test.tsx b/packages/twenty-front/src/modules/mention/components/__tests__/MentionSuggestionMenu.test.tsx
new file mode 100644
index 00000000000..174317da5d6
--- /dev/null
+++ b/packages/twenty-front/src/modules/mention/components/__tests__/MentionSuggestionMenu.test.tsx
@@ -0,0 +1,100 @@
+import { i18n } from '@lingui/core';
+import { I18nProvider } from '@lingui/react';
+import { render, screen } from '@testing-library/react';
+import { type Editor } from '@tiptap/core';
+import { CoreObjectNameSingular } from 'twenty-shared/types';
+
+import { MentionSuggestionMenu } from '@/mention/components/MentionSuggestionMenu';
+import type { MentionSearchResult } from '@/mention/types/MentionSearchResult';
+import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';
+
+const editor = {
+ view: { coordsAtPos: () => ({ left: 0, top: 0, bottom: 16 }) },
+} as unknown as Editor;
+
+const buildResult = (
+ label: string,
+ objectNameSingular: string,
+ objectLabelPlural: string,
+): MentionSearchResult => ({
+ recordId: `${label}-id`,
+ objectNameSingular,
+ objectLabelSingular: objectNameSingular,
+ objectLabelPlural,
+ label,
+ imageUrl: '',
+});
+
+describe('MentionSuggestionMenu', () => {
+ it('lists teammates first, then each object in its own section', () => {
+ const Wrapper = getJestMetadataAndApolloMocksWrapper({});
+
+ render(
+ <Wrapper>
+ <I18nProvider i18n={i18n}>
+ <MentionSuggestionMenu
+ items={[
+ buildResult(
+ 'Grace Hopper',
+ CoreObjectNameSingular.WorkspaceMember,
+ 'Teammates',
+ ),
+ buildResult('Acme', CoreObjectNameSingular.Company, 'Companies'),
+ buildResult(
+ 'Ada Lovelace',
+ CoreObjectNameSingular.Person,
+ 'People',
+ ),
+ buildResult(
+ 'Globex',
+ CoreObjectNameSingular.Company,
+ 'Companies',
+ ),
+ ]}
+ onSelect={jest.fn()}
+ editor={editor}
+ range={{ from: 0, to: 1 }}
+ />
+ </I18nProvider>
+ </Wrapper>,
+ );
+
+ expect(
+ screen
+ .getAllByText(
+ /^(Teammates|Companies|People|Grace Hopper|Acme|Ada Lovelace|Globex)$/,
+ )
+ .map((element) => element.textContent),
+ ).toEqual([
+ 'Teammates',
+ 'Grace Hopper',
+ 'Companies',
+ 'Acme',
+ 'Globex',
+ 'People',
+ 'Ada Lovelace',
+ ]);
+ });
+
+ it('keeps objects sharing a plural label in separate sections', () => {
+ const Wrapper = getJestMetadataAndApolloMocksWrapper({});
+
+ render(
+ <Wrapper>
+ <I18nProvider i18n={i18n}>
+ <MentionSuggestionMenu
+ items={[
+ buildResult('Acme', CoreObjectNameSingular.Company, 'Accounts'),
+ buildResult('Globex', 'account', 'Accounts'),
+ ]}
+ onSelect={jest.fn()}
+ editor={editor}
+ range={{ from: 0, to: 1 }}
+ />
+ </I18nProvider>
+ </Wrapper>,
+ );
+
+ expect(screen.getAllByText('Accounts')).toHaveLength(2);
+ });
+});
diff --git a/packages/twenty-front/src/modules/mention/constants/MentionSuggestionTeammateLimit.ts b/packages/twenty-front/src/modules/mention/constants/MentionSuggestionTeammateLimit.ts
new file mode 100644
index 00000000000..44eda88eee8
--- /dev/null
+++ b/packages/twenty-front/src/modules/mention/constants/MentionSuggestionTeammateLimit.ts
@@ -0,0 +1 @@
+export const MENTION_SUGGESTION_TEAMMATE_LIMIT = 5;
diff --git a/packages/twenty-front/src/modules/mention/constants/WorkspaceMemberMentionSuggestionPluginKey.ts b/packages/twenty-front/src/modules/mention/constants/WorkspaceMemberMentionSuggestionPluginKey.ts
deleted file mode 100644
index e87b395c923..00000000000
--- a/packages/twenty-front/src/modules/mention/constants/WorkspaceMemberMentionSuggestionPluginKey.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { PluginKey } from '@tiptap/pm/state';
-
-export const WORKSPACE_MEMBER_MENTION_SUGGESTION_PLUGIN_KEY = new PluginKey(
- 'workspace-member-mention-suggestion',
-);
diff --git a/packages/twenty-front/src/modules/mention/extensions/MentionSuggestion.ts b/packages/twenty-front/src/modules/mention/extensions/MentionSuggestion.ts
index 667bf3f68a3..ac56071244a 100644
--- a/packages/twenty-front/src/modules/mention/extensions/MentionSuggestion.ts
+++ b/packages/twenty-front/src/modules/mention/extensions/MentionSuggestion.ts
@@ -3,12 +3,15 @@ import Suggestion from '@tiptap/suggestion';
import { MentionSuggestionMenu } from '@/mention/components/MentionSuggestionMenu';
import { MENTION_SUGGESTION_PLUGIN_KEY } from '@/mention/constants/MentionSuggestionPluginKey';
+import { MENTION_SUGGESTION_TEAMMATE_LIMIT } from '@/mention/constants/MentionSuggestionTeammateLimit';
import type { MentionSearchResult } from '@/mention/types/MentionSearchResult';
import { getMentionTagContent } from '@/mention/utils/getMentionTagContent';
+import { isWorkspaceMemberMentionSearchResult } from '@/mention/utils/isWorkspaceMemberMentionSearchResult';
import { createSuggestionRenderLifecycle } from '@/ui/suggestion/components/createSuggestionRenderLifecycle';
type MentionSuggestionOptions = {
searchMentionRecords: (query: string) => Promise<MentionSearchResult[]>;
+ searchWorkspaceMembers: (query: string) => MentionSearchResult[];
};
export const MentionSuggestion = Extension.create<MentionSuggestionOptions>({
@@ -16,11 +19,13 @@ export const MentionSuggestion = Extension.create<MentionSuggestionOptions>({
addOptions: () => ({
searchMentionRecords: async () => [],
+ searchWorkspaceMembers: () => [],
}),
addStorage() {
return {
searchMentionRecords: this.options.searchMentionRecords,
+ searchWorkspaceMembers: this.options.searchWorkspaceMembers,
};
},
@@ -29,7 +34,7 @@ export const MentionSuggestion = Extension.create<MentionSuggestionOptions>({
Suggestion<MentionSearchResult>({
pluginKey: MENTION_SUGGESTION_PLUGIN_KEY,
editor: this.editor,
- char: '#',
+ char: '@',
items: async ({ query }) => {
try {
return await this.storage.searchMentionRecords(query);
@@ -42,7 +47,13 @@ export const MentionSuggestion = Extension.create<MentionSuggestionOptions>({
.chain()
.focus()
.deleteRange(range)
- .insertContent(getMentionTagContent(selectedItem))
+ .insertContent(
+ getMentionTagContent({
+ ...selectedItem,
+ shouldAddAsParticipant:
+ isWorkspaceMemberMentionSearchResult(selectedItem),
+ }),
+ )
.run();
},
render: () =>
@@ -55,6 +66,19 @@ export const MentionSuggestion = Extension.create<MentionSuggestionOptions>({
editor,
range,
}),
+ // Teammates are searched locally, so they show before records
+ // load; all of them once a name is typed, else only a few
+ getLocalItems: (query) => {
+ const workspaceMemberResults =
+ this.storage.searchWorkspaceMembers(query);
+
+ return query === ''
+ ? workspaceMemberResults.slice(
+ 0,
+ MENTION_SUGGESTION_TEAMMATE_LIMIT,
+ )
+ : workspaceMemberResults;
+ },
},
this.editor,
),
diff --git a/packages/twenty-front/src/modules/mention/extensions/MentionTag.ts b/packages/twenty-front/src/modules/mention/extensions/MentionTag.ts
index da457bb7071..770b1d9e549 100644
--- a/packages/twenty-front/src/modules/mention/extensions/MentionTag.ts
+++ b/packages/twenty-front/src/modules/mention/extensions/MentionTag.ts
@@ -2,7 +2,6 @@ import { formatRecordReference } from '@/ai/utils/formatRecordReference';
import { MentionChip } from '@/mention/components/MentionChip';
import { Node } from '@tiptap/core';
import { mergeAttributes, ReactNodeViewRenderer } from '@tiptap/react';
-import { CoreObjectNameSingular } from 'twenty-shared/types';
export const MentionTag = Node.create({
name: 'mentionTag',
@@ -57,7 +56,7 @@ export const MentionTag = Node.create({
'data-type': 'mentionTag',
class: 'mention-tag',
}),
- `${node.attrs.objectNameSingular === CoreObjectNameSingular.WorkspaceMember ? '@' : '#'}${node.attrs.label}`,
+ `@${node.attrs.label}`,
];
},
diff --git a/packages/twenty-front/src/modules/mention/extensions/WorkspaceMemberMentionSuggestion.ts b/packages/twenty-front/src/modules/mention/extensions/WorkspaceMemberMentionSuggestion.ts
deleted file mode 100644
index baa663009dd..00000000000
--- a/packages/twenty-front/src/modules/mention/extensions/WorkspaceMemberMentionSuggestion.ts
+++ /dev/null
@@ -1,64 +0,0 @@
-import { Extension } from '@tiptap/core';
-import Suggestion from '@tiptap/suggestion';
-
-import { MentionSuggestionMenu } from '@/mention/components/MentionSuggestionMenu';
-import { WORKSPACE_MEMBER_MENTION_SUGGESTION_PLUGIN_KEY } from '@/mention/constants/WorkspaceMemberMentionSuggestionPluginKey';
-import type { MentionSearchResult } from '@/mention/types/MentionSearchResult';
-import { getMentionTagContent } from '@/mention/utils/getMentionTagContent';
-import { createSuggestionRenderLifecycle } from '@/ui/suggestion/components/createSuggestionRenderLifecycle';
-
-type WorkspaceMemberMentionSuggestionOptions = {
- searchWorkspaceMembers: (query: string) => MentionSearchResult[];
-};
-
-export const WorkspaceMemberMentionSuggestion =
- Extension.create<WorkspaceMemberMentionSuggestionOptions>({
- name: 'workspace-member-mention-suggestion',
-
- addOptions: () => ({
- searchWorkspaceMembers: () => [],
- }),
-
- addStorage() {
- return {
- searchWorkspaceMembers: this.options.searchWorkspaceMembers,
- };
- },
-
- addProseMirrorPlugins() {
- return [
- Suggestion<MentionSearchResult>({
- pluginKey: WORKSPACE_MEMBER_MENTION_SUGGESTION_PLUGIN_KEY,
- editor: this.editor,
- char: '@',
- items: ({ query }) => this.storage.searchWorkspaceMembers(query),
- command: ({ editor, range, props: selectedItem }) => {
- editor
- .chain()
- .focus()
- .deleteRange(range)
- .insertContent(
- getMentionTagContent({
- ...selectedItem,
- shouldAddAsParticipant: true,
- }),
- )
- .run();
- },
- render: () =>
- createSuggestionRenderLifecycle(
- {
- component: MentionSuggestionMenu,
- getMenuProps: ({ items, onSelect, editor, range }) => ({
- items,
- onSelect,
- editor,
- range,
- }),
- },
- this.editor,
- ),
- }),
- ];
- },
- });
diff --git a/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionSuggestion.test.ts b/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionSuggestion.test.ts
index 175a6c3b0a4..0b3dcbeddcb 100644
--- a/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionSuggestion.test.ts
+++ b/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionSuggestion.test.ts
@@ -61,12 +61,12 @@ describe('MentionSuggestion', () => {
expect(hasSuggestionPlugin).toBe(true);
});
- it('should accept # character in editor content', () => {
+ it('should accept @ character in editor content', () => {
editor.commands.setContent('<p></p>');
editor.commands.focus();
- editor.commands.insertContent('#');
+ editor.commands.insertContent('@');
- expect(editor.getText()).toContain('#');
+ expect(editor.getText()).toContain('@');
});
it('should use default empty search function when not configured', () => {
diff --git a/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionTag.test.ts b/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionTag.test.ts
index d45738252dd..5ad1aa51d1e 100644
--- a/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionTag.test.ts
+++ b/packages/twenty-front/src/modules/mention/extensions/__tests__/MentionTag.test.ts
@@ -183,7 +183,7 @@ describe('MentionTag', () => {
expect(html).toContain('class="mention-tag"');
});
- it('should prefix records with # and workspace members with @', () => {
+ it('should keep the participant flag out of the copied HTML', () => {
editor.commands.setContent({
type: 'doc',
content: [
@@ -219,7 +219,7 @@ describe('MentionTag', () => {
.querySelectorAll('.mention-tag'),
).map((mention) => mention.textContent);
- expect(renderedMentionTexts).toEqual(['#Linus', '@Grace']);
+ expect(renderedMentionTexts).toEqual(['@Linus', '@Grace']);
expect(html).not.toContain('shouldAddAsParticipant');
});
});
diff --git a/packages/twenty-front/src/modules/mention/hooks/useMentionSearch.ts b/packages/twenty-front/src/modules/mention/hooks/useMentionSearch.ts
index 2bb6eabbad0..96bc937e895 100644
--- a/packages/twenty-front/src/modules/mention/hooks/useMentionSearch.ts
+++ b/packages/twenty-front/src/modules/mention/hooks/useMentionSearch.ts
@@ -26,6 +26,17 @@ export const useMentionSearch = () => {
[activeObjectMetadataItems, objectPermissionsByObjectMetadataId],
);
+ const objectLabelPluralByNameSingular = useMemo(
+ () =>
+ Object.fromEntries(
+ searchableObjectMetadataItems.map(({ nameSingular, labelPlural }) => [
+ nameSingular,
+ labelPlural,
+ ]),
+ ),
+ [searchableObjectMetadataItems],
+ );
+
const objectsToSearch = useMemo(
() => searchableObjectMetadataItems.map(({ nameSingular }) => nameSingular),
[searchableObjectMetadataItems],
@@ -51,11 +62,14 @@ export const useMentionSearch = () => {
recordId: searchRecord.recordId,
objectNameSingular: searchRecord.objectNameSingular,
objectLabelSingular: searchRecord.objectLabelSingular,
+ objectLabelPlural:
+ objectLabelPluralByNameSingular[searchRecord.objectNameSingular] ??
+ searchRecord.objectLabelSingular,
label: searchRecord.label,
imageUrl: searchRecord.imageUrl ?? '',
}));
},
- [apolloCoreClient, objectsToSearch],
+ [apolloCoreClient, objectsToSearch, objectLabelPluralByNameSingular],
);
return { searchMentionRecords, searchableObjectMetadataItems };
diff --git a/packages/twenty-front/src/modules/mention/hooks/useWorkspaceMemberMentionSearch.ts b/packages/twenty-front/src/modules/mention/hooks/useWorkspaceMemberMentionSearch.ts
index 7e0824fed99..46eba8b4720 100644
--- a/packages/twenty-front/src/modules/mention/hooks/useWorkspaceMemberMentionSearch.ts
+++ b/packages/twenty-front/src/modules/mention/hooks/useWorkspaceMemberMentionSearch.ts
@@ -1,3 +1,4 @@
+import { useLingui } from '@lingui/react/macro';
import { useCallback } from 'react';
import { CoreObjectNameSingular } from 'twenty-shared/types';
@@ -8,6 +9,7 @@ import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomState
const WORKSPACE_MEMBER_MENTION_SEARCH_LIMIT = 50;
export const useWorkspaceMemberMentionSearch = () => {
+ const { t } = useLingui();
const currentWorkspaceMembers = useAtomStateValue(
currentWorkspaceMembersState,
);
@@ -32,11 +34,12 @@ export const useWorkspaceMemberMentionSearch = () => {
recordId: workspaceMember.id,
objectNameSingular: CoreObjectNameSingular.WorkspaceMember,
objectLabelSingular: workspaceMember.userEmail,
+ objectLabelPlural: t`Teammates`,
label: fullName || workspaceMember.userEmail,
imageUrl: workspaceMember.avatarUrl ?? '',
}));
},
- [currentWorkspaceMembers],
+ [currentWorkspaceMembers, t],
);
return { searchWorkspaceMembers };
diff --git a/packages/twenty-front/src/modules/mention/types/MentionSearchResult.ts b/packages/twenty-front/src/modules/mention/types/MentionSearchResult.ts
index 83a0da69627..0415e015dd3 100644
--- a/packages/twenty-front/src/modules/mention/types/MentionSearchResult.ts
+++ b/packages/twenty-front/src/modules/mention/types/MentionSearchResult.ts
@@ -2,6 +2,7 @@ export type MentionSearchResult = {
recordId: string;
objectNameSingular: string;
objectLabelSingular: string;
+ objectLabelPlural: string;
label: string;
imageUrl: string;
};
diff --git a/packages/twenty-front/src/modules/mention/utils/__tests__/groupMentionSearchResultsBySection.test.ts b/packages/twenty-front/src/modules/mention/utils/__tests__/groupMentionSearchResultsBySection.test.ts
new file mode 100644
index 00000000000..aa69be9ea57
--- /dev/null
+++ b/packages/twenty-front/src/modules/mention/utils/__tests__/groupMentionSearchResultsBySection.test.ts
@@ -0,0 +1,33 @@
+import { CoreObjectNameSingular } from 'twenty-shared/types';
+
+import type { MentionSearchResult } from '@/mention/types/MentionSearchResult';
+import { groupMentionSearchResultsBySection } from '@/mention/utils/groupMentionSearchResultsBySection';
+
+const buildResult = (
+ label: string,
+ objectNameSingular: string,
+): MentionSearchResult => ({
+ recordId: `${label}-id`,
+ objectNameSingular,
+ objectLabelSingular: objectNameSingular,
+ objectLabelPlural: objectNameSingular,
+ label,
+ imageUrl: '',
+});
+
+describe('groupMentionSearchResultsBySection', () => {
+ it('puts teammates first and groups records by object in rank order', () => {
+ const items = [
+ buildResult('Acme', CoreObjectNameSingular.Company),
+ buildResult('Grace', CoreObjectNameSingular.WorkspaceMember),
+ buildResult('Ada', CoreObjectNameSingular.Person),
+ buildResult('Phil', CoreObjectNameSingular.WorkspaceMember),
+ buildResult('Globex', CoreObjectNameSingular.Company),
+ buildResult('Tim', CoreObjectNameSingular.WorkspaceMember),
+ ];
+
+ expect(
+ groupMentionSearchResultsBySection(items).map(({ label }) => label),
+ ).toEqual(['Grace', 'Phil', 'Tim', 'Acme', 'Globex', 'Ada']);
+ });
+});
diff --git a/packages/twenty-front/src/modules/mention/utils/groupMentionSearchResultsBySection.ts b/packages/twenty-front/src/modules/mention/utils/groupMentionSearchResultsBySection.ts
new file mode 100644
index 00000000000..83be2983aec
--- /dev/null
+++ b/packages/twenty-front/src/modules/mention/utils/groupMentionSearchResultsBySection.ts
@@ -0,0 +1,22 @@
+import type { MentionSearchResult } from '@/mention/types/MentionSearchResult';
+import { isWorkspaceMemberMentionSearchResult } from '@/mention/utils/isWorkspaceMemberMentionSearchResult';
+
+export const groupMentionSearchResultsBySection = (
+ items: MentionSearchResult[],
+): MentionSearchResult[] => {
+ const recordItems = items.filter(
+ (item) => !isWorkspaceMemberMentionSearchResult(item),
+ );
+ const objectNamesInRankOrder = [
+ ...new Set(recordItems.map(({ objectNameSingular }) => objectNameSingular)),
+ ];
+
+ return [
+ ...items.filter(isWorkspaceMemberMentionSearchResult),
+ ...objectNamesInRankOrder.flatMap((objectNameSingular) =>
+ recordItems.filter(
+ (item) => item.objectNameSingular === objectNameSingular,
+ ),
+ ),
+ ];
+};
diff --git a/packages/twenty-front/src/modules/mention/utils/isWorkspaceMemberMentionSearchResult.ts b/packages/twenty-front/src/modules/mention/utils/isWorkspaceMemberMentionSearchResult.ts
new file mode 100644
index 00000000000..3bb4e2712aa
--- /dev/null
+++ b/packages/twenty-front/src/modules/mention/utils/isWorkspaceMemberMentionSearchResult.ts
@@ -0,0 +1,8 @@
+import { CoreObjectNameSingular } from 'twenty-shared/types';
+
+import type { MentionSearchResult } from '@/mention/types/MentionSearchResult';
+
+export const isWorkspaceMemberMentionSearchResult = ({
+ objectNameSingular,
+}: Pick<MentionSearchResult, 'objectNameSingular'>) =>
+ objectNameSingular === CoreObjectNameSingular.WorkspaceMember;
diff --git a/packages/twenty-front/src/modules/ui/suggestion/components/SuggestionMenu.tsx b/packages/twenty-front/src/modules/ui/suggestion/components/SuggestionMenu.tsx
index 19021166812..acf8ff15756 100644
--- a/packages/twenty-front/src/modules/ui/suggestion/components/SuggestionMenu.tsx
+++ b/packages/twenty-front/src/modules/ui/suggestion/components/SuggestionMenu.tsx
@@ -16,11 +16,13 @@ import {
} from 'react';
import { RootStackingContextZIndices } from '@/ui/layout/constants/RootStackingContextZIndices';
+import { DropdownMenuSectionLabel } from '@/ui/layout/dropdown/components/DropdownMenuSectionLabel';
import { OverlayMenuList } from '@/ui/layout/overlay/components/OverlayMenuList';
import { OverlayContainer } from '@/ui/layout/overlay/components/OverlayContainer';
import { SuggestionItemPreviewTooltip } from '@/ui/suggestion/components/SuggestionItemPreviewTooltip';
import type { SuggestionMenuProps } from '@/ui/suggestion/types/SuggestionMenuProps';
import { getSuggestionMenuItemAnchorId } from '@/ui/suggestion/utils/getSuggestionMenuItemAnchorId';
+import { isDefined } from 'twenty-shared/utils';
import { useIsMobile } from 'twenty-ui/utilities';
type SuggestionMenuInnerProps<TItem> = SuggestionMenuProps<TItem>;
@@ -38,6 +40,7 @@ const SuggestionMenuInner = <TItem,>(
getItemKey,
renderItem,
selectedItemPreview,
+ getItemSection,
onKeyDown,
} = props;
@@ -204,6 +207,11 @@ const SuggestionMenuInner = <TItem,>(
<OverlayMenuList ref={listContainerRef}>
{items.map((item, index) => {
const isSelected = index === clampedSelectedIndex;
+ const section = getItemSection?.(item);
+ const isFirstOfSection =
+ isDefined(section) &&
+ (index === 0 ||
+ getItemSection?.(items[index - 1])?.key !== section.key);
return (
<div
@@ -214,6 +222,9 @@ const SuggestionMenuInner = <TItem,>(
event.preventDefault();
}}
>
+ {isFirstOfSection && (
+ <DropdownMenuSectionLabel label={section.label} />
+ )}
{renderItem(item, isSelected)}
</div>
);
diff --git a/packages/twenty-front/src/modules/ui/suggestion/components/__tests__/createSuggestionRenderLifecycle.test.ts b/packages/twenty-front/src/modules/ui/suggestion/components/__tests__/createSuggestionRenderLifecycle.test.ts
index 39e45f2adc4..d7c8a156b1b 100644
--- a/packages/twenty-front/src/modules/ui/suggestion/components/__tests__/createSuggestionRenderLifecycle.test.ts
+++ b/packages/twenty-front/src/modules/ui/suggestion/components/__tests__/createSuggestionRenderLifecycle.test.ts
@@ -182,4 +182,48 @@ describe('createSuggestionRenderLifecycle', () => {
expect(mockDestroy).toHaveBeenCalled();
});
});
+
+ describe('local items', () => {
+ it('opens with the local items while searched items load, then lists them first', () => {
+ const localItem = { id: 'local', label: 'Local' };
+ const searchedItem = { id: 'searched', label: 'Searched' };
+ const { ReactRenderer } = jest.requireMock('@tiptap/react');
+ const lifecycle = createSuggestionRenderLifecycle<
+ TestItem,
+ TestMenuProps
+ >(
+ {
+ component: (() =>
+ null) as unknown as React.ComponentType<TestMenuProps>,
+ getMenuProps: ({ items, onSelect, editor, range }) => ({
+ items,
+ onSelect,
+ editor,
+ range,
+ }),
+ getLocalItems: (query) => (query === 'lo' ? [localItem] : []),
+ },
+ mockEditor,
+ );
+
+ lifecycle.onStart(createMockCallbackProps({ items: [], query: 'lo' }));
+
+ expect(ReactRenderer).toHaveBeenCalledWith(
+ expect.anything(),
+ expect.objectContaining({
+ props: expect.objectContaining({ items: [localItem] }),
+ }),
+ );
+
+ lifecycle.onUpdate(
+ createMockCallbackProps({ items: [searchedItem], query: 'lo' }),
+ );
+
+ expect(mockUpdateProps).toHaveBeenCalledWith(
+ expect.objectContaining({ items: [localItem, searchedItem] }),
+ );
+
+ lifecycle.onExit();
+ });
+ });
});
diff --git a/packages/twenty-front/src/modules/ui/suggestion/components/createSuggestionRenderLifecycle.ts b/packages/twenty-front/src/modules/ui/suggestion/components/createSuggestionRenderLifecycle.ts
index 1ebafb738bb..0fa09bc35aa 100644
--- a/packages/twenty-front/src/modules/ui/suggestion/components/createSuggestionRenderLifecycle.ts
+++ b/packages/twenty-front/src/modules/ui/suggestion/components/createSuggestionRenderLifecycle.ts
@@ -25,6 +25,8 @@ type SuggestionRenderLifecycleConfig<TItem, TMenuProps extends AnyRecord> = {
range: Range;
query: string;
}) => TMenuProps;
+ // Shown at once and kept above the searched items, which arrive later
+ getLocalItems?: (query: string) => TItem[];
};
export const createSuggestionRenderLifecycle = <
@@ -43,9 +45,17 @@ export const createSuggestionRenderLifecycle = <
}
};
- const buildMenuProps = (props: SuggestionCallbackProps<TItem>) =>
+ const getItems = (props: SuggestionCallbackProps<TItem>) => [
+ ...(config.getLocalItems?.(props.query) ?? []),
+ ...props.items,
+ ];
+
+ const buildMenuProps = (
+ props: SuggestionCallbackProps<TItem>,
+ items: TItem[],
+ ) =>
config.getMenuProps({
- items: props.items,
+ items,
onSelect: (item: TItem) => {
props.command(item);
closeMenu();
@@ -55,38 +65,49 @@ export const createSuggestionRenderLifecycle = <
query: props.query,
});
- const createRenderer = (props: SuggestionCallbackProps<TItem>) => {
+ const createRenderer = (
+ props: SuggestionCallbackProps<TItem>,
+ items: TItem[],
+ ) => {
renderer = new ReactRenderer(config.component, {
editor,
- props: buildMenuProps(props),
+ props: buildMenuProps(props, items),
});
document.body.appendChild(renderer.element);
};
return {
onStart: (props: SuggestionCallbackProps<TItem>) => {
- if (!props.clientRect || props.items.length === 0) {
+ if (!props.clientRect) {
return;
}
- createRenderer(props);
+ const items = getItems(props);
+
+ if (items.length === 0) {
+ return;
+ }
+
+ createRenderer(props, items);
},
onUpdate: (props: SuggestionCallbackProps<TItem>) => {
if (!props.clientRect) {
return;
}
- if (props.items.length === 0) {
+ const items = getItems(props);
+
+ if (items.length === 0) {
closeMenu();
return;
}
if (renderer === null) {
- createRenderer(props);
+ createRenderer(props, items);
return;
}
- renderer.updateProps(buildMenuProps(props));
+ renderer.updateProps(buildMenuProps(props, items));
},
onKeyDown: (props: { event: KeyboardEvent }) => {
if (props.event.key === 'Escape') {
diff --git a/packages/twenty-front/src/modules/ui/suggestion/types/SuggestionMenuProps.ts b/packages/twenty-front/src/modules/ui/suggestion/types/SuggestionMenuProps.ts
index d66206abf65..f93c4fd03ef 100644
--- a/packages/twenty-front/src/modules/ui/suggestion/types/SuggestionMenuProps.ts
+++ b/packages/twenty-front/src/modules/ui/suggestion/types/SuggestionMenuProps.ts
@@ -6,6 +6,11 @@ export type SuggestionMenuSelectedItemPreview<TItem> = {
width: number;
};
+export type SuggestionMenuSection = {
+ key: string;
+ label: string;
+};
+
export type SuggestionMenuProps<TItem> = {
items: TItem[];
onSelect: (item: TItem) => void;
@@ -14,6 +19,7 @@ export type SuggestionMenuProps<TItem> = {
getItemKey: (item: TItem) => string;
renderItem: (item: TItem, isSelected: boolean) => ReactNode;
selectedItemPreview?: SuggestionMenuSelectedItemPreview<TItem>;
+ getItemSection?: (item: TItem) => SuggestionMenuSection;
onKeyDown?: (
event: KeyboardEvent,
selectedIndex: number,