The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Add DM sidebar and member picker presentation (#29300)
Raphaël Bosi0ef48ff1027c
diff --git a/packages/twenty-front/src/modules/ai/components/DirectMessageMemberPicker.tsx b/packages/twenty-front/src/modules/ai/components/DirectMessageMemberPicker.tsxnew file mode 100644index 00000000000..63ae7428777--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/DirectMessageMemberPicker.tsx@@ -0,0 +1,105 @@+import { type DirectMessageMember } from '@/ai/types/DirectMessageMember';+import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';+import { DropdownRoot } from '@/ui/layout/dropdown/components/DropdownRoot';+import { GenericDropdownContentWidth } from '@/ui/layout/dropdown/constants/GenericDropdownContentWidth';+import { useLingui } from '@lingui/react/macro';+import { isNonEmptyArray } from 'twenty-shared/utils';+import { LightIconButton } from 'twenty-ui/components/input';+import { Dropdown } from 'twenty-ui/components/navigation';+import { IconPlus } from 'twenty-ui/icon';+import { Avatar } from 'twenty-ui/primitives/data-display';++const DIRECT_MESSAGE_MEMBER_PICKER_DROPDOWN_ID = 'direct-message-member-picker';++type DirectMessageMemberPickerProps = {+ humanMembers: readonly DirectMessageMember[];+ currentWorkspaceMemberId: string;+ searchValue: string;+ onSearchChange: (searchValue: string) => void;+ isLoading: boolean;+ onSelect: (workspaceMemberId: string) => void;+};++export const DirectMessageMemberPicker = ({+ humanMembers,+ currentWorkspaceMemberId,+ searchValue,+ onSearchChange,+ isLoading,+ onSelect,+}: DirectMessageMemberPickerProps) => {+ const { t } = useLingui();+ const otherHumanMembers = humanMembers.filter(+ ({ workspaceMemberId }) => workspaceMemberId !== currentWorkspaceMemberId,+ );+ const hasMembers = isNonEmptyArray(otherHumanMembers);++ const handleOpenChange = (isOpen: boolean) => {+ if (isOpen) {+ onSearchChange('');+ }+ };++ return (+ <DropdownRoot+ type="picker"+ dropdownId={DIRECT_MESSAGE_MEMBER_PICKER_DROPDOWN_ID}+ onOpenChange={handleOpenChange}+ >+ <Dropdown.Trigger+ render={+ <LightIconButton+ emphasis="subtle"+ aria-label={t({+ message: 'Add DM',+ comment: 'Abbreviation of add direct message, sidebar button',+ })}+ >+ <IconPlus />+ </LightIconButton>+ }+ />+ <DropdownContent width={GenericDropdownContentWidth.ExtraLarge}>+ <Dropdown.Search+ aria-label={t`Search members`}+ placeholder={t`Search members`}+ value={searchValue}+ onValueChange={onSearchChange}+ />+ <Dropdown.Separator />+ <Dropdown.Section scrollable>+ {hasMembers && (+ <Dropdown.SelectionGroup aria-label={t`Members`} value={null}>+ {otherHumanMembers.map((member) => (+ <Dropdown.OptionItem+ key={member.workspaceMemberId}+ value={member.workspaceMemberId}+ disabled={isLoading}+ onSelect={() => onSelect(member.workspaceMemberId)}+ startIcon={+ <Avatar+ name={member.name}+ src={member.avatarUrl}+ colorSeed={member.workspaceMemberId}+ shape="circle"+ size="md"+ />+ }+ description={member.email}+ >+ {member.name}+ </Dropdown.OptionItem>+ ))}+ </Dropdown.SelectionGroup>+ )}+ {!hasMembers && isLoading && (+ <Dropdown.Loading>{t`Loading members`}</Dropdown.Loading>+ )}+ {!hasMembers && !isLoading && (+ <Dropdown.Empty>{t`No members found`}</Dropdown.Empty>+ )}+ </Dropdown.Section>+ </DropdownContent>+ </DropdownRoot>+ );+};diff --git a/packages/twenty-front/src/modules/ai/components/NavigationDrawerDirectMessageItem.tsx b/packages/twenty-front/src/modules/ai/components/NavigationDrawerDirectMessageItem.tsxnew file mode 100644index 00000000000..e5ee91dd751--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/NavigationDrawerDirectMessageItem.tsx@@ -0,0 +1,33 @@+import { type DirectMessageSidebarItem } from '@/ai/types/DirectMessageSidebarItem';+import { NavigationDrawerItem } from '@/ui/navigation/navigation-drawer/components/NavigationDrawerItem';+import { Avatar } from 'twenty-ui/primitives/data-display';++type NavigationDrawerDirectMessageItemProps = {+ directMessage: DirectMessageSidebarItem;+ isActive: boolean;+ onSelect: (threadId: string) => void;+};++export const NavigationDrawerDirectMessageItem = ({+ directMessage,+ isActive,+ onSelect,+}: NavigationDrawerDirectMessageItemProps) => (+ <NavigationDrawerItem+ label={directMessage.peer.name}+ Icon={+ <Avatar+ name={directMessage.peer.name}+ src={directMessage.peer.avatarUrl}+ colorSeed={directMessage.peer.workspaceMemberId}+ shape="circle"+ size="md"+ />+ }+ active={isActive}+ isUnread={directMessage.isUnread}+ onClick={() => onSelect(directMessage.threadId)}+ triggerEvent="CLICK"+ preventCollapseOnMobile+ />+);diff --git a/packages/twenty-front/src/modules/ai/components/NavigationDrawerDirectMessagesSection.tsx b/packages/twenty-front/src/modules/ai/components/NavigationDrawerDirectMessagesSection.tsxnew file mode 100644index 00000000000..05bcb47268e--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/NavigationDrawerDirectMessagesSection.tsx@@ -0,0 +1,44 @@+import { NavigationDrawerDirectMessageItem } from '@/ai/components/NavigationDrawerDirectMessageItem';+import { type DirectMessageSidebarItem } from '@/ai/types/DirectMessageSidebarItem';+import { CollapsibleNavigationDrawerSection } from '@/ui/navigation/navigation-drawer/components/CollapsibleNavigationDrawerSection';+import { useLingui } from '@lingui/react/macro';+import { type ReactNode } from 'react';++const AI_CHAT_DIRECT_MESSAGES_NAVIGATION_SECTION_ID = 'AiChatDirectMessages';++type NavigationDrawerDirectMessagesSectionProps = {+ directMessages: readonly DirectMessageSidebarItem[];+ currentThreadId: string | null;+ onSelect: (threadId: string) => void;+ memberPicker: ReactNode;+};++export const NavigationDrawerDirectMessagesSection = ({+ directMessages,+ currentThreadId,+ onSelect,+ memberPicker,+}: NavigationDrawerDirectMessagesSectionProps) => {+ const { t } = useLingui();++ return (+ <CollapsibleNavigationDrawerSection+ sectionId={AI_CHAT_DIRECT_MESSAGES_NAVIGATION_SECTION_ID}+ label={t({+ message: 'DMs',+ comment: 'Abbreviation of direct messages, sidebar section title',+ })}+ rightIcon={memberPicker}+ alwaysShowRightIcon+ >+ {directMessages.map((directMessage) => (+ <NavigationDrawerDirectMessageItem+ key={directMessage.threadId}+ directMessage={directMessage}+ isActive={directMessage.threadId === currentThreadId}+ onSelect={onSelect}+ />+ ))}+ </CollapsibleNavigationDrawerSection>+ );+};diff --git a/packages/twenty-front/src/modules/ai/components/__stories__/DirectMessagePresentationExample.tsx b/packages/twenty-front/src/modules/ai/components/__stories__/DirectMessagePresentationExample.tsxnew file mode 100644index 00000000000..ee23be66cf6--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/__stories__/DirectMessagePresentationExample.tsx@@ -0,0 +1,92 @@+import { styled } from '@linaria/react';+import { createStore, Provider } from 'jotai';+import { useState } from 'react';+import { IconHash, IconMessage } from 'twenty-ui/icon';+import { themeCssVariables } from 'twenty-ui/theme';++import { DirectMessageMemberPicker } from '@/ai/components/DirectMessageMemberPicker';+import { NavigationDrawerDirectMessagesSection } from '@/ai/components/NavigationDrawerDirectMessagesSection';+import { DIRECT_MESSAGE_PRESENTATION_FIXTURE } from '@/ai/components/__stories__/directMessagePresentationFixture';+import { CollapsibleNavigationDrawerSection } from '@/ui/navigation/navigation-drawer/components/CollapsibleNavigationDrawerSection';+import { NavigationDrawerItem } from '@/ui/navigation/navigation-drawer/components/NavigationDrawerItem';+import { NAVIGATION_DRAWER_CONSTRAINTS } from '@/ui/navigation/navigation-drawer/constants/NavigationDrawerConstraints';+import { isNavigationDrawerExpandedState } from '@/ui/navigation/states/isNavigationDrawerExpanded';+import { filterBySearchQuery } from '~/utils/filterBySearchQuery';++const StyledSidebar = styled.div`+ background: ${themeCssVariables.background.secondary};+ box-sizing: border-box;+ display: flex;+ flex-direction: column;+ gap: ${themeCssVariables.spacing[3]};+ padding: ${themeCssVariables.spacing[2]};+ padding-right: 0;+ width: ${NAVIGATION_DRAWER_CONSTRAINTS.default}px;+`;++type DirectMessagePresentationExampleProps = {+ onSelectThread: (threadId: string) => void;+ onSelectMember: (workspaceMemberId: string) => void;+ memberState?: 'loading' | 'refreshing' | 'results';+};++export const DirectMessagePresentationExample = ({+ onSelectThread,+ onSelectMember,+ memberState = 'results',+}: DirectMessagePresentationExampleProps) => {+ const [store] = useState(() => {+ const initialStore = createStore();+ initialStore.set(isNavigationDrawerExpandedState.atom, true);+ return initialStore;+ });+ const [currentThreadId, setCurrentThreadId] = useState('dm-anna');+ const [searchValue, setSearchValue] = useState('');+ const { currentMember, directMessages } = DIRECT_MESSAGE_PRESENTATION_FIXTURE;+ const matchingHumanMembers = filterBySearchQuery({+ items: [currentMember, ...directMessages.map(({ peer }) => peer)],+ searchQuery: searchValue,+ getSearchableValues: ({ name, email }) => [name, email],+ });++ return (+ <Provider store={store}>+ <StyledSidebar>+ <CollapsibleNavigationDrawerSection+ sectionId="stories/direct-messages/channels"+ label="Channels"+ >+ <NavigationDrawerItem label="General" Icon={IconHash} />+ <NavigationDrawerItem label="Product" Icon={IconHash} />+ </CollapsibleNavigationDrawerSection>+ <NavigationDrawerDirectMessagesSection+ directMessages={directMessages}+ currentThreadId={currentThreadId}+ onSelect={(threadId) => {+ setCurrentThreadId(threadId);+ onSelectThread(threadId);+ }}+ memberPicker={+ <DirectMessageMemberPicker+ humanMembers={+ memberState === 'loading' ? [] : matchingHumanMembers+ }+ currentWorkspaceMemberId={currentMember.workspaceMemberId}+ searchValue={searchValue}+ onSearchChange={setSearchValue}+ isLoading={memberState !== 'results'}+ onSelect={onSelectMember}+ />+ }+ />+ <CollapsibleNavigationDrawerSection+ sectionId="stories/direct-messages/recent"+ label="Recent"+ >+ <NavigationDrawerItem label="Research notes" Icon={IconMessage} />+ <NavigationDrawerItem label="Quarterly planning" Icon={IconMessage} />+ </CollapsibleNavigationDrawerSection>+ </StyledSidebar>+ </Provider>+ );+};diff --git a/packages/twenty-front/src/modules/ai/components/__stories__/NavigationDrawerDirectMessagesSection.stories.tsx b/packages/twenty-front/src/modules/ai/components/__stories__/NavigationDrawerDirectMessagesSection.stories.tsxnew file mode 100644index 00000000000..b467910e10b--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/__stories__/NavigationDrawerDirectMessagesSection.stories.tsx@@ -0,0 +1,140 @@+import { type Meta, type StoryObj } from '@storybook/react-vite';+import { expect, fn, userEvent, waitFor, within } from 'storybook/test';+import { ComponentDecorator } from 'twenty-ui/testing';++import { DirectMessagePresentationExample } from '@/ai/components/__stories__/DirectMessagePresentationExample';+import { MemoryRouterDecorator } from '~/testing/decorators/MemoryRouterDecorator';++const meta = {+ title: 'Modules/AI/NavigationDrawerDirectMessagesSection',+ component: DirectMessagePresentationExample,+ decorators: [ComponentDecorator, MemoryRouterDecorator],+ args: {+ onSelectThread: fn(),+ onSelectMember: fn(),+ },+} satisfies Meta<typeof DirectMessagePresentationExample>;++export default meta;+type Story = StoryObj<typeof meta>;++export const SidebarSelectionAndCollapse: Story = {+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const anna = canvas.getByRole('button', { name: 'Anna' });+ const felix = canvas.getByRole('button', { name: /Felix\s*, unread/ });+ const sectionTitle = canvas.getByText('DMs');++ await expect(anna).toHaveAttribute('aria-current', 'true');+ await expect(+ canvas.getByRole('button', { name: /Alice\s*, unread/ }),+ ).toBeVisible();++ felix.focus();+ await userEvent.keyboard('{Enter}');+ await expect(args.onSelectThread).toHaveBeenCalledTimes(1);+ await expect(args.onSelectThread).toHaveBeenCalledWith('dm-felix');+ await expect(felix).toHaveAttribute('aria-current', 'true');+ await expect(anna).not.toHaveAttribute('aria-current');++ await userEvent.click(sectionTitle);+ await waitFor(() =>+ expect(+ canvas.queryByRole('button', { name: 'Anna' }),+ ).not.toBeInTheDocument(),+ );++ await userEvent.click(sectionTitle);+ await expect(+ await canvas.findByRole('button', { name: /Felix\s*, unread/ }),+ ).toHaveAttribute('aria-current', 'true');+ await expect(args.onSelectThread).toHaveBeenCalledTimes(1);+ },+};++export const SearchAndKeyboardSelection: Story = {+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const body = within(canvasElement.ownerDocument.body);+ const trigger = canvas.getByRole('button', { name: 'Add DM' });++ await expect(trigger).toBeVisible();+ await userEvent.click(trigger);+ const search = await body.findByRole('searchbox', {+ name: 'Search members',+ });+ await expect(body.getByRole('dialog', { name: 'Add DM' })).toBeVisible();+ await waitFor(() => expect(search).toHaveFocus());+ await expect(+ body.queryByRole('option', { name: /Raphael/ }),+ ).not.toBeInTheDocument();++ await userEvent.type(search, 'raphael');+ await expect(body.getByRole('status')).toHaveTextContent(+ 'No members found',+ );+ await userEvent.clear(search);++ await userEvent.type(search, 'ali');+ await expect(body.getAllByRole('option')).toHaveLength(1);+ await expect(body.getByRole('option', { name: /Alice/ })).toBeVisible();+ await userEvent.keyboard('{ArrowDown}{Enter}');+ await expect(args.onSelectMember).toHaveBeenCalledTimes(1);+ await expect(args.onSelectMember).toHaveBeenCalledWith('alice');+ await waitFor(() =>+ expect(+ body.queryByRole('dialog', { name: 'Add DM' }),+ ).not.toBeInTheDocument(),+ );+ await waitFor(() => expect(trigger).toHaveFocus());++ await userEvent.keyboard('{Enter}');+ const reopenedSearch = await body.findByRole('searchbox', {+ name: 'Search members',+ });+ await waitFor(() => expect(reopenedSearch).toHaveFocus());+ await expect(reopenedSearch).toHaveValue('');+ await userEvent.keyboard('{Escape}');+ await waitFor(() =>+ expect(+ body.queryByRole('dialog', { name: 'Add DM' }),+ ).not.toBeInTheDocument(),+ );+ await waitFor(() => expect(trigger).toHaveFocus());+ await expect(args.onSelectMember).toHaveBeenCalledTimes(1);+ },+};++export const LoadingMembers: Story = {+ args: { memberState: 'loading' },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);+ const body = within(canvasElement.ownerDocument.body);++ await userEvent.click(canvas.getByRole('button', { name: 'Add DM' }));++ await expect(await body.findByRole('status')).toHaveTextContent(+ 'Loading members',+ );+ await expect(body.queryByRole('option')).not.toBeInTheDocument();+ },+};++export const RefreshingMembersDisablesStaleRows: Story = {+ args: { memberState: 'refreshing' },+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const body = within(canvasElement.ownerDocument.body);++ await userEvent.click(canvas.getByRole('button', { name: 'Add DM' }));+ const search = await body.findByRole('searchbox', {+ name: 'Search members',+ });+ await waitFor(() => expect(search).toHaveFocus());++ await userEvent.type(search, 'anna');+ await expect(body.getByRole('option', { name: /Anna/ })).toBeDisabled();+ await userEvent.keyboard('{Enter}');+ await expect(args.onSelectMember).not.toHaveBeenCalled();+ },+};diff --git a/packages/twenty-front/src/modules/ai/components/__stories__/directMessagePresentationFixture.ts b/packages/twenty-front/src/modules/ai/components/__stories__/directMessagePresentationFixture.tsnew file mode 100644index 00000000000..cba184ad151--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/__stories__/directMessagePresentationFixture.ts@@ -0,0 +1,60 @@+import { type DirectMessageMember } from '@/ai/types/DirectMessageMember';+import { type DirectMessageSidebarItem } from '@/ai/types/DirectMessageSidebarItem';++export const DIRECT_MESSAGE_PRESENTATION_FIXTURE = {+ currentMember: {+ workspaceMemberId: 'raphael',+ name: 'Raphael',+ email: '[email protected]',+ },+ directMessages: [+ {+ threadId: 'dm-anna',+ peer: {+ workspaceMemberId: 'anna',+ name: 'Anna',+ email: '[email protected]',+ },+ isUnread: false,+ },+ {+ threadId: 'dm-felix',+ peer: {+ workspaceMemberId: 'felix',+ name: 'Felix',+ email: '[email protected]',+ },+ isUnread: true,+ },+ {+ threadId: 'dm-alice',+ peer: {+ workspaceMemberId: 'alice',+ name: 'Alice',+ email: '[email protected]',+ },+ isUnread: true,+ },+ {+ threadId: 'dm-thomas',+ peer: {+ workspaceMemberId: 'thomas',+ name: 'Thomas',+ email: '[email protected]',+ },+ isUnread: false,+ },+ {+ threadId: 'dm-mike',+ peer: {+ workspaceMemberId: 'mike',+ name: 'Mike',+ email: '[email protected]',+ },+ isUnread: false,+ },+ ],+} satisfies {+ currentMember: DirectMessageMember;+ directMessages: DirectMessageSidebarItem[];+};diff --git a/packages/twenty-front/src/modules/ai/types/DirectMessageMember.ts b/packages/twenty-front/src/modules/ai/types/DirectMessageMember.tsnew file mode 100644index 00000000000..7dfbc838b0b--- /dev/null+++ b/packages/twenty-front/src/modules/ai/types/DirectMessageMember.ts@@ -0,0 +1,6 @@+export type DirectMessageMember = {+ workspaceMemberId: string;+ name: string;+ avatarUrl?: string | null;+ email?: string;+};diff --git a/packages/twenty-front/src/modules/ai/types/DirectMessageSidebarItem.ts b/packages/twenty-front/src/modules/ai/types/DirectMessageSidebarItem.tsnew file mode 100644index 00000000000..0a8a06b8201--- /dev/null+++ b/packages/twenty-front/src/modules/ai/types/DirectMessageSidebarItem.ts@@ -0,0 +1,7 @@+import { type DirectMessageMember } from '@/ai/types/DirectMessageMember';++export type DirectMessageSidebarItem = {+ threadId: string;+ peer: DirectMessageMember;+ isUnread: boolean;+};