[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>
This commit is contained in:
Paul Rastoin
2025-03-26 13:19:08 +01:00
committed by GitHub
parent c36cfb3af9
commit f55a20addc

View File

@ -1,25 +1,45 @@
import { actionMenuEntriesComponentState } from '@/action-menu/states/actionMenuEntriesComponentState';
import { ActionMenuEntry } from '@/action-menu/types/ActionMenuEntry';
import { useSetRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useSetRecoilComponentStateV2';
import { useRecoilComponentCallbackStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentCallbackStateV2';
import { useRecoilCallback } from 'recoil';
export const useActionMenuEntries = () => {
const setActionMenuEntries = useSetRecoilComponentStateV2(
const actionMenuEntryState = useRecoilComponentCallbackStateV2(
actionMenuEntriesComponentState,
);
const addActionMenuEntry = (entry: ActionMenuEntry) => {
setActionMenuEntries(
(prevEntries) => new Map([...prevEntries, [entry.key, entry]]),
);
};
const addActionMenuEntry = useRecoilCallback(
({ snapshot, set }) =>
async (entryToAdd: ActionMenuEntry) => {
const currentEntries = snapshot
.getLoadable(actionMenuEntryState)
.getValue();
const removeActionMenuEntry = (key: string) => {
setActionMenuEntries((prevEntries) => {
const newMap = new Map(prevEntries);
newMap.delete(key);
return newMap;
});
};
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,