The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { useCallback } from 'react';
import { PageLayoutComponentInstanceContext } from '@/page-layout/states/contexts/PageLayoutComponentInstanceContext';
import { pageLayoutDraftComponentState } from '@/page-layout/states/pageLayoutDraftComponentState';
import { pageLayoutEditingWidgetIdComponentState } from '@/page-layout/states/pageLayoutEditingWidgetIdComponentState';
import { pageLayoutTabSettingsOpenTabIdComponentState } from '@/page-layout/states/pageLayoutTabSettingsOpenTabIdComponentState';
import { getIsSingleWidgetTab } from '@/page-layout/utils/getIsSingleWidgetTab';
import { isViewportFillingWidget } from '@/page-layout/widgets/utils/isViewportFillingWidget';
import { useSidePanelMenu } from '@/side-panel/hooks/useSidePanelMenu';
import { useNavigatePageLayoutSidePanel } from '@/side-panel/pages/page-layout/hooks/useNavigatePageLayoutSidePanel';
import { useLayoutRenderingContext } from '@/ui/layout/contexts/LayoutRenderingContext';
import { useAvailableComponentInstanceIdOrThrow } from '@/ui/utilities/state/component-state/hooks/useAvailableComponentInstanceIdOrThrow';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';
import { t } from '@lingui/core/macro';
import { SidePanelPages } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import {
PageLayoutTabLayoutMode,
PageLayoutType,
WidgetType,
} from '~/generated-metadata/graphql';
export const useOpenWidgetSettingsInSidePanel = (
pageLayoutIdFromProps?: string,
) => {
const pageLayoutId = useAvailableComponentInstanceIdOrThrow(
PageLayoutComponentInstanceContext,
pageLayoutIdFromProps,
);
const { layoutType } = useLayoutRenderingContext();
const isDashboardPageLayout = layoutType === PageLayoutType.DASHBOARD;
const setPageLayoutEditingWidgetId = useSetAtomComponentState(
pageLayoutEditingWidgetIdComponentState,
pageLayoutId,
);
const { navigatePageLayoutSidePanel } = useNavigatePageLayoutSidePanel();
const { closeSidePanelMenu } = useSidePanelMenu();
const pageLayoutDraft = useAtomComponentStateValue(
pageLayoutDraftComponentState,
pageLayoutId,
);
const setPageLayoutTabSettingsOpenTabId = useSetAtomComponentState(
pageLayoutTabSettingsOpenTabIdComponentState,
pageLayoutId,
);
const openWidgetSettingsInSidePanel = useCallback(
({
widgetId,
widgetType,
}: {
widgetId: string;
widgetType: WidgetType;
}) => {
if (widgetType === WidgetType.IFRAME && isDashboardPageLayout) {
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.DashboardIframeSettings,
pageTitle: t`Edit iFrame`,
resetNavigationStack: true,
});
setPageLayoutEditingWidgetId(widgetId);
return;
}
if (widgetType === WidgetType.GRAPH && isDashboardPageLayout) {
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.DashboardChartSettings,
pageTitle: t`Edit Graph`,
resetNavigationStack: true,
});
setPageLayoutEditingWidgetId(widgetId);
return;
}
if (widgetType === WidgetType.FIELDS) {
if (!isDashboardPageLayout) {
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.RecordPageFieldsSettings,
pageTitle: t`Edit Fields`,
resetNavigationStack: true,
});
setPageLayoutEditingWidgetId(widgetId);
}
return;
}
if (widgetType === WidgetType.FIELD) {
if (!isDashboardPageLayout) {
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.RecordPageFieldSettings,
pageTitle: t`Field widget`,
resetNavigationStack: true,
});
setPageLayoutEditingWidgetId(widgetId);
}
return;
}
if (widgetType === WidgetType.RECORD_TABLE && isDashboardPageLayout) {
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.DashboardRecordTableSettings,
pageTitle: t`Edit Record Table`,
resetNavigationStack: true,
});
setPageLayoutEditingWidgetId(widgetId);
return;
}
if (widgetType === WidgetType.STANDALONE_RICH_TEXT) {
if (isDashboardPageLayout) {
closeSidePanelMenu();
} else {
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.PageLayoutWidgetSettings,
pageTitle: t`Note`,
resetNavigationStack: true,
});
}
setPageLayoutEditingWidgetId(widgetId);
return;
}
const containingTab = pageLayoutDraft.tabs.find((tab) =>
tab.widgets.some((w) => w.id === widgetId),
);
const isContainingTabSingleWidget =
isDefined(containingTab) &&
getIsSingleWidgetTab({ tab: containingTab });
const widgetInContainingTab = containingTab?.widgets.find(
(widget) => widget.id === widgetId,
);
const isViewportFillingWidgetInVerticalList =
isDefined(containingTab) &&
containingTab.layoutMode === PageLayoutTabLayoutMode.VERTICAL_LIST &&
isDefined(widgetInContainingTab) &&
isViewportFillingWidget(widgetInContainingTab);
if (
isContainingTabSingleWidget &&
!isViewportFillingWidgetInVerticalList
) {
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.PageLayoutTabSettings,
resetNavigationStack: true,
});
setPageLayoutTabSettingsOpenTabId(containingTab.id);
return;
}
navigatePageLayoutSidePanel({
sidePanelPage: SidePanelPages.PageLayoutWidgetSettings,
resetNavigationStack: true,
});
setPageLayoutEditingWidgetId(widgetId);
},
[
isDashboardPageLayout,
pageLayoutDraft,
setPageLayoutEditingWidgetId,
setPageLayoutTabSettingsOpenTabId,
navigatePageLayoutSidePanel,
closeSidePanelMenu,
],
);
return {
openWidgetSettingsInSidePanel,
};
};