forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Speed up RecordTableCell by 5x (twentyhq#5023)
Improved table cell performances by putting all hooks from RecordTableCell in RecordTableContext and RecordTable component, so that each cell now only subscribes to a reference to those hooks' returned function. We couldn't do memoization here since the problem is not to memoize between re-renders but to share the same function reference between hundreds of different components, so a context it the fastest way for this. I had to refactor the hooks a little bit so that they take as arguments what was previously taken from the cell's context.
- Loading branch information
1 parent
3e60c00
commit 86afc34
Showing
19 changed files
with
734 additions
and
108 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/twenty-front/src/modules/object-record/record-field/hooks/useInitDraftValueV2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { isUndefined } from '@sniptt/guards'; | ||
import { useRecoilCallback } from 'recoil'; | ||
|
||
import { recordFieldInputDraftValueComponentSelector } from '@/object-record/record-field/states/selectors/recordFieldInputDraftValueComponentSelector'; | ||
import { FieldDefinition } from '@/object-record/record-field/types/FieldDefinition'; | ||
import { FieldInputDraftValue } from '@/object-record/record-field/types/FieldInputDraftValue'; | ||
import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { computeDraftValueFromFieldValue } from '@/object-record/record-field/utils/computeDraftValueFromFieldValue'; | ||
import { computeDraftValueFromString } from '@/object-record/record-field/utils/computeDraftValueFromString'; | ||
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector'; | ||
import { extractComponentSelector } from '@/ui/utilities/state/component-state/utils/extractComponentSelector'; | ||
|
||
export const useInitDraftValueV2 = <FieldValue>() => { | ||
return useRecoilCallback( | ||
({ set, snapshot }) => | ||
({ | ||
value, | ||
entityId, | ||
fieldDefinition, | ||
}: { | ||
value?: string; | ||
entityId: string; | ||
fieldDefinition: FieldDefinition<FieldMetadata>; | ||
}) => { | ||
const recordFieldInputScopeId = `${entityId}-${fieldDefinition?.metadata?.fieldName}-scope`; | ||
|
||
const getDraftValueSelector = extractComponentSelector< | ||
FieldInputDraftValue<FieldValue> | undefined | ||
>(recordFieldInputDraftValueComponentSelector, recordFieldInputScopeId); | ||
|
||
const recordFieldValue = snapshot | ||
.getLoadable( | ||
recordStoreFamilySelector<FieldValue>({ | ||
recordId: entityId, | ||
fieldName: fieldDefinition.metadata.fieldName, | ||
}), | ||
) | ||
.getValue(); | ||
|
||
if (isUndefined(value)) { | ||
set( | ||
getDraftValueSelector(), | ||
computeDraftValueFromFieldValue<FieldValue>({ | ||
fieldValue: recordFieldValue, | ||
fieldDefinition, | ||
}), | ||
); | ||
} else { | ||
set( | ||
getDraftValueSelector(), | ||
computeDraftValueFromString<FieldValue>({ value, fieldDefinition }), | ||
); | ||
} | ||
}, | ||
[], | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ont/src/modules/object-record/record-table/hooks/internal/useHandleContainerMouseEnter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useRecoilCallback } from 'recoil'; | ||
|
||
import { useMoveSoftFocusToCellOnHoverV2 } from '@/object-record/record-table/record-table-cell/hooks/useMoveSoftFocusToCellOnHoverV2'; | ||
import { currentTableCellInEditModePositionComponentState } from '@/object-record/record-table/states/currentTableCellInEditModePositionComponentState'; | ||
import { isSoftFocusUsingMouseState } from '@/object-record/record-table/states/isSoftFocusUsingMouseState'; | ||
import { isTableCellInEditModeComponentFamilyState } from '@/object-record/record-table/states/isTableCellInEditModeComponentFamilyState'; | ||
import { TableCellPosition } from '@/object-record/record-table/types/TableCellPosition'; | ||
import { getScopeIdFromComponentId } from '@/ui/utilities/recoil-scope/utils/getScopeIdFromComponentId'; | ||
import { getSnapshotValue } from '@/ui/utilities/recoil-scope/utils/getSnapshotValue'; | ||
import { extractComponentFamilyState } from '@/ui/utilities/state/component-state/utils/extractComponentFamilyState'; | ||
import { extractComponentState } from '@/ui/utilities/state/component-state/utils/extractComponentState'; | ||
|
||
export type HandleContainerMouseEnterArgs = { | ||
isHovered: boolean; | ||
setIsHovered: React.Dispatch<React.SetStateAction<boolean>>; | ||
cellPosition: TableCellPosition; | ||
}; | ||
|
||
export const useHandleContainerMouseEnter = ({ | ||
recordTableId, | ||
}: { | ||
recordTableId: string; | ||
}) => { | ||
const tableScopeId = getScopeIdFromComponentId(recordTableId); | ||
|
||
const { moveSoftFocusToCell } = | ||
useMoveSoftFocusToCellOnHoverV2(recordTableId); | ||
|
||
const handleContainerMouseEnter = useRecoilCallback( | ||
({ snapshot, set }) => | ||
({ | ||
isHovered, | ||
setIsHovered, | ||
cellPosition, | ||
}: HandleContainerMouseEnterArgs) => { | ||
const currentTableCellInEditModePositionState = extractComponentState( | ||
currentTableCellInEditModePositionComponentState, | ||
tableScopeId, | ||
); | ||
|
||
const currentTableCellInEditModePosition = getSnapshotValue( | ||
snapshot, | ||
currentTableCellInEditModePositionState, | ||
); | ||
|
||
const isTableCellInEditModeFamilyState = extractComponentFamilyState( | ||
isTableCellInEditModeComponentFamilyState, | ||
tableScopeId, | ||
); | ||
|
||
const isSomeCellInEditMode = getSnapshotValue( | ||
snapshot, | ||
isTableCellInEditModeFamilyState(currentTableCellInEditModePosition), | ||
); | ||
|
||
if (!isHovered && !isSomeCellInEditMode) { | ||
setIsHovered(true); | ||
moveSoftFocusToCell(cellPosition); | ||
set(isSoftFocusUsingMouseState, true); | ||
} | ||
}, | ||
[tableScopeId, moveSoftFocusToCell], | ||
); | ||
|
||
return { | ||
handleContainerMouseEnter, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.