create hook to register global actions

This commit is contained in:
bosiraphael 2024-11-20 18:23:02 +01:00
parent 2968085e73
commit f4a1b4b589
7 changed files with 54 additions and 31 deletions

View File

@ -1,8 +0,0 @@
import { WorkflowRunActionEffect } from '@/action-menu/actions/global-actions/workflow-run-actions/components/WorkflowRunActionEffect';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
export const GlobalActionMenuEntriesSetter = () => {
const isWorkflowEnabled = useIsFeatureEnabled('IS_WORKFLOW_ENABLED');
return <>{isWorkflowEnabled && <WorkflowRunActionEffect />}</>;
};

View File

@ -0,0 +1,16 @@
import { useGlobalActions } from '@/action-menu/actions/global-actions/hooks/useGlobalActions';
import { useEffect } from 'react';
export const GlobalActionMenuEntriesSetterEffect = () => {
const { registerGlobalActions, unregisterGlobalActions } = useGlobalActions();
useEffect(() => {
registerGlobalActions();
return () => {
unregisterGlobalActions();
};
}, [registerGlobalActions, unregisterGlobalActions]);
return null;
};

View File

@ -0,0 +1,23 @@
import { useWorkflowRunActions } from '@/action-menu/actions/global-actions/workflow-run-actions/hooks/useWorkflowRunActions';
import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';
export const useGlobalActions = () => {
const isWorkflowEnabled = useIsFeatureEnabled('IS_WORKFLOW_ENABLED');
const { addWorkflowRunActions, removeWorkflowRunActions } =
useWorkflowRunActions();
const registerGlobalActions = () => {
if (isWorkflowEnabled) {
addWorkflowRunActions();
}
};
const unregisterGlobalActions = () => {
if (isWorkflowEnabled) {
removeWorkflowRunActions();
}
};
return { registerGlobalActions, unregisterGlobalActions };
};

View File

@ -5,11 +5,10 @@ import { useAllActiveWorkflowVersions } from '@/workflow/hooks/useAllActiveWorkf
import { useRunWorkflowVersion } from '@/workflow/hooks/useRunWorkflowVersion';
import { useTheme } from '@emotion/react';
import { useEffect } from 'react';
import { IconSettingsAutomation } from 'twenty-ui';
import { capitalize } from '~/utils/string/capitalize';
export const WorkflowRunActionEffect = () => {
export const useWorkflowRunActions = () => {
const { addActionMenuEntry, removeActionMenuEntry } = useActionMenuEntries();
const { records: activeWorkflowVersions } = useAllActiveWorkflowVersions({
@ -22,7 +21,7 @@ export const WorkflowRunActionEffect = () => {
const theme = useTheme();
useEffect(() => {
const addWorkflowRunActions = () => {
for (const [
index,
activeWorkflowVersion,
@ -52,20 +51,13 @@ export const WorkflowRunActionEffect = () => {
},
});
}
};
return () => {
for (const activeWorkflowVersion of activeWorkflowVersions) {
removeActionMenuEntry(`workflow-run-${activeWorkflowVersion.id}`);
}
};
}, [
activeWorkflowVersions,
addActionMenuEntry,
enqueueSnackBar,
removeActionMenuEntry,
runWorkflowVersion,
theme.snackBar.success.color,
]);
const removeWorkflowRunActions = () => {
for (const activeWorkflowVersion of activeWorkflowVersions) {
removeActionMenuEntry(`workflow-run-${activeWorkflowVersion.id}`);
}
};
return null;
return { addWorkflowRunActions, removeWorkflowRunActions };
};

View File

@ -1,4 +1,4 @@
import { GlobalActionMenuEntriesSetter } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetter';
import { GlobalActionMenuEntriesSetterEffect } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetterEffect';
import { RecordActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/RecordActionMenuEntriesSetter';
import { ActionMenuConfirmationModals } from '@/action-menu/components/ActionMenuConfirmationModals';
import { RecordIndexActionMenuBar } from '@/action-menu/components/RecordIndexActionMenuBar';
@ -28,7 +28,7 @@ export const RecordIndexActionMenu = () => {
<ActionMenuConfirmationModals />
<RecordIndexActionMenuEffect />
<RecordActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetterEffect />
</ActionMenuContext.Provider>
)}
</>

View File

@ -1,4 +1,4 @@
import { GlobalActionMenuEntriesSetter } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetter';
import { GlobalActionMenuEntriesSetterEffect } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetterEffect';
import { RecordActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/RecordActionMenuEntriesSetter';
import { ActionMenuConfirmationModals } from '@/action-menu/components/ActionMenuConfirmationModals';
import { ActionMenuContext } from '@/action-menu/contexts/ActionMenuContext';
@ -48,7 +48,7 @@ export const RecordShowActionMenu = ({
/>
<ActionMenuConfirmationModals />
<RecordActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetterEffect />
</ActionMenuContext.Provider>
)}
</>

View File

@ -1,4 +1,4 @@
import { GlobalActionMenuEntriesSetter } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetter';
import { GlobalActionMenuEntriesSetterEffect } from '@/action-menu/actions/global-actions/components/GlobalActionMenuEntriesSetterEffect';
import { RecordActionMenuEntriesSetter } from '@/action-menu/actions/record-actions/components/RecordActionMenuEntriesSetter';
import { ActionMenuConfirmationModals } from '@/action-menu/components/ActionMenuConfirmationModals';
import { RightDrawerActionMenuDropdown } from '@/action-menu/components/RightDrawerActionMenuDropdown';
@ -24,7 +24,7 @@ export const RecordShowRightDrawerActionMenu = () => {
<RightDrawerActionMenuDropdown />
<ActionMenuConfirmationModals />
<RecordActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetter />
<GlobalActionMenuEntriesSetterEffect />
</ActionMenuContext.Provider>
)}
</>