The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Keep the native account app tab bar stable while accounts load (#28244)
nitin0208192ed5b1
diff --git a/packages/twenty-front/src/modules/settings/app-preferences/hooks/useEnabledNativeAccountApps.ts b/packages/twenty-front/src/modules/settings/app-preferences/hooks/useEnabledNativeAccountApps.tsindex 6067aece31c..8cb5382f7ab 100644--- a/packages/twenty-front/src/modules/settings/app-preferences/hooks/useEnabledNativeAccountApps.ts+++ b/packages/twenty-front/src/modules/settings/app-preferences/hooks/useEnabledNativeAccountApps.ts@@ -24,15 +24,22 @@ export const useEnabledNativeAccountApps = () => {isImapSmtpCaldavEnabledState,);- const isAppEnabled: Record<NativeAccountApp['id'], boolean> = {- google: isGoogleMessagingEnabled || isGoogleCalendarEnabled,- outlook: isMicrosoftMessagingEnabled || isMicrosoftCalendarEnabled,+ const isEmailsEnabled: Record<NativeAccountApp['id'], boolean> = {+ google: isGoogleMessagingEnabled,+ outlook: isMicrosoftMessagingEnabled,+ imap: isImapSmtpCaldavEnabled,+ };+ const isCalendarEnabled: Record<NativeAccountApp['id'], boolean> = {+ google: isGoogleCalendarEnabled,+ outlook: isMicrosoftCalendarEnabled,imap: isImapSmtpCaldavEnabled,};return {- enabledNativeAccountApps: NATIVE_ACCOUNT_APPS.filter(- (app) => isAppEnabled[app.id],- ),+ enabledNativeAccountApps: NATIVE_ACCOUNT_APPS.map((app) => ({+ ...app,+ hasEmails: app.hasEmails && isEmailsEnabled[app.id],+ hasCalendar: app.hasCalendar && isCalendarEnabled[app.id],+ })).filter((app) => app.hasEmails || app.hasCalendar),};};diff --git a/packages/twenty-front/src/modules/settings/app-preferences/utils/__tests__/getConnectedAccountSettingsChannels.test.ts b/packages/twenty-front/src/modules/settings/app-preferences/utils/__tests__/getConnectedAccountSettingsChannels.test.tsindex 1ddbe1b0709..2527e657408 100644--- a/packages/twenty-front/src/modules/settings/app-preferences/utils/__tests__/getConnectedAccountSettingsChannels.test.ts+++ b/packages/twenty-front/src/modules/settings/app-preferences/utils/__tests__/getConnectedAccountSettingsChannels.test.ts@@ -25,24 +25,28 @@ describe('getConnectedAccountSettingsChannels', () => {).toEqual({ messageChannel: mailbox, calendarChannel: calendar });});- it('skips channels that still need setup', () => {+ it('returns channels that still need setup separately', () => {+ const mailbox = {+ id: 'mailbox',+ type: MessageChannelType.EMAIL,+ syncStage: MessageChannelSyncStage.PENDING_CONFIGURATION,+ };+ const calendar = {+ id: 'calendar',+ syncStage: CalendarChannelSyncStage.PENDING_CONFIGURATION,+ };+expect(getConnectedAccountSettingsChannels({- messageChannels: [- {- id: 'mailbox',- type: MessageChannelType.EMAIL,- syncStage: MessageChannelSyncStage.PENDING_CONFIGURATION,- },- ],- calendarChannels: [- {- id: 'calendar',- syncStage: CalendarChannelSyncStage.PENDING_CONFIGURATION,- },- ],+ messageChannels: [mailbox],+ calendarChannels: [calendar],}),- ).toEqual({ messageChannel: undefined, calendarChannel: undefined });+ ).toEqual({+ messageChannel: undefined,+ calendarChannel: undefined,+ pendingSetupMessageChannel: mailbox,+ pendingSetupCalendarChannel: calendar,+ });});it('skips message channels that are not mailboxes', () => {diff --git a/packages/twenty-front/src/modules/settings/app-preferences/utils/__tests__/getNativeAccountAppChannelTabsVisibility.test.ts b/packages/twenty-front/src/modules/settings/app-preferences/utils/__tests__/getNativeAccountAppChannelTabsVisibility.test.tsnew file mode 100644index 00000000000..57b84737a12--- /dev/null+++ b/packages/twenty-front/src/modules/settings/app-preferences/utils/__tests__/getNativeAccountAppChannelTabsVisibility.test.ts@@ -0,0 +1,116 @@+import { getNativeAccountAppChannelTabsVisibility } from '@/settings/app-preferences/utils/getNativeAccountAppChannelTabsVisibility';+import {+ CalendarChannelSyncStage,+ MessageChannelSyncStage,+ MessageChannelType,+} from 'twenty-shared/types';++const READY_MAILBOX = {+ type: MessageChannelType.EMAIL,+ syncStage: MessageChannelSyncStage.MESSAGE_LIST_FETCH_PENDING,+};+const PENDING_MAILBOX = {+ type: MessageChannelType.EMAIL,+ syncStage: MessageChannelSyncStage.PENDING_CONFIGURATION,+};+const READY_CALENDAR = {+ syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING,+};+const PENDING_CALENDAR = {+ syncStage: CalendarChannelSyncStage.PENDING_CONFIGURATION,+};+const OUTLOOK = { hasEmails: true, hasCalendar: true };++describe('getNativeAccountAppChannelTabsVisibility', () => {+ it('shows the tabs the app supports when there are no accounts', () => {+ expect(+ getNativeAccountAppChannelTabsVisibility({+ app: OUTLOOK,+ accounts: [],+ isLoading: false,+ }),+ ).toEqual({+ isEmailsTabVisible: true,+ isCalendarTabVisible: true,+ });+ });++ it('hides the tabs the app does not support', () => {+ expect(+ getNativeAccountAppChannelTabsVisibility({+ app: { hasEmails: true, hasCalendar: false },+ accounts: [+ {+ messageChannels: [READY_MAILBOX],+ calendarChannels: [READY_CALENDAR],+ },+ ],+ isLoading: false,+ }),+ ).toEqual({ isEmailsTabVisible: true, isCalendarTabVisible: false });+ });++ it('hides a tab whose accounts are all pending setup', () => {+ expect(+ getNativeAccountAppChannelTabsVisibility({+ app: OUTLOOK,+ accounts: [+ {+ messageChannels: [READY_MAILBOX],+ calendarChannels: [PENDING_CALENDAR],+ },+ ],+ isLoading: true,+ }),+ ).toEqual({ isEmailsTabVisible: true, isCalendarTabVisible: false });+ });++ it('keeps a tab when one account is ready and another is pending setup', () => {+ expect(+ getNativeAccountAppChannelTabsVisibility({+ app: OUTLOOK,+ accounts: [+ { messageChannels: [PENDING_MAILBOX], calendarChannels: [] },+ {+ messageChannels: [READY_MAILBOX],+ calendarChannels: [READY_CALENDAR],+ },+ ],+ isLoading: false,+ }),+ ).toEqual({ isEmailsTabVisible: true, isCalendarTabVisible: true });+ });++ it('hides a tab once loaded when the accounts have no channel for it', () => {+ expect(+ getNativeAccountAppChannelTabsVisibility({+ app: OUTLOOK,+ accounts: [{ messageChannels: [READY_MAILBOX], calendarChannels: [] }],+ isLoading: false,+ }),+ ).toEqual({ isEmailsTabVisible: true, isCalendarTabVisible: false });+ });++ it('keeps a tab while channels may still be loading', () => {+ expect(+ getNativeAccountAppChannelTabsVisibility({+ app: OUTLOOK,+ accounts: [{ messageChannels: [], calendarChannels: [] }],+ isLoading: true,+ }),+ ).toEqual({ isEmailsTabVisible: true, isCalendarTabVisible: true });+ });++ it('hides every tab without an app', () => {+ expect(+ getNativeAccountAppChannelTabsVisibility({+ app: undefined,+ accounts: [],+ isLoading: false,+ }),+ ).toEqual({+ isEmailsTabVisible: false,+ isCalendarTabVisible: false,+ });+ });+});diff --git a/packages/twenty-front/src/modules/settings/app-preferences/utils/getConnectedAccountSettingsChannels.ts b/packages/twenty-front/src/modules/settings/app-preferences/utils/getConnectedAccountSettingsChannels.tsindex 477e21ecdff..b0f551ed621 100644--- a/packages/twenty-front/src/modules/settings/app-preferences/utils/getConnectedAccountSettingsChannels.ts+++ b/packages/twenty-front/src/modules/settings/app-preferences/utils/getConnectedAccountSettingsChannels.ts@@ -16,14 +16,29 @@ export const getConnectedAccountSettingsChannels = <calendarChannels: TCalendarChannel[];}| undefined,-) => ({- messageChannel: account?.messageChannels.find(- (channel) =>- channel.type === MessageChannelType.EMAIL &&- channel.syncStage !== MessageChannelSyncStage.PENDING_CONFIGURATION,- ),- calendarChannel: account?.calendarChannels.find(- (channel) =>- channel.syncStage !== CalendarChannelSyncStage.PENDING_CONFIGURATION,- ),-});+) => {+ const mailboxes =+ account?.messageChannels.filter(+ (channel) => channel.type === MessageChannelType.EMAIL,+ ) ?? [];+ const calendars = account?.calendarChannels ?? [];++ return {+ messageChannel: mailboxes.find(+ (channel) =>+ channel.syncStage !== MessageChannelSyncStage.PENDING_CONFIGURATION,+ ),+ calendarChannel: calendars.find(+ (channel) =>+ channel.syncStage !== CalendarChannelSyncStage.PENDING_CONFIGURATION,+ ),+ pendingSetupMessageChannel: mailboxes.find(+ (channel) =>+ channel.syncStage === MessageChannelSyncStage.PENDING_CONFIGURATION,+ ),+ pendingSetupCalendarChannel: calendars.find(+ (channel) =>+ channel.syncStage === CalendarChannelSyncStage.PENDING_CONFIGURATION,+ ),+ };+};diff --git a/packages/twenty-front/src/modules/settings/app-preferences/utils/getNativeAccountAppChannelTabsVisibility.ts b/packages/twenty-front/src/modules/settings/app-preferences/utils/getNativeAccountAppChannelTabsVisibility.tsnew file mode 100644index 00000000000..ec2ddd2c33d--- /dev/null+++ b/packages/twenty-front/src/modules/settings/app-preferences/utils/getNativeAccountAppChannelTabsVisibility.ts@@ -0,0 +1,86 @@+import { type CalendarChannel } from '@/accounts/types/CalendarChannel';+import { type MessageChannel } from '@/accounts/types/MessageChannel';+import { type NativeAccountApp } from '@/settings/app-preferences/types/NativeAccountApp';+import { getConnectedAccountSettingsChannels } from '@/settings/app-preferences/utils/getConnectedAccountSettingsChannels';+import { isDefined } from 'twenty-shared/utils';++type AccountWithChannels = {+ messageChannels: Pick<MessageChannel, 'type' | 'syncStage'>[];+ calendarChannels: Pick<CalendarChannel, 'syncStage'>[];+};++type GetNativeAccountAppChannelTabsVisibilityArgs = {+ app: Pick<NativeAccountApp, 'hasEmails' | 'hasCalendar'> | undefined;+ accounts: AccountWithChannels[];+ isLoading: boolean;+};++type IsChannelTabVisibleArgs = {+ isSupported: boolean;+ hasReadyChannel: boolean;+ hasPendingSetupChannel: boolean;+ hasAccounts: boolean;+ isLoading: boolean;+};++// A tab only offers to connect when no account exists; setup and reconnection live in the account row menu.+// Accounts load before their channels, so a missing channel only hides the tab once loading is done.+const isChannelTabVisible = ({+ isSupported,+ hasReadyChannel,+ hasPendingSetupChannel,+ hasAccounts,+ isLoading,+}: IsChannelTabVisibleArgs) => {+ if (!isSupported) {+ return false;+ }++ if (hasReadyChannel) {+ return true;+ }++ if (hasPendingSetupChannel) {+ return false;+ }++ return isLoading || !hasAccounts;+};++export const getNativeAccountAppChannelTabsVisibility = ({+ app,+ accounts,+ isLoading,+}: GetNativeAccountAppChannelTabsVisibilityArgs) => {+ if (!isDefined(app)) {+ return { isEmailsTabVisible: false, isCalendarTabVisible: false };+ }++ const channels = accounts.map(getConnectedAccountSettingsChannels);+ const hasAccounts = accounts.length > 0;++ return {+ isEmailsTabVisible: isChannelTabVisible({+ isSupported: app.hasEmails,+ hasReadyChannel: channels.some(({ messageChannel }) =>+ isDefined(messageChannel),+ ),+ hasPendingSetupChannel: channels.some(({ pendingSetupMessageChannel }) =>+ isDefined(pendingSetupMessageChannel),+ ),+ hasAccounts,+ isLoading,+ }),+ isCalendarTabVisible: isChannelTabVisible({+ isSupported: app.hasCalendar,+ hasReadyChannel: channels.some(({ calendarChannel }) =>+ isDefined(calendarChannel),+ ),+ hasPendingSetupChannel: channels.some(({ pendingSetupCalendarChannel }) =>+ isDefined(pendingSetupCalendarChannel),+ ),+ hasAccounts,+ isLoading,+ }),+ };+};diff --git a/packages/twenty-front/src/pages/settings/accounts/SettingsNativeAccountApp.tsx b/packages/twenty-front/src/pages/settings/accounts/SettingsNativeAccountApp.tsxindex d88d20e3c57..c7c4262fd1b 100644--- a/packages/twenty-front/src/pages/settings/accounts/SettingsNativeAccountApp.tsx+++ b/packages/twenty-front/src/pages/settings/accounts/SettingsNativeAccountApp.tsx@@ -1,5 +1,6 @@import { type ConnectedAccount } from '@/accounts/types/ConnectedAccount';import { SettingsAccountGroupsSection } from '@/settings/app-preferences/components/SettingsAccountGroupsSection';+import { SettingsAppPreferencesListEmptyStateCard } from '@/settings/app-preferences/components/SettingsAppPreferencesListEmptyStateCard';import { SettingsAccountsBlocklistSection } from '@/settings/accounts/components/SettingsAccountsBlocklistSection';import { SettingsAccountsCalendarChannelDetails } from '@/settings/accounts/components/SettingsAccountsCalendarChannelDetails';import { SettingsAppPreferencesSelectedMessageChannelDetails } from '~/pages/settings/accounts/SettingsAppPreferencesSelectedMessageChannelDetails';@@ -8,6 +9,7 @@ import { SETTINGS_NATIVE_ACCOUNT_APP_TAB_LIST_COMPONENT_ID } from '@/settings/apimport { useEnabledNativeAccountApps } from '@/settings/app-preferences/hooks/useEnabledNativeAccountApps';import { useMyAccountGroups } from '@/settings/app-preferences/hooks/useMyAccountGroups';import { getConnectedAccountSettingsChannels } from '@/settings/app-preferences/utils/getConnectedAccountSettingsChannels';+import { getNativeAccountAppChannelTabsVisibility } from '@/settings/app-preferences/utils/getNativeAccountAppChannelTabsVisibility';import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';import { SettingsSectionSkeletonLoader } from '@/settings/components/SettingsSectionSkeletonLoader';import { SettingsPageLayout } from '@/settings/components/layout/SettingsPageLayout';@@ -38,7 +40,6 @@ export const SettingsNativeAccountApp = () => {const { groups, loading } = useMyAccountGroups();const [selectedAccountId, setSelectedAccountId] = useState<string>();- const isInitialLoading = loading && groups.length === 0;const tabListComponentInstanceId = `${SETTINGS_NATIVE_ACCOUNT_APP_TAB_LIST_COMPONENT_ID}-${nativeAccountAppId}`;const providerAccounts = groups@@ -55,13 +56,19 @@ export const SettingsNativeAccountApp = () => {isDefined(getConnectedAccountSettingsChannels(account).calendarChannel),): [];+ const { isEmailsTabVisible, isCalendarTabVisible } =+ getNativeAccountAppChannelTabsVisibility({+ app,+ accounts: providerAccounts,+ isLoading: loading,+ });const tabs = [{ id: 'general', title: t`General`, Icon: IconSettings },- emailAccounts.length > 0+ isEmailsTabVisible? { id: 'emails', title: t`Emails`, Icon: IconMail }: undefined,- calendarAccounts.length > 0+ isCalendarTabVisible? { id: 'calendar', title: t`Calendar`, Icon: IconCalendarEvent }: undefined,].filter(isDefined);@@ -77,7 +84,9 @@ export const SettingsNativeAccountApp = () => {const appName = t(app.name);- const getSelectedAccount = (accounts: ConnectedAccount[]) =>+ const getSelectedAccount = (+ accounts: ConnectedAccount[],+ ): ConnectedAccount | undefined =>accounts.find((account) => account.id === selectedAccountId) ?? accounts[0];const renderAccountSelect = (@@ -99,12 +108,19 @@ export const SettingsNativeAccountApp = () => {);const renderContent = () => {- if (isInitialLoading) {- return <SettingsSectionSkeletonLoader />;- }-if (activeTabId === 'emails') {+ if (loading && emailAccounts.length === 0) {+ return <SettingsSectionSkeletonLoader />;+ }+const selectedAccount = getSelectedAccount(emailAccounts);++ if (!isDefined(selectedAccount)) {+ return (+ <SettingsAppPreferencesListEmptyStateCard provider={app.provider} />+ );+ }+const { messageChannel } =getConnectedAccountSettingsChannels(selectedAccount);@@ -121,7 +137,18 @@ export const SettingsNativeAccountApp = () => {}if (activeTabId === 'calendar') {+ if (loading && calendarAccounts.length === 0) {+ return <SettingsSectionSkeletonLoader />;+ }+const selectedAccount = getSelectedAccount(calendarAccounts);++ if (!isDefined(selectedAccount)) {+ return (+ <SettingsAppPreferencesListEmptyStateCard provider={app.provider} />+ );+ }+const { calendarChannel } =getConnectedAccountSettingsChannels(selectedAccount);@@ -161,13 +188,11 @@ export const SettingsNativeAccountApp = () => {{ children: appName },]}secondaryBar={- isInitialLoading ? undefined : (- <SettingsTabBar- aria-label={t`${appName} preferences`}- tabs={tabs}- componentInstanceId={tabListComponentInstanceId}- />- )+ <SettingsTabBar+ aria-label={t`${appName} preferences`}+ tabs={tabs}+ componentInstanceId={tabListComponentInstanceId}+ />}><SettingsPageContainer>{renderContent()}</SettingsPageContainer>diff --git a/packages/twenty-front/src/pages/settings/accounts/__stories__/SettingsNativeAccountApp.stories.tsx b/packages/twenty-front/src/pages/settings/accounts/__stories__/SettingsNativeAccountApp.stories.tsxindex 0e0791544d4..87641f30a28 100644--- a/packages/twenty-front/src/pages/settings/accounts/__stories__/SettingsNativeAccountApp.stories.tsx+++ b/packages/twenty-front/src/pages/settings/accounts/__stories__/SettingsNativeAccountApp.stories.tsx@@ -1,5 +1,5 @@import { type Meta, type StoryObj } from '@storybook/react-vite';-import { HttpResponse, graphql } from 'msw';+import { HttpResponse, delay, graphql } from 'msw';import { expect, userEvent, within } from 'storybook/test';import {@@ -7,9 +7,12 @@ import {type PageDecoratorArgs,} from '~/testing/decorators/PageDecorator';+import { isMicrosoftCalendarEnabledState } from '@/client-config/states/isMicrosoftCalendarEnabledState';+import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';import { SettingsNativeAccountApp } from '~/pages/settings/accounts/SettingsNativeAccountApp';import {ACCOUNT_GROUPS_GRAPHQL_HANDLERS,+ CALENDAR_CHANNELS,CONNECTED_ACCOUNTS,MICROSOFT_ACCOUNT_ID,seedAccountGroupsStory,@@ -58,6 +61,35 @@ export const GoogleGeneralShowsAccountsAndBlocklist: Story = {},};+export const TabBarAndHeadersStayWhileLoading: Story = {+ parameters: {+ msw: {+ handlers: [+ graphql.query('MyConnectedAccounts', async () => {+ await delay('infinite');+ }),+ ...ACCOUNT_GROUPS_GRAPHQL_HANDLERS,+ ],+ },+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ expect(+ await canvas.findByText('Shared accounts between apps', undefined, {+ timeout: 3000,+ }),+ ).toBeVisible();+ expect(canvas.getByTestId('tab-general')).toBeVisible();+ expect(canvas.getByTestId('tab-emails')).toBeVisible();+ expect(canvas.getByTestId('tab-calendar')).toBeVisible();+ expect(canvas.getByText('Blocklist')).toBeVisible();+ expect(canvas.getByText('Permissions')).toBeVisible();+ expect(canvasElement.querySelector('[aria-busy="true"]')).not.toBeNull();+ expect(canvas.queryByText('[email protected]')).not.toBeInTheDocument();+ },+};+export const GoogleEmailsTabSwitchesAccount: Story = {play: async ({ canvasElement }) => {const canvas = within(canvasElement);@@ -121,6 +153,57 @@ export const OutlookHasEmailsAndCalendarTabs: Story = {},};+export const OutlookWithoutCalendarServiceHasNoCalendarTab: Story = {+ args: {+ routeParams: { ':nativeAccountAppId': 'outlook' },+ },+ beforeEach: async () => {+ await seedAccountGroupsStory();+ jotaiStore.set(isMicrosoftCalendarEnabledState.atom, false);+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ expect(+ await canvas.findByTestId('tab-emails', undefined, { timeout: 3000 }),+ ).toBeVisible();+ expect(canvas.queryByTestId('tab-calendar')).not.toBeInTheDocument();+ },+};++export const CalendarPendingSetupHidesCalendarTab: Story = {+ args: {+ routeParams: { ':nativeAccountAppId': 'outlook' },+ },+ parameters: {+ msw: {+ handlers: [+ graphql.query('MyCalendarChannels', () =>+ HttpResponse.json({+ data: {+ myCalendarChannels: CALENDAR_CHANNELS.map((channel) =>+ channel.connectedAccountId === MICROSOFT_ACCOUNT_ID+ ? { ...channel, syncStage: 'PENDING_CONFIGURATION' }+ : channel,+ ),+ },+ }),+ ),+ ...ACCOUNT_GROUPS_GRAPHQL_HANDLERS,+ ],+ },+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);++ expect(+ await canvas.findByText('[email protected]', undefined, { timeout: 3000 }),+ ).toBeVisible();+ expect(canvas.getByTestId('tab-emails')).toBeVisible();+ expect(canvas.queryByTestId('tab-calendar')).not.toBeInTheDocument();+ },+};+export const DisabledAppGoesBackToAccounts: Story = {args: {routeParams: { ':nativeAccountAppId': 'imap' },diff --git a/packages/twenty-front/src/pages/settings/accounts/__stories__/mockedAccountGroups.ts b/packages/twenty-front/src/pages/settings/accounts/__stories__/mockedAccountGroups.tsindex 8c93be68b34..62d6b7de66b 100644--- a/packages/twenty-front/src/pages/settings/accounts/__stories__/mockedAccountGroups.ts+++ b/packages/twenty-front/src/pages/settings/accounts/__stories__/mockedAccountGroups.ts@@ -202,7 +202,7 @@ const MESSAGE_CHANNELS = [),];-const CALENDAR_CHANNELS = [+export const CALENDAR_CHANNELS = [buildCalendarChannel('20202020-1a2b-4c3d-8e4f-000000000041',GOOGLE_ACCOUNT_ID,