Files
twenty/packages/twenty-front/src/modules/page-layout/hooks/usePageLayoutTabWithVisibleWidgetsOrThrow.ts
T
Baptiste Devessier e0d1f74648 Page layout conditional display (#15802)
- Allow widgets to be hidden based on device's type conditions: desktop
or mobile
- Create two widgets for rich text fields on tasks and notes. One widget
is displayed below fields on mobile (and in the right drawer); the other
is displayed on a separate tab on desktop
- In read mode, hide tabs if they contain no visible widgets. If there
is no tab left to display, display at least the first one with no
widgets.
- In edit mode, display all tabs and all widgets.

## Demo


https://github.com/user-attachments/assets/65ef1261-3902-4432-a420-48983b763b2c

Closes https://github.com/twentyhq/core-team-issues/issues/1811
2025-11-18 10:35:38 +01:00

42 lines
1.6 KiB
TypeScript

import { useCurrentPageLayout } from '@/page-layout/hooks/useCurrentPageLayout';
import { isPageLayoutInEditModeComponentState } from '@/page-layout/states/isPageLayoutInEditModeComponentState';
import { type PageLayoutTab } from '@/page-layout/types/PageLayoutTab';
import { buildWidgetVisibilityContext } from '@/page-layout/utils/buildWidgetVisibilityContext';
import { filterVisibleWidgets } from '@/page-layout/utils/filterVisibleWidgets';
import { useLayoutRenderingContext } from '@/ui/layout/contexts/LayoutRenderingContext';
import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
import { isDefined } from 'twenty-shared/utils';
export const usePageLayoutTabWithVisibleWidgetsOrThrow = (
tabId: string,
): PageLayoutTab => {
const { currentPageLayout } = useCurrentPageLayout();
const isMobile = useIsMobile();
const { isInRightDrawer } = useLayoutRenderingContext();
const isPageLayoutInEditMode = useRecoilComponentValue(
isPageLayoutInEditModeComponentState,
);
if (!isDefined(currentPageLayout)) {
throw new Error('currentPageLayout is not defined');
}
const tab = currentPageLayout.tabs.find((t) => t.id === tabId);
if (!isDefined(tab)) {
throw new Error('Tab not found');
}
if (isPageLayoutInEditMode) {
return tab;
}
const context = buildWidgetVisibilityContext({ isMobile, isInRightDrawer });
return {
...tab,
widgets: filterVisibleWidgets({ widgets: tab.widgets, context }),
};
};