The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Stop remounting every device-session row on each render of the devices section (#25232)
Félix Malfaitb086f7dbf7af
diff --git a/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsDeviceSessionRowDropdownMenu.tsx b/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsDeviceSessionRowDropdownMenu.tsxindex d3ee4c41770..ac761d6f595 100644--- a/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsDeviceSessionRowDropdownMenu.tsx+++ b/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsDeviceSessionRowDropdownMenu.tsx@@ -9,23 +9,26 @@ import { useCloseDropdown } from '@/ui/layout/dropdown/hooks/useCloseDropdown';import { IconDotsVertical, IconLogout } from 'twenty-ui/icon';import { LightIconButton } from 'twenty-ui/input';import { MenuItem } from 'twenty-ui/navigation';-import { RevokeUserSessionDocument } from '~/generated-metadata/graphql';+import {+ CurrentUserSessionsDocument,+ RevokeUserSessionDocument,+} from '~/generated-metadata/graphql';type SettingsDeviceSessionRowDropdownMenuProps = {userSessionId: string;- onRevoked: () => void;};export const SettingsDeviceSessionRowDropdownMenu = ({userSessionId,- onRevoked,}: SettingsDeviceSessionRowDropdownMenuProps) => {const dropdownId = `settings-device-session-row-${userSessionId}`;const { enqueueErrorSnackBar, enqueueSuccessSnackBar } = useSnackBar();const { closeDropdown } = useCloseDropdown();- const [revokeUserSession] = useMutation(RevokeUserSessionDocument);+ const [revokeUserSession] = useMutation(RevokeUserSessionDocument, {+ refetchQueries: [CurrentUserSessionsDocument],+ });const handleRevokeSession = async () => {closeDropdown(dropdownId);@@ -33,7 +36,6 @@ export const SettingsDeviceSessionRowDropdownMenu = ({try {await revokeUserSession({ variables: { userSessionId } });enqueueSuccessSnackBar({ message: t`Device logged out` });- onRevoked();} catch {enqueueErrorSnackBar({ message: t`Failed to log out this device` });}diff --git a/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsDeviceSessionRowRightComponent.tsx b/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsDeviceSessionRowRightComponent.tsxnew file mode 100644index 00000000000..5f51c60379b--- /dev/null+++ b/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsDeviceSessionRowRightComponent.tsx@@ -0,0 +1,29 @@+import { useLingui } from '@lingui/react/macro';++import { SettingsDeviceSessionRowDropdownMenu } from '@/settings/profile/devices/components/SettingsDeviceSessionRowDropdownMenu';+import { Status } from 'twenty-ui/data-display';+import { type CurrentUserSessionsQuery } from '~/generated-metadata/graphql';++type UserSessionListItem =+ CurrentUserSessionsQuery['currentUserSessions'][number];++export const SettingsDeviceSessionRowRightComponent = ({+ item: session,+}: {+ item: UserSessionListItem;+}) => {+ const { t } = useLingui();++ return (+ <>+ {session.isImpersonating && (+ <Status color="orange" text={t`Impersonation`} />+ )}+ {session.isCurrent ? (+ <Status color="turquoise" text={t`This device`} />+ ) : (+ <SettingsDeviceSessionRowDropdownMenu userSessionId={session.id} />+ )}+ </>+ );+};diff --git a/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsProfileDevicesSection.tsx b/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsProfileDevicesSection.tsxindex 4794f3b0ff7..239e0830fec 100644--- a/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsProfileDevicesSection.tsx+++ b/packages/twenty-front/src/modules/settings/profile/devices/components/SettingsProfileDevicesSection.tsx@@ -5,11 +5,10 @@ import { isNonEmptyString } from '@sniptt/guards';import { useSnackBarOnQueryError } from '@/apollo/hooks/useSnackBarOnQueryError';import { SettingsListCard } from '@/settings/components/SettingsListCard';-import { SettingsDeviceSessionRowDropdownMenu } from '@/settings/profile/devices/components/SettingsDeviceSessionRowDropdownMenu';+import { SettingsDeviceSessionRowRightComponent } from '@/settings/profile/devices/components/SettingsDeviceSessionRowRightComponent';import { parseUserAgentDescription } from '@/settings/profile/devices/utils/parseUserAgentDescription';import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';-import { Status } from 'twenty-ui/data-display';import { IconDeviceDesktop, IconLogout } from 'twenty-ui/icon';import { Button } from 'twenty-ui/input';import { Section } from 'twenty-ui/layout';@@ -110,21 +109,7 @@ export const SettingsProfileDevicesSection = () => {getItemLabel={getSessionLabel}getItemDescription={getSessionDescription}RowIcon={IconDeviceDesktop}- RowRightComponent={({ item: session }) => (- <>- {session.isImpersonating && (- <Status color="orange" text={t`Impersonation`} />- )}- {session.isCurrent ? (- <Status color="turquoise" text={t`This device`} />- ) : (- <SettingsDeviceSessionRowDropdownMenu- userSessionId={session.id}- onRevoked={() => void refetch()}- />- )}- </>- )}+ RowRightComponent={SettingsDeviceSessionRowRightComponent}/>{hasOtherSessions && (<StyledButtonContainer>diff --git a/packages/twenty-front/src/modules/settings/profile/devices/components/__tests__/SettingsProfileDevicesSection.test.tsx b/packages/twenty-front/src/modules/settings/profile/devices/components/__tests__/SettingsProfileDevicesSection.test.tsxnew file mode 100644index 00000000000..5afebd54078--- /dev/null+++ b/packages/twenty-front/src/modules/settings/profile/devices/components/__tests__/SettingsProfileDevicesSection.test.tsx@@ -0,0 +1,57 @@+import { i18n } from '@lingui/core';+import { I18nProvider } from '@lingui/react';+import { render, screen } from '@testing-library/react';+import { MemoryRouter } from 'react-router-dom';++import { SettingsProfileDevicesSection } from '@/settings/profile/devices/components/SettingsProfileDevicesSection';+import { CurrentUserSessionsDocument } from '~/generated-metadata/graphql';+import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';++const buildSession = (id: string, isCurrent: boolean) => ({+ __typename: 'UserSession',+ id,+ isCurrent,+ isImpersonating: false,+ userAgent: 'Mozilla/5.0 (Macintosh) Chrome/128.0',+ ipAddress: '127.0.0.1',+ lastActiveAt: new Date().toISOString(),+});++const Wrapper = getJestMetadataAndApolloMocksWrapper({+ apolloMocks: [+ {+ request: { query: CurrentUserSessionsDocument },+ result: {+ data: {+ currentUserSessions: [+ buildSession('session-current', true),+ buildSession('session-other', false),+ ],+ },+ },+ },+ ],+});++describe('SettingsProfileDevicesSection', () => {+ it('keeps a session row mounted across a re-render of the section', async () => {+ const tree = (+ <I18nProvider i18n={i18n}>+ <MemoryRouter>+ <SettingsProfileDevicesSection />+ </MemoryRouter>+ </I18nProvider>+ );++ const { rerender } = render(tree, { wrapper: Wrapper });++ expect(await screen.findByText('This device')).toBeVisible();+ const rowDropdownTrigger = screen.getByRole('button', { expanded: false });++ rerender(tree);++ expect(screen.getByRole('button', { expanded: false })).toBe(+ rowDropdownTrigger,+ );+ });+});