Fix/table rerenders (#609)

* Fixed top bar rerenders

* Fixed rerender on editable cell

* Fix lint

* asd

* Fix

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Lucas Bordeau
2023-07-12 05:51:24 +02:00
committed by GitHub
parent b5de2abd48
commit 5e0e449e4c
17 changed files with 211 additions and 126 deletions

View File

@ -1,4 +1,4 @@
import { useRecoilValue } from 'recoil';
import { useRecoilCallback } from 'recoil';
import { useSetHotkeysScope } from '@/hotkeys/hooks/useSetHotkeysScope';
import { currentHotkeysScopeState } from '@/hotkeys/states/internal/currentHotkeysScopeState';
@ -11,29 +11,39 @@ import { useCloseCurrentCellInEditMode } from './useClearCellInEditMode';
import { useDisableSoftFocus } from './useDisableSoftFocus';
export function useLeaveTableFocus() {
const currentHotkeysScope = useRecoilValue(currentHotkeysScopeState);
const disableSoftFocus = useDisableSoftFocus();
const closeCurrentCellInEditMode = useCloseCurrentCellInEditMode();
const setHotkeysScope = useSetHotkeysScope();
const isSoftFocusActive = useRecoilValue(isSoftFocusActiveState);
const isSomeInputInEditMode = useRecoilValue(isSomeInputInEditModeState);
return useRecoilCallback(
({ snapshot }) =>
() => {
const isSoftFocusActive = snapshot
.getLoadable(isSoftFocusActiveState)
.valueOrThrow();
return async function leaveTableFocus() {
// TODO: replace with scope ancestor ?
if (!isSoftFocusActive && !isSomeInputInEditMode) {
return;
}
const isSomeInputInEditMode = snapshot
.getLoadable(isSomeInputInEditModeState)
.valueOrThrow();
if (currentHotkeysScope?.scope === InternalHotkeysScope.Table) {
return;
}
const currentHotkeysScope = snapshot
.getLoadable(currentHotkeysScopeState)
.valueOrThrow();
closeCurrentCellInEditMode();
disableSoftFocus();
if (!isSoftFocusActive && !isSomeInputInEditMode) {
return;
}
setHotkeysScope(InternalHotkeysScope.Table, { goto: true });
};
if (currentHotkeysScope?.scope === InternalHotkeysScope.Table) {
return;
}
closeCurrentCellInEditMode();
disableSoftFocus();
setHotkeysScope(InternalHotkeysScope.Table, { goto: true });
},
[setHotkeysScope, closeCurrentCellInEditMode, disableSoftFocus],
);
}