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