Skip to content

The open alternative to Salesforce, designed for AI.

Refactor Widget components (#15996)

Baptiste Devessierf2df45658886
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/components/DashboardWidgetPlaceholder.tsx b/packages/twenty-front/src/modules/page-layout/widgets/components/DashboardWidgetPlaceholder.tsx
new file mode 100644
index 00000000000..ca47a81a452
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/components/DashboardWidgetPlaceholder.tsx
@@ -0,0 +1,76 @@
+import { useNavigatePageLayoutCommandMenu } from '@/command-menu/pages/page-layout/hooks/useNavigatePageLayoutCommandMenu';
+import { CommandMenuPages } from '@/command-menu/types/CommandMenuPages';
+import { useSetIsPageLayoutInEditMode } from '@/page-layout/hooks/useSetIsPageLayoutInEditMode';
+import { PageLayoutComponentInstanceContext } from '@/page-layout/states/contexts/PageLayoutComponentInstanceContext';
+import { isPageLayoutInEditModeComponentState } from '@/page-layout/states/isPageLayoutInEditModeComponentState';
+import { WidgetCard } from '@/page-layout/widgets/widget-card/components/WidgetCard';
+import { WidgetCardHeader } from '@/page-layout/widgets/widget-card/components/WidgetCardHeader';
+import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';
+import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
+import { t } from '@lingui/core/macro';
+import { Trans } from '@lingui/react/macro';
+import {
+ AnimatedPlaceholder,
+ AnimatedPlaceholderEmptyContainer,
+ AnimatedPlaceholderEmptySubTitle,
+ AnimatedPlaceholderEmptyTextContainer,
+ AnimatedPlaceholderEmptyTitle,
+ EMPTY_PLACEHOLDER_TRANSITION_PROPS,
+} from 'twenty-ui/layout';
+
+export const DashboardWidgetPlaceholder = () => {
+ const pageLayoutId = useAvailableComponentInstanceIdOrThrow(
+ PageLayoutComponentInstanceContext,
+ );
+
+ const isPageLayoutInEditMode = useRecoilComponentValue(
+ isPageLayoutInEditModeComponentState,
+ );
+
+ const { setIsPageLayoutInEditMode } =
+ useSetIsPageLayoutInEditMode(pageLayoutId);
+
+ const { navigatePageLayoutCommandMenu } = useNavigatePageLayoutCommandMenu();
+
+ const handleClick = () => {
+ if (!isPageLayoutInEditMode) {
+ setIsPageLayoutInEditMode(true);
+ }
+ navigatePageLayoutCommandMenu({
+ commandMenuPage: CommandMenuPages.PageLayoutWidgetTypeSelect,
+ });
+ };
+
+ return (
+ <WidgetCard
+ variant="dashboard"
+ isEditable={true}
+ isEditing={false}
+ isDragging={false}
+ isResizing={false}
+ onClick={handleClick}
+ >
+ <WidgetCardHeader
+ widgetId="widget-placeholder"
+ isInEditMode={isPageLayoutInEditMode}
+ isResizing={false}
+ title={t`Add Widget`}
+ isEmpty
+ />
+ <AnimatedPlaceholderEmptyContainer
+ // eslint-disable-next-line react/jsx-props-no-spreading
+ {...EMPTY_PLACEHOLDER_TRANSITION_PROPS}
+ >
+ <AnimatedPlaceholder type="noWidgets" />
+ <AnimatedPlaceholderEmptyTextContainer>
+ <AnimatedPlaceholderEmptyTitle>
+ <Trans>Add widget</Trans>
+ </AnimatedPlaceholderEmptyTitle>
+ <AnimatedPlaceholderEmptySubTitle>
+ <Trans>Click to add your first widget</Trans>
+ </AnimatedPlaceholderEmptySubTitle>
+ </AnimatedPlaceholderEmptyTextContainer>
+ </AnimatedPlaceholderEmptyContainer>
+ </WidgetCard>
+ );
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetPlaceholder.tsx b/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetPlaceholder.tsx
index 6c9d3e8dc75..ed2eea936ad 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetPlaceholder.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetPlaceholder.tsx
@@ -1,83 +1,14 @@
-import { useNavigatePageLayoutCommandMenu } from '@/command-menu/pages/page-layout/hooks/useNavigatePageLayoutCommandMenu';
-import { CommandMenuPages } from '@/command-menu/types/CommandMenuPages';
-import { usePageLayoutContentContext } from '@/page-layout/contexts/PageLayoutContentContext';
import { useCurrentPageLayoutOrThrow } from '@/page-layout/hooks/useCurrentPageLayoutOrThrow';
-import { useSetIsPageLayoutInEditMode } from '@/page-layout/hooks/useSetIsPageLayoutInEditMode';
-import { PageLayoutComponentInstanceContext } from '@/page-layout/states/contexts/PageLayoutComponentInstanceContext';
-import { isPageLayoutInEditModeComponentState } from '@/page-layout/states/isPageLayoutInEditModeComponentState';
-import { useIsInPinnedTab } from '@/page-layout/widgets/hooks/useIsInPinnedTab';
-import { WidgetCard } from '@/page-layout/widgets/widget-card/components/WidgetCard';
-import { WidgetCardHeader } from '@/page-layout/widgets/widget-card/components/WidgetCardHeader';
-import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';
-import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
-import { t } from '@lingui/core/macro';
-import { Trans } from '@lingui/react/macro';
-import {
- AnimatedPlaceholder,
- AnimatedPlaceholderEmptyContainer,
- AnimatedPlaceholderEmptySubTitle,
- AnimatedPlaceholderEmptyTextContainer,
- AnimatedPlaceholderEmptyTitle,
- EMPTY_PLACEHOLDER_TRANSITION_PROPS,
-} from 'twenty-ui/layout';
+import { DashboardWidgetPlaceholder } from '@/page-layout/widgets/components/DashboardWidgetPlaceholder';
+import { PageLayoutType } from '~/generated/graphql';
export const WidgetPlaceholder = () => {
- const pageLayoutId = useAvailableComponentInstanceIdOrThrow(
- PageLayoutComponentInstanceContext,
- );
-
- const isPageLayoutInEditMode = useRecoilComponentValue(
- isPageLayoutInEditModeComponentState,
- );
-
- const { setIsPageLayoutInEditMode } =
- useSetIsPageLayoutInEditMode(pageLayoutId);
-
- const { navigatePageLayoutCommandMenu } = useNavigatePageLayoutCommandMenu();
-
const { currentPageLayout } = useCurrentPageLayoutOrThrow();
- const { layoutMode } = usePageLayoutContentContext();
- const { isInPinnedTab } = useIsInPinnedTab();
- const handleClick = () => {
- if (!isPageLayoutInEditMode) {
- setIsPageLayoutInEditMode(true);
- }
- navigatePageLayoutCommandMenu({
- commandMenuPage: CommandMenuPages.PageLayoutWidgetTypeSelect,
- });
- };
+ if (currentPageLayout.type === PageLayoutType.DASHBOARD) {
+ return <DashboardWidgetPlaceholder />;
+ }
- return (
- <WidgetCard
- layoutMode={layoutMode}
- pageLayoutType={currentPageLayout.type}
- isInPinnedTab={isInPinnedTab}
- isEditing={false}
- isDragging={false}
- onClick={handleClick}
- >
- <WidgetCardHeader
- widgetId="widget-placeholder"
- isInEditMode={isPageLayoutInEditMode}
- isResizing={false}
- title={t`Add Widget`}
- isEmpty
- />
- <AnimatedPlaceholderEmptyContainer
- // eslint-disable-next-line react/jsx-props-no-spreading
- {...EMPTY_PLACEHOLDER_TRANSITION_PROPS}
- >
- <AnimatedPlaceholder type="noWidgets" />
- <AnimatedPlaceholderEmptyTextContainer>
- <AnimatedPlaceholderEmptyTitle>
- <Trans>Add widget</Trans>
- </AnimatedPlaceholderEmptyTitle>
- <AnimatedPlaceholderEmptySubTitle>
- <Trans>Click to add your first widget</Trans>
- </AnimatedPlaceholderEmptySubTitle>
- </AnimatedPlaceholderEmptyTextContainer>
- </AnimatedPlaceholderEmptyContainer>
- </WidgetCard>
- );
+ // TODO: Implement RecordPageWidgetPlaceholder when needed
+ return <DashboardWidgetPlaceholder />;
};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetRenderer.tsx b/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetRenderer.tsx
index 4b5b953f89f..fb2b80c6277 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetRenderer.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/components/WidgetRenderer.tsx
@@ -10,16 +10,17 @@ import { PageLayoutWidgetForbiddenDisplay } from '@/page-layout/widgets/componen
import { WidgetContentRenderer } from '@/page-layout/widgets/components/WidgetContentRenderer';
import { useIsInPinnedTab } from '@/page-layout/widgets/hooks/useIsInPinnedTab';
import { useWidgetPermissions } from '@/page-layout/widgets/hooks/useWidgetPermissions';
+import { widgetCardHoveredComponentFamilyState } from '@/page-layout/widgets/states/widgetCardHoveredComponentFamilyState';
+import { getWidgetCardVariant } from '@/page-layout/widgets/utils/getWidgetCardVariant';
import { WidgetCard } from '@/page-layout/widgets/widget-card/components/WidgetCard';
import { WidgetCardContent } from '@/page-layout/widgets/widget-card/components/WidgetCardContent';
import { WidgetCardHeader } from '@/page-layout/widgets/widget-card/components/WidgetCardHeader';
import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
import { useSetRecoilComponentFamilyState } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentFamilyState';
-import { widgetCardHoveredComponentFamilyState } from '@/page-layout/widgets/states/widgetCardHoveredComponentFamilyState';
import { useTheme } from '@emotion/react';
import { type MouseEvent } from 'react';
import { IconLock } from 'twenty-ui/display';
-import { PageLayoutType, type PageLayoutWidget } from '~/generated/graphql';
+import { type PageLayoutWidget } from '~/generated/graphql';
type WidgetRendererProps = {
widget: PageLayoutWidget;
@@ -86,15 +87,20 @@ export const WidgetRenderer = ({ widget }: WidgetRendererProps) => {
setIsHovered(false);
};
+ const variant = getWidgetCardVariant({
+ layoutMode,
+ isInPinnedTab,
+ pageLayoutType: currentPageLayout.type,
+ });
+
return (
<WidgetCard
+ variant={variant}
+ isEditable={isPageLayoutInEditMode}
+ onClick={isPageLayoutInEditMode ? handleClick : undefined}
+ isEditing={isEditing}
isDragging={isDragging}
isResizing={isResizing}
- layoutMode={layoutMode}
- isEditing={isEditing}
- pageLayoutType={currentPageLayout.type}
- isInPinnedTab={isInPinnedTab}
- onClick={isPageLayoutInEditMode ? handleClick : undefined}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
@@ -116,14 +122,9 @@ export const WidgetRenderer = ({ widget }: WidgetRendererProps) => {
/>
)}
- <WidgetCardContent
- layoutMode={layoutMode}
- pageLayoutType={currentPageLayout.type}
- isInPinnedTab={isInPinnedTab}
- isPageLayoutInEditMode={isPageLayoutInEditMode}
- >
+ <WidgetCardContent variant={variant}>
{hasAccess && <WidgetContentRenderer widget={widget} />}
- {!hasAccess && currentPageLayout.type === PageLayoutType.DASHBOARD && (
+ {!hasAccess && (
<IconLock
color={theme.font.color.tertiary}
stroke={theme.icon.stroke.sm}
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/WidgetPlaceholder.stories.tsx b/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/DashboardWidgetPlaceholder.stories.tsx
similarity index 89%
rename from packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/WidgetPlaceholder.stories.tsx
rename to packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/DashboardWidgetPlaceholder.stories.tsx
index c932cb7587d..ea1d598c24c 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/WidgetPlaceholder.stories.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/DashboardWidgetPlaceholder.stories.tsx
@@ -7,7 +7,7 @@ import {
} from '@/page-layout/hooks/__tests__/PageLayoutTestWrapper';
import { pageLayoutPersistedComponentState } from '@/page-layout/states/pageLayoutPersistedComponentState';
import { type PageLayout } from '@/page-layout/types/PageLayout';
-import { WidgetPlaceholder } from '@/page-layout/widgets/components/WidgetPlaceholder';
+import { DashboardWidgetPlaceholder } from '@/page-layout/widgets/components/DashboardWidgetPlaceholder';
import { LayoutRenderingProvider } from '@/ui/layout/contexts/LayoutRenderingContext';
import { type Meta, type StoryObj } from '@storybook/react';
import { type MutableSnapshot } from 'recoil';
@@ -26,9 +26,9 @@ const mockPageLayout: PageLayout = {
updatedAt: '2024-01-01T00:00:00.000Z',
};
-const meta: Meta<typeof WidgetPlaceholder> = {
- title: 'Modules/PageLayout/Widgets/WidgetPlaceholder',
- component: WidgetPlaceholder,
+const meta: Meta<typeof DashboardWidgetPlaceholder> = {
+ title: 'Modules/PageLayout/Widgets/DashboardWidgetPlaceholder',
+ component: DashboardWidgetPlaceholder,
decorators: [
(Story) => {
const initializeState = (snapshot: MutableSnapshot) => {
@@ -78,7 +78,7 @@ const meta: Meta<typeof WidgetPlaceholder> = {
};
export default meta;
-type Story = StoryObj<typeof WidgetPlaceholder>;
+type Story = StoryObj<typeof DashboardWidgetPlaceholder>;
export const Default: Story = {
parameters: {
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/WidgetRenderer.stories.tsx b/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/WidgetRenderer.stories.tsx
index d1ba80eff37..f7ac3061e8c 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/WidgetRenderer.stories.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/components/__stories__/WidgetRenderer.stories.tsx
@@ -7,7 +7,9 @@ import { type MockedResponse } from '@apollo/client/testing';
import { type Meta, type StoryObj } from '@storybook/react';
import { MemoryRouter } from 'react-router-dom';
import { type MutableSnapshot } from 'recoil';
+import { CatalogDecorator, type CatalogStory } from 'twenty-ui/testing';
+import { currentUserWorkspaceState } from '@/auth/states/currentUserWorkspaceState';
import { ApolloCoreClientContext } from '@/object-metadata/contexts/ApolloCoreClientContext';
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { shouldAppBeLoadingState } from '@/object-metadata/states/shouldAppBeLoadingState';
@@ -22,6 +24,13 @@ import {
PAGE_LAYOUT_TEST_INSTANCE_ID,
PageLayoutTestWrapper,
} from '@/page-layout/hooks/__tests__/PageLayoutTestWrapper';
+import { isPageLayoutInEditModeComponentState } from '@/page-layout/states/isPageLayoutInEditModeComponentState';
+import { pageLayoutDraftComponentState } from '@/page-layout/states/pageLayoutDraftComponentState';
+import { pageLayoutDraggingWidgetIdComponentState } from '@/page-layout/states/pageLayoutDraggingWidgetIdComponentState';
+import { pageLayoutEditingWidgetIdComponentState } from '@/page-layout/states/pageLayoutEditingWidgetIdComponentState';
+import { type PageLayout } from '@/page-layout/types/PageLayout';
+import { widgetCardHoveredComponentFamilyState } from '@/page-layout/widgets/states/widgetCardHoveredComponentFamilyState';
+import { type WidgetCardVariant } from '@/page-layout/widgets/types/WidgetCardVariant';
import { LayoutRenderingProvider } from '@/ui/layout/contexts/LayoutRenderingContext';
import {
GraphOrderBy,
@@ -34,6 +43,7 @@ import {
PageLayoutType,
type PageLayoutWidget,
} from '~/generated/graphql';
+import { I18nFrontDecorator } from '~/testing/decorators/I18nFrontDecorator';
import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';
import { generatedMockObjectMetadataItems } from '~/testing/utils/generatedMockObjectMetadataItems';
import { getMockFieldMetadataItemOrThrow } from '~/testing/utils/getMockFieldMetadataItemOrThrow';
@@ -125,6 +135,7 @@ const meta: Meta<typeof WidgetRenderer> = {
title: 'Modules/PageLayout/Widgets/WidgetRenderer',
component: WidgetRenderer,
decorators: [
+ I18nFrontDecorator,
(Story) => {
const initializeState = (snapshot: MutableSnapshot) => {
snapshot.set(
@@ -521,3 +532,262 @@ export const TallWidget: Story = {
</div>
),
};
+
+export const Catalog: CatalogStory<Story, typeof WidgetRenderer> = {
+ args: {
+ widget: {
+ __typename: 'PageLayoutWidget',
+ id: 'catalog-widget',
+ pageLayoutTabId: 'tab-overview',
+ type: WidgetType.GRAPH,
+ title: 'Widget name',
+ objectMetadataId: companyObjectMetadataItem.id,
+ gridPosition: {
+ __typename: 'GridPosition',
+ row: 0,
+ column: 0,
+ rowSpan: 2,
+ columnSpan: 3,
+ },
+ configuration: {
+ __typename: 'AggregateChartConfiguration',
+ graphType: GraphType.AGGREGATE,
+ aggregateOperation: AggregateOperations.COUNT,
+ aggregateFieldMetadataId: idField.id,
+ displayDataLabel: true,
+ },
+ createdAt: '2024-01-01T00:00:00Z',
+ updatedAt: '2024-01-01T00:00:00Z',
+ deletedAt: null,
+ } as PageLayoutWidget,
+ },
+ parameters: {
+ catalog: {
+ dimensions: [
+ {
+ name: 'state',
+ values: ['default', 'hover', 'selected', 'dragging', 'read'],
+ props: (state: string) => ({ catalogState: state }) as any,
+ labels: (state: string) => {
+ const labelMap: Record<string, string> = {
+ default: 'Default',
+ hover: 'Hover',
+ selected: 'Selected',
+ dragging: 'Dragging',
+ read: 'Read',
+ };
+ return labelMap[state] ?? state;
+ },
+ },
+ {
+ name: 'variant',
+ values: [
+ 'record-page',
+ 'side-column',
+ 'record-page-restricted',
+ 'dashboard',
+ 'dashboard-restricted',
+ ],
+ props: (variant: string) => ({ catalogVariant: variant }) as any,
+ labels: (variant: string) => {
+ const labelMap: Record<string, string> = {
+ 'record-page': 'Record page > Default',
+ 'side-column': 'Record page > Side column',
+ 'record-page-restricted': 'Record page - content restriction',
+ dashboard: 'Dashboard',
+ 'dashboard-restricted': 'Dashboard - content restriction',
+ };
+ return labelMap[variant] ?? variant;
+ },
+ },
+ ],
+ options: {
+ elementContainer: {
+ width: 300,
+ },
+ },
+ },
+ pseudo: {
+ hover: ['.hover-state *'],
+ },
+ },
+ render: (args) => {
+ const state = (args as any).catalogState || 'default';
+ const variantKey = (args as any).catalogVariant || 'record-page';
+
+ const isRestricted = variantKey.includes('-restricted');
+ const variant = variantKey.replace('-restricted', '') as WidgetCardVariant;
+
+ const isInEditMode = state !== 'read';
+
+ const pageLayoutType =
+ variant === 'dashboard'
+ ? PageLayoutType.DASHBOARD
+ : PageLayoutType.RECORD_PAGE;
+
+ const layoutMode =
+ variant === 'canvas'
+ ? ('canvas' as const)
+ : variant === 'side-column'
+ ? ('grid' as const)
+ : ('grid' as const);
+
+ const initializeState = (snapshot: MutableSnapshot) => {
+ snapshot.set(objectMetadataItemsState, generatedMockObjectMetadataItems);
+ snapshot.set(shouldAppBeLoadingState, false);
+
+ if (state === 'hover') {
+ snapshot.set(
+ widgetCardHoveredComponentFamilyState.atomFamily({
+ instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ familyKey: args.widget.id,
+ }),
+ true,
+ );
+ }
+
+ if (state === 'selected') {
+ snapshot.set(
+ pageLayoutEditingWidgetIdComponentState.atomFamily({
+ instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ }),
+ args.widget.id,
+ );
+ }
+
+ if (state === 'dragging') {
+ snapshot.set(
+ pageLayoutDraggingWidgetIdComponentState.atomFamily({
+ instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ }),
+ args.widget.id,
+ );
+ }
+
+ if (isRestricted === true) {
+ snapshot.set(currentUserWorkspaceState, {
+ permissionFlags: [],
+ twoFactorAuthenticationMethodSummary: null,
+ objectsPermissions: [
+ {
+ objectMetadataId: companyObjectMetadataItem.id,
+ canReadObjectRecords: false,
+ canUpdateObjectRecords: false,
+ canSoftDeleteObjectRecords: false,
+ canDestroyObjectRecords: false,
+ restrictedFields: {},
+ },
+ ],
+ });
+ } else {
+ snapshot.set(currentUserWorkspaceState, {
+ permissionFlags: [],
+ twoFactorAuthenticationMethodSummary: null,
+ objectsPermissions: [
+ {
+ objectMetadataId: companyObjectMetadataItem.id,
+ canReadObjectRecords: true,
+ canUpdateObjectRecords: true,
+ canSoftDeleteObjectRecords: true,
+ canDestroyObjectRecords: true,
+ restrictedFields: {},
+ },
+ ],
+ });
+ }
+
+ const pageLayoutData: PageLayout = {
+ id: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ name: 'Mock Page Layout',
+ type: pageLayoutType,
+ objectMetadataId: companyObjectMetadataItem.id,
+ tabs:
+ variant === 'side-column'
+ ? [
+ {
+ __typename: 'PageLayoutTab',
+ id: 'pinned-tab',
+ title: 'Pinned Tab',
+ position: 0,
+ pageLayoutId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ widgets: [args.widget],
+ createdAt: '2024-01-01T00:00:00Z',
+ updatedAt: '2024-01-01T00:00:00Z',
+ deletedAt: null,
+ },
+ {
+ __typename: 'PageLayoutTab',
+ id: 'other-tab',
+ title: 'Other Tab',
+ position: 1,
+ pageLayoutId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ widgets: [args.widget],
+ createdAt: '2024-01-01T00:00:00Z',
+ updatedAt: '2024-01-01T00:00:00Z',
+ deletedAt: null,
+ },
+ ]
+ : [],
+ createdAt: '2024-01-01T00:00:00Z',
+ updatedAt: '2024-01-01T00:00:00Z',
+ deletedAt: null,
+ };
+
+ snapshot.set(
+ pageLayoutPersistedComponentState.atomFamily({
+ instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ }),
+ pageLayoutData,
+ );
+ snapshot.set(
+ pageLayoutDraftComponentState.atomFamily({
+ instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ }),
+ pageLayoutData,
+ );
+ snapshot.set(
+ isPageLayoutInEditModeComponentState.atomFamily({
+ instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
+ }),
+ isInEditMode,
+ );
+ };
+
+ const containerClassName = state === 'hover' ? 'hover-state' : '';
+
+ return (
+ <div
+ style={{ width: '300px', height: '200px' }}
+ className={containerClassName}
+ >
+ <JestMetadataAndApolloMocksWrapper>
+ <CoreClientProviderWrapper>
+ <PageLayoutTestWrapper initializeState={initializeState}>
+ <LayoutRenderingProvider
+ value={{
+ isInRightDrawer: false,
+ layoutType: pageLayoutType,
+ targetRecordIdentifier: {
+ id: companyObjectMetadataItem.id,
+ targetObjectNameSingular:
+ companyObjectMetadataItem.nameSingular,
+ },
+ }}
+ >
+ <PageLayoutContentProvider
+ value={{
+ layoutMode,
+ tabId: variant === 'side-column' ? 'pinned-tab' : 'fields',
+ }}
+ >
+ <WidgetRenderer widget={args.widget} />
+ </PageLayoutContentProvider>
+ </LayoutRenderingProvider>
+ </PageLayoutTestWrapper>
+ </CoreClientProviderWrapper>
+ </JestMetadataAndApolloMocksWrapper>
+ </div>
+ );
+ },
+ decorators: [CatalogDecorator],
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/types/WidgetCardVariant.ts b/packages/twenty-front/src/modules/page-layout/widgets/types/WidgetCardVariant.ts
new file mode 100644
index 00000000000..e57d8477160
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/types/WidgetCardVariant.ts
@@ -0,0 +1,5 @@
+export type WidgetCardVariant =
+ | 'canvas'
+ | 'side-column'
+ | 'dashboard'
+ | 'record-page';
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/utils/getWidgetCardVariant.ts b/packages/twenty-front/src/modules/page-layout/widgets/utils/getWidgetCardVariant.ts
new file mode 100644
index 00000000000..8a1400f8ad9
--- /dev/null
+++ b/packages/twenty-front/src/modules/page-layout/widgets/utils/getWidgetCardVariant.ts
@@ -0,0 +1,29 @@
+import { type PageLayoutTabLayoutMode } from '@/page-layout/types/PageLayoutTabLayoutMode';
+import { PageLayoutType } from '~/generated/graphql';
+import { type WidgetCardVariant } from '~/modules/page-layout/widgets/types/WidgetCardVariant';
+
+type GetWidgetCardVariantParams = {
+ layoutMode: PageLayoutTabLayoutMode;
+ isInPinnedTab: boolean;
+ pageLayoutType: PageLayoutType | null;
+};
+
+export const getWidgetCardVariant = ({
+ layoutMode,
+ isInPinnedTab,
+ pageLayoutType,
+}: GetWidgetCardVariantParams): WidgetCardVariant => {
+ if (pageLayoutType === PageLayoutType.DASHBOARD) {
+ return 'dashboard';
+ }
+
+ if (layoutMode === 'canvas') {
+ return 'canvas';
+ }
+
+ if (isInPinnedTab) {
+ return 'side-column';
+ }
+
+ return 'record-page';
+};
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCard.tsx b/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCard.tsx
index 3cab3da6fd0..cfd4401bdb1 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCard.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCard.tsx
@@ -1,32 +1,12 @@
-import { isPageLayoutInEditModeComponentState } from '@/page-layout/states/isPageLayoutInEditModeComponentState';
-import { type PageLayoutTabLayoutMode } from '@/page-layout/types/PageLayoutTabLayoutMode';
-import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
import { css } from '@emotion/react';
import styled from '@emotion/styled';
-import { type ReactNode } from 'react';
import { isDefined } from 'twenty-shared/utils';
-import { PageLayoutType } from '~/generated/graphql';
-
-export type WidgetCardProps = {
- children?: ReactNode;
- pageLayoutType: PageLayoutType;
- layoutMode: PageLayoutTabLayoutMode;
- onClick?: () => void;
- isEditing: boolean;
- isDragging: boolean;
- isInPinnedTab: boolean;
- isResizing?: boolean;
- className?: string;
- onMouseEnter?: () => void;
- onMouseLeave?: () => void;
-};
+import { type WidgetCardVariant } from '~/modules/page-layout/widgets/types/WidgetCardVariant';
const StyledWidgetCard = styled.div<{
+ variant: WidgetCardVariant;
+ isEditable: boolean;
onClick?: () => void;
- pageLayoutType: PageLayoutType;
- layoutMode: PageLayoutTabLayoutMode;
- isInPinnedTab: boolean;
- isPageLayoutInEditMode: boolean;
isEditing: boolean;
isDragging: boolean;
isResizing: boolean;
@@ -40,145 +20,116 @@ const StyledWidgetCard = styled.div<{
${({
theme,
- pageLayoutType,
- layoutMode,
- isPageLayoutInEditMode,
+ variant,
+ isEditable,
isEditing,
isDragging,
- isInPinnedTab,
isResizing,
onClick,
}) => {
- if (layoutMode === 'canvas') {
+ if (variant === 'dashboard' && !isEditable) {
return css`
- height: 100%;
+ background: ${theme.background.secondary};
+ border: 1px solid ${theme.border.color.light};
+ border-radius: ${theme.border.radius.md};
+ padding: ${theme.spacing(2)};
+ gap: ${theme.spacing(2)};
`;
}
- switch (pageLayoutType) {
- case PageLayoutType.DASHBOARD: {
- return css`
- background: ${theme.background.secondary};
- border: 1px solid ${theme.border.color.light};
- border-radius: ${theme.border.radius.md};
- padding: ${theme.spacing(2)};
- gap: ${theme.spacing(2)};
+ if (variant === 'dashboard' && isEditable) {
+ return css`
+ background: ${theme.background.secondary};
+ border: 1px solid ${theme.border.color.light};
+ border-radius: ${theme.border.radius.md};
+ padding: ${theme.spacing(2)};
+ gap: ${theme.spacing(2)};
- ${isPageLayoutInEditMode &&
- !isDragging &&
- !isEditing &&
- !isResizing &&
- css`
- &:hover {
- border: 1px solid ${theme.border.color.strong};
- cursor: ${isDefined(onClick) ? 'pointer' : 'default'};
- }
- `}
+ ${!isDragging &&
+ !isEditing &&
+ !isResizing &&
+ css`
+ &:hover {
+ border: 1px solid ${theme.border.color.strong};
+ cursor: ${isDefined(onClick) ? 'pointer' : 'default'};
+ }
+ `}
- ${isEditing &&
- !isDragging &&
- css`
- border: 1px solid ${theme.color.blue} !important;
- `}
+ ${isEditing &&
+ !isDragging &&
+ css`
+ border: 1px solid ${theme.color.blue} !important;
+ `}
- ${isDragging &&
- css`
- background: linear-gradient(
- 0deg,
- ${theme.background.transparent.lighter} 0%,
- ${theme.background.transparent.lighter} 100%
- ),
- ${theme.background.secondary};
- border: 1px solid ${theme.color.blue} !important;
- `}
- `;
- }
+ ${isDragging &&
+ css`
+ background: linear-gradient(
+ 0deg,
+ ${theme.background.transparent.lighter} 0%,
+ ${theme.background.transparent.lighter} 100%
+ ),
+ ${theme.background.secondary};
+ border: 1px solid ${theme.color.blue} !important;
+ `}
+ `;
+ }
- case PageLayoutType.RECORD_PAGE: {
- return css`
- background: ${theme.background.primary};
- border: 1px solid transparent;
- border-radius: ${theme.border.radius.md};
- gap: ${theme.spacing(2)};
- padding: ${theme.spacing(2)};
+ if (variant === 'side-column' && !isEditable) {
+ return css`
+ background: ${theme.background.secondary};
+ `;
+ }
- ${isPageLayoutInEditMode &&
- !isDragging &&
- !isEditing &&
- !isResizing &&
- css`
- &:hover {
- border: 1px solid ${theme.border.color.strong};
- cursor: ${isDefined(onClick) ? 'pointer' : 'default'};
- }
- `}
+ if (
+ (variant === 'side-column' && isEditable) ||
+ (variant === 'record-page' && isEditable)
+ ) {
+ return css`
+ background: ${theme.background.primary};
+ border: 1px solid transparent;
+ border-radius: ${theme.border.radius.md};
+ gap: ${theme.spacing(2)};
+ padding: ${theme.spacing(2)};
- ${isEditing &&
- !isDragging &&
- css`
- border: 1px solid ${theme.color.blue} !important;
- `}
+ ${!isDragging &&
+ !isEditing &&
+ !isResizing &&
+ css`
+ &:hover {
+ border: 1px solid ${theme.border.color.strong};
+ cursor: ${isDefined(onClick) ? 'pointer' : 'default'};
+ }
+ `}
- ${isDragging &&
- css`
- background: linear-gradient(
- 0deg,
- ${theme.background.transparent.lighter} 0%,
- ${theme.background.transparent.lighter} 100%
- ),
- ${theme.background.secondary};
- border: 1px solid ${theme.color.blue} !important;
- `}
+ ${isEditing &&
+ !isDragging &&
+ css`
+ border: 1px solid ${theme.color.blue} !important;
+ `}
- ${isInPinnedTab &&
- !isPageLayoutInEditMode &&
- css`
- border: none;
- padding: 0;
- border-radius: 0;
- background: ${theme.background.secondary};
- `}
- `;
- }
+ ${isDragging &&
+ css`
+ background: linear-gradient(
+ 0deg,
+ ${theme.background.transparent.lighter} 0%,
+ ${theme.background.transparent.lighter} 100%
+ ),
+ ${theme.background.secondary};
+ border: 1px solid ${theme.color.blue} !important;
+ `}
+ `;
+ }
- default:
- return undefined;
+ if (variant === 'record-page' && !isEditable) {
+ return css`
+ background: ${theme.background.primary};
+ border: 1px solid transparent;
+ border-radius: ${theme.border.radius.md};
+ gap: ${theme.spacing(2)};
+ padding: ${theme.spacing(2)};
+ `;
}
}}
`;
-export const WidgetCard = ({
- children,
- pageLayoutType,
- layoutMode,
- onClick,
- isEditing,
- isDragging,
- isInPinnedTab,
- isResizing = false,
- className,
- onMouseEnter,
- onMouseLeave,
-}: WidgetCardProps) => {
- const isPageLayoutInEditMode = useRecoilComponentValue(
- isPageLayoutInEditModeComponentState,
- );
-
- return (
- <StyledWidgetCard
- onClick={onClick}
- pageLayoutType={pageLayoutType}
- layoutMode={layoutMode}
- isPageLayoutInEditMode={isPageLayoutInEditMode}
- isEditing={isEditing}
- isDragging={isDragging}
- isInPinnedTab={isInPinnedTab}
- isResizing={isResizing}
- className={className}
- onMouseEnter={onMouseEnter}
- onMouseLeave={onMouseLeave}
- >
- {children}
- </StyledWidgetCard>
- );
-};
+export { StyledWidgetCard as WidgetCard };
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCardContent.tsx b/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCardContent.tsx
index 731d6dabe5f..6361bb5395c 100644
--- a/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCardContent.tsx
+++ b/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/WidgetCardContent.tsx
@@ -1,56 +1,28 @@
-import { type PageLayoutTabLayoutMode } from '@/page-layout/types/PageLayoutTabLayoutMode';
import { css } from '@emotion/react';
import styled from '@emotion/styled';
-import { type ReactNode } from 'react';
-import { type PageLayoutType } from '~/generated/graphql';
+import { type WidgetCardVariant } from '~/modules/page-layout/widgets/types/WidgetCardVariant';
-export type WidgetCardContentProps = {
- children?: ReactNode;
- pageLayoutType: PageLayoutType;
- isInPinnedTab: boolean;
- isPageLayoutInEditMode: boolean;
- layoutMode: PageLayoutTabLayoutMode;
- className?: string;
-};
-
-const StyledWidgetCardContent = styled.div<WidgetCardContentProps>`
+const StyledWidgetCardContent = styled.div<{ variant: WidgetCardVariant }>`
align-items: center;
display: flex;
height: 100%;
width: 100%;
justify-content: center;
box-sizing: border-box;
- padding: ${({ theme }) => theme.spacing(2)};
- ${({
- theme,
- pageLayoutType,
- layoutMode,
- isPageLayoutInEditMode,
- isInPinnedTab,
- }) => {
- if (layoutMode === 'canvas') {
+ ${({ theme, variant }) => {
+ if (variant === 'dashboard') {
return css`
- padding: 0;
+ padding: ${theme.spacing(2)};
`;
}
- switch (pageLayoutType) {
- case 'RECORD_PAGE':
- return css`
- border: 1px solid ${theme.border.color.medium};
- border-radius: ${theme.border.radius.md};
-
- ${isInPinnedTab &&
- !isPageLayoutInEditMode &&
- css`
- border: none;
- padding: 0;
- `}
- `;
-
- default:
- return '';
+ if (variant === 'record-page') {
+ return css`
+ border: 1px solid ${theme.border.color.medium};
+ border-radius: ${theme.border.radius.md};
+ padding: ${theme.spacing(2)};
+ `;
}
}}
`;
diff --git a/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/__stories__/WidgetCard.stories.tsx b/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/__stories__/WidgetCard.stories.tsx
deleted file mode 100644
index 6268cfce709..00000000000
--- a/packages/twenty-front/src/modules/page-layout/widgets/widget-card/components/__stories__/WidgetCard.stories.tsx
+++ /dev/null
@@ -1,219 +0,0 @@
-import styled from '@emotion/styled';
-import { type Meta, type StoryObj } from '@storybook/react';
-import { useEffect, type ReactNode } from 'react';
-import { CatalogDecorator, type CatalogStory } from 'twenty-ui/testing';
-
-import { ForbiddenFieldDisplay } from '@/object-record/record-field/ui/meta-types/display/components/ForbiddenFieldDisplay';
-import { PageLayoutTestWrapper } from '@/page-layout/hooks/__tests__/PageLayoutTestWrapper';
-import { isPageLayoutInEditModeComponentState } from '@/page-layout/states/isPageLayoutInEditModeComponentState';
-import { WidgetCard } from '@/page-layout/widgets/widget-card/components/WidgetCard';
-import { WidgetCardContent } from '@/page-layout/widgets/widget-card/components/WidgetCardContent';
-import { WidgetCardHeader } from '@/page-layout/widgets/widget-card/components/WidgetCardHeader';
-import { useSetRecoilComponentState } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentState';
-import { PageLayoutType } from '~/generated/graphql';
-import { I18nFrontDecorator } from '~/testing/decorators/I18nFrontDecorator';
-
-const StyledContainer = styled.div`
- height: 200px;
- width: 300px;
-`;
-
-const StyledMockContent = styled.div`
- align-items: center;
- color: ${({ theme }) => theme.font.color.tertiary};
- display: flex;
- font-size: ${({ theme }) => theme.font.size.sm};
- justify-content: center;
-`;
-
-const EditModeWrapper = ({
- children,
- isInEditMode,
-}: {
- children: ReactNode;
- isInEditMode: boolean;
-}) => {
- const setIsPageLayoutInEditMode = useSetRecoilComponentState(
- isPageLayoutInEditModeComponentState,
- );
-
- useEffect(() => {
- setIsPageLayoutInEditMode(isInEditMode);
- }, [isInEditMode, setIsPageLayoutInEditMode]);
-
- return <>{children}</>;
-};
-
-const meta: Meta<typeof WidgetCard> = {
- title: 'Modules/PageLayout/Widgets/WidgetCard',
- component: WidgetCard,
-};
-
-export default meta;
-type Story = StoryObj<typeof WidgetCard>;
-
-export const Default: Story = {
- decorators: [
- (Story) => {
- return (
- <PageLayoutTestWrapper>
- <EditModeWrapper isInEditMode={true}>
- <StyledContainer>
- <Story />
- </StyledContainer>
- </EditModeWrapper>
- </PageLayoutTestWrapper>
- );
- },
- I18nFrontDecorator,
- ],
- parameters: {
- pseudo: { hover: false },
- },
- args: {
- pageLayoutType: PageLayoutType.DASHBOARD,
- layoutMode: 'grid',
- isInPinnedTab: false,
- isEditing: false,
- isDragging: false,
- },
- render: (args) => (
- <WidgetCard
- pageLayoutType={args.pageLayoutType}
- layoutMode={args.layoutMode}
- isInPinnedTab={args.isInPinnedTab}
- isEditing={args.isEditing}
- isDragging={args.isDragging}
- >
- <WidgetCardHeader
- widgetId="story-widget"
- isInEditMode={true}
- onRemove={() => {}}
- title="Widget name"
- />
- <WidgetCardContent
- pageLayoutType={args.pageLayoutType}
- layoutMode={args.layoutMode}
- isInPinnedTab={args.isInPinnedTab}
- isPageLayoutInEditMode={args.isEditing}
- >
- <StyledMockContent>Widget</StyledMockContent>
- </WidgetCardContent>
- </WidgetCard>
- ),
-};
-
-export const Catalog: CatalogStory<Story, typeof WidgetCard> = {
- decorators: [
- (Story) => {
- return (
- <StyledContainer>
- <Story />
- </StyledContainer>
- );
- },
- CatalogDecorator,
- I18nFrontDecorator,
- ],
- parameters: {
- pseudo: { hover: ['.hover'] },
- catalog: {
- dimensions: [
- {
- name: 'contextVariant',
- values: [
- 'Record Page - Default',
- 'Record Page - Default - Pinned',
- 'Record Page - Restriction',
- 'Record Page - Restriction - Pinned',
- 'Dashboard - Default',
- 'Dashboard - Restriction',
- ],
- props: (contextName: string) => {
- const pageLayoutType = contextName.includes('Record')
- ? PageLayoutType.RECORD_PAGE
- : PageLayoutType.DASHBOARD;
- const hasRestriction = contextName.includes('Restriction');
- const isInPinnedTab = contextName.includes('Pinned');
-
- return {
- pageLayoutType,
- contextVariant: contextName,
- hasRestriction,
- isInPinnedTab,
- };
- },
- },
- {
- name: 'state',
- values: ['Default', 'Hover', 'Selected', 'Dragging', 'Read Mode'],
- props: (state: string) => {
- const stateProps: {
- className?: string;
- isDragging?: boolean;
- isEditing?: boolean;
- } = {};
-
- switch (state) {
- case 'Hover':
- stateProps.className = 'hover';
- break;
- case 'Selected':
- stateProps.isEditing = true;
- break;
- case 'Dragging':
- stateProps.isDragging = true;
- break;
- case 'Read Mode':
- stateProps.className = 'read-mode';
- break;
- }
-
- return { ...stateProps, state };
- },
- },
- ],
- },
- },
- render: (args: any) => {
- const isReadMode = args.state === 'Read Mode';
- const pageLayoutType = args.pageLayoutType || PageLayoutType.DASHBOARD;
- const layoutMode = args.layoutMode || 'grid';
- const isInPinnedTab = args.isInPinnedTab || false;
- const hasRestriction = args.hasRestriction || false;
- const isPageLayoutInEditMode = false;
-
- return (
- <PageLayoutTestWrapper>
- <EditModeWrapper isInEditMode={!isReadMode}>
- <WidgetCard
- className={args.className}
- isDragging={args.isDragging ?? false}
- isEditing={args.isEditing ?? false}
- pageLayoutType={pageLayoutType}
- layoutMode={layoutMode}
- isInPinnedTab={isInPinnedTab}
- >
- <WidgetCardHeader
- widgetId="catalog-widget"
- forbiddenDisplay={
- hasRestriction ? <ForbiddenFieldDisplay /> : undefined
- }
- isInEditMode={!isReadMode}
- onRemove={!isReadMode ? () => {} : undefined}
- title="Widget name"
- />
- <WidgetCardContent
- pageLayoutType={pageLayoutType}
- layoutMode={layoutMode}
- isInPinnedTab={isInPinnedTab}
- isPageLayoutInEditMode={isPageLayoutInEditMode}
- >
- <StyledMockContent>Widget</StyledMockContent>
- </WidgetCardContent>
- </WidgetCard>
- </EditModeWrapper>
- </PageLayoutTestWrapper>
- );
- },
-};