Files
twenty/packages/twenty-front/src/modules/action-menu/hooks/useActionMenuEntries.ts
T
Paul Rastoin f55a20addc [ENH] UseActionMenuEntries optimization (#11183)
# Introduction
Dynamically set in remove actions only if map has given key
Thanks @charlesBochet for the class on Recoil !

Related to https://github.com/twentyhq/twenty/issues/11079

## Conclusion
As always any suggestions are welcomed !

---------

Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
2025-03-26 13:19:08 +01:00

49 lines
1.5 KiB
TypeScript

import { actionMenuEntriesComponentState } from '@/action-menu/states/actionMenuEntriesComponentState';
import { ActionMenuEntry } from '@/action-menu/types/ActionMenuEntry';
import { useRecoilComponentCallbackStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentCallbackStateV2';
import { useRecoilCallback } from 'recoil';
export const useActionMenuEntries = () => {
const actionMenuEntryState = useRecoilComponentCallbackStateV2(
actionMenuEntriesComponentState,
);
const addActionMenuEntry = useRecoilCallback(
({ snapshot, set }) =>
async (entryToAdd: ActionMenuEntry) => {
const currentEntries = snapshot
.getLoadable(actionMenuEntryState)
.getValue();
const newEntries = new Map([
...currentEntries,
[entryToAdd.key, entryToAdd],
]);
set(actionMenuEntryState, newEntries);
},
[actionMenuEntryState],
);
const removeActionMenuEntry = useRecoilCallback(
({ snapshot, set }) =>
async (entryKeyToRemove: string) => {
const currentEntries = snapshot
.getLoadable(actionMenuEntryState)
.getValue();
if (!currentEntries.has(entryKeyToRemove)) {
return;
}
const newEntries = new Map(currentEntries);
newEntries.delete(entryKeyToRemove);
set(actionMenuEntryState, newEntries);
},
[actionMenuEntryState],
);
return {
addActionMenuEntry,
removeActionMenuEntry,
};
};