Skip to content

The open alternative to Salesforce, designed for AI.

Render the virtualized record table from row slots (#28294)

Charles Bochet091659c20fdc
diff --git a/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts b/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts
index f53aee502e0..f8aeaab76a4 100644
--- a/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts
+++ b/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts
@@ -5,6 +5,7 @@ import {
} from 'twenty-shared/types';
import { isDefined, isPlainObject } from 'twenty-shared/utils';
+import { isAscendingOrder } from '@/object-record/graphql/utils/isAscendingOrder';
import { isOrderByDirection } from '@/object-record/graphql/utils/isOrderByDirection';
type CursorOrderByField = {
@@ -13,9 +14,6 @@ type CursorOrderByField = {
subFieldName?: string;
};
-const isAscendingOrder = (direction: string): boolean =>
- direction === 'AscNullsFirst' || direction === 'AscNullsLast';
-
const computeOperator = (
isAscending: boolean,
isForwardPagination: boolean,
diff --git a/packages/twenty-front/src/modules/object-record/graphql/utils/isAscendingOrder.ts b/packages/twenty-front/src/modules/object-record/graphql/utils/isAscendingOrder.ts
new file mode 100644
index 00000000000..354cd1cf1f3
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/graphql/utils/isAscendingOrder.ts
@@ -0,0 +1,2 @@
+export const isAscendingOrder = (direction: string): boolean =>
+ direction === 'AscNullsFirst' || direction === 'AscNullsLast';
diff --git a/packages/twenty-front/src/modules/object-record/graphql/utils/isOrderByDirection.ts b/packages/twenty-front/src/modules/object-record/graphql/utils/isOrderByDirection.ts
index 3c7400c3d19..a2018c62415 100644
--- a/packages/twenty-front/src/modules/object-record/graphql/utils/isOrderByDirection.ts
+++ b/packages/twenty-front/src/modules/object-record/graphql/utils/isOrderByDirection.ts
@@ -1,4 +1,5 @@
import { isNonEmptyString } from '@sniptt/guards';
+import { type OrderBy } from 'twenty-shared/types';
const ORDER_DIRECTIONS = new Set([
'AscNullsFirst',
@@ -7,5 +8,5 @@ const ORDER_DIRECTIONS = new Set([
'DescNullsLast',
]);
-export const isOrderByDirection = (value: unknown): value is string =>
+export const isOrderByDirection = (value: unknown): value is OrderBy =>
isNonEmptyString(value) && ORDER_DIRECTIONS.has(value);
diff --git a/packages/twenty-front/src/modules/object-record/record-drag/hooks/useEndRecordDrag.ts b/packages/twenty-front/src/modules/object-record/record-drag/hooks/useEndRecordDrag.ts
index efe2cfe6d4c..eafe2a3052f 100644
--- a/packages/twenty-front/src/modules/object-record/record-drag/hooks/useEndRecordDrag.ts
+++ b/packages/twenty-front/src/modules/object-record/record-drag/hooks/useEndRecordDrag.ts
@@ -3,6 +3,7 @@ import { useCallback } from 'react';
import { draggedRecordIdsComponentState } from '@/object-record/record-drag/states/draggedRecordIdsComponentState';
import { isDraggingRecordComponentState } from '@/object-record/record-drag/states/isDraggingRecordComponentState';
+import { isRecordIdPrimaryDragComponentFamilyState } from '@/object-record/record-drag/states/isRecordIdPrimaryDragComponentFamilyState';
import { isRecordIdSecondaryDragMultipleComponentFamilyState } from '@/object-record/record-drag/states/isRecordIdSecondaryDragMultipleComponentFamilyState';
import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
@@ -20,6 +21,10 @@ export const useEndRecordDrag = () => {
useAtomComponentFamilyStateCallbackState(
isRecordIdSecondaryDragMultipleComponentFamilyState,
);
+ const isRecordIdPrimaryDragCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ isRecordIdPrimaryDragComponentFamilyState,
+ );
const endRecordDrag = useCallback(() => {
for (const recordId of store.get(draggedRecordIdsCallbackState)) {
@@ -27,6 +32,7 @@ export const useEndRecordDrag = () => {
isRecordIdSecondaryDragMultipleCallbackState({ recordId }),
false,
);
+ store.set(isRecordIdPrimaryDragCallbackState({ recordId }), false);
}
store.set(draggedRecordIdsCallbackState, []);
@@ -36,6 +42,7 @@ export const useEndRecordDrag = () => {
isDraggingRecordCallbackState,
draggedRecordIdsCallbackState,
isRecordIdSecondaryDragMultipleCallbackState,
+ isRecordIdPrimaryDragCallbackState,
]);
return { endRecordDrag };
diff --git a/packages/twenty-front/src/modules/object-record/record-drag/hooks/useStartRecordDrag.ts b/packages/twenty-front/src/modules/object-record/record-drag/hooks/useStartRecordDrag.ts
index 158bf2384d6..b0e0e243d5a 100644
--- a/packages/twenty-front/src/modules/object-record/record-drag/hooks/useStartRecordDrag.ts
+++ b/packages/twenty-front/src/modules/object-record/record-drag/hooks/useStartRecordDrag.ts
@@ -3,6 +3,7 @@ import { useCallback } from 'react';
import { draggedRecordIdsComponentState } from '@/object-record/record-drag/states/draggedRecordIdsComponentState';
import { isDraggingRecordComponentState } from '@/object-record/record-drag/states/isDraggingRecordComponentState';
+import { isRecordIdPrimaryDragComponentFamilyState } from '@/object-record/record-drag/states/isRecordIdPrimaryDragComponentFamilyState';
import { isRecordIdSecondaryDragMultipleComponentFamilyState } from '@/object-record/record-drag/states/isRecordIdSecondaryDragMultipleComponentFamilyState';
import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
@@ -20,6 +21,10 @@ export const useStartRecordDrag = () => {
useAtomComponentFamilyStateCallbackState(
isRecordIdSecondaryDragMultipleComponentFamilyState,
);
+ const isRecordIdPrimaryDragCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ isRecordIdPrimaryDragComponentFamilyState,
+ );
const startRecordDrag = useCallback(
(draggedRecordId: string, selectedRecordIds: string[]) => {
@@ -32,6 +37,10 @@ export const useStartRecordDrag = () => {
store.set(isDraggingRecordCallbackState, true);
store.set(draggedRecordIdsCallbackState, draggedRecordIds);
+ store.set(
+ isRecordIdPrimaryDragCallbackState({ recordId: draggedRecordId }),
+ true,
+ );
for (const recordId of draggedRecordIds) {
if (recordId !== draggedRecordId) {
@@ -47,6 +56,7 @@ export const useStartRecordDrag = () => {
isDraggingRecordCallbackState,
draggedRecordIdsCallbackState,
isRecordIdSecondaryDragMultipleCallbackState,
+ isRecordIdPrimaryDragCallbackState,
],
);
diff --git a/packages/twenty-front/src/modules/object-record/record-drag/hooks/useTriggerTableWithoutGroupDragAndDropOptimisticUpdate.ts b/packages/twenty-front/src/modules/object-record/record-drag/hooks/useTriggerTableWithoutGroupDragAndDropOptimisticUpdate.ts
index a8138df6c07..c1ee6daa980 100644
--- a/packages/twenty-front/src/modules/object-record/record-drag/hooks/useTriggerTableWithoutGroupDragAndDropOptimisticUpdate.ts
+++ b/packages/twenty-front/src/modules/object-record/record-drag/hooks/useTriggerTableWithoutGroupDragAndDropOptimisticUpdate.ts
@@ -2,71 +2,72 @@ import { useCallback } from 'react';
import { useStore } from 'jotai';
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
-import { useLoadRecordsToVirtualRows } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsToVirtualRows';
+import { useLoadRecordsAtRowIndexes } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsAtRowIndexes';
import { lastScrollPositionComponentState } from '@/object-record/record-table/virtualization/states/lastScrollPositionComponentState';
-import { recordIdByRealIndexComponentFamilySelector } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentFamilySelector';
+import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
+import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
import { getVirtualizationOverscanWindow } from '@/object-record/record-table/virtualization/utils/getVirtualizationOverscanWindow';
import { type RecordWithPosition } from '@/object-record/utils/computeNewPositionOfDraggedRecord';
import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
-import { useAtomComponentFamilySelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorCallbackState';
+import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
import { findById, isDefined } from 'twenty-shared/utils';
import { sortByProperty } from '~/utils/array/sortByProperty';
export const useTriggerTableWithoutGroupDragAndDropOptimisticUpdate = () => {
const store = useStore();
- const recordIdByRealIndexCallbackSelector =
- useAtomComponentFamilySelectorCallbackState(
- recordIdByRealIndexComponentFamilySelector,
+ const recordIdsByGroupCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ recordIndexRecordIdsByGroupComponentFamilyState,
);
const lastScrollPositionCallbackState = useAtomComponentStateCallbackState(
lastScrollPositionComponentState,
);
- const { scrollWrapperHTMLElement } = useScrollWrapperHTMLElement();
- const totalNumberOfRecordsToVirtualizeCallbackState =
- useAtomComponentStateCallbackState(
- totalNumberOfRecordsToVirtualizeComponentState,
- );
+ const { getScrollWrapperElement } = useScrollWrapperHTMLElement();
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
+ );
- const { loadRecordsToVirtualRows } = useLoadRecordsToVirtualRows();
+ const { loadRecordsAtRowIndexes } = useLoadRecordsAtRowIndexes();
const triggerTableWithoutGroupDragAndDropOptimisticUpdate = useCallback(
(updatedRecords: RecordWithPosition[]) => {
const tableScrollWrapperHeight =
- scrollWrapperHTMLElement?.clientHeight ?? 0;
+ getScrollWrapperElement().scrollWrapperElement?.clientHeight ?? 0;
const lastScrollPosition = store.get(lastScrollPositionCallbackState);
- const totalNumberOfRecordsToVirtualize =
- store.get(totalNumberOfRecordsToVirtualizeCallbackState) ?? 0;
+ const totalNumberOfRows = store.get(totalNumberOfRowsCallbackState) ?? 0;
- const { firstRealIndexInOverscanWindow, lastRealIndexInOverscanWindow } =
- getVirtualizationOverscanWindow(
- lastScrollPosition,
- tableScrollWrapperHeight,
- totalNumberOfRecordsToVirtualize,
- );
+ const { firstRowIndexInOverscanWindow, lastRowIndexInOverscanWindow } =
+ getVirtualizationOverscanWindow({
+ scrollPosition: lastScrollPosition,
+ scrollWrapperHeight: tableScrollWrapperHeight,
+ totalNumberOfRows,
+ });
+
+ const recordIds = store.get(
+ recordIdsByGroupCallbackState(NO_RECORD_GROUP_FAMILY_KEY),
+ );
const recordsInOverscanWindowToReorder: RecordWithPosition[] = [];
for (
- let realIndex = firstRealIndexInOverscanWindow;
- realIndex <= lastRealIndexInOverscanWindow;
- realIndex++
+ let rowIndex = firstRowIndexInOverscanWindow;
+ rowIndex <= lastRowIndexInOverscanWindow;
+ rowIndex++
) {
- const recordIdAtRealIndex = store.get(
- recordIdByRealIndexCallbackSelector(realIndex),
- );
+ const recordIdAtRowIndex = recordIds[rowIndex];
- if (!isDefined(recordIdAtRealIndex)) {
+ if (!isDefined(recordIdAtRowIndex)) {
continue;
}
const correspondingRecordInStore = store.get(
- recordStoreFamilyState.atomFamily(recordIdAtRealIndex),
+ recordStoreFamilyState.atomFamily(recordIdAtRowIndex),
);
if (
@@ -100,18 +101,18 @@ export const useTriggerTableWithoutGroupDragAndDropOptimisticUpdate = () => {
const reorderedRecordsWithPosition =
recordsInOverscanWindowToReorder.toSorted(sortByProperty('position'));
- loadRecordsToVirtualRows({
+ loadRecordsAtRowIndexes({
records: reorderedRecordsWithPosition as any[],
- startingRealIndex: firstRealIndexInOverscanWindow,
+ startingRowIndex: firstRowIndexInOverscanWindow,
});
},
[
lastScrollPositionCallbackState,
- loadRecordsToVirtualRows,
- recordIdByRealIndexCallbackSelector,
- scrollWrapperHTMLElement?.clientHeight,
+ loadRecordsAtRowIndexes,
+ recordIdsByGroupCallbackState,
+ getScrollWrapperElement,
store,
- totalNumberOfRecordsToVirtualizeCallbackState,
+ totalNumberOfRowsCallbackState,
],
);
diff --git a/packages/twenty-front/src/modules/object-record/record-drag/states/isRecordIdPrimaryDragComponentFamilyState.ts b/packages/twenty-front/src/modules/object-record/record-drag/states/isRecordIdPrimaryDragComponentFamilyState.ts
new file mode 100644
index 00000000000..0a309806c6e
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-drag/states/isRecordIdPrimaryDragComponentFamilyState.ts
@@ -0,0 +1,9 @@
+import { RecordSelectionComponentInstanceContext } from '@/object-record/record-selection/states/contexts/RecordSelectionComponentInstanceContext';
+import { createAtomComponentFamilyState } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilyState';
+
+export const isRecordIdPrimaryDragComponentFamilyState =
+ createAtomComponentFamilyState<boolean, { recordId: string }>({
+ key: 'isRecordIdPrimaryDragComponentFamilyState',
+ defaultValue: false,
+ componentInstanceContext: RecordSelectionComponentInstanceContext,
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableContent.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableContent.tsx
index af7785138ea..0f174f1a2d8 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableContent.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableContent.tsx
@@ -18,12 +18,14 @@ import { RecordTableHeader } from '@/object-record/record-table/record-table-hea
import { useMoveHoverToCurrentCell } from '@/object-record/record-table/record-table-cell/hooks/useMoveHoverToCurrentCell';
import { RecordSelectionDragSelect } from '@/object-record/record-selection/components/RecordSelectionDragSelect';
import { recordTableHoverPositionComponentState } from '@/object-record/record-table/states/recordTableHoverPositionComponentState';
+import { recordTableRowIndexByRecordIdComponentSelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexByRecordIdComponentSelector';
import { isSomeCellInEditModeComponentSelector } from '@/object-record/record-table/states/selectors/isSomeCellInEditModeComponentSelector';
import { useAtomComponentSelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorCallbackState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { styled } from '@linaria/react';
import { useStore } from 'jotai';
+import { isDefined } from 'twenty-shared/utils';
import { useCallback, useMemo, useRef, useState } from 'react';
const StyledTableContainer = styled.div`
@@ -88,6 +90,10 @@ export const RecordTableContent = ({
const { moveHoverToCurrentCell } = useMoveHoverToCurrentCell(recordTableId);
+ const rowIndexByRecordIdCallbackState = useAtomComponentSelectorCallbackState(
+ recordTableRowIndexByRecordIdComponentSelector,
+ );
+
const handleDelegatedMouseMove = useCallback(
(event: React.MouseEvent) => {
const target = event.target as HTMLElement;
@@ -100,15 +106,19 @@ export const RecordTableContent = ({
}
const column = Number(cellElement.dataset.recordTableCol);
- const row = Number(cellElement.dataset.recordTableRow);
+ const recordId = cellElement.dataset.recordTableRecordId;
+
+ const row = isDefined(recordId)
+ ? store.get(rowIndexByRecordIdCallbackState).get(recordId)
+ : undefined;
- if (isNaN(column) || isNaN(row)) {
+ if (isNaN(column) || !isDefined(row)) {
return;
}
moveHoverToCurrentCell({ column, row });
},
- [moveHoverToCurrentCell],
+ [moveHoverToCurrentCell, rowIndexByRecordIdCallbackState, store],
);
const isRecordTableDragColumnHidden = useAtomComponentStateValue(
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupAddNew.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupAddNew.tsx
index 8e96dc70676..285bbd28ad9 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupAddNew.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupAddNew.tsx
@@ -9,8 +9,12 @@ import { RecordTableWidgetJunctionAddNewRow } from '@/object-record/record-table
import { RecordTableWidgetNestedRelationAddNewRow } from '@/object-record/record-table-widget/components/RecordTableWidgetNestedRelationAddNewRow';
import { RecordTableWidgetContext } from '@/object-record/record-table-widget/contexts/RecordTableWidgetContext';
import { canCreateRecordsForObjectMetadataItem } from '@/object-record/utils/canCreateRecordsForObjectMetadataItem';
-import { useLoadRecordsToVirtualRows } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsToVirtualRows';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { useLoadRecordsAtRowIndexes } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsAtRowIndexes';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
+import { recordTableRowIndexByRecordIdComponentSelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexByRecordIdComponentSelector';
+import { useAtomComponentSelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorCallbackState';
+import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
+import { useStore } from 'jotai';
import { useAtomComponentSelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorValue';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { t } from '@lingui/core/macro';
@@ -43,11 +47,17 @@ export const RecordTableNoRecordGroupAddNew = () => {
hasAnySoftDeleteFilterOnViewComponentSelector,
);
- const totalNumberOfRecordsToVirtualize = useAtomComponentStateValue(
- totalNumberOfRecordsToVirtualizeComponentState,
+ const store = useStore();
+
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
+ );
+
+ const rowIndexByRecordIdCallbackState = useAtomComponentSelectorCallbackState(
+ recordTableRowIndexByRecordIdComponentSelector,
);
- const { loadRecordsToVirtualRows } = useLoadRecordsToVirtualRows();
+ const { loadRecordsAtRowIndexes } = useLoadRecordsAtRowIndexes();
const { upsertRecordsInStore } = useUpsertRecordsInStore();
const handleCreateRecord = useCallback(
@@ -63,18 +73,26 @@ export const RecordTableNoRecordGroupAddNew = () => {
upsertRecordsInStore({ partialRecords: [createdRecord] });
- if (isDefined(totalNumberOfRecordsToVirtualize)) {
- loadRecordsToVirtualRows({
+ const isAlreadyPlaced = store
+ .get(rowIndexByRecordIdCallbackState)
+ .has(createdRecord.id);
+
+ const totalNumberOfRows = store.get(totalNumberOfRowsCallbackState);
+
+ if (!isAlreadyPlaced && isDefined(totalNumberOfRows)) {
+ loadRecordsAtRowIndexes({
records: [createdRecord],
- startingRealIndex: totalNumberOfRecordsToVirtualize,
+ startingRowIndex: totalNumberOfRows,
});
}
},
[
createNewIndexRecord,
upsertRecordsInStore,
- loadRecordsToVirtualRows,
- totalNumberOfRecordsToVirtualize,
+ loadRecordsAtRowIndexes,
+ rowIndexByRecordIdCallbackState,
+ store,
+ totalNumberOfRowsCallbackState,
],
);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupAddNewDropZone.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupAddNewDropZone.tsx
new file mode 100644
index 00000000000..a0fcda407b1
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupAddNewDropZone.tsx
@@ -0,0 +1,21 @@
+import { RecordDragEndDropZone } from '@/object-record/record-drag/components/RecordDragEndDropZone';
+import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
+import { RecordTableNoRecordGroupAddNew } from '@/object-record/record-table/components/RecordTableNoRecordGroupAddNew';
+import { RECORD_TABLE_ROW_DND_TYPE } from '@/object-record/record-table/constants/RecordTableRowDndType';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
+import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
+
+export const RecordTableNoRecordGroupAddNewDropZone = () => {
+ const totalNumberOfRows =
+ useAtomComponentStateValue(totalNumberOfRowsComponentState) ?? 0;
+
+ return (
+ <RecordDragEndDropZone
+ droppableId={NO_RECORD_GROUP_FAMILY_KEY}
+ dndType={RECORD_TABLE_ROW_DND_TYPE}
+ index={totalNumberOfRows}
+ >
+ <RecordTableNoRecordGroupAddNew />
+ </RecordDragEndDropZone>
+ );
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupRows.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupRows.tsx
index 21841ae8b62..58a4aee33f4 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupRows.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableNoRecordGroupRows.tsx
@@ -1,16 +1,10 @@
import { styled } from '@linaria/react';
-import { getContiguousIncrementalValues } from 'twenty-shared/utils';
-import { RecordTableNoRecordGroupAddNew } from '@/object-record/record-table/components/RecordTableNoRecordGroupAddNew';
-import { RecordDragEndDropZone } from '@/object-record/record-drag/components/RecordDragEndDropZone';
-import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
-import { RECORD_TABLE_ROW_DND_TYPE } from '@/object-record/record-table/constants/RecordTableRowDndType';
-import { RecordTableRowVirtualizedContainer } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedContainer';
+import { RecordTableNoRecordGroupAddNewDropZone } from '@/object-record/record-table/components/RecordTableNoRecordGroupAddNewDropZone';
+import { RecordTableRowSlot } from '@/object-record/record-table/virtualization/components/RecordTableRowSlot';
import { RecordTableVirtualizedBodyPlaceholder } from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedBodyPlaceholder';
import { RecordTableVirtualizedDebugHelper } from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedDebugHelper';
-import { NUMBER_OF_VIRTUALIZED_ROWS } from '@/object-record/record-table/virtualization/constants/NumberOfVirtualizedRows';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
-import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
+import { ROW_SLOTS } from '@/object-record/record-table/virtualization/constants/RowSlots';
const StyledNoRecordGroupContainer = styled.div`
display: flex;
@@ -18,38 +12,13 @@ const StyledNoRecordGroupContainer = styled.div`
width: 100%;
`;
-export const RecordTableNoRecordGroupRows = () => {
- const totalNumberOfRecordsToVirtualize =
- useAtomComponentStateValue(
- totalNumberOfRecordsToVirtualizeComponentState,
- ) ?? 0;
-
- const numberOfRows = Math.min(
- totalNumberOfRecordsToVirtualize,
- NUMBER_OF_VIRTUALIZED_ROWS,
- );
-
- const virtualRowIndices = getContiguousIncrementalValues(numberOfRows);
-
- return (
- <StyledNoRecordGroupContainer>
- <RecordTableVirtualizedBodyPlaceholder />
- {virtualRowIndices.map((virtualRowIndex) => {
- return (
- <RecordTableRowVirtualizedContainer
- key={virtualRowIndex}
- virtualIndex={virtualRowIndex}
- />
- );
- })}
- <RecordDragEndDropZone
- droppableId={NO_RECORD_GROUP_FAMILY_KEY}
- dndType={RECORD_TABLE_ROW_DND_TYPE}
- index={totalNumberOfRecordsToVirtualize}
- >
- <RecordTableNoRecordGroupAddNew />
- </RecordDragEndDropZone>
- <RecordTableVirtualizedDebugHelper />
- </StyledNoRecordGroupContainer>
- );
-};
+export const RecordTableNoRecordGroupRows = () => (
+ <StyledNoRecordGroupContainer>
+ <RecordTableVirtualizedBodyPlaceholder />
+ {ROW_SLOTS.map((rowSlot) => (
+ <RecordTableRowSlot key={rowSlot} rowSlot={rowSlot} />
+ ))}
+ <RecordTableNoRecordGroupAddNewDropZone />
+ <RecordTableVirtualizedDebugHelper />
+ </StyledNoRecordGroupContainer>
+);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRecordGroupRows.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRecordGroupRows.tsx
index aea88aeba02..4bf3ea924bf 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRecordGroupRows.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableRecordGroupRows.tsx
@@ -2,7 +2,6 @@ import { RecordDragEndDropZone } from '@/object-record/record-drag/components/Re
import { useCurrentRecordGroupId } from '@/object-record/record-group/hooks/useCurrentRecordGroupId';
import { useShouldHideRecordGroup } from '@/object-record/record-group/hooks/useShouldHideRecordGroup';
import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
-import { recordIndexAllRecordIdsComponentSelector } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
import { RECORD_TABLE_ROW_DND_TYPE } from '@/object-record/record-table/constants/RecordTableRowDndType';
import { RecordTableAggregateFooter } from '@/object-record/record-table/record-table-footer/components/RecordTableAggregateFooter';
import { RecordTableRow } from '@/object-record/record-table/record-table-row/components/RecordTableRow';
@@ -10,19 +9,12 @@ import { RecordTableRecordGroupSectionAddNew } from '@/object-record/record-tabl
import { RecordTableRecordGroupSectionLoadMore } from '@/object-record/record-table/record-table-section/components/RecordTableRecordGroupSectionLoadMore';
import { isRecordGroupTableSectionToggledComponentState } from '@/object-record/record-table/record-table-section/states/isRecordGroupTableSectionToggledComponentState';
import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
-import { useAtomComponentSelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorValue';
-import { useMemo } from 'react';
-import { isDefined } from 'twenty-shared/utils';
export const RecordTableRecordGroupRows = () => {
const currentRecordGroupId = useCurrentRecordGroupId();
const shouldHide = useShouldHideRecordGroup(currentRecordGroupId);
- const allRecordIds = useAtomComponentSelectorValue(
- recordIndexAllRecordIdsComponentSelector,
- );
-
const recordIndexRecordIdsByGroup = useAtomComponentFamilyStateValue(
recordIndexRecordIdsByGroupComponentFamilyState,
currentRecordGroupId,
@@ -33,11 +25,6 @@ export const RecordTableRecordGroupRows = () => {
currentRecordGroupId,
);
- const rowIndexMap = useMemo(
- () => new Map(allRecordIds.map((recordId, index) => [recordId, index])),
- [allRecordIds],
- );
-
if (shouldHide) {
return null;
}
@@ -48,22 +35,13 @@ export const RecordTableRecordGroupRows = () => {
return (
<>
- {recordIndexRecordIdsByGroup.map((recordId, rowIndexInGroup) => {
- const rowIndex = rowIndexMap.get(recordId);
-
- if (!isDefined(rowIndex)) {
- return null;
- }
-
- return (
- <RecordTableRow
- key={recordId}
- recordId={recordId}
- rowIndexForFocus={rowIndex}
- rowIndexForDrag={rowIndexInGroup}
- />
- );
- })}
+ {recordIndexRecordIdsByGroup.map((recordId, rowIndexInGroup) => (
+ <RecordTableRow
+ key={recordId}
+ recordId={recordId}
+ rowIndexForDrag={rowIndexInGroup}
+ />
+ ))}
<RecordDragEndDropZone
droppableId={currentRecordGroupId}
dndType={RECORD_TABLE_ROW_DND_TYPE}
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedCellEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedCellEffect.tsx
index 928f9c1bdb4..d9fc55e624f 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedCellEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedCellEffect.tsx
@@ -4,7 +4,10 @@ import { isRecordTableCellFocusActiveComponentState } from '@/object-record/reco
import { recordTableFocusPositionComponentState } from '@/object-record/record-table/states/recordTableFocusPositionComponentState';
import { getRecordTableCellId } from '@/object-record/record-table/utils/getRecordTableCellId';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
+import { useStore } from 'jotai';
import { useEffect } from 'react';
+import { recordIndexAllRecordIdsComponentSelector } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
+import { useAtomComponentSelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorCallbackState';
import { isDefined } from 'twenty-shared/utils';
export const RecordTableScrollToFocusedCellEffect = () => {
@@ -15,6 +18,13 @@ export const RecordTableScrollToFocusedCellEffect = () => {
recordTableId,
);
+ const store = useStore();
+
+ const allRecordIdsCallbackState = useAtomComponentSelectorCallbackState(
+ recordIndexAllRecordIdsComponentSelector,
+ recordTableId,
+ );
+
const recordTableFocusPosition = useAtomComponentStateValue(
recordTableFocusPositionComponentState,
recordTableId,
@@ -29,12 +39,20 @@ export const RecordTableScrollToFocusedCellEffect = () => {
return;
}
+ const recordId = store.get(allRecordIdsCallbackState)[
+ recordTableFocusPosition.row
+ ];
+
+ if (!isDefined(recordId)) {
+ return;
+ }
+
const focusElement = document.getElementById(
- getRecordTableCellId(
+ getRecordTableCellId({
recordTableId,
- recordTableFocusPosition.column,
- recordTableFocusPosition.row,
- ),
+ columnIndex: recordTableFocusPosition.column,
+ recordId,
+ }),
);
if (!focusElement) {
@@ -45,10 +63,10 @@ export const RecordTableScrollToFocusedCellEffect = () => {
if (isSecondColumn) {
const checkBoxColumnCell = document.getElementById(
- getRecordTableCellId(recordTableId, 0, 0),
+ getRecordTableCellId({ recordTableId, columnIndex: 0, recordId }),
);
const firstColumnCell = document.getElementById(
- getRecordTableCellId(recordTableId, 1, 0),
+ getRecordTableCellId({ recordTableId, columnIndex: 1, recordId }),
);
if (isDefined(checkBoxColumnCell) && isDefined(firstColumnCell)) {
@@ -69,7 +87,13 @@ export const RecordTableScrollToFocusedCellEffect = () => {
focusElement.style.scrollMarginBottom = '';
}
};
- }, [recordTableId, recordTableFocusPosition, isRecordTableCellFocusActive]);
+ }, [
+ recordTableId,
+ recordTableFocusPosition,
+ isRecordTableCellFocusActive,
+ allRecordIdsCallbackState,
+ store,
+ ]);
return null;
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedRowEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedRowEffect.tsx
index 0f89f530879..be2670bfc13 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedRowEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/RecordTableScrollToFocusedRowEffect.tsx
@@ -43,7 +43,7 @@ export const RecordTableScrollToFocusedRowEffect = () => {
}
const focusElement = document.getElementById(
- getRecordTableCellId(recordTableId, 0, focusedRecordTableRowIndex),
+ getRecordTableCellId({ recordTableId, columnIndex: 0, recordId }),
);
if (!focusElement) {
diff --git a/packages/twenty-front/src/modules/object-record/record-table/components/__stories__/perf/RecordTableCell.perf.stories.tsx b/packages/twenty-front/src/modules/object-record/record-table/components/__stories__/perf/RecordTableCell.perf.stories.tsx
index 15048b9d7b1..58d2068eb09 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/components/__stories__/perf/RecordTableCell.perf.stories.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/components/__stories__/perf/RecordTableCell.perf.stories.tsx
@@ -201,7 +201,6 @@ const meta: Meta = {
objectNameSingular:
mockPerformance.entityValue.__typename.toLocaleLowerCase(),
recordId: mockPerformance.recordId,
- rowIndex: 0,
pathToShowPage:
getBasePathToShowPage({
objectNameSingular:
@@ -217,7 +216,7 @@ const meta: Meta = {
>
<RecordTableCellContext.Provider
value={{
- cellPosition: { row: 0, column: 0 },
+ columnIndex: 0,
recordField: {
fieldMetadataItemId:
mockPerformance.fieldDefinition.fieldMetadataId,
diff --git a/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableCellContext.ts b/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableCellContext.ts
index 33da8cdbda2..fdfcbc5cc31 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableCellContext.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableCellContext.ts
@@ -1,11 +1,10 @@
import { createContext } from 'react';
import { type RecordField } from '@/object-record/record-field/types/RecordField';
-import { type TableCellPosition } from '@/object-record/record-table/types/TableCellPosition';
export type RecordTableCellContextValue = {
recordField: RecordField;
- cellPosition: TableCellPosition;
+ columnIndex: number;
};
export const RecordTableCellContext =
diff --git a/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableRowContext.ts b/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableRowContext.ts
index aef52858035..a888507dc8a 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableRowContext.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/contexts/RecordTableRowContext.ts
@@ -4,7 +4,6 @@ export type RecordTableRowContextValue = {
pathToShowPage: string;
objectNameSingular: string;
recordId: string;
- rowIndex: number;
isSelected: boolean;
isRecordReadOnly?: boolean;
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/hooks/internal/useSetRecordTableData.ts b/packages/twenty-front/src/modules/object-record/record-table/hooks/internal/useSetRecordTableData.ts
index d3f269c46d6..b98bdcac58e 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/hooks/internal/useSetRecordTableData.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/hooks/internal/useSetRecordTableData.ts
@@ -12,10 +12,8 @@ import { useFocusedRecordTableRow } from '@/object-record/record-table/hooks/use
import { useUnfocusRecordTableCell } from '@/object-record/record-table/record-table-cell/hooks/useUnfocusRecordTableCell';
import { hasUserSelectedAllRecordsComponentState } from '@/object-record/record-selection/states/hasUserSelectedAllRecordsComponentState';
import { isRecordSelectedComponentFamilyState } from '@/object-record/record-selection/states/isRecordSelectedComponentFamilyState';
-import { isRecordTableInitialLoadingComponentState } from '@/object-record/record-table/states/isRecordTableInitialLoadingComponentState';
import { recordTableHoverPositionComponentState } from '@/object-record/record-table/states/recordTableHoverPositionComponentState';
-import { recordIdByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState';
import { type ObjectRecord } from '@/object-record/types/ObjectRecord';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
@@ -50,16 +48,6 @@ export const useSetRecordTableData = ({
recordTableId,
);
- const isRecordTableInitialLoading = useAtomComponentStateCallbackState(
- isRecordTableInitialLoadingComponentState,
- recordTableId,
- );
-
- const recordIdByRealIndex = useAtomComponentStateCallbackState(
- recordIdByRealIndexComponentState,
- recordTableId,
- );
-
const setRecordTableHoverPosition = useSetAtomComponentState(
recordTableHoverPositionComponentState,
recordTableId,
@@ -124,25 +112,6 @@ export const useSetRecordTableData = ({
recordIds,
);
}
-
- const isTableInitialLoading = store.get(isRecordTableInitialLoading);
-
- if (isTableInitialLoading) {
- const currentMap = store.get(recordIdByRealIndex);
- const newMap = new Map(currentMap);
- let mapChanged = false;
-
- for (const [realIndex, recordId] of recordIds.entries()) {
- if (recordId !== currentMap.get(realIndex)) {
- newMap.set(realIndex, recordId);
- mapChanged = true;
- }
- }
-
- if (mapChanged) {
- store.set(recordIdByRealIndex, newMap);
- }
- }
}
},
[
@@ -153,8 +122,6 @@ export const useSetRecordTableData = ({
unfocusRecordTableRow,
setRecordTableHoverPosition,
isRowSelectedFamilyState,
- recordIdByRealIndex,
- isRecordTableInitialLoading,
store,
],
);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyLoading.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyLoading.tsx
index e3c2b13cc78..377c0edcf37 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyLoading.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyLoading.tsx
@@ -28,7 +28,6 @@ export const RecordTableBodyLoading = () => {
pathToShowPage: '',
objectNameSingular: '',
recordId: `${rowIndex}`,
- rowIndex,
isSelected: false,
}}
>
@@ -39,7 +38,6 @@ export const RecordTableBodyLoading = () => {
>
<RecordTableTr
recordId={`${rowIndex}`}
- focusIndex={0}
isDragging={false}
data-testid={`row-id-${rowIndex}`}
data-selectable-id={`row-id-${rowIndex}`}
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyNoRecordGroupDragDropContextProvider.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyNoRecordGroupDragDropContextProvider.tsx
index 0dea88be312..8afda15a5ca 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyNoRecordGroupDragDropContextProvider.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableBodyNoRecordGroupDragDropContextProvider.tsx
@@ -6,7 +6,7 @@ import { useProcessRecordWithoutGroupDrop } from '@/object-record/record-drag/ho
import { useTriggerTableWithoutGroupDragAndDropOptimisticUpdate } from '@/object-record/record-drag/hooks/useTriggerTableWithoutGroupDragAndDropOptimisticUpdate';
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
import { RecordTableRowDragOverlayContent } from '@/object-record/record-table/record-table-row/components/RecordTableRowDragOverlayContent';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
type RecordTableBodyNoRecordGroupDragDropContextProviderProps = {
@@ -18,8 +18,8 @@ export const RecordTableBodyNoRecordGroupDragDropContextProvider = ({
}: RecordTableBodyNoRecordGroupDragDropContextProviderProps) => {
const { recordTableId } = useRecordTableContextOrThrow();
- const totalNumberOfRecordsToVirtualize = useAtomComponentStateCallbackState(
- totalNumberOfRecordsToVirtualizeComponentState,
+ const totalNumberOfRows = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
recordTableId,
);
@@ -34,9 +34,7 @@ export const RecordTableBodyNoRecordGroupDragDropContextProvider = ({
return (
<RecordDragDropContextProvider
- getDroppableItemCount={() =>
- store.get(totalNumberOfRecordsToVirtualize) ?? 0
- }
+ getDroppableItemCount={() => store.get(totalNumberOfRows) ?? 0}
onRecordDrop={processRecordWithoutGroupDrop}
renderDragOverlay={(source) => (
<RecordTableRowDragOverlayContent source={source} />
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableNoRecordGroupBody.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableNoRecordGroupBody.tsx
index 21678769864..054f2c92db6 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableNoRecordGroupBody.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-body/components/RecordTableNoRecordGroupBody.tsx
@@ -11,7 +11,7 @@ import { isRecordTableInitialLoadingComponentState } from '@/object-record/recor
import { RecordTableVirtualizedDataChangedEffect } from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedDataChangedEffect';
import { RecordTableVirtualizedJunctionDataChangedEffect } from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedJunctionDataChangedEffect';
import { RecordTableVirtualizedSSESubscribeEffect } from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedSSESubscribeEffect';
-import { RecordTableVirtualizedRowTreadmillEffect } from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedRowTreadmillEffect';
+import { RecordTableVirtualizedScrollEffect } from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedScrollEffect';
import { useAtomComponentSelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorValue';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
@@ -38,7 +38,7 @@ export const RecordTableNoRecordGroupBody = () => {
{!isRecordTableInitialLoading && recordTableHasRecords && (
<RecordTableAggregateFooter />
)}
- <RecordTableVirtualizedRowTreadmillEffect />
+ <RecordTableVirtualizedScrollEffect />
<RecordTableVirtualizedDataChangedEffect />
<RecordTableVirtualizedJunctionDataChangedEffect />
<RecordTableVirtualizedSSESubscribeEffect />
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellBaseContainer.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellBaseContainer.tsx
index 227a252da26..1f05689b6a0 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellBaseContainer.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellBaseContainer.tsx
@@ -6,6 +6,7 @@ import { isFieldIdentifierDisplay } from '@/object-record/record-field/ui/meta-t
import { RECORD_CHIP_CLICK_OUTSIDE_ID } from '@/object-record/record-table/constants/RecordChipClickOutsideId';
import { RECORD_TABLE_CELL_CONTENT_CLASS_NAME } from '@/object-record/record-table/constants/RecordTableCellContentClassName';
import { RecordTableCellContext } from '@/object-record/record-table/contexts/RecordTableCellContext';
+import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
import { useOpenRecordTableCellFromCell } from '@/object-record/record-table/record-table-cell/hooks/useOpenRecordTableCellFromCell';
import { getRecordTableCellId } from '@/object-record/record-table/utils/getRecordTableCellId';
@@ -56,7 +57,8 @@ export const RecordTableCellBaseContainer = ({
const { openTableCell } = useOpenRecordTableCellFromCell();
const theme = useTheme();
- const { cellPosition } = useContext(RecordTableCellContext);
+ const { columnIndex } = useContext(RecordTableCellContext);
+ const { recordId } = useRecordTableRowContextOrThrow();
const { recordTableId } = useRecordTableContextOrThrow();
const isChipDisplay = isFieldIdentifierDisplay(
@@ -77,13 +79,9 @@ export const RecordTableCellBaseContainer = ({
fontColorMedium={theme.border.color.medium}
isReadOnly={isReadOnly ?? false}
data-read-only={isReadOnly || undefined}
- id={getRecordTableCellId(
- recordTableId,
- cellPosition.column,
- cellPosition.row,
- )}
- data-record-table-col={cellPosition.column}
- data-record-table-row={cellPosition.row}
+ id={getRecordTableCellId({ recordTableId, columnIndex, recordId })}
+ data-record-table-col={columnIndex}
+ data-record-table-record-id={recordId}
data-click-outside-id={
isChipDisplay ? RECORD_CHIP_CLICK_OUTSIDE_ID : undefined
}
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditButton.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditButton.tsx
index 8bfa59a9de4..d954c745f65 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditButton.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditButton.tsx
@@ -11,10 +11,10 @@ import { isDefined } from 'twenty-shared/utils';
import { IconArrowUpRight, IconPencil } from 'twenty-ui/icon';
export const RecordTableCellEditButton = () => {
- const { cellPosition } = useContext(RecordTableCellContext);
+ const { columnIndex } = useContext(RecordTableCellContext);
const { openTableCell } = useOpenRecordTableCellFromCell();
const isFieldInputOnly = useIsFieldInputOnly();
- const isFirstColumn = cellPosition.column === 0;
+ const isFirstColumn = columnIndex === 0;
const customButtonIcon = useGetButtonIcon();
const secondaryButton = useGetSecondaryFieldButton();
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditMode.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditMode.tsx
index e7a70f28a9e..0c22b9a9e4c 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditMode.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellEditMode.tsx
@@ -8,7 +8,7 @@ import { RecordFieldComponentInstanceContext } from '@/object-record/record-fiel
import { recordFieldInputIsFieldInErrorComponentState } from '@/object-record/record-field/ui/states/recordFieldInputIsFieldInErrorComponentState';
import { recordFieldInputLayoutDirectionComponentState } from '@/object-record/record-field/ui/states/recordFieldInputLayoutDirectionComponentState';
import { recordFieldInputLayoutDirectionLoadingComponentState } from '@/object-record/record-field/ui/states/recordFieldInputLayoutDirectionLoadingComponentState';
-import { RecordTableCellContext } from '@/object-record/record-table/contexts/RecordTableCellContext';
+import { useGetRecordTableCellPosition } from '@/object-record/record-table/record-table-cell/hooks/useGetRecordTableCellPosition';
import { useFocusRecordTableCell } from '@/object-record/record-table/record-table-cell/hooks/useFocusRecordTableCell';
import { StyledOverlayPortalLayer } from '@/ui/layout/overlay/components/StyledOverlayPortalLayer';
import { OverlayContainer } from '@/ui/layout/overlay/components/OverlayContainer';
@@ -25,6 +25,7 @@ import {
type MiddlewareState,
} from '@floating-ui/react';
import { useContext, type ReactElement } from 'react';
+import { isDefined } from 'twenty-shared/utils';
const TABLE_FIELD_INPUT_SIDE_OFFSET = -33;
const TABLE_FIELD_INPUT_ALIGN_OFFSET = -3;
@@ -107,7 +108,7 @@ export const RecordTableCellEditMode = ({
const isFieldInputOnly = useIsFieldInputOnly();
- const { cellPosition } = useContext(RecordTableCellContext);
+ const { getRecordTableCellPosition } = useGetRecordTableCellPosition();
const { focusRecordTableCell } = useFocusRecordTableCell();
@@ -116,7 +117,11 @@ export const RecordTableCellEditMode = ({
return (
<StyledInputModeOnlyContainer
onClick={() => {
- focusRecordTableCell(cellPosition);
+ const cellPosition = getRecordTableCellPosition();
+
+ if (isDefined(cellPosition)) {
+ focusRecordTableCell(cellPosition);
+ }
}}
>
{children}
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldContextLabelIdentifier.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldContextLabelIdentifier.tsx
index 00ad79a8b21..1d10eb5c99e 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldContextLabelIdentifier.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFieldContextLabelIdentifier.tsx
@@ -2,6 +2,7 @@ import { getObjectPermissionsForObject } from '@/object-metadata/utils/getObject
import { isRecordFieldReadOnly } from '@/object-record/read-only/utils/isRecordFieldReadOnly';
import { FieldContext } from '@/object-record/record-field/ui/contexts/FieldContext';
import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
+import { useGetRecordTableRowIndex } from '@/object-record/record-table/record-table-row/hooks/useGetRecordTableRowIndex';
import { RecordTableCellContext } from '@/object-record/record-table/contexts/RecordTableCellContext';
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
@@ -23,8 +24,9 @@ export const RecordTableCellFieldContextLabelIdentifier = ({
fieldMetadataItemByFieldMetadataItemId,
fieldDefinitionByFieldMetadataItemId,
} = useRecordIndexContextOrThrow();
- const { recordId, isRecordReadOnly, rowIndex } =
- useRecordTableRowContextOrThrow();
+ const { recordId, isRecordReadOnly } = useRecordTableRowContextOrThrow();
+
+ const { getRecordTableRowIndex } = useGetRecordTableRowIndex();
const isRecordTableCellsNonEditable = useAtomComponentStateValue(
isRecordTableCellsNonEditableComponentState,
@@ -53,7 +55,11 @@ export const RecordTableCellFieldContextLabelIdentifier = ({
}
const handleChipClick = () => {
- onRecordIdentifierClick?.(rowIndex, recordId);
+ const rowIndex = getRecordTableRowIndex();
+
+ if (isDefined(rowIndex)) {
+ onRecordIdentifierClick?.(rowIndex, recordId);
+ }
};
return (
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFocusedPortalContent.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFocusedPortalContent.tsx
index ca521d2aab8..e156be2efc7 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFocusedPortalContent.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellFocusedPortalContent.tsx
@@ -4,10 +4,10 @@ import { useRecordTableBodyContextOrThrow } from '@/object-record/record-table/c
import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
import { RecordTableCellDisplayMode } from '@/object-record/record-table/record-table-cell/components/RecordTableCellDisplayMode';
-import { isRecordTableRowActiveComponentFamilyState } from '@/object-record/record-table/states/isRecordTableRowActiveComponentFamilyState';
+import { isRecordTableRowActiveByRecordIdComponentFamilySelector } from '@/object-record/record-table/states/selectors/isRecordTableRowActiveByRecordIdComponentFamilySelector';
+import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
import { recordTableFocusPositionComponentState } from '@/object-record/record-table/states/recordTableFocusPositionComponentState';
import { recordTableHoverPositionComponentState } from '@/object-record/record-table/states/recordTableHoverPositionComponentState';
-import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { styled } from '@linaria/react';
import { isDefined } from 'twenty-shared/utils';
@@ -35,16 +35,16 @@ const StyledRecordTableCellFocusPortalContent = styled.div<{
`;
export const RecordTableCellFocusedPortalContent = () => {
- const { rowIndex } = useRecordTableRowContextOrThrow();
+ const { recordId } = useRecordTableRowContextOrThrow();
const { onMoveHoverToCurrentCell } = useRecordTableBodyContextOrThrow();
const recordTableFocusPosition = useAtomComponentStateValue(
recordTableFocusPositionComponentState,
);
- const isRecordTableRowActive = useAtomComponentFamilyStateValue(
- isRecordTableRowActiveComponentFamilyState,
- rowIndex,
+ const isRecordTableRowActive = useAtomComponentFamilySelectorValue(
+ isRecordTableRowActiveByRecordIdComponentFamilySelector,
+ recordId,
);
const recordTableHoverPosition = useAtomComponentStateValue(
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellHoveredPortalContent.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellHoveredPortalContent.tsx
index 4dfa3f7252a..b564f56c1c7 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellHoveredPortalContent.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellHoveredPortalContent.tsx
@@ -9,9 +9,9 @@ import { RecordTableCellEditButton } from '@/object-record/record-table/record-t
import { RecordTableCellEditMode } from '@/object-record/record-table/record-table-cell/components/RecordTableCellEditMode';
import { RecordTableCellFieldInput } from '@/object-record/record-table/record-table-cell/components/RecordTableCellFieldInput';
-import { isRecordTableRowActiveComponentFamilyState } from '@/object-record/record-table/states/isRecordTableRowActiveComponentFamilyState';
+import { isRecordTableRowActiveByRecordIdComponentFamilySelector } from '@/object-record/record-table/states/selectors/isRecordTableRowActiveByRecordIdComponentFamilySelector';
+import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
import { recordTableHoverPositionComponentState } from '@/object-record/record-table/states/recordTableHoverPositionComponentState';
-import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { styled } from '@linaria/react';
import { useContext } from 'react';
@@ -68,11 +68,11 @@ export const RecordTableCellHoveredPortalContent = () => {
const showInteractiveStyle = !isReadOnly || (isFirstColumn && showButton);
- const { rowIndex } = useRecordTableRowContextOrThrow();
+ const { recordId } = useRecordTableRowContextOrThrow();
- const isRecordTableRowActive = useAtomComponentFamilyStateValue(
- isRecordTableRowActiveComponentFamilyState,
- rowIndex,
+ const isRecordTableRowActive = useAtomComponentFamilySelectorValue(
+ isRecordTableRowActiveByRecordIdComponentFamilySelector,
+ recordId,
);
return (
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalAnchorEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalAnchorEffect.tsx
new file mode 100644
index 00000000000..8af4ca1c08d
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalAnchorEffect.tsx
@@ -0,0 +1,23 @@
+import { useLayoutEffect } from 'react';
+import { isDefined } from 'twenty-shared/utils';
+
+type RecordTableCellPortalAnchorEffectProps = {
+ tableCellId: string;
+ onAnchorElementMounted: (element: HTMLElement) => void;
+};
+
+// The cell can mount in the same commit as the portal, after the portal's render
+export const RecordTableCellPortalAnchorEffect = ({
+ tableCellId,
+ onAnchorElementMounted,
+}: RecordTableCellPortalAnchorEffectProps) => {
+ useLayoutEffect(() => {
+ const element = document.getElementById(tableCellId);
+
+ if (isDefined(element)) {
+ onAnchorElementMounted(element);
+ }
+ }, [tableCellId, onAnchorElementMounted]);
+
+ return null;
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalContexts.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalContexts.tsx
index 40c1bb87834..804b0a13ea4 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalContexts.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalContexts.tsx
@@ -49,7 +49,6 @@ export const RecordTableCellPortalContexts = ({
<RecordTableRowContextProvider
value={{
recordId,
- rowIndex: position.row,
isSelected: false,
pathToShowPage:
getBasePathToShowPage({
@@ -62,7 +61,7 @@ export const RecordTableCellPortalContexts = ({
<RecordTableCellContext.Provider
value={{
recordField,
- cellPosition: position,
+ columnIndex: position.column,
}}
>
<RecordTableCellFieldContextWrapper recordField={recordField}>
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalWrapper.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalWrapper.tsx
index ee739d37386..b3019815532 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalWrapper.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellPortalWrapper.tsx
@@ -1,4 +1,6 @@
+import { recordIndexAllRecordIdsComponentSelector } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
+import { RecordTableCellPortalAnchorEffect } from '@/object-record/record-table/record-table-cell/components/RecordTableCellPortalAnchorEffect';
import { RecordTableCellPortalContexts } from '@/object-record/record-table/record-table-cell/components/RecordTableCellPortalContexts';
import { type TableCellPosition } from '@/object-record/record-table/types/TableCellPosition';
import { getRecordTableCellId } from '@/object-record/record-table/utils/getRecordTableCellId';
@@ -7,6 +9,7 @@ import { isRecordTableColumnRendered } from '@/object-record/record-table/virtua
import { recordTableRenderedColumnRangeComponentState } from '@/object-record/record-table/virtualization/states/recordTableRenderedColumnRangeComponentState';
import { useAtomComponentSelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorValue';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
+import { useState } from 'react';
import { createPortal } from 'react-dom';
import { isDefined } from 'twenty-shared/utils';
@@ -19,6 +22,10 @@ export const RecordTableCellPortalWrapper = ({
}) => {
const { recordTableId } = useRecordTableContextOrThrow();
+ const allRecordIds = useAtomComponentSelectorValue(
+ recordIndexAllRecordIdsComponentSelector,
+ );
+
const recordTableRenderedColumnRange = useAtomComponentStateValue(
recordTableRenderedColumnRangeComponentState,
);
@@ -35,16 +42,47 @@ export const RecordTableCellPortalWrapper = ({
recordTableColumnIndexesRenderedOutsideRange,
});
- const tableCellAnchorElement = isColumnRendered
- ? document.body.querySelector<HTMLAnchorElement>(
- `#${getRecordTableCellId(recordTableId, position.column, position.row)}`,
- )
- : null;
+ const recordId = allRecordIds[position.row];
- if (!isDefined(tableCellAnchorElement)) {
+ const tableCellId =
+ isColumnRendered && isDefined(recordId)
+ ? getRecordTableCellId({
+ recordTableId,
+ columnIndex: position.column,
+ recordId,
+ })
+ : null;
+
+ const [anchorElementMountedLater, setAnchorElementMountedLater] = useState<{
+ tableCellId: string;
+ element: HTMLElement;
+ } | null>(null);
+
+ if (!isDefined(tableCellId)) {
return null;
}
+ const isAnchorElementMountedLaterValid =
+ anchorElementMountedLater?.tableCellId === tableCellId &&
+ anchorElementMountedLater.element.isConnected;
+
+ const tableCellAnchorElement =
+ document.getElementById(tableCellId) ??
+ (isAnchorElementMountedLaterValid
+ ? anchorElementMountedLater.element
+ : null);
+
+ if (!isDefined(tableCellAnchorElement)) {
+ return (
+ <RecordTableCellPortalAnchorEffect
+ tableCellId={tableCellId}
+ onAnchorElementMounted={(element) =>
+ setAnchorElementMountedLater({ tableCellId, element })
+ }
+ />
+ );
+ }
+
return (
<>
{createPortal(
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellWrapper.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellWrapper.tsx
index e4d967626ec..f2dfbcf22be 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellWrapper.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/components/RecordTableCellWrapper.tsx
@@ -1,9 +1,8 @@
+import { useMemo } from 'react';
+
import { type RecordField } from '@/object-record/record-field/types/RecordField';
import { RecordTableCellContext } from '@/object-record/record-table/contexts/RecordTableCellContext';
-import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
import { RecordTableCellFieldContextWrapper } from '@/object-record/record-table/record-table-cell/components/RecordTableCellFieldContextWrapper';
-import { type TableCellPosition } from '@/object-record/record-table/types/TableCellPosition';
-import { useMemo } from 'react';
export const RecordTableCellWrapper = ({
children,
@@ -14,22 +13,14 @@ export const RecordTableCellWrapper = ({
recordFieldIndex: number;
children: React.ReactNode;
}) => {
- const { rowIndex } = useRecordTableRowContextOrThrow();
-
- const currentTableCellPosition: TableCellPosition = useMemo(
- () => ({
- column: recordFieldIndex,
- row: rowIndex,
- }),
- [recordFieldIndex, rowIndex],
+ const recordTableCellContextValue = useMemo(
+ () => ({ recordField, columnIndex: recordFieldIndex }),
+ [recordField, recordFieldIndex],
);
return (
<RecordTableCellContext.Provider
- value={{
- recordField,
- cellPosition: currentTableCellPosition,
- }}
+ value={recordTableCellContextValue}
key={recordField.fieldMetadataItemId}
>
<RecordTableCellFieldContextWrapper recordField={recordField}>
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/__mocks__/cell.ts b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/__mocks__/cell.ts
index 4bc7e395003..00321e49967 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/__mocks__/cell.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/__mocks__/cell.ts
@@ -3,7 +3,6 @@ import { RecordTableRowContextValue } from '@/object-record/record-table/context
import { RecordTableRowDraggableContextValue } from '@/object-record/record-table/contexts/RecordTableRowDraggableContext';
export const recordTableRowContextValue: RecordTableRowContextValue = {
- rowIndex: 2,
isSelected: false,
recordId: 'recordId',
pathToShowPage: '/',
@@ -23,8 +22,5 @@ export const recordTableCellContextValue: RecordTableCellContextValue = {
id: 'id',
isVisible: true,
},
- cellPosition: {
- row: 2,
- column: 3,
- },
+ columnIndex: 3,
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/useGetRecordTableCellPosition.ts b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/useGetRecordTableCellPosition.ts
new file mode 100644
index 00000000000..1ca5fff975d
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/useGetRecordTableCellPosition.ts
@@ -0,0 +1,19 @@
+import { useContext } from 'react';
+import { isDefined } from 'twenty-shared/utils';
+
+import { RecordTableCellContext } from '@/object-record/record-table/contexts/RecordTableCellContext';
+import { useGetRecordTableRowIndex } from '@/object-record/record-table/record-table-row/hooks/useGetRecordTableRowIndex';
+import { type TableCellPosition } from '@/object-record/record-table/types/TableCellPosition';
+
+export const useGetRecordTableCellPosition = () => {
+ const { columnIndex } = useContext(RecordTableCellContext);
+ const { getRecordTableRowIndex } = useGetRecordTableRowIndex();
+
+ const getRecordTableCellPosition = (): TableCellPosition | null => {
+ const rowIndex = getRecordTableRowIndex();
+
+ return isDefined(rowIndex) ? { row: rowIndex, column: columnIndex } : null;
+ };
+
+ return { getRecordTableCellPosition };
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/useOpenRecordTableCellFromCell.ts b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/useOpenRecordTableCellFromCell.ts
index 6ff50028a2f..be55e4c3ee3 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/useOpenRecordTableCellFromCell.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-cell/hooks/useOpenRecordTableCellFromCell.ts
@@ -2,7 +2,8 @@ import { useContext } from 'react';
import { FieldContext } from '@/object-record/record-field/ui/contexts/FieldContext';
import { useRecordTableBodyContextOrThrow } from '@/object-record/record-table/contexts/RecordTableBodyContext';
-import { RecordTableCellContext } from '@/object-record/record-table/contexts/RecordTableCellContext';
+import { useGetRecordTableCellPosition } from '@/object-record/record-table/record-table-cell/hooks/useGetRecordTableCellPosition';
+import { isDefined } from 'twenty-shared/utils';
export const useOpenRecordTableCellFromCell = () => {
const {
@@ -13,9 +14,15 @@ export const useOpenRecordTableCellFromCell = () => {
const { onOpenTableCell } = useRecordTableBodyContextOrThrow();
- const { cellPosition } = useContext(RecordTableCellContext);
+ const { getRecordTableCellPosition } = useGetRecordTableCellPosition();
const openTableCell = (initialValue?: string, isNavigating = false) => {
+ const cellPosition = getRecordTableCellPosition();
+
+ if (!isDefined(cellPosition)) {
+ return;
+ }
+
onOpenTableCell({
cellPosition,
recordId,
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableDraggableTr.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableDraggableTr.tsx
index 84d86325305..375b4079c4f 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableDraggableTr.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableDraggableTr.tsx
@@ -1,37 +1,21 @@
-import { useSortable } from '@dnd-kit/react/sortable';
-import { styled } from '@linaria/react';
-import { type ReactNode, useContext, useState } from 'react';
+import { type ReactNode, useCallback, useContext, useRef } from 'react';
import { isDefined } from 'twenty-shared/utils';
-import { v4 } from 'uuid';
+import { isRecordIdPrimaryDragComponentFamilyState } from '@/object-record/record-drag/states/isRecordIdPrimaryDragComponentFamilyState';
+import { isRecordIdSecondaryDragMultipleComponentFamilyState } from '@/object-record/record-drag/states/isRecordIdSecondaryDragMultipleComponentFamilyState';
import { RecordGroupContext } from '@/object-record/record-group/states/context/RecordGroupContext';
import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
-import { RECORD_TABLE_ROW_DND_TYPE } from '@/object-record/record-table/constants/RecordTableRowDndType';
-import { TABLE_Z_INDEX } from '@/object-record/record-table/constants/TableZIndex';
import { RecordTableRowDraggableContextProvider } from '@/object-record/record-table/contexts/RecordTableRowDraggableContext';
+import { RecordTableRowSortable } from '@/object-record/record-table/record-table-row/components/RecordTableRowSortable';
import { RecordTableTr } from '@/object-record/record-table/record-table-row/components/RecordTableTr';
-import { isRecordIdSecondaryDragMultipleComponentFamilyState } from '@/object-record/record-drag/states/isRecordIdSecondaryDragMultipleComponentFamilyState';
-import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
-import { type RecordTableRowDragData } from '@/object-record/record-table/types/RecordTableRowDragData';
-import { DragDropItemDropTarget } from '@/ui/utilities/drag-and-drop/components/DragDropItemDropTarget';
-import { DND_KIT_PLUGINS_WITHOUT_OPTIMISTIC } from '@/ui/utilities/drag-and-drop/constants/DndKitPluginsWithoutOptimistic';
import { DRAG_SOURCE_OPACITY } from '@/ui/utilities/drag-and-drop/constants/DragSourceOpacity';
import { DragDropItemSortableHandleRefContext } from '@/ui/utilities/drag-and-drop/context/DragDropItemSortableHandleRefContext';
-
-// Above TABLE_Z_INDEX.cell.sticky so sticky cells don't paint over the insertion line.
-const StyledRowDropTargetSlot = styled.div`
- left: 0;
- position: absolute;
- right: 0;
- top: -1px;
- z-index: ${TABLE_Z_INDEX.rowDropLine};
-`;
+import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
type RecordTableDraggableTrProps = {
className?: string;
recordId: string;
- draggableIndex: number;
- focusIndex: number;
+ draggableIndex?: number;
isDragDisabled?: boolean;
onClick?: (event: React.MouseEvent<HTMLTableRowElement>) => void;
children: ReactNode;
@@ -41,7 +25,6 @@ export const RecordTableDraggableTr = ({
className,
recordId,
draggableIndex,
- focusIndex,
isDragDisabled,
onClick,
children,
@@ -51,44 +34,32 @@ export const RecordTableDraggableTr = ({
{ recordId },
);
+ const isRecordIdPrimaryDrag = useAtomComponentFamilyStateValue(
+ isRecordIdPrimaryDragComponentFamilyState,
+ { recordId },
+ );
+
const { recordGroupId } = useContext(RecordGroupContext);
const droppableId = isDefined(recordGroupId)
? recordGroupId
: NO_RECORD_GROUP_FAMILY_KEY;
- const rowDragData: RecordTableRowDragData = {
- droppableId,
- index: draggableIndex,
- recordId,
- focusIndex,
- };
-
- // Stable per instance: dnd-kit breaks a row when virtualization shifts recordIds across mounted rows.
- const [sortableId] = useState(() => v4());
+ const rowElementRef = useRef<HTMLDivElement>(null);
+ const handleElementRef = useRef<Element>(null);
- const { handleRef, ref, isDragSource } = useSortable({
- id: sortableId,
- index: draggableIndex,
- group: droppableId,
- type: RECORD_TABLE_ROW_DND_TYPE,
- accept: RECORD_TABLE_ROW_DND_TYPE,
- data: rowDragData,
- disabled: isDragDisabled,
- transition: null,
- plugins: DND_KIT_PLUGINS_WITHOUT_OPTIMISTIC,
- feedback: 'clone',
- });
+ const setHandleElement = useCallback((element: Element | null) => {
+ handleElementRef.current = element;
+ }, []);
return (
<RecordTableTr
recordId={recordId}
- focusIndex={focusIndex}
- ref={ref}
+ ref={rowElementRef}
className={className}
style={{
opacity:
- isDragSource || isRecordIdSecondaryDragMultiple
+ isRecordIdPrimaryDrag || isRecordIdSecondaryDragMultiple
? DRAG_SOURCE_OPACITY
: undefined,
}}
@@ -97,22 +68,19 @@ export const RecordTableDraggableTr = ({
data-selectable-id={recordId}
onClick={onClick}
>
- <DragDropItemSortableHandleRefContext.Provider value={handleRef}>
+ <DragDropItemSortableHandleRefContext.Provider value={setHandleElement}>
<RecordTableRowDraggableContextProvider value={{ isDragging: false }}>
{children}
</RecordTableRowDraggableContextProvider>
</DragDropItemSortableHandleRefContext.Provider>
- {!isDragSource && (
- <StyledRowDropTargetSlot>
- <DragDropItemDropTarget
- index={draggableIndex}
- droppableId={droppableId}
- orientation="horizontal"
- compact
- seamAligned
- />
- </StyledRowDropTargetSlot>
- )}
+ <RecordTableRowSortable
+ recordId={recordId}
+ droppableId={droppableId}
+ draggableIndex={draggableIndex}
+ isDragDisabled={isDragDisabled}
+ rowElementRef={rowElementRef}
+ handleElementRef={handleElementRef}
+ />
</RecordTableTr>
);
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableFieldsCells.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableFieldsCells.tsx
index 39828f79216..ddd9f9a9c14 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableFieldsCells.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableFieldsCells.tsx
@@ -10,11 +10,18 @@ import { RecordTableCellWrapper } from '@/object-record/record-table/record-tabl
import { RecordTableFieldCell } from '@/object-record/record-table/record-table-row/components/RecordTableFieldCell';
import { getRecordTableColumnFieldWidthClassName } from '@/object-record/record-table/utils/getRecordTableColumnFieldWidthClassName';
import { RecordTableColumnSpacer } from '@/object-record/record-table/virtualization/components/RecordTableColumnSpacer';
+import { isRecordTableFirstRowComponentFamilySelector } from '@/object-record/record-table/states/selectors/isRecordTableFirstRowComponentFamilySelector';
+import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
import { useAtomComponentSelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorValue';
import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';
export const RecordTableFieldsCells = () => {
- const { isSelected, rowIndex } = useRecordTableRowContextOrThrow();
+ const { isSelected, recordId } = useRecordTableRowContextOrThrow();
+
+ const isFirstRow = useAtomComponentFamilySelectorValue(
+ isRecordTableFirstRowComponentFamilySelector,
+ recordId,
+ );
const { isDragging } = useRecordTableRowDraggableContextOrThrow();
@@ -31,8 +38,6 @@ export const RecordTableFieldsCells = () => {
return null;
}
- const isFirstRow = rowIndex === 0;
-
const firstRecordField = visibleRecordFields[0];
if (!isDefined(firstRecordField)) {
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRow.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRow.tsx
index b6b0b197760..e3be145a012 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRow.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRow.tsx
@@ -6,13 +6,11 @@ import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/use
type RecordTableRowProps = {
recordId: string;
- rowIndexForFocus: number;
rowIndexForDrag: number;
};
export const RecordTableRow = ({
recordId,
- rowIndexForFocus,
rowIndexForDrag,
}: RecordTableRowProps) => {
const isRecordTableDragColumnHidden = useAtomComponentStateValue(
@@ -21,8 +19,8 @@ export const RecordTableRow = ({
if (isRecordTableDragColumnHidden) {
return (
- <RecordTableStaticTr recordId={recordId} focusIndex={rowIndexForFocus}>
- <RecordTableRowCells rowIndexForFocus={rowIndexForFocus} />
+ <RecordTableStaticTr recordId={recordId}>
+ <RecordTableRowCells />
</RecordTableStaticTr>
);
}
@@ -31,9 +29,8 @@ export const RecordTableRow = ({
<RecordTableDraggableTr
recordId={recordId}
draggableIndex={rowIndexForDrag}
- focusIndex={rowIndexForFocus}
>
- <RecordTableRowCells rowIndexForFocus={rowIndexForFocus} />
+ <RecordTableRowCells />
</RecordTableDraggableTr>
);
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowArrowKeysEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowArrowKeysEffect.tsx
index 951b64a9009..bddfe444ce2 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowArrowKeysEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowArrowKeysEffect.tsx
@@ -1,12 +1,13 @@
import { useRecordTableRowFocusHotkeys } from '@/object-record/record-table/hooks/useRecordTableRowFocusHotkeys';
-import { useRecordTableRowFocusId } from '@/object-record/record-table/record-table-row/hooks/useRecordTableRowFocusId';
-export const RecordTableRowArrowKeysEffect = () => {
- const recordTableRowFocusId = useRecordTableRowFocusId();
+type RecordTableRowArrowKeysEffectProps = {
+ focusId: string;
+};
- useRecordTableRowFocusHotkeys({
- focusId: recordTableRowFocusId,
- });
+export const RecordTableRowArrowKeysEffect = ({
+ focusId,
+}: RecordTableRowArrowKeysEffectProps) => {
+ useRecordTableRowFocusHotkeys({ focusId });
return null;
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowCells.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowCells.tsx
index 507d06b4e96..0c50fe48e92 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowCells.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowCells.tsx
@@ -4,27 +4,22 @@ import { RecordTableCellDragAndDrop } from '@/object-record/record-table/record-
import { RecordTableLastEmptyCell } from '@/object-record/record-table/record-table-cell/components/RecordTableLastEmptyCell';
import { RecordTablePlusButtonCellPlaceholder } from '@/object-record/record-table/record-table-cell/components/RecordTablePlusButtonCellPlaceholder';
import { RecordTableFieldsCells } from '@/object-record/record-table/record-table-row/components/RecordTableFieldsCells';
-import { RecordTableRowArrowKeysEffect } from '@/object-record/record-table/record-table-row/components/RecordTableRowArrowKeysEffect';
-import { RecordTableRowHotkeyEffect } from '@/object-record/record-table/record-table-row/components/RecordTableRowHotkeyEffect';
+import { RecordTableRowFocusEffects } from '@/object-record/record-table/record-table-row/components/RecordTableRowFocusEffects';
import { useIsRecordTableCheckboxColumnHidden } from '@/object-record/record-table/hooks/useIsRecordTableCheckboxColumnHidden';
import { isRecordTableDragColumnHiddenComponentState } from '@/object-record/record-table/states/isRecordTableDragColumnHiddenComponentState';
import { isRecordTableRowFocusActiveComponentState } from '@/object-record/record-table/states/isRecordTableRowFocusActiveComponentState';
-import { isRecordTableRowFocusedComponentFamilyState } from '@/object-record/record-table/states/isRecordTableRowFocusedComponentFamilyState';
-import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
+import { isRecordTableRowFocusedByRecordIdComponentFamilySelector } from '@/object-record/record-table/states/selectors/isRecordTableRowFocusedByRecordIdComponentFamilySelector';
+import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
+import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
-type RecordTableRowCellsProps = {
- rowIndexForFocus: number;
-};
-
-export const RecordTableRowCells = ({
- rowIndexForFocus,
-}: RecordTableRowCellsProps) => {
+export const RecordTableRowCells = () => {
const { recordTableId } = useRecordTableContextOrThrow();
+ const { recordId } = useRecordTableRowContextOrThrow();
- const isRecordTableRowFocused = useAtomComponentFamilyStateValue(
- isRecordTableRowFocusedComponentFamilyState,
- rowIndexForFocus,
+ const isRecordTableRowFocused = useAtomComponentFamilySelectorValue(
+ isRecordTableRowFocusedByRecordIdComponentFamilySelector,
+ recordId,
);
const isRecordTableRowFocusActive = useAtomComponentStateValue(
@@ -42,10 +37,7 @@ export const RecordTableRowCells = ({
return (
<>
{isRecordTableRowFocusActive && isRecordTableRowFocused && (
- <>
- <RecordTableRowHotkeyEffect />
- <RecordTableRowArrowKeysEffect />
- </>
+ <RecordTableRowFocusEffects />
)}
{!isRecordTableDragColumnHidden && <RecordTableCellDragAndDrop />}
{!isRecordTableCheckboxColumnHidden && <RecordTableCellCheckbox />}
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowDragOverlayContent.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowDragOverlayContent.tsx
index 7456291d981..a006d9898f9 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowDragOverlayContent.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowDragOverlayContent.tsx
@@ -31,7 +31,7 @@ import { RecordTablePlusButtonCellPlaceholder } from '@/object-record/record-tab
import { RecordTableFieldsCells } from '@/object-record/record-table/record-table-row/components/RecordTableFieldsCells';
import { RecordDragMultiDragCounterChip } from '@/object-record/record-drag/components/RecordDragMultiDragCounterChip';
import { RecordTableTr } from '@/object-record/record-table/record-table-row/components/RecordTableTr';
-import { type RecordTableRowDragData } from '@/object-record/record-table/types/RecordTableRowDragData';
+import { type RecordDragData } from '@/object-record/record-drag/types/RecordDragData';
import { RecordTableColumnVirtualizationItemsContextValueProvider } from '@/object-record/record-table/contexts/RecordTableColumnVirtualizationItemsContext';
import { computeRecordTableColumnRange } from '@/object-record/record-table/virtualization/utils/computeRecordTableColumnRange';
import { computeRecordTableColumnVirtualizationItems } from '@/object-record/record-table/virtualization/utils/computeRecordTableColumnVirtualizationItems';
@@ -154,7 +154,7 @@ export const RecordTableRowDragOverlayContent = ({
isRecordTableCheckboxColumnHidden,
]);
- const sourceData = source?.data as RecordTableRowDragData | undefined;
+ const sourceData = source?.data as RecordDragData | undefined;
if (!isDefined(source) || !isDefined(sourceData)) {
return null;
@@ -189,7 +189,6 @@ export const RecordTableRowDragOverlayContent = ({
>
<RecordTableTr
recordId={recordId}
- focusIndex={sourceData.focusIndex}
style={{
background: themeCssVariables.background.secondary,
borderColor: themeCssVariables.border.color.medium,
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowFocusEffects.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowFocusEffects.tsx
new file mode 100644
index 00000000000..fac82db55fa
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowFocusEffects.tsx
@@ -0,0 +1,20 @@
+import { isDefined } from 'twenty-shared/utils';
+
+import { RecordTableRowArrowKeysEffect } from '@/object-record/record-table/record-table-row/components/RecordTableRowArrowKeysEffect';
+import { RecordTableRowHotkeyEffect } from '@/object-record/record-table/record-table-row/components/RecordTableRowHotkeyEffect';
+import { useRecordTableRowFocusId } from '@/object-record/record-table/record-table-row/hooks/useRecordTableRowFocusId';
+
+export const RecordTableRowFocusEffects = () => {
+ const focusId = useRecordTableRowFocusId();
+
+ if (!isDefined(focusId)) {
+ return null;
+ }
+
+ return (
+ <>
+ <RecordTableRowHotkeyEffect focusId={focusId} />
+ <RecordTableRowArrowKeysEffect focusId={focusId} />
+ </>
+ );
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowHotkeyEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowHotkeyEffect.tsx
index c317a34e211..167ff6bf789 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowHotkeyEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowHotkeyEffect.tsx
@@ -1,10 +1,13 @@
-import { useRecordTableRowFocusId } from '@/object-record/record-table/record-table-row/hooks/useRecordTableRowFocusId';
import { useRecordTableRowHotkeys } from '@/object-record/record-table/record-table-row/hooks/useRecordTableRowHotkeys';
-export const RecordTableRowHotkeyEffect = () => {
- const recordTableRowFocusId = useRecordTableRowFocusId();
+type RecordTableRowHotkeyEffectProps = {
+ focusId: string;
+};
- useRecordTableRowHotkeys(recordTableRowFocusId);
+export const RecordTableRowHotkeyEffect = ({
+ focusId,
+}: RecordTableRowHotkeyEffectProps) => {
+ useRecordTableRowHotkeys(focusId);
return null;
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowSortable.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowSortable.tsx
new file mode 100644
index 00000000000..ed09da79cd0
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableRowSortable.tsx
@@ -0,0 +1,86 @@
+import { useSortable } from '@dnd-kit/react/sortable';
+import { styled } from '@linaria/react';
+import { type RefObject, useState } from 'react';
+import { v4 } from 'uuid';
+
+import { RECORD_TABLE_ROW_DND_TYPE } from '@/object-record/record-table/constants/RecordTableRowDndType';
+import { TABLE_Z_INDEX } from '@/object-record/record-table/constants/TableZIndex';
+import { recordTableRowIndexOfRecordComponentFamilySelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector';
+import { type RecordDragData } from '@/object-record/record-drag/types/RecordDragData';
+import { DragDropItemDropTarget } from '@/ui/utilities/drag-and-drop/components/DragDropItemDropTarget';
+import { DND_KIT_PLUGINS_WITHOUT_OPTIMISTIC } from '@/ui/utilities/drag-and-drop/constants/DndKitPluginsWithoutOptimistic';
+import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
+
+// Above TABLE_Z_INDEX.cell.sticky so sticky cells don't paint over the insertion line.
+const StyledRowDropTargetSlot = styled.div`
+ left: 0;
+ position: absolute;
+ right: 0;
+ top: -1px;
+ z-index: ${TABLE_Z_INDEX.rowDropLine};
+`;
+
+type RecordTableRowSortableProps = {
+ recordId: string;
+ droppableId: string;
+ draggableIndex?: number;
+ isDragDisabled?: boolean;
+ rowElementRef: RefObject<HTMLDivElement | null>;
+ handleElementRef: RefObject<Element | null>;
+};
+
+export const RecordTableRowSortable = ({
+ recordId,
+ droppableId,
+ draggableIndex,
+ isDragDisabled,
+ rowElementRef,
+ handleElementRef,
+}: RecordTableRowSortableProps) => {
+ const rowIndex = useAtomComponentFamilySelectorValue(
+ recordTableRowIndexOfRecordComponentFamilySelector,
+ recordId,
+ );
+
+ const index = draggableIndex ?? rowIndex ?? 0;
+
+ const rowDragData: RecordDragData = {
+ droppableId,
+ index,
+ recordId,
+ };
+
+ // Stable per instance: dnd-kit breaks a row when its id changes while mounted.
+ const [sortableId] = useState(() => v4());
+
+ const { isDragSource } = useSortable({
+ id: sortableId,
+ index,
+ group: droppableId,
+ type: RECORD_TABLE_ROW_DND_TYPE,
+ accept: RECORD_TABLE_ROW_DND_TYPE,
+ data: rowDragData,
+ disabled: isDragDisabled,
+ transition: null,
+ plugins: DND_KIT_PLUGINS_WITHOUT_OPTIMISTIC,
+ feedback: 'clone',
+ element: rowElementRef,
+ handle: handleElementRef,
+ });
+
+ if (isDragSource) {
+ return null;
+ }
+
+ return (
+ <StyledRowDropTargetSlot>
+ <DragDropItemDropTarget
+ index={index}
+ droppableId={droppableId}
+ orientation="horizontal"
+ compact
+ seamAligned
+ />
+ </StyledRowDropTargetSlot>
+ );
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableStaticTr.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableStaticTr.tsx
index a88beda003f..5b111129c87 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableStaticTr.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableStaticTr.tsx
@@ -5,28 +5,21 @@ import { RecordTableTr } from '@/object-record/record-table/record-table-row/com
type RecordTableStaticTrProps = {
recordId: string;
- focusIndex: number;
children: ReactNode;
};
export const RecordTableStaticTr = ({
recordId,
- focusIndex,
children,
}: RecordTableStaticTrProps) => {
return (
<RecordTableTr
recordId={recordId}
- focusIndex={focusIndex}
isDragging={false}
data-testid={`row-id-${recordId}`}
data-selectable-id={recordId}
>
- <RecordTableRowDraggableContextProvider
- value={{
- isDragging: false,
- }}
- >
+ <RecordTableRowDraggableContextProvider value={{ isDragging: false }}>
{children}
</RecordTableRowDraggableContextProvider>
</RecordTableTr>
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableTr.tsx b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableTr.tsx
index dc3d5c83e67..2ee951a2e76 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableTr.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/components/RecordTableTr.tsx
@@ -4,18 +4,18 @@ import { useRecordTableContextOrThrow } from '@/object-record/record-table/conte
import { RecordTableRowContextProvider } from '@/object-record/record-table/contexts/RecordTableRowContext';
import { RecordTableRowDiv } from '@/object-record/record-table/record-table-row/components/RecordTableRowDiv';
import { isRecordSelectedComponentFamilyState } from '@/object-record/record-selection/states/isRecordSelectedComponentFamilyState';
-import { isRecordTableRowActiveComponentFamilyState } from '@/object-record/record-table/states/isRecordTableRowActiveComponentFamilyState';
import { isRecordTableRowFocusActiveComponentState } from '@/object-record/record-table/states/isRecordTableRowFocusActiveComponentState';
-import { isRecordTableRowFocusedComponentFamilyState } from '@/object-record/record-table/states/isRecordTableRowFocusedComponentFamilyState';
+import { isRecordTableRowActiveByRecordIdComponentFamilySelector } from '@/object-record/record-table/states/selectors/isRecordTableRowActiveByRecordIdComponentFamilySelector';
+import { isRecordTableRowFocusedByRecordIdComponentFamilySelector } from '@/object-record/record-table/states/selectors/isRecordTableRowFocusedByRecordIdComponentFamilySelector';
+import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
-import { forwardRef, type ReactNode } from 'react';
+import { forwardRef, useMemo, type ReactNode } from 'react';
type RecordTableTrProps = {
children: ReactNode;
recordId: string;
- focusIndex: number;
isDragging?: boolean;
} & Omit<
React.ComponentProps<typeof RecordTableRowDiv>,
@@ -23,7 +23,7 @@ type RecordTableTrProps = {
>;
export const RecordTableTr = forwardRef<HTMLDivElement, RecordTableTrProps>(
- ({ children, recordId, focusIndex, isDragging = false, ...props }, ref) => {
+ ({ children, recordId, isDragging = false, ...props }, ref) => {
const { objectMetadataItem, isObjectReadOnly } =
useRecordTableContextOrThrow();
@@ -32,14 +32,14 @@ export const RecordTableTr = forwardRef<HTMLDivElement, RecordTableTrProps>(
recordId,
);
- const isRecordTableRowActive = useAtomComponentFamilyStateValue(
- isRecordTableRowActiveComponentFamilyState,
- focusIndex,
+ const isRecordTableRowActive = useAtomComponentFamilySelectorValue(
+ isRecordTableRowActiveByRecordIdComponentFamilySelector,
+ recordId,
);
- const isRecordTableRowFocused = useAtomComponentFamilyStateValue(
- isRecordTableRowFocusedComponentFamilyState,
- focusIndex,
+ const isRecordTableRowFocused = useAtomComponentFamilySelectorValue(
+ isRecordTableRowFocusedByRecordIdComponentFamilySelector,
+ recordId,
);
const isRecordTableRowFocusActive = useAtomComponentStateValue(
@@ -48,20 +48,28 @@ export const RecordTableTr = forwardRef<HTMLDivElement, RecordTableTrProps>(
const isRecordDeleted = useIsRecordDeleted({ recordId });
+ const recordTableRowContextValue = useMemo(
+ () => ({
+ recordId,
+ pathToShowPage:
+ getBasePathToShowPage({
+ objectNameSingular: objectMetadataItem.nameSingular,
+ }) + recordId,
+ objectNameSingular: objectMetadataItem.nameSingular,
+ isSelected: isRecordSelected,
+ isRecordReadOnly: isObjectReadOnly || isRecordDeleted,
+ }),
+ [
+ isObjectReadOnly,
+ isRecordDeleted,
+ isRecordSelected,
+ objectMetadataItem.nameSingular,
+ recordId,
+ ],
+ );
+
return (
- <RecordTableRowContextProvider
- value={{
- recordId: recordId,
- rowIndex: focusIndex,
- pathToShowPage:
- getBasePathToShowPage({
- objectNameSingular: objectMetadataItem.nameSingular,
- }) + recordId,
- objectNameSingular: objectMetadataItem.nameSingular,
- isSelected: isRecordSelected,
- isRecordReadOnly: isObjectReadOnly || isRecordDeleted,
- }}
- >
+ <RecordTableRowContextProvider value={recordTableRowContextValue}>
<RecordTableRowDiv
className="table-row"
isDragging={isDragging}
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useGetRecordTableRowIndex.ts b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useGetRecordTableRowIndex.ts
new file mode 100644
index 00000000000..dc36cb84a41
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useGetRecordTableRowIndex.ts
@@ -0,0 +1,20 @@
+import { useStore } from 'jotai';
+
+import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
+import { recordTableRowIndexOfRecordComponentFamilySelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector';
+import { useAtomComponentFamilySelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorCallbackState';
+
+export const useGetRecordTableRowIndex = () => {
+ const store = useStore();
+ const { recordId } = useRecordTableRowContextOrThrow();
+
+ const rowIndexOfRecordCallbackState =
+ useAtomComponentFamilySelectorCallbackState(
+ recordTableRowIndexOfRecordComponentFamilySelector,
+ );
+
+ const getRecordTableRowIndex = () =>
+ store.get(rowIndexOfRecordCallbackState(recordId));
+
+ return { getRecordTableRowIndex };
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowFocusId.ts b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowFocusId.ts
index 2e242064a09..15e230f8dd5 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowFocusId.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowFocusId.ts
@@ -1,14 +1,15 @@
+import { isDefined } from 'twenty-shared/utils';
+
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
-import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
+import { useRecordTableRowIndex } from '@/object-record/record-table/record-table-row/hooks/useRecordTableRowIndex';
import { getRecordTableRowFocusId } from '@/object-record/record-table/record-table-row/utils/getRecordTableRowFocusId';
export const useRecordTableRowFocusId = () => {
- const { rowIndex } = useRecordTableRowContextOrThrow();
+ const rowIndex = useRecordTableRowIndex();
const { recordTableId } = useRecordTableContextOrThrow();
- return getRecordTableRowFocusId({
- recordTableId,
- rowIndex,
- });
+ return isDefined(rowIndex)
+ ? getRecordTableRowFocusId({ recordTableId, rowIndex })
+ : null;
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowHotkeys.ts b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowHotkeys.ts
index 9e6a0f44943..8367675c0ce 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowHotkeys.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowHotkeys.ts
@@ -14,10 +14,13 @@ import { FocusComponentType } from '@/ui/utilities/focus/types/FocusComponentTyp
import { useHotkeysOnFocusedElement } from '@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement';
import { useSetAtomComponentState } from '@/ui/utilities/state/jotai/hooks/useSetAtomComponentState';
import { Key } from 'ts-key-enum';
+import { isDefined } from 'twenty-shared/utils';
+import { useGetRecordTableRowIndex } from '@/object-record/record-table/record-table-row/hooks/useGetRecordTableRowIndex';
export const useRecordTableRowHotkeys = (focusId: string) => {
- const { recordId, objectNameSingular, rowIndex } =
- useRecordTableRowContextOrThrow();
+ const { recordId, objectNameSingular } = useRecordTableRowContextOrThrow();
+
+ const { getRecordTableRowIndex } = useGetRecordTableRowIndex();
const { toggleRecordSelection } = useToggleRecordSelection();
@@ -50,10 +53,20 @@ export const useRecordTableRowHotkeys = (focusId: string) => {
isNewRecord: false,
});
- activateRecordTableRow(rowIndex);
+ const rowIndex = getRecordTableRowIndex();
+
+ if (isDefined(rowIndex)) {
+ activateRecordTableRow(rowIndex);
+ }
};
const handleEnterRow = () => {
+ const rowIndex = getRecordTableRowIndex();
+
+ if (!isDefined(rowIndex)) {
+ return;
+ }
+
setIsRecordTableRowFocusActive(false);
const cellPosition = {
row: rowIndex,
diff --git a/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowIndex.ts b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowIndex.ts
new file mode 100644
index 00000000000..01e930543c3
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/record-table-row/hooks/useRecordTableRowIndex.ts
@@ -0,0 +1,12 @@
+import { useRecordTableRowContextOrThrow } from '@/object-record/record-table/contexts/RecordTableRowContext';
+import { recordTableRowIndexOfRecordComponentFamilySelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector';
+import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
+
+export const useRecordTableRowIndex = () => {
+ const { recordId } = useRecordTableRowContextOrThrow();
+
+ return useAtomComponentFamilySelectorValue(
+ recordTableRowIndexOfRecordComponentFamilySelector,
+ recordId,
+ );
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableFirstRowComponentFamilySelector.ts b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableFirstRowComponentFamilySelector.ts
new file mode 100644
index 00000000000..4b71328bb0c
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableFirstRowComponentFamilySelector.ts
@@ -0,0 +1,16 @@
+import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
+import { recordTableRowIndexOfRecordComponentFamilySelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector';
+import { createAtomComponentFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilySelector';
+
+export const isRecordTableFirstRowComponentFamilySelector =
+ createAtomComponentFamilySelector<boolean, string>({
+ key: 'isRecordTableFirstRowComponentFamilySelector',
+ componentInstanceContext: RecordTableComponentInstanceContext,
+ get:
+ ({ instanceId, familyKey }) =>
+ ({ get }) =>
+ get(recordTableRowIndexOfRecordComponentFamilySelector, {
+ instanceId,
+ familyKey,
+ }) === 0,
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableRowActiveByRecordIdComponentFamilySelector.ts b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableRowActiveByRecordIdComponentFamilySelector.ts
new file mode 100644
index 00000000000..2be646118a8
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableRowActiveByRecordIdComponentFamilySelector.ts
@@ -0,0 +1,30 @@
+import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
+import { isRecordTableRowActiveComponentFamilyState } from '@/object-record/record-table/states/isRecordTableRowActiveComponentFamilyState';
+import { recordTableRowIndexOfRecordComponentFamilySelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector';
+import { createAtomComponentFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilySelector';
+import { isDefined } from 'twenty-shared/utils';
+
+export const isRecordTableRowActiveByRecordIdComponentFamilySelector =
+ createAtomComponentFamilySelector<boolean, string>({
+ key: 'isRecordTableRowActiveByRecordIdComponentFamilySelector',
+ componentInstanceContext: RecordTableComponentInstanceContext,
+ get:
+ ({ instanceId, familyKey }) =>
+ ({ get }) => {
+ const rowIndex = get(
+ recordTableRowIndexOfRecordComponentFamilySelector,
+ {
+ instanceId,
+ familyKey,
+ },
+ );
+
+ return (
+ isDefined(rowIndex) &&
+ get(isRecordTableRowActiveComponentFamilyState, {
+ instanceId,
+ familyKey: rowIndex,
+ })
+ );
+ },
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableRowFocusedByRecordIdComponentFamilySelector.ts b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableRowFocusedByRecordIdComponentFamilySelector.ts
new file mode 100644
index 00000000000..c064ad62100
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/isRecordTableRowFocusedByRecordIdComponentFamilySelector.ts
@@ -0,0 +1,30 @@
+import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
+import { isRecordTableRowFocusedComponentFamilyState } from '@/object-record/record-table/states/isRecordTableRowFocusedComponentFamilyState';
+import { recordTableRowIndexOfRecordComponentFamilySelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector';
+import { createAtomComponentFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilySelector';
+import { isDefined } from 'twenty-shared/utils';
+
+export const isRecordTableRowFocusedByRecordIdComponentFamilySelector =
+ createAtomComponentFamilySelector<boolean, string>({
+ key: 'isRecordTableRowFocusedByRecordIdComponentFamilySelector',
+ componentInstanceContext: RecordTableComponentInstanceContext,
+ get:
+ ({ instanceId, familyKey }) =>
+ ({ get }) => {
+ const rowIndex = get(
+ recordTableRowIndexOfRecordComponentFamilySelector,
+ {
+ instanceId,
+ familyKey,
+ },
+ );
+
+ return (
+ isDefined(rowIndex) &&
+ get(isRecordTableRowFocusedComponentFamilyState, {
+ instanceId,
+ familyKey: rowIndex,
+ })
+ );
+ },
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/states/selectors/recordTableRowIndexByRecordIdComponentSelector.ts b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/recordTableRowIndexByRecordIdComponentSelector.ts
new file mode 100644
index 00000000000..4ae6e359a9c
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/recordTableRowIndexByRecordIdComponentSelector.ts
@@ -0,0 +1,24 @@
+import { recordIndexAllRecordIdsComponentSelector } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
+import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
+import { createAtomComponentSelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentSelector';
+
+export const recordTableRowIndexByRecordIdComponentSelector =
+ createAtomComponentSelector<Map<string, number>>({
+ key: 'recordTableRowIndexByRecordIdComponentSelector',
+ componentInstanceContext: RecordTableComponentInstanceContext,
+ get:
+ ({ instanceId }) =>
+ ({ get }) => {
+ const allRecordIds = get(recordIndexAllRecordIdsComponentSelector, {
+ instanceId,
+ });
+
+ const rowIndexByRecordId = new Map<string, number>();
+
+ allRecordIds.forEach((recordId, rowIndex) => {
+ rowIndexByRecordId.set(recordId, rowIndex);
+ });
+
+ return rowIndexByRecordId;
+ },
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector.ts b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector.ts
new file mode 100644
index 00000000000..9ffbf18a69a
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector.ts
@@ -0,0 +1,15 @@
+import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
+import { recordTableRowIndexByRecordIdComponentSelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexByRecordIdComponentSelector';
+import { createAtomComponentFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilySelector';
+
+export const recordTableRowIndexOfRecordComponentFamilySelector =
+ createAtomComponentFamilySelector<number | null, string>({
+ key: 'recordTableRowIndexOfRecordComponentFamilySelector',
+ componentInstanceContext: RecordTableComponentInstanceContext,
+ get:
+ ({ instanceId, familyKey }) =>
+ ({ get }) =>
+ get(recordTableRowIndexByRecordIdComponentSelector, {
+ instanceId,
+ }).get(familyKey) ?? null,
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/types/RecordTableRowDragData.ts b/packages/twenty-front/src/modules/object-record/record-table/types/RecordTableRowDragData.ts
deleted file mode 100644
index c5b357afaf2..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/types/RecordTableRowDragData.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import { type RecordDragData } from '@/object-record/record-drag/types/RecordDragData';
-
-export type RecordTableRowDragData = RecordDragData & {
- focusIndex: number;
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/utils/__tests__/getRecordTableCellId.test.ts b/packages/twenty-front/src/modules/object-record/record-table/utils/__tests__/getRecordTableCellId.test.ts
index e81c8ef64c0..740f40a4a49 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/utils/__tests__/getRecordTableCellId.test.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/utils/__tests__/getRecordTableCellId.test.ts
@@ -2,14 +2,12 @@ import { getRecordTableCellId } from '@/object-record/record-table/utils/getReco
describe('getRecordTableCellId', () => {
it('returns the correct cell id format', () => {
- expect(getRecordTableCellId('my-table', 2, 5)).toBe(
- 'record-table-cell-my-table-2-5',
- );
- });
-
- it('returns the correct cell id format with different values', () => {
- expect(getRecordTableCellId('other-table', 0, 0)).toBe(
- 'record-table-cell-other-table-0-0',
- );
+ expect(
+ getRecordTableCellId({
+ recordTableId: 'my-table',
+ columnIndex: 2,
+ recordId: 'record-id',
+ }),
+ ).toBe('record-table-cell-my-table-2-record-id');
});
});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/utils/getRecordTableCellId.ts b/packages/twenty-front/src/modules/object-record/record-table/utils/getRecordTableCellId.ts
index cf1fae243ae..e4a993b392d 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/utils/getRecordTableCellId.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/utils/getRecordTableCellId.ts
@@ -1,7 +1,9 @@
-export const getRecordTableCellId = (
- recordTableId: string,
- column: number,
- row: number,
-): string => {
- return `record-table-cell-${recordTableId}-${column}-${row}`;
-};
+export const getRecordTableCellId = ({
+ recordTableId,
+ columnIndex,
+ recordId,
+}: {
+ recordTableId: string;
+ columnIndex: number;
+ recordId: string;
+}): string => `record-table-cell-${recordTableId}-${columnIndex}-${recordId}`;
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableNoRecordGroupScrollToPreviousRecordEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableNoRecordGroupScrollToPreviousRecordEffect.tsx
index 182b69b2f3c..b8979afe3b1 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableNoRecordGroupScrollToPreviousRecordEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableNoRecordGroupScrollToPreviousRecordEffect.tsx
@@ -2,7 +2,7 @@ import { lastShowPageRecordIdState } from '@/object-record/record-field/ui/state
import { recordIndexAllRecordIdsComponentSelector } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
import { useScrollTableToPosition } from '@/object-record/record-table/hooks/useScrollTableToPosition';
-import { useProcessTreadmillScrollTop } from '@/object-record/record-table/virtualization/hooks/useProcessTreadmillScrollTop';
+import { useProcessScrollTop } from '@/object-record/record-table/virtualization/hooks/useProcessScrollTop';
import { useTriggerFetchPages } from '@/object-record/record-table/virtualization/hooks/useTriggerFetchPages';
import { useTriggerInitialRecordTableDataLoad } from '@/object-record/record-table/virtualization/hooks/useTriggerInitialRecordTableDataLoad';
import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
@@ -32,7 +32,7 @@ export const RecordTableNoRecordGroupScrollToPreviousRecordEffect = () => {
const { triggerInitialRecordTableDataLoad } =
useTriggerInitialRecordTableDataLoad();
- const { processTreadmillScrollTop } = useProcessTreadmillScrollTop();
+ const { processScrollTop } = useProcessScrollTop();
const { triggerFetchPagesWithoutDebounce } = useTriggerFetchPages();
@@ -79,7 +79,7 @@ export const RecordTableNoRecordGroupScrollToPreviousRecordEffect = () => {
verticalScrollInPx: targetScrollPositionInPx,
});
- processTreadmillScrollTop(targetScrollPositionInPx);
+ processScrollTop(targetScrollPositionInPx);
setHasInitializedScroll(true);
@@ -94,7 +94,7 @@ export const RecordTableNoRecordGroupScrollToPreviousRecordEffect = () => {
scrollTableToPosition,
allRecordIds,
triggerInitialRecordTableDataLoad,
- processTreadmillScrollTop,
+ processScrollTop,
getScrollWrapperElement,
triggerFetchPagesWithoutDebounce,
]);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlot.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlot.tsx
new file mode 100644
index 00000000000..cba1648e501
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlot.tsx
@@ -0,0 +1,35 @@
+import { isDefined } from 'twenty-shared/utils';
+
+import { RecordTableRowSlotContainer } from '@/object-record/record-table/virtualization/components/RecordTableRowSlotContainer';
+import { RecordTableRowSlotRecord } from '@/object-record/record-table/virtualization/components/RecordTableRowSlotRecord';
+import { RecordTableRowVirtualizedSkeleton } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedSkeleton';
+import { getRowSlotTop } from '@/object-record/record-table/virtualization/utils/getRowSlotTop';
+import { rowSlotBindingComponentFamilyState } from '@/object-record/record-table/virtualization/states/rowSlotBindingComponentFamilyState';
+import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
+
+type RecordTableRowSlotProps = {
+ rowSlot: number;
+};
+
+export const RecordTableRowSlot = ({ rowSlot }: RecordTableRowSlotProps) => {
+ const rowSlotBinding = useAtomComponentFamilyStateValue(
+ rowSlotBindingComponentFamilyState,
+ rowSlot,
+ );
+
+ if (!isDefined(rowSlotBinding)) {
+ return null;
+ }
+
+ if (rowSlotBinding.type === 'record') {
+ return <RecordTableRowSlotRecord recordId={rowSlotBinding.recordId} />;
+ }
+
+ return (
+ <RecordTableRowSlotContainer
+ style={{ top: getRowSlotTop(rowSlotBinding.rowIndex) }}
+ >
+ <RecordTableRowVirtualizedSkeleton />
+ </RecordTableRowSlotContainer>
+ );
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotContainer.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotContainer.tsx
new file mode 100644
index 00000000000..2ee1c933dec
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotContainer.tsx
@@ -0,0 +1,30 @@
+import { styled } from '@linaria/react';
+import { type CSSProperties, type ReactNode, type Ref } from 'react';
+
+import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
+
+// top rather than transform: a transformed ancestor would anchor the cells' fixed-position dropdowns
+const StyledRowSlotContainer = styled.div`
+ height: ${RECORD_TABLE_ROW_HEIGHT + 1}px;
+ position: absolute;
+`;
+
+type RecordTableRowSlotContainerProps = {
+ ref?: Ref<HTMLDivElement>;
+ style?: CSSProperties;
+ children: ReactNode;
+};
+
+export const RecordTableRowSlotContainer = ({
+ ref,
+ style,
+ children,
+}: RecordTableRowSlotContainerProps) => (
+ <StyledRowSlotContainer
+ ref={ref}
+ style={style}
+ data-replay-ignore-mutations="true"
+ >
+ {children}
+ </StyledRowSlotContainer>
+);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotPositionEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotPositionEffect.tsx
new file mode 100644
index 00000000000..e55c6829ba6
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotPositionEffect.tsx
@@ -0,0 +1,47 @@
+import { useStore } from 'jotai';
+import { type RefObject, useLayoutEffect } from 'react';
+import { isDefined } from 'twenty-shared/utils';
+
+import { recordTableRowIndexOfRecordComponentFamilySelector } from '@/object-record/record-table/states/selectors/recordTableRowIndexOfRecordComponentFamilySelector';
+import { getRowSlotTop } from '@/object-record/record-table/virtualization/utils/getRowSlotTop';
+import { useAtomComponentFamilySelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorCallbackState';
+
+type RecordTableRowSlotPositionEffectProps = {
+ recordId: string;
+ rowSlotElementRef: RefObject<HTMLDivElement | null>;
+};
+
+// The row index is written to the DOM directly so a shift never re-renders the row
+export const RecordTableRowSlotPositionEffect = ({
+ recordId,
+ rowSlotElementRef,
+}: RecordTableRowSlotPositionEffectProps) => {
+ const store = useStore();
+
+ const rowIndexOfRecordCallbackState =
+ useAtomComponentFamilySelectorCallbackState(
+ recordTableRowIndexOfRecordComponentFamilySelector,
+ );
+
+ useLayoutEffect(() => {
+ const rowIndexAtom = rowIndexOfRecordCallbackState(recordId);
+
+ const applyRowIndex = () => {
+ const rowSlotElement = rowSlotElementRef.current;
+ const rowIndex = store.get(rowIndexAtom);
+
+ if (!isDefined(rowSlotElement)) {
+ return;
+ }
+
+ rowSlotElement.style.display = isDefined(rowIndex) ? '' : 'none';
+ rowSlotElement.style.top = `${getRowSlotTop(rowIndex ?? 0)}px`;
+ };
+
+ applyRowIndex();
+
+ return store.sub(rowIndexAtom, applyRowIndex);
+ }, [recordId, rowIndexOfRecordCallbackState, rowSlotElementRef, store]);
+
+ return null;
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotRecord.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotRecord.tsx
new file mode 100644
index 00000000000..afc781ff0cc
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotRecord.tsx
@@ -0,0 +1,27 @@
+import { useRef } from 'react';
+
+import { RecordTableRowSlotContainer } from '@/object-record/record-table/virtualization/components/RecordTableRowSlotContainer';
+import { RecordTableRowSlotPositionEffect } from '@/object-record/record-table/virtualization/components/RecordTableRowSlotPositionEffect';
+import { RecordTableRowSlotRecordContent } from '@/object-record/record-table/virtualization/components/RecordTableRowSlotRecordContent';
+
+type RecordTableRowSlotRecordProps = {
+ recordId: string;
+};
+
+export const RecordTableRowSlotRecord = ({
+ recordId,
+}: RecordTableRowSlotRecordProps) => {
+ const rowSlotElementRef = useRef<HTMLDivElement>(null);
+
+ return (
+ <>
+ <RecordTableRowSlotContainer ref={rowSlotElementRef}>
+ <RecordTableRowSlotRecordContent recordId={recordId} />
+ </RecordTableRowSlotContainer>
+ <RecordTableRowSlotPositionEffect
+ recordId={recordId}
+ rowSlotElementRef={rowSlotElementRef}
+ />
+ </>
+ );
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedFullData.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotRecordContent.tsx
similarity index 52%
rename from packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedFullData.tsx
rename to packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotRecordContent.tsx
index 8efde4a13bb..263a0b258eb 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedFullData.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowSlotRecordContent.tsx
@@ -3,46 +3,39 @@ import { RecordTableRowCells } from '@/object-record/record-table/record-table-r
import { RecordTableStaticTr } from '@/object-record/record-table/record-table-row/components/RecordTableStaticTr';
import { isRecordTableDragColumnHiddenComponentState } from '@/object-record/record-table/states/isRecordTableDragColumnHiddenComponentState';
import { RecordTableRowVirtualizedSkeleton } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedSkeleton';
-import { recordIdByRealIndexComponentFamilySelector } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentFamilySelector';
-import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
+import { lowDetailsActivatedComponentState } from '@/object-record/record-table/virtualization/states/lowDetailsActivatedComponentState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
-import { isDefined } from 'twenty-shared/utils';
-type RecordTableRowVirtualizedFullDataProps = {
- realIndex: number;
+type RecordTableRowSlotRecordContentProps = {
+ recordId: string;
};
-export const RecordTableRowVirtualizedFullData = ({
- realIndex,
-}: RecordTableRowVirtualizedFullDataProps) => {
- const isRecordTableDragColumnHidden = useAtomComponentStateValue(
- isRecordTableDragColumnHiddenComponentState,
+export const RecordTableRowSlotRecordContent = ({
+ recordId,
+}: RecordTableRowSlotRecordContentProps) => {
+ const lowDetailsActivated = useAtomComponentStateValue(
+ lowDetailsActivatedComponentState,
);
- const recordId = useAtomComponentFamilySelectorValue(
- recordIdByRealIndexComponentFamilySelector,
- realIndex,
+ const isRecordTableDragColumnHidden = useAtomComponentStateValue(
+ isRecordTableDragColumnHiddenComponentState,
);
- if (!isDefined(recordId)) {
+ if (lowDetailsActivated) {
return <RecordTableRowVirtualizedSkeleton />;
}
if (isRecordTableDragColumnHidden) {
return (
- <RecordTableStaticTr recordId={recordId} focusIndex={realIndex}>
- <RecordTableRowCells rowIndexForFocus={realIndex} />
+ <RecordTableStaticTr recordId={recordId}>
+ <RecordTableRowCells />
</RecordTableStaticTr>
);
}
return (
- <RecordTableDraggableTr
- recordId={recordId}
- draggableIndex={realIndex}
- focusIndex={realIndex}
- >
- <RecordTableRowCells rowIndexForFocus={realIndex} />
+ <RecordTableDraggableTr recordId={recordId}>
+ <RecordTableRowCells />
</RecordTableDraggableTr>
);
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedContainer.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedContainer.tsx
deleted file mode 100644
index 7cf58b29fc8..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedContainer.tsx
+++ /dev/null
@@ -1,64 +0,0 @@
-import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
-import { RecordTableRowVirtualizedDebugRowHelper } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedDebugRowHelper';
-import { RecordTableRowVirtualizedRouterLevel1 } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel1';
-import { TABLE_VIRTUALIZATION_DEBUG_ACTIVATED } from '@/object-record/record-table/virtualization/constants/TableVirtualizationDebugActivated';
-
-import { realIndexByVirtualIndexComponentFamilyState } from '@/object-record/record-table/virtualization/states/realIndexByVirtualIndexComponentFamilyState';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
-
-import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
-import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
-import { styled } from '@linaria/react';
-import { isDefined } from 'twenty-shared/utils';
-
-const StyledVirtualizedRowContainer = styled.div<{
- pixelsFromTop: number;
-}>`
- height: 33px;
- position: absolute;
- top: ${({ pixelsFromTop }) => pixelsFromTop}px;
-`;
-
-type RecordTableRowVirtualizedContainerProps = {
- virtualIndex: number;
-};
-
-export const RecordTableRowVirtualizedContainer = ({
- virtualIndex,
-}: RecordTableRowVirtualizedContainerProps) => {
- const realIndexByVirtualIndex = useAtomComponentFamilyStateValue(
- realIndexByVirtualIndexComponentFamilyState,
- { virtualIndex },
- );
-
- const totalNumberOfRecordsToVirtualize =
- useAtomComponentStateValue(
- totalNumberOfRecordsToVirtualizeComponentState,
- ) ?? 0;
-
- if (
- !isDefined(realIndexByVirtualIndex) ||
- realIndexByVirtualIndex >= totalNumberOfRecordsToVirtualize
- ) {
- return null;
- }
-
- const pixelsFromTop =
- realIndexByVirtualIndex * (RECORD_TABLE_ROW_HEIGHT + 1) +
- (RECORD_TABLE_ROW_HEIGHT + 1);
-
- return (
- <StyledVirtualizedRowContainer
- id={`row-virtual-index-${virtualIndex}`}
- pixelsFromTop={pixelsFromTop}
- data-replay-ignore-mutations="true"
- >
- {TABLE_VIRTUALIZATION_DEBUG_ACTIVATED && (
- <RecordTableRowVirtualizedDebugRowHelper virtualIndex={virtualIndex} />
- )}
- <RecordTableRowVirtualizedRouterLevel1
- realIndex={realIndexByVirtualIndex}
- />
- </StyledVirtualizedRowContainer>
- );
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedDebugRowHelper.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedDebugRowHelper.tsx
deleted file mode 100644
index 29bdd980c6f..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedDebugRowHelper.tsx
+++ /dev/null
@@ -1,120 +0,0 @@
-import { getLabelIdentifierFieldMetadataItem } from '@/object-metadata/utils/getLabelIdentifierFieldMetadataItem';
-import { getLabelIdentifierFieldValue } from '@/object-metadata/utils/getLabelIdentifierFieldValue';
-import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
-import { useAtomFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilyStateValue';
-import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
-import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
-import { dataLoadingStatusByRealIndexComponentFamilySelector } from '@/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentFamilySelector';
-import { realIndexByVirtualIndexComponentFamilyState } from '@/object-record/record-table/virtualization/states/realIndexByVirtualIndexComponentFamilyState';
-import { recordIdByRealIndexComponentFamilySelector } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentFamilySelector';
-
-import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
-import { useAtomComponentFamilyStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateValue';
-import { styled } from '@linaria/react';
-import { isDefined } from 'twenty-shared/utils';
-import { doNotTranslate } from 'twenty-shared/i18n';
-import { themeCssVariables } from 'twenty-ui/theme';
-
-const StyledDebugRow = styled.div`
- background-color: ${themeCssVariables.color.gray3};
- border: 1px solid ${themeCssVariables.color.blue8};
- color: ${themeCssVariables.font.color.primary};
- display: flex;
- left: 250px;
- max-height: ${themeCssVariables.spacing[4]};
- overflow: hidden;
- padding: ${themeCssVariables.spacing['0.5']};
- position: absolute;
- top: 5px;
-
- z-index: 20;
-`;
-
-const StyledDebugColumn = styled.div<{ width: number }>`
- display: flex;
- max-width: ${({ width }) => width}px;
- min-width: ${({ width }) => width}px;
-
- overflow: hidden;
- padding-left: ${themeCssVariables.spacing['0.5']};
- padding-right: ${themeCssVariables.spacing['0.5']};
- text-wrap-mode: nowrap;
-`;
-
-type RecordTableRowVirtualizedDebugRowHelperProps = {
- virtualIndex: number;
-};
-
-export const RecordTableRowVirtualizedDebugRowHelper = ({
- virtualIndex,
-}: RecordTableRowVirtualizedDebugRowHelperProps) => {
- const realIndexByVirtualIndex = useAtomComponentFamilyStateValue(
- realIndexByVirtualIndexComponentFamilyState,
- { virtualIndex },
- );
-
- const recordId = useAtomComponentFamilySelectorValue(
- recordIdByRealIndexComponentFamilySelector,
- realIndexByVirtualIndex,
- );
-
- const dataLoadingStatus = useAtomComponentFamilySelectorValue(
- dataLoadingStatusByRealIndexComponentFamilySelector,
- realIndexByVirtualIndex,
- );
-
- const pixelsFromTop =
- (realIndexByVirtualIndex ?? 0) * (RECORD_TABLE_ROW_HEIGHT + 1) +
- (RECORD_TABLE_ROW_HEIGHT + 1);
-
- const recordStore = useAtomFamilyStateValue(
- recordStoreFamilyState,
- recordId ?? '',
- );
- const { objectMetadataItem } = useRecordTableContextOrThrow();
- const labelIdentifierFieldMetadataItem =
- getLabelIdentifierFieldMetadataItem(objectMetadataItem);
-
- const labelIdentifier = isDefined(recordStore)
- ? getLabelIdentifierFieldValue(
- recordStore,
- labelIdentifierFieldMetadataItem,
- )
- : '-';
-
- const position = recordStore?.position;
-
- return (
- <StyledDebugRow>
- <StyledDebugColumn width={70}>
- {doNotTranslate('virtual :')}
- {virtualIndex}
- </StyledDebugColumn>
- <StyledDebugColumn width={70}>
- {doNotTranslate('real :')}
- {realIndexByVirtualIndex}
- </StyledDebugColumn>
- <StyledDebugColumn width={100}>
- {doNotTranslate('pos :')}
- {position}
- </StyledDebugColumn>
- <StyledDebugColumn width={80}>
- {doNotTranslate('px:')}
- {pixelsFromTop}
- </StyledDebugColumn>
- <StyledDebugColumn width={150}>
- <b>{labelIdentifier}</b>
- </StyledDebugColumn>
- <StyledDebugColumn width={300}>
- {doNotTranslate('id:')}
- {recordId}
- </StyledDebugColumn>
- <StyledDebugColumn width={100}>
- {doNotTranslate('status :')}
- {isDefined(dataLoadingStatus)
- ? dataLoadingStatus
- : doNotTranslate('undefined')}
- </StyledDebugColumn>
- </StyledDebugRow>
- );
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel1.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel1.tsx
deleted file mode 100644
index 34700b6151e..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel1.tsx
+++ /dev/null
@@ -1,23 +0,0 @@
-import { RecordTableRowVirtualizedRouterLevel2 } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel2';
-import { RecordTableRowVirtualizedSkeleton } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedSkeleton';
-import { lowDetailsActivatedComponentState } from '@/object-record/record-table/virtualization/states/lowDetailsActivatedComponentState';
-
-import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
-
-type RecordTableRowVirtualizedRouterLevel1Props = {
- realIndex: number;
-};
-
-export const RecordTableRowVirtualizedRouterLevel1 = ({
- realIndex,
-}: RecordTableRowVirtualizedRouterLevel1Props) => {
- const lowDetailsActivated = useAtomComponentStateValue(
- lowDetailsActivatedComponentState,
- );
-
- if (lowDetailsActivated === true) {
- return <RecordTableRowVirtualizedSkeleton />;
- }
-
- return <RecordTableRowVirtualizedRouterLevel2 realIndex={realIndex} />;
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel2.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel2.tsx
deleted file mode 100644
index d021483e20a..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableRowVirtualizedRouterLevel2.tsx
+++ /dev/null
@@ -1,25 +0,0 @@
-import { RecordTableRowVirtualizedFullData } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedFullData';
-
-import { RecordTableRowVirtualizedSkeleton } from '@/object-record/record-table/virtualization/components/RecordTableRowVirtualizedSkeleton';
-import { dataLoadingStatusByRealIndexComponentFamilySelector } from '@/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentFamilySelector';
-
-import { useAtomComponentFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilySelectorValue';
-
-type RecordTableRowVirtualizedRouterLevel2Props = {
- realIndex: number;
-};
-
-export const RecordTableRowVirtualizedRouterLevel2 = ({
- realIndex,
-}: RecordTableRowVirtualizedRouterLevel2Props) => {
- const dataLoadingStatus = useAtomComponentFamilySelectorValue(
- dataLoadingStatusByRealIndexComponentFamilySelector,
- realIndex,
- );
-
- if (dataLoadingStatus !== 'loaded') {
- return <RecordTableRowVirtualizedSkeleton />;
- }
-
- return <RecordTableRowVirtualizedFullData realIndex={realIndex} />;
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedBodyPlaceholder.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedBodyPlaceholder.tsx
index dd6d5d0cae9..d7d14c7d0cc 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedBodyPlaceholder.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedBodyPlaceholder.tsx
@@ -1,6 +1,6 @@
import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { styled } from '@linaria/react';
import { isDefined } from 'twenty-shared/utils';
@@ -18,12 +18,12 @@ const StyledVirtualizationContainer = styled.div<{
`;
export const RecordTableVirtualizedBodyPlaceholder = () => {
- const totalNumberOfRecordsToVirtualize = useAtomComponentStateValue(
- totalNumberOfRecordsToVirtualizeComponentState,
+ const totalNumberOfRows = useAtomComponentStateValue(
+ totalNumberOfRowsComponentState,
);
- const totalHeight = isDefined(totalNumberOfRecordsToVirtualize)
- ? totalNumberOfRecordsToVirtualize * (RECORD_TABLE_ROW_HEIGHT + 1)
+ const totalHeight = isDefined(totalNumberOfRows)
+ ? totalNumberOfRows * (RECORD_TABLE_ROW_HEIGHT + 1)
: 0;
return <StyledVirtualizationContainer height={totalHeight} />;
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDataChangedEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDataChangedEffect.tsx
index f266e45c5c7..05a7025eb64 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDataChangedEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDataChangedEffect.tsx
@@ -1,11 +1,13 @@
+import { useDebouncedCallback } from 'use-debounce';
+
import { useListenToObjectRecordOperationBrowserEvent } from '@/browser-event/hooks/useListenToObjectRecordOperationBrowserEvent';
+import { type ObjectRecordOperationBrowserEventDetail } from '@/browser-event/types/ObjectRecordOperationBrowserEventDetail';
import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
import { SSE_TABLE_DEBOUNCE_TIME_IN_MS_TO_AVOID_SSE_OWN_EVENTS_RACE_CONDITION } from '@/object-record/record-table/virtualization/constants/SseTableDebounceTimeInMsToAvoidSseOwnEventsRaceCondition';
import { useGetShouldResetTableVirtualizationForUpdateInputs } from '@/object-record/record-table/virtualization/hooks/useGetShouldResetTableVirtualizationForUpdateInputs';
import { useResetVirtualizationBecauseDataChanged } from '@/object-record/record-table/virtualization/hooks/useResetVirtualizationBecauseDataChanged';
-import { type ObjectRecordOperationBrowserEventDetail } from '@/browser-event/types/ObjectRecordOperationBrowserEventDetail';
-import { useDebouncedCallback } from 'use-debounce';
+import { useApplyRecordOperationToRows } from '@/object-record/record-table/virtualization/hooks/useApplyRecordOperationToRows';
export const RecordTableVirtualizedDataChangedEffect = () => {
const { objectMetadataItem } = useRecordIndexContextOrThrow();
@@ -17,12 +19,16 @@ export const RecordTableVirtualizedDataChangedEffect = () => {
const { getShouldResetTableVirtualizationForUpdateInputs } =
useGetShouldResetTableVirtualizationForUpdateInputs();
- const debouncedResertVirtualizationBecauseDataChanged = useDebouncedCallback(
+ const {
+ forgetAppliedDeletionOfRecords,
+ insertRowForCreatedRecord,
+ removeRowsOfDeletedRecords,
+ } = useApplyRecordOperationToRows();
+
+ const debouncedResetVirtualizationBecauseDataChanged = useDebouncedCallback(
resetVirtualizationBecauseDataChanged,
SSE_TABLE_DEBOUNCE_TIME_IN_MS_TO_AVOID_SSE_OWN_EVENTS_RACE_CONDITION,
- {
- leading: false,
- },
+ { leading: false },
);
const handleObjectRecordOperation = (
@@ -30,24 +36,57 @@ export const RecordTableVirtualizedDataChangedEffect = () => {
) => {
const objectRecordOperation = objectRecordOperationEventDetail.operation;
- const isUpdateOperation =
- objectRecordOperation.type === 'update-one' ||
- objectRecordOperation.type === 'update-many';
+ switch (objectRecordOperation.type) {
+ case 'update-one':
+ case 'update-many': {
+ const updateInputs =
+ objectRecordOperation.type === 'update-one'
+ ? [objectRecordOperation.result.updateInput]
+ : objectRecordOperation.result.updateInputs;
+
+ if (getShouldResetTableVirtualizationForUpdateInputs(updateInputs)) {
+ debouncedResetVirtualizationBecauseDataChanged();
+ }
- if (isUpdateOperation) {
- const updateInputs =
- objectRecordOperation.type === 'update-one'
- ? [objectRecordOperation.result.updateInput]
- : objectRecordOperation.result.updateInputs;
+ return;
+ }
+ case 'create-one': {
+ if (!insertRowForCreatedRecord(objectRecordOperation.createdRecord)) {
+ debouncedResetVirtualizationBecauseDataChanged();
+ }
+
+ return;
+ }
+ case 'delete-one':
+ case 'delete-many': {
+ const deletedRecordIds =
+ objectRecordOperation.type === 'delete-one'
+ ? [objectRecordOperation.deletedRecordId]
+ : objectRecordOperation.deletedRecordIds;
- const shouldResetForUpdateOperation =
- getShouldResetTableVirtualizationForUpdateInputs(updateInputs);
+ if (!removeRowsOfDeletedRecords(deletedRecordIds)) {
+ debouncedResetVirtualizationBecauseDataChanged();
+ }
- if (shouldResetForUpdateOperation) {
- debouncedResertVirtualizationBecauseDataChanged();
+ return;
+ }
+ case 'restore-one':
+ case 'restore-many': {
+ const restoredRecords =
+ objectRecordOperation.type === 'restore-one'
+ ? [objectRecordOperation.restoredRecord]
+ : objectRecordOperation.restoredRecords;
+
+ forgetAppliedDeletionOfRecords(
+ restoredRecords.map((record) => record.id),
+ );
+ debouncedResetVirtualizationBecauseDataChanged();
+
+ return;
+ }
+ default: {
+ debouncedResetVirtualizationBecauseDataChanged();
}
- } else {
- debouncedResertVirtualizationBecauseDataChanged();
}
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDebugHelper.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDebugHelper.tsx
index d3ce2ac1b98..94bd9665909 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDebugHelper.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedDebugHelper.tsx
@@ -1,7 +1,7 @@
import {
SCROLL_SPEED_THRESHOLD_IN_ROWS_PER_SECOND_TO_ACTIVATE_LOW_DETAILS,
SCROLL_SPEED_THRESHOLD_IN_ROWS_PER_SECOND_TO_DEACTIVATE_LOW_DETAILS,
-} from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedRowTreadmillEffect';
+} from '@/object-record/record-table/virtualization/components/RecordTableVirtualizedScrollEffect';
import { TABLE_VIRTUALIZATION_DEBUG_ACTIVATED } from '@/object-record/record-table/virtualization/constants/TableVirtualizationDebugActivated';
import { lowDetailsActivatedComponentState } from '@/object-record/record-table/virtualization/states/lowDetailsActivatedComponentState';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedRowTreadmillEffect.tsx b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedScrollEffect.tsx
similarity index 76%
rename from packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedRowTreadmillEffect.tsx
rename to packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedScrollEffect.tsx
index b7a2fd089f7..0a22052ec41 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedRowTreadmillEffect.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/components/RecordTableVirtualizedScrollEffect.tsx
@@ -2,13 +2,13 @@ import { useCallback, useEffect } from 'react';
import { useStore } from 'jotai';
import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
-import { NUMBER_OF_VIRTUALIZED_ROWS } from '@/object-record/record-table/virtualization/constants/NumberOfVirtualizedRows';
-import { useProcessTreadmillScrollTop } from '@/object-record/record-table/virtualization/hooks/useProcessTreadmillScrollTop';
+import { ROW_SLOT_COUNT } from '@/object-record/record-table/virtualization/constants/RowSlotCount';
+import { useProcessScrollTop } from '@/object-record/record-table/virtualization/hooks/useProcessScrollTop';
import { useTriggerFetchPages } from '@/object-record/record-table/virtualization/hooks/useTriggerFetchPages';
import { lastScrollMeasurementsComponentState } from '@/object-record/record-table/virtualization/states/lastScrollMeasurementsComponentState';
import { lowDetailsActivatedComponentState } from '@/object-record/record-table/virtualization/states/lowDetailsActivatedComponentState';
-import { scrollAtRealIndexComponentState } from '@/object-record/record-table/virtualization/states/scrollAtRealIndexComponentState';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { scrollAtRowIndexComponentState } from '@/object-record/record-table/virtualization/states/scrollAtRowIndexComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
import { useDebouncedCallback } from 'use-debounce';
@@ -22,11 +22,11 @@ export const NUMBER_OF_EVENTS_TO_COMPUTE_AVERAGE = 10;
const TIME_BETWEEN_TWO_SCROLL_HANDLING = 20;
const LAST_SCROLL_DEBOUNCE_TIME = 300;
-export const RecordTableVirtualizedRowTreadmillEffect = () => {
+export const RecordTableVirtualizedScrollEffect = () => {
const { scrollWrapperHTMLElement } = useScrollWrapperHTMLElement();
- const scrollAtRealIndexCallbackState = useAtomComponentStateCallbackState(
- scrollAtRealIndexComponentState,
+ const scrollAtRowIndexCallbackState = useAtomComponentStateCallbackState(
+ scrollAtRowIndexComponentState,
);
const lowDetailsActivatedCallbackState = useAtomComponentStateCallbackState(
@@ -36,19 +36,18 @@ export const RecordTableVirtualizedRowTreadmillEffect = () => {
const lastScrollMeasurementsCallbackState =
useAtomComponentStateCallbackState(lastScrollMeasurementsComponentState);
- const totalNumberOfRecordsToVirtualizeCallbackState =
- useAtomComponentStateCallbackState(
- totalNumberOfRecordsToVirtualizeComponentState,
- );
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
+ );
const { triggerFetchPages } = useTriggerFetchPages();
- const { processTreadmillScrollTop } = useProcessTreadmillScrollTop();
+ const { processScrollTop } = useProcessScrollTop();
const store = useStore();
const handleScrollDebounced = useDebouncedCallback(
- processTreadmillScrollTop,
+ processScrollTop,
TIME_BETWEEN_TWO_SCROLL_HANDLING,
{
leading: true,
@@ -82,16 +81,12 @@ export const RecordTableVirtualizedRowTreadmillEffect = () => {
lowDetailsActivatedCallbackState,
);
- const scrollAtRealIndex = store.get(scrollAtRealIndexCallbackState);
+ const scrollAtRowIndex = store.get(scrollAtRowIndexCallbackState);
- const totalNumberOfRecordsToVirtualize =
- store.get(totalNumberOfRecordsToVirtualizeCallbackState) ??
- NUMBER_OF_VIRTUALIZED_ROWS;
+ const totalNumberOfRows =
+ store.get(totalNumberOfRowsCallbackState) ?? ROW_SLOT_COUNT;
- if (
- scrollAtRealIndex < 100 ||
- scrollAtRealIndex > totalNumberOfRecordsToVirtualize - 100
- ) {
+ if (scrollAtRowIndex < 100 || scrollAtRowIndex > totalNumberOfRows - 100) {
return;
}
@@ -101,23 +96,23 @@ export const RecordTableVirtualizedRowTreadmillEffect = () => {
}, [
lowDetailsActivatedCallbackState,
deactivateLowDetailsDebounced,
- scrollAtRealIndexCallbackState,
- totalNumberOfRecordsToVirtualizeCallbackState,
+ scrollAtRowIndexCallbackState,
+ totalNumberOfRowsCallbackState,
store,
]);
const handleAfterLastScroll = useCallback(
- (scrollEvent: Event) => {
+ (scrollTop: number) => {
deactivateLowDetails();
- processTreadmillScrollTop((scrollEvent.target as any).scrollTop);
+ processScrollTop(scrollTop);
triggerFetchPages();
store.set(lastScrollMeasurementsCallbackState, []);
},
[
- processTreadmillScrollTop,
+ processScrollTop,
triggerFetchPages,
deactivateLowDetails,
lastScrollMeasurementsCallbackState,
@@ -131,16 +126,14 @@ export const RecordTableVirtualizedRowTreadmillEffect = () => {
);
const handleScrollData = useCallback(
- (scrollEvent: Event) => {
- const distanceFromTop = (scrollEvent.target as any).scrollTop;
-
+ ({ scrollTop, timeStamp }: { scrollTop: number; timeStamp: number }) => {
const lastScrollMeasurements = store
.get(lastScrollMeasurementsCallbackState)
.concat();
lastScrollMeasurements.push({
- scrollToTop: distanceFromTop,
- timestamp: scrollEvent.timeStamp,
+ scrollToTop: scrollTop,
+ timestamp: timeStamp,
});
store.set(lastScrollMeasurementsCallbackState, lastScrollMeasurements);
@@ -214,9 +207,15 @@ export const RecordTableVirtualizedRowTreadmillEffect = () => {
const handleScrollEvent = useCallback(
(scrollEvent: Event) => {
- handleScrollData(scrollEvent);
- handleScrollDebounced((scrollEvent.target as any).scrollTop);
- handleAfterLastScrollDebounced(scrollEvent);
+ if (!(scrollEvent.target instanceof Element)) {
+ return;
+ }
+
+ const { scrollTop } = scrollEvent.target;
+
+ handleScrollData({ scrollTop, timeStamp: scrollEvent.timeStamp });
+ handleScrollDebounced(scrollTop);
+ handleAfterLastScrollDebounced(scrollTop);
},
[handleScrollDebounced, handleScrollData, handleAfterLastScrollDebounced],
);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/NumberOfVirtualizedRows.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/NumberOfVirtualizedRows.ts
deleted file mode 100644
index 4b89cde38f6..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/NumberOfVirtualizedRows.ts
+++ /dev/null
@@ -1 +0,0 @@
-export const NUMBER_OF_VIRTUALIZED_ROWS = 240;
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/RowSlotCount.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/RowSlotCount.ts
new file mode 100644
index 00000000000..fb05f1f9591
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/RowSlotCount.ts
@@ -0,0 +1 @@
+export const ROW_SLOT_COUNT = 240;
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/RowSlots.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/RowSlots.ts
new file mode 100644
index 00000000000..01096ad1559
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/constants/RowSlots.ts
@@ -0,0 +1,5 @@
+import { getContiguousIncrementalValues } from 'twenty-shared/utils';
+
+import { ROW_SLOT_COUNT } from '@/object-record/record-table/virtualization/constants/RowSlotCount';
+
+export const ROW_SLOTS = getContiguousIncrementalValues(ROW_SLOT_COUNT);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useAllocateRowSlots.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useAllocateRowSlots.ts
new file mode 100644
index 00000000000..d6d762427b8
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useAllocateRowSlots.ts
@@ -0,0 +1,74 @@
+import { useStore } from 'jotai';
+import { useCallback } from 'react';
+
+import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
+import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
+import { ROW_SLOTS } from '@/object-record/record-table/virtualization/constants/RowSlots';
+import { lastScrollPositionComponentState } from '@/object-record/record-table/virtualization/states/lastScrollPositionComponentState';
+import { rowSlotBindingComponentFamilyState } from '@/object-record/record-table/virtualization/states/rowSlotBindingComponentFamilyState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
+import { computeRowSlotBindings } from '@/object-record/record-table/virtualization/utils/computeRowSlotBindings';
+import { getRowSlotWindow } from '@/object-record/record-table/virtualization/utils/getRowSlotWindow';
+import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
+import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
+import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
+
+export const useAllocateRowSlots = () => {
+ const store = useStore();
+ const { getScrollWrapperElement } = useScrollWrapperHTMLElement();
+
+ const rowSlotBindingCallbackState = useAtomComponentFamilyStateCallbackState(
+ rowSlotBindingComponentFamilyState,
+ );
+
+ const recordIdsByGroupCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ recordIndexRecordIdsByGroupComponentFamilyState,
+ );
+
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
+ );
+
+ const lastScrollPositionCallbackState = useAtomComponentStateCallbackState(
+ lastScrollPositionComponentState,
+ );
+
+ const allocateRowSlots = useCallback(() => {
+ const { scrollWrapperElement } = getScrollWrapperElement();
+
+ const { firstRowIndex, lastRowIndex } = getRowSlotWindow({
+ scrollTop: store.get(lastScrollPositionCallbackState),
+ viewportHeight: scrollWrapperElement?.clientHeight ?? 0,
+ totalNumberOfRows: store.get(totalNumberOfRowsCallbackState) ?? 0,
+ });
+
+ const currentBindings = ROW_SLOTS.map((rowSlot) =>
+ store.get(rowSlotBindingCallbackState(rowSlot)),
+ );
+
+ const nextBindings = computeRowSlotBindings({
+ currentBindings,
+ recordIdsByRowIndex: store.get(
+ recordIdsByGroupCallbackState(NO_RECORD_GROUP_FAMILY_KEY),
+ ),
+ firstRowIndex,
+ lastRowIndex,
+ });
+
+ for (const rowSlot of ROW_SLOTS) {
+ if (nextBindings[rowSlot] !== currentBindings[rowSlot]) {
+ store.set(rowSlotBindingCallbackState(rowSlot), nextBindings[rowSlot]);
+ }
+ }
+ }, [
+ getScrollWrapperElement,
+ lastScrollPositionCallbackState,
+ recordIdsByGroupCallbackState,
+ rowSlotBindingCallbackState,
+ store,
+ totalNumberOfRowsCallbackState,
+ ]);
+
+ return { allocateRowSlots };
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useApplyRecordOperationToRows.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useApplyRecordOperationToRows.ts
new file mode 100644
index 00000000000..df85f282961
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useApplyRecordOperationToRows.ts
@@ -0,0 +1,216 @@
+import { useStore } from 'jotai';
+import { type ObjectRecord } from 'twenty-shared/types';
+import { isDefined } from 'twenty-shared/utils';
+
+import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
+import { hasAnySoftDeleteFilterOnViewComponentSelector } from '@/object-record/record-filter/states/hasAnySoftDeleteFilterOnView';
+import { isRecordMatchingFilter } from '@/object-record/record-filter/utils/isRecordMatchingFilter';
+import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
+import { useFindManyRecordIndexTableParams } from '@/object-record/record-index/hooks/useFindManyRecordIndexTableParams';
+import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
+import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
+import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
+import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
+import { useAllocateRowSlots } from '@/object-record/record-table/virtualization/hooks/useAllocateRowSlots';
+import { useTriggerFetchPages } from '@/object-record/record-table/virtualization/hooks/useTriggerFetchPages';
+import { numberOfPendingPageFetchesComponentState } from '@/object-record/record-table/virtualization/states/numberOfPendingPageFetchesComponentState';
+import { appliedRecordOperationKeysComponentState } from '@/object-record/record-table/virtualization/states/appliedRecordOperationKeysComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
+import { insertEmptyRowAtIndex } from '@/object-record/record-table/virtualization/utils/insertEmptyRowAtIndex';
+import { getAppliedRecordOperationKey } from '@/object-record/record-table/virtualization/utils/getAppliedRecordOperationKey';
+import { getRowIndexToInsertRecord } from '@/object-record/record-table/virtualization/utils/getRowIndexToInsertRecord';
+import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
+import { useAtomComponentSelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorCallbackState';
+import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
+
+export const useApplyRecordOperationToRows = () => {
+ const store = useStore();
+ const { objectMetadataItem, recordLimit } = useRecordIndexContextOrThrow();
+ const { objectNameSingular } = useRecordTableContextOrThrow();
+ const { objectMetadataItems } = useObjectMetadataItems();
+ const { filter, orderBy } =
+ useFindManyRecordIndexTableParams(objectNameSingular);
+
+ const recordIdsByGroupCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ recordIndexRecordIdsByGroupComponentFamilyState,
+ );
+
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
+ );
+
+ const hasAnySoftDeleteFilterOnViewCallbackState =
+ useAtomComponentSelectorCallbackState(
+ hasAnySoftDeleteFilterOnViewComponentSelector,
+ );
+
+ const numberOfPendingPageFetchesCallbackState =
+ useAtomComponentStateCallbackState(
+ numberOfPendingPageFetchesComponentState,
+ );
+
+ const appliedRecordOperationKeysCallbackState =
+ useAtomComponentStateCallbackState(
+ appliedRecordOperationKeysComponentState,
+ );
+
+ const { allocateRowSlots } = useAllocateRowSlots();
+ const { triggerFetchPages } = useTriggerFetchPages();
+
+ const canShiftRows = () =>
+ !isDefined(recordLimit) &&
+ !store.get(hasAnySoftDeleteFilterOnViewCallbackState) &&
+ store.get(numberOfPendingPageFetchesCallbackState) === 0;
+
+ const markOperationAsApplied = (operationKey: string) =>
+ store.set(appliedRecordOperationKeysCallbackState, (operationKeys) =>
+ new Set(operationKeys).add(operationKey),
+ );
+
+ const insertRowForCreatedRecord = (createdRecord: ObjectRecord): boolean => {
+ const operationKey = getAppliedRecordOperationKey({
+ operationType: 'create',
+ recordId: createdRecord.id,
+ });
+
+ if (store.get(appliedRecordOperationKeysCallbackState).has(operationKey)) {
+ return true;
+ }
+
+ const recordIdsCallbackState = recordIdsByGroupCallbackState(
+ NO_RECORD_GROUP_FAMILY_KEY,
+ );
+
+ const recordIds = store.get(recordIdsCallbackState);
+ const loadedRowIndex = recordIds.indexOf(createdRecord.id);
+
+ if (loadedRowIndex >= 0) {
+ store.set(totalNumberOfRowsCallbackState, (totalNumberOfRows) =>
+ Math.max(totalNumberOfRows ?? 0, loadedRowIndex + 1),
+ );
+ markOperationAsApplied(operationKey);
+
+ return true;
+ }
+
+ if (!canShiftRows()) {
+ return false;
+ }
+
+ try {
+ // false is not proof: unloaded relation values make the filter reject matching records
+ if (
+ !isRecordMatchingFilter({
+ record: createdRecord,
+ filter,
+ objectMetadataItem,
+ objectMetadataItems,
+ })
+ ) {
+ return false;
+ }
+ } catch {
+ return false;
+ }
+
+ const loadedRows: { rowIndex: number; position: number | null }[] = [];
+
+ recordIds.forEach((recordId, rowIndex) => {
+ const record = store.get(recordStoreFamilyState.atomFamily(recordId));
+
+ if (isDefined(record)) {
+ loadedRows.push({ rowIndex, position: record.position });
+ }
+ });
+
+ const totalNumberOfRows = store.get(totalNumberOfRowsCallbackState) ?? 0;
+
+ const rowIndexToInsert = getRowIndexToInsertRecord({
+ loadedRows,
+ position: createdRecord.position,
+ totalNumberOfRows,
+ orderBy,
+ });
+
+ if (!isDefined(rowIndexToInsert)) {
+ return false;
+ }
+
+ store.set(
+ recordIdsCallbackState,
+ insertEmptyRowAtIndex({
+ recordIdsByRowIndex: recordIds,
+ rowIndex: rowIndexToInsert,
+ }),
+ );
+
+ store.set(totalNumberOfRowsCallbackState, totalNumberOfRows + 1);
+ markOperationAsApplied(operationKey);
+
+ allocateRowSlots();
+ triggerFetchPages();
+
+ return true;
+ };
+
+ const removeRowsOfDeletedRecords = (deletedRecordIds: string[]): boolean => {
+ const recordIdsCallbackState = recordIdsByGroupCallbackState(
+ NO_RECORD_GROUP_FAMILY_KEY,
+ );
+
+ for (const deletedRecordId of deletedRecordIds) {
+ const operationKey = getAppliedRecordOperationKey({
+ operationType: 'delete',
+ recordId: deletedRecordId,
+ });
+
+ if (
+ store.get(appliedRecordOperationKeysCallbackState).has(operationKey)
+ ) {
+ continue;
+ }
+
+ const recordIds = store.get(recordIdsCallbackState);
+ const loadedRowIndex = recordIds.indexOf(deletedRecordId);
+
+ if (!canShiftRows() || loadedRowIndex < 0) {
+ return false;
+ }
+
+ const nextRecordIds = recordIds.slice();
+
+ nextRecordIds.splice(loadedRowIndex, 1);
+
+ store.set(recordIdsCallbackState, nextRecordIds);
+ store.set(totalNumberOfRowsCallbackState, (totalNumberOfRows) =>
+ Math.max(0, (totalNumberOfRows ?? 0) - 1),
+ );
+ markOperationAsApplied(operationKey);
+ }
+
+ allocateRowSlots();
+ triggerFetchPages();
+
+ return true;
+ };
+
+ const forgetAppliedDeletionOfRecords = (recordIds: string[]) =>
+ store.set(appliedRecordOperationKeysCallbackState, (operationKeys) => {
+ const remainingOperationKeys = new Set(operationKeys);
+
+ for (const recordId of recordIds) {
+ remainingOperationKeys.delete(
+ getAppliedRecordOperationKey({ operationType: 'delete', recordId }),
+ );
+ }
+
+ return remainingOperationKeys;
+ });
+
+ return {
+ forgetAppliedDeletionOfRecords,
+ insertRowForCreatedRecord,
+ removeRowsOfDeletedRecords,
+ };
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useLoadRecordsAtRowIndexes.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useLoadRecordsAtRowIndexes.ts
new file mode 100644
index 00000000000..70c8da9afb7
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useLoadRecordsAtRowIndexes.ts
@@ -0,0 +1,74 @@
+import { useStore } from 'jotai';
+import { useCallback } from 'react';
+
+import { hasUserSelectedAllRecordsComponentState } from '@/object-record/record-selection/states/hasUserSelectedAllRecordsComponentState';
+import { isRecordSelectedComponentFamilyState } from '@/object-record/record-selection/states/isRecordSelectedComponentFamilyState';
+import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
+import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
+import { placeRecordIdsAtRowIndexes } from '@/object-record/record-table/virtualization/utils/placeRecordIdsAtRowIndexes';
+import { useAllocateRowSlots } from '@/object-record/record-table/virtualization/hooks/useAllocateRowSlots';
+import { type ObjectRecord } from '@/object-record/types/ObjectRecord';
+import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
+import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
+
+export const useLoadRecordsAtRowIndexes = () => {
+ const store = useStore();
+
+ const recordIdsByGroupCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ recordIndexRecordIdsByGroupComponentFamilyState,
+ );
+
+ const hasUserSelectedAllRecordsCallbackState =
+ useAtomComponentStateCallbackState(hasUserSelectedAllRecordsComponentState);
+
+ const isRecordSelectedCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ isRecordSelectedComponentFamilyState,
+ );
+
+ const { allocateRowSlots } = useAllocateRowSlots();
+
+ const loadRecordsAtRowIndexes = useCallback(
+ ({
+ records,
+ startingRowIndex,
+ }: {
+ records: ObjectRecord[];
+ startingRowIndex: number;
+ }) => {
+ const recordIdsCallbackState = recordIdsByGroupCallbackState(
+ NO_RECORD_GROUP_FAMILY_KEY,
+ );
+
+ const isAllRowsSelected = store.get(
+ hasUserSelectedAllRecordsCallbackState,
+ );
+
+ const nextRecordIds = placeRecordIdsAtRowIndexes({
+ recordIdsByRowIndex: store.get(recordIdsCallbackState),
+ recordIds: records.map((record) => record.id),
+ startingRowIndex,
+ });
+
+ if (isAllRowsSelected) {
+ for (const record of records) {
+ store.set(isRecordSelectedCallbackState(record.id), true);
+ }
+ }
+
+ store.set(recordIdsCallbackState, nextRecordIds);
+
+ allocateRowSlots();
+ },
+ [
+ allocateRowSlots,
+ hasUserSelectedAllRecordsCallbackState,
+ isRecordSelectedCallbackState,
+ recordIdsByGroupCallbackState,
+ store,
+ ],
+ );
+
+ return { loadRecordsAtRowIndexes };
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useLoadRecordsToVirtualRows.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useLoadRecordsToVirtualRows.ts
deleted file mode 100644
index 4c21f6ddd54..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useLoadRecordsToVirtualRows.ts
+++ /dev/null
@@ -1,100 +0,0 @@
-import { useCallback } from 'react';
-import { useStore } from 'jotai';
-
-import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
-import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
-import { hasUserSelectedAllRecordsComponentState } from '@/object-record/record-selection/states/hasUserSelectedAllRecordsComponentState';
-import { isRecordSelectedComponentFamilyState } from '@/object-record/record-selection/states/isRecordSelectedComponentFamilyState';
-import { dataLoadingStatusByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentState';
-import { recordIdByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState';
-import { type ObjectRecord } from '@/object-record/types/ObjectRecord';
-import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
-import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
-
-export const useLoadRecordsToVirtualRows = () => {
- const recordIdByRealIndex = useAtomComponentStateCallbackState(
- recordIdByRealIndexComponentState,
- );
-
- const dataLoadingStatusByRealIndex = useAtomComponentStateCallbackState(
- dataLoadingStatusByRealIndexComponentState,
- );
-
- const recordIndexRecordIdsByGroupFamilyState =
- useAtomComponentFamilyStateCallbackState(
- recordIndexRecordIdsByGroupComponentFamilyState,
- );
-
- const hasUserSelectedAllRecords = useAtomComponentStateCallbackState(
- hasUserSelectedAllRecordsComponentState,
- );
-
- const isRowSelectedFamilyState = useAtomComponentFamilyStateCallbackState(
- isRecordSelectedComponentFamilyState,
- );
-
- const store = useStore();
-
- const loadRecordsToVirtualRows = useCallback(
- ({
- records,
- startingRealIndex,
- }: {
- records: ObjectRecord[];
- startingRealIndex: number;
- }) => {
- const isAllRowsSelected = store.get(hasUserSelectedAllRecords);
-
- const currentRecordIdMap = store.get(recordIdByRealIndex);
- const newRecordIdMap = new Map(currentRecordIdMap);
- const currentStatusMap = store.get(dataLoadingStatusByRealIndex);
- const newStatusMap = new Map(currentStatusMap);
-
- for (const [recordIndex, record] of records.entries()) {
- const realIndex = startingRealIndex + recordIndex;
-
- if (record.id !== currentRecordIdMap.get(realIndex)) {
- newRecordIdMap.set(realIndex, record.id);
- }
-
- newStatusMap.set(realIndex, 'loaded');
- }
-
- store.set(recordIdByRealIndex, newRecordIdMap);
- store.set(dataLoadingStatusByRealIndex, newStatusMap);
-
- const currentAllRecordIds = store.get(
- recordIndexRecordIdsByGroupFamilyState(NO_RECORD_GROUP_FAMILY_KEY),
- );
-
- const recordIds = records.map((record) => record.id);
-
- const newAllRecordIds = currentAllRecordIds.concat();
-
- for (let i = 0; i < records.length; i++) {
- newAllRecordIds[i + startingRealIndex] = recordIds[i];
-
- if (isAllRowsSelected) {
- store.set(isRowSelectedFamilyState(recordIds[i]), true);
- }
- }
-
- store.set(
- recordIndexRecordIdsByGroupFamilyState(NO_RECORD_GROUP_FAMILY_KEY),
- newAllRecordIds,
- );
- },
- [
- recordIdByRealIndex,
- dataLoadingStatusByRealIndex,
- recordIndexRecordIdsByGroupFamilyState,
- isRowSelectedFamilyState,
- hasUserSelectedAllRecords,
- store,
- ],
- );
-
- return {
- loadRecordsToVirtualRows,
- };
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useProcessScrollTop.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useProcessScrollTop.ts
new file mode 100644
index 00000000000..fea9baed36e
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useProcessScrollTop.ts
@@ -0,0 +1,50 @@
+import { useStore } from 'jotai';
+import { useCallback } from 'react';
+
+import { useAllocateRowSlots } from '@/object-record/record-table/virtualization/hooks/useAllocateRowSlots';
+import { lastScrollPositionComponentState } from '@/object-record/record-table/virtualization/states/lastScrollPositionComponentState';
+import { scrollAtRowIndexComponentState } from '@/object-record/record-table/virtualization/states/scrollAtRowIndexComponentState';
+import { getRowIndexAtViewportMiddle } from '@/object-record/record-table/virtualization/utils/getRowIndexAtViewportMiddle';
+import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
+import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
+
+export const useProcessScrollTop = () => {
+ const store = useStore();
+ const { getScrollWrapperElement } = useScrollWrapperHTMLElement();
+
+ const lastScrollPositionCallbackState = useAtomComponentStateCallbackState(
+ lastScrollPositionComponentState,
+ );
+
+ const scrollAtRowIndexCallbackState = useAtomComponentStateCallbackState(
+ scrollAtRowIndexComponentState,
+ );
+
+ const { allocateRowSlots } = useAllocateRowSlots();
+
+ const processScrollTop = useCallback(
+ (scrollTop: number) => {
+ const { scrollWrapperElement } = getScrollWrapperElement();
+
+ store.set(lastScrollPositionCallbackState, scrollTop);
+ store.set(
+ scrollAtRowIndexCallbackState,
+ getRowIndexAtViewportMiddle({
+ scrollTop,
+ viewportHeight: scrollWrapperElement?.clientHeight ?? 0,
+ }),
+ );
+
+ allocateRowSlots();
+ },
+ [
+ allocateRowSlots,
+ getScrollWrapperElement,
+ lastScrollPositionCallbackState,
+ scrollAtRowIndexCallbackState,
+ store,
+ ],
+ );
+
+ return { processScrollTop };
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useProcessTreadmillScrollTop.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useProcessTreadmillScrollTop.ts
deleted file mode 100644
index f775a4e8b19..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useProcessTreadmillScrollTop.ts
+++ /dev/null
@@ -1,149 +0,0 @@
-import { useCallback } from 'react';
-import { useStore } from 'jotai';
-
-import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
-import { NUMBER_OF_VIRTUALIZED_ROWS } from '@/object-record/record-table/virtualization/constants/NumberOfVirtualizedRows';
-import { lastRealIndexSetComponentState } from '@/object-record/record-table/virtualization/states/lastRealIndexSetComponentState';
-import { lastScrollPositionComponentState } from '@/object-record/record-table/virtualization/states/lastScrollPositionComponentState';
-import { realIndexByVirtualIndexComponentFamilyState } from '@/object-record/record-table/virtualization/states/realIndexByVirtualIndexComponentFamilyState';
-import { scrollAtRealIndexComponentState } from '@/object-record/record-table/virtualization/states/scrollAtRealIndexComponentState';
-import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
-import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
-import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
-
-export const useProcessTreadmillScrollTop = () => {
- const { getScrollWrapperElement } = useScrollWrapperHTMLElement();
-
- const lastScrollPositionCallbackState = useAtomComponentStateCallbackState(
- lastScrollPositionComponentState,
- );
-
- const lastRealIndexSetCallbackState = useAtomComponentStateCallbackState(
- lastRealIndexSetComponentState,
- );
-
- const scrollAtRealIndexCallbackState = useAtomComponentStateCallbackState(
- scrollAtRealIndexComponentState,
- );
-
- const virtualizedRowRealIndexByVirtualIndexCallbackState =
- useAtomComponentFamilyStateCallbackState(
- realIndexByVirtualIndexComponentFamilyState,
- );
-
- const store = useStore();
-
- const processTreadmillScrollTop = useCallback(
- (scrollTop: number) => {
- const distanceFromTop = scrollTop;
-
- const { scrollWrapperElement } = getScrollWrapperElement();
-
- const tableScrollWrapperHeight = scrollWrapperElement?.clientHeight ?? 0;
-
- const lastScrollPosition = store.get(lastScrollPositionCallbackState);
-
- store.set(lastScrollPositionCallbackState, distanceFromTop);
-
- const scrollDirection =
- distanceFromTop > lastScrollPosition ? 'downward' : 'upward';
-
- const numberOfRowsDisplayedInTable = Math.min(
- Math.floor(tableScrollWrapperHeight / (RECORD_TABLE_ROW_HEIGHT + 1)),
- 40,
- );
-
- const halfNumberOfRowsVisible = Math.floor(
- numberOfRowsDisplayedInTable / 2,
- );
-
- const realIndexAtTheMiddleOfTheTable =
- Math.floor(distanceFromTop / (RECORD_TABLE_ROW_HEIGHT + 1)) +
- halfNumberOfRowsVisible;
-
- const lastCorrectlySetRealIndex =
- store.get(lastRealIndexSetCallbackState) ??
- numberOfRowsDisplayedInTable;
-
- store.set(scrollAtRealIndexCallbackState, realIndexAtTheMiddleOfTheTable);
-
- if (scrollDirection === 'downward') {
- const newTargetRealIndexToReachAtTheBottomOfTheOverscan =
- realIndexAtTheMiddleOfTheTable +
- Math.floor(NUMBER_OF_VIRTUALIZED_ROWS / 2);
-
- const numberOfRealIndexToSet =
- newTargetRealIndexToReachAtTheBottomOfTheOverscan -
- lastCorrectlySetRealIndex;
-
- if (numberOfRealIndexToSet <= 0) {
- return;
- }
-
- for (let i = 0; i < numberOfRealIndexToSet + 1; i++) {
- const realIndexThatWillBeSet = lastCorrectlySetRealIndex + i;
-
- const correspondingVirtualIndex =
- realIndexThatWillBeSet % NUMBER_OF_VIRTUALIZED_ROWS;
-
- store.set(
- virtualizedRowRealIndexByVirtualIndexCallbackState({
- virtualIndex: correspondingVirtualIndex,
- }),
- realIndexThatWillBeSet,
- );
- }
-
- store.set(
- lastRealIndexSetCallbackState,
- lastCorrectlySetRealIndex + numberOfRealIndexToSet,
- );
- } else {
- const newTargetRealIndexToReachAtTheTopOfTheOverscan = Math.max(
- 0,
- realIndexAtTheMiddleOfTheTable -
- Math.floor(NUMBER_OF_VIRTUALIZED_ROWS / 2),
- );
-
- const numberOfRealIndexToSet =
- lastCorrectlySetRealIndex -
- newTargetRealIndexToReachAtTheTopOfTheOverscan;
-
- if (numberOfRealIndexToSet <= 0) {
- return;
- }
-
- for (let i = 0; i <= numberOfRealIndexToSet; i++) {
- const realIndexThatWillBeSet = lastCorrectlySetRealIndex - i;
-
- const correspondingVirtualIndex =
- realIndexThatWillBeSet % NUMBER_OF_VIRTUALIZED_ROWS;
-
- store.set(
- virtualizedRowRealIndexByVirtualIndexCallbackState({
- virtualIndex: correspondingVirtualIndex,
- }),
- realIndexThatWillBeSet,
- );
- }
-
- store.set(
- lastRealIndexSetCallbackState,
- lastCorrectlySetRealIndex - numberOfRealIndexToSet,
- );
- }
- },
- [
- lastRealIndexSetCallbackState,
- virtualizedRowRealIndexByVirtualIndexCallbackState,
- lastScrollPositionCallbackState,
- scrollAtRealIndexCallbackState,
- getScrollWrapperElement,
- store,
- ],
- );
-
- return {
- processTreadmillScrollTop,
- };
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useResetVirtualizationBecauseDataChanged.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useResetVirtualizationBecauseDataChanged.ts
index 0a2fdca584d..8f9ab31569e 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useResetVirtualizationBecauseDataChanged.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useResetVirtualizationBecauseDataChanged.ts
@@ -4,16 +4,18 @@ import { useCallback } from 'react';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useLazyFindManyRecords } from '@/object-record/hooks/useLazyFindManyRecords';
import { useRelevantRecordsGqlFields } from '@/object-record/record-field/hooks/useRelevantRecordsGqlFields';
-
+import { useRecordIndexContextOrThrow } from '@/object-record/record-index/contexts/RecordIndexContext';
import { useFindManyRecordIndexTableParams } from '@/object-record/record-index/hooks/useFindManyRecordIndexTableParams';
+import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
+import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
+import { useAllocateRowSlots } from '@/object-record/record-table/virtualization/hooks/useAllocateRowSlots';
import { useTriggerFetchPages } from '@/object-record/record-table/virtualization/hooks/useTriggerFetchPages';
-import { dataLoadingStatusByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentState';
-import { dataPagesLoadedComponentState } from '@/object-record/record-table/virtualization/states/dataPagesLoadedComponentState';
import { lastScrollPositionComponentState } from '@/object-record/record-table/virtualization/states/lastScrollPositionComponentState';
-import { recordIdByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
+import { keepRecordIdsInRowIndexRange } from '@/object-record/record-table/virtualization/utils/keepRecordIdsInRowIndexRange';
import { getVirtualizationOverscanWindow } from '@/object-record/record-table/virtualization/utils/getVirtualizationOverscanWindow';
import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
+import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
import { isDefined } from 'twenty-shared/utils';
import { sleep } from '~/utils/sleep';
@@ -21,15 +23,17 @@ import { sleep } from '~/utils/sleep';
export const useResetVirtualizationBecauseDataChanged = (
objectNameSingular: string,
) => {
+ const store = useStore();
+ const { recordLimit } = useRecordIndexContextOrThrow();
+
const { objectMetadataItem } = useObjectMetadataItem({
objectNameSingular,
});
- const { scrollWrapperHTMLElement } = useScrollWrapperHTMLElement();
+ const { getScrollWrapperElement } = useScrollWrapperHTMLElement();
const params = useFindManyRecordIndexTableParams(objectNameSingular);
- // TODO: we could optimize this by using an aggregate or using only id: true in recordGqlFields
const recordGqlFields = useRelevantRecordsGqlFields({
objectMetadataItem,
});
@@ -41,114 +45,68 @@ export const useResetVirtualizationBecauseDataChanged = (
limit: 1,
});
- const totalNumberOfRecordsToVirtualizeCallbackState =
- useAtomComponentStateCallbackState(
- totalNumberOfRecordsToVirtualizeComponentState,
- );
-
- const dataPagesLoadedCallbackState = useAtomComponentStateCallbackState(
- dataPagesLoadedComponentState,
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
);
- const { triggerFetchPagesWithoutDebounce } = useTriggerFetchPages();
-
const lastScrollPositionCallbackState = useAtomComponentStateCallbackState(
lastScrollPositionComponentState,
);
- const recordIdByRealIndexCallbackState = useAtomComponentStateCallbackState(
- recordIdByRealIndexComponentState,
- );
-
- const dataLoadingStatusByRealIndexCallbackState =
- useAtomComponentStateCallbackState(
- dataLoadingStatusByRealIndexComponentState,
+ const recordIdsByGroupCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ recordIndexRecordIdsByGroupComponentFamilyState,
);
- const store = useStore();
-
- const resetVirtualization = useCallback(async () => {
- const { totalCount } = await findManyRecordsLazy();
-
- const tableScrollWrapperHeight =
- scrollWrapperHTMLElement?.clientHeight ?? 0;
+ const { allocateRowSlots } = useAllocateRowSlots();
- const lastScrollPosition = store.get(lastScrollPositionCallbackState);
-
- const totalNumberOfRecordsToVirtualize =
- store.get(totalNumberOfRecordsToVirtualizeCallbackState) ?? 0;
-
- const { firstRealIndexInOverscanWindow, lastRealIndexInOverscanWindow } =
- getVirtualizationOverscanWindow(
- lastScrollPosition,
- tableScrollWrapperHeight,
- totalNumberOfRecordsToVirtualize,
- );
-
- const recordIdByRealIndex = store.get(recordIdByRealIndexCallbackState);
+ const { triggerFetchPagesWithoutDebounce } = useTriggerFetchPages();
- const dataLoadingStatusByRealIndex = store.get(
- dataLoadingStatusByRealIndexCallbackState,
+ const resetVirtualizationBecauseDataChanged = useCallback(async () => {
+ const { totalCount: unboundedTotalCount } = await findManyRecordsLazy();
+
+ const totalCount = isDefined(recordLimit)
+ ? Math.min(unboundedTotalCount, recordLimit)
+ : unboundedTotalCount;
+
+ const { firstRowIndexInOverscanWindow, lastRowIndexInOverscanWindow } =
+ getVirtualizationOverscanWindow({
+ scrollPosition: store.get(lastScrollPositionCallbackState),
+ scrollWrapperHeight:
+ getScrollWrapperElement().scrollWrapperElement?.clientHeight ?? 0,
+ totalNumberOfRows: store.get(totalNumberOfRowsCallbackState) ?? 0,
+ });
+
+ const recordIdsCallbackState = recordIdsByGroupCallbackState(
+ NO_RECORD_GROUP_FAMILY_KEY,
);
- const lengthOfOverscanWindow =
- lastRealIndexInOverscanWindow - firstRealIndexInOverscanWindow + 1;
-
- const newRecordIdByRealIndex = new Map<number, string>();
- const newDataLoadingStatusByRealIndex = new Map<
- number,
- 'loaded' | 'not-loaded'
- >();
-
- for (let i = 0; i < lengthOfOverscanWindow; i++) {
- const realIndex = firstRealIndexInOverscanWindow + i;
-
- const existingRecordId = recordIdByRealIndex.get(realIndex);
-
- if (isDefined(existingRecordId)) {
- newRecordIdByRealIndex.set(realIndex, existingRecordId);
- }
-
- const existingDataLoadingStatus =
- dataLoadingStatusByRealIndex.get(realIndex);
-
- if (isDefined(existingDataLoadingStatus)) {
- newDataLoadingStatusByRealIndex.set(
- realIndex,
- existingDataLoadingStatus,
- );
- }
- }
-
- store.set(recordIdByRealIndexCallbackState, newRecordIdByRealIndex);
store.set(
- dataLoadingStatusByRealIndexCallbackState,
- newDataLoadingStatusByRealIndex,
+ recordIdsCallbackState,
+ keepRecordIdsInRowIndexRange({
+ recordIdsByRowIndex: store.get(recordIdsCallbackState),
+ firstRowIndex: firstRowIndexInOverscanWindow,
+ lastRowIndex: Math.min(lastRowIndexInOverscanWindow, totalCount - 1),
+ }),
);
+ store.set(totalNumberOfRowsCallbackState, totalCount);
- store.set(dataPagesLoadedCallbackState, []);
- store.set(totalNumberOfRecordsToVirtualizeCallbackState, totalCount);
+ allocateRowSlots();
+
+ await sleep(50);
+
+ await triggerFetchPagesWithoutDebounce({ shouldRefetchLoadedPages: true });
}, [
+ allocateRowSlots,
findManyRecordsLazy,
- scrollWrapperHTMLElement?.clientHeight,
+ recordLimit,
lastScrollPositionCallbackState,
- totalNumberOfRecordsToVirtualizeCallbackState,
- dataPagesLoadedCallbackState,
- dataLoadingStatusByRealIndexCallbackState,
- recordIdByRealIndexCallbackState,
+ recordIdsByGroupCallbackState,
+ getScrollWrapperElement,
store,
+ totalNumberOfRowsCallbackState,
+ triggerFetchPagesWithoutDebounce,
]);
- const resetVirtualizationBecauseDataChanged = useCallback(async () => {
- await resetVirtualization();
-
- await sleep(50);
-
- await triggerFetchPagesWithoutDebounce();
- }, [resetVirtualization, triggerFetchPagesWithoutDebounce]);
-
- return {
- resetVirtualizationBecauseDataChanged,
- resetVirtualization,
- };
+ return { resetVirtualizationBecauseDataChanged };
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useResetVirtualizedRowTreadmill.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useResetVirtualizedRowTreadmill.ts
deleted file mode 100644
index 6321c5e5916..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useResetVirtualizedRowTreadmill.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { useCallback } from 'react';
-import { useStore } from 'jotai';
-
-import { NUMBER_OF_VIRTUALIZED_ROWS } from '@/object-record/record-table/virtualization/constants/NumberOfVirtualizedRows';
-import { realIndexByVirtualIndexComponentFamilyState } from '@/object-record/record-table/virtualization/states/realIndexByVirtualIndexComponentFamilyState';
-import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
-import { getContiguousIncrementalValues } from 'twenty-shared/utils';
-
-export const useResetVirtualizedRowTreadmill = () => {
- const realIndexByVirtualIndexCallbackState =
- useAtomComponentFamilyStateCallbackState(
- realIndexByVirtualIndexComponentFamilyState,
- );
-
- const store = useStore();
-
- const resetVirtualizedRowTreadmill = useCallback(() => {
- const virtualIndices = getContiguousIncrementalValues(
- NUMBER_OF_VIRTUALIZED_ROWS,
- );
-
- for (const virtualIndex of virtualIndices) {
- const realIndex = virtualIndex;
-
- store.set(
- realIndexByVirtualIndexCallbackState({
- virtualIndex: virtualIndex,
- }),
- realIndex,
- );
- }
- }, [realIndexByVirtualIndexCallbackState, store]);
-
- return { resetVirtualizedRowTreadmill };
-};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerFetchPages.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerFetchPages.ts
index 7dcf15e3074..98421c5977b 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerFetchPages.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerFetchPages.ts
@@ -1,195 +1,169 @@
-import { useCallback } from 'react';
import { useStore } from 'jotai';
+import { useCallback } from 'react';
+import { getContiguousIncrementalValues, isDefined } from 'twenty-shared/utils';
+import { useDebouncedCallback } from 'use-debounce';
import { useLazyFindManyRecordsWithOffset } from '@/object-record/hooks/useLazyFindManyRecordsWithOffset';
+import { recordIndexRecordIdsByGroupComponentFamilyState } from '@/object-record/record-index/states/recordIndexRecordIdsByGroupComponentFamilyState';
+import { NO_RECORD_GROUP_FAMILY_KEY } from '@/object-record/record-index/states/selectors/recordIndexAllRecordIdsComponentSelector';
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
import { useRecordTableContextOrThrow } from '@/object-record/record-table/contexts/RecordTableContext';
import { TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE } from '@/object-record/record-table/virtualization/constants/TableVirtualizationNumberOfRecordsPerPage';
-import { useLoadRecordsToVirtualRows } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsToVirtualRows';
-
-import { dataPagesLoadedComponentState } from '@/object-record/record-table/virtualization/states/dataPagesLoadedComponentState';
+import { useLoadRecordsAtRowIndexes } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsAtRowIndexes';
import { lastScrollPositionComponentState } from '@/object-record/record-table/virtualization/states/lastScrollPositionComponentState';
import { lowDetailsActivatedComponentState } from '@/object-record/record-table/virtualization/states/lowDetailsActivatedComponentState';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { numberOfPendingPageFetchesComponentState } from '@/object-record/record-table/virtualization/states/numberOfPendingPageFetchesComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
+import { getPagesWithUnloadedRows } from '@/object-record/record-table/virtualization/utils/getPagesWithUnloadedRows';
import { getVirtualizationOverscanWindow } from '@/object-record/record-table/virtualization/utils/getVirtualizationOverscanWindow';
import { useScrollWrapperHTMLElement } from '@/ui/utilities/scroll/hooks/useScrollWrapperHTMLElement';
+import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
-import { getContiguousIncrementalValues, isDefined } from 'twenty-shared/utils';
-import { useDebouncedCallback } from 'use-debounce';
const TIME_BETWEEN_UI_BATCH_UPDATE = 25;
const PAGING_FOR_UI_UPDATE = 10;
-const TIME_BETWEEN_TWO_REQUETS = 25;
+const TIME_BETWEEN_TWO_REQUESTS = 25;
export const useTriggerFetchPages = () => {
const { objectNameSingular } = useRecordTableContextOrThrow();
- const { scrollWrapperHTMLElement } = useScrollWrapperHTMLElement();
+ const { getScrollWrapperElement } = useScrollWrapperHTMLElement();
+ const store = useStore();
const { upsertRecordsInStore } = useUpsertRecordsInStore();
+ const { loadRecordsAtRowIndexes } = useLoadRecordsAtRowIndexes();
- const { loadRecordsToVirtualRows } = useLoadRecordsToVirtualRows();
+ const { findManyRecordsLazyWithOffset } = useLazyFindManyRecordsWithOffset({
+ objectNameSingular,
+ });
- const totalNumberOfRecordsToVirtualizeCallbackState =
- useAtomComponentStateCallbackState(
- totalNumberOfRecordsToVirtualizeComponentState,
- );
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
+ );
const lastScrollPositionCallbackState = useAtomComponentStateCallbackState(
lastScrollPositionComponentState,
);
- const dataPagesLoadedCallbackState = useAtomComponentStateCallbackState(
- dataPagesLoadedComponentState,
- );
-
- const { findManyRecordsLazyWithOffset } = useLazyFindManyRecordsWithOffset({
- objectNameSingular,
- });
-
const lowDetailsActivatedCallbackState = useAtomComponentStateCallbackState(
lowDetailsActivatedComponentState,
);
- const store = useStore();
-
- const triggerFetchPagesWithoutDebounce = useCallback(async () => {
- const lowDetailsActivated = store.get(lowDetailsActivatedCallbackState);
-
- if (lowDetailsActivated) {
- return;
- }
-
- const tableScrollWrapperHeight =
- scrollWrapperHTMLElement?.clientHeight ?? 0;
-
- const lastScrollPosition = store.get(lastScrollPositionCallbackState);
-
- const totalNumberOfRecordsToVirtualize =
- store.get(totalNumberOfRecordsToVirtualizeCallbackState) ?? 0;
-
- const { overscanPageAtBottom, overscanPageAtTop } =
- getVirtualizationOverscanWindow(
- lastScrollPosition,
- tableScrollWrapperHeight,
- totalNumberOfRecordsToVirtualize,
- );
-
- const pagesAlreadyLoaded = store.get(dataPagesLoadedCallbackState);
-
- const numberOfPages = overscanPageAtBottom - overscanPageAtTop;
-
- const pagesToFetchInitial = getContiguousIncrementalValues(
- numberOfPages,
- overscanPageAtTop,
+ const numberOfPendingPageFetchesCallbackState =
+ useAtomComponentStateCallbackState(
+ numberOfPendingPageFetchesComponentState,
);
- const pagesToFetch = pagesToFetchInitial.filter(
- (pageNumber) => !pagesAlreadyLoaded.includes(pageNumber),
+ const recordIdsByGroupCallbackState =
+ useAtomComponentFamilyStateCallbackState(
+ recordIndexRecordIdsByGroupComponentFamilyState,
);
- if (pagesToFetch.length === 0) {
- return;
- }
-
- let pagesAreContiguous = true;
+ const triggerFetchPagesWithoutDebounce = useCallback(
+ async ({
+ shouldRefetchLoadedPages = false,
+ }: { shouldRefetchLoadedPages?: boolean } = {}) => {
+ if (store.get(lowDetailsActivatedCallbackState)) {
+ return;
+ }
- for (const [index, pageNumber] of pagesToFetch.entries()) {
- const isLastElementOfArray = index === pagesToFetch.length - 1;
+ const totalNumberOfRows = store.get(totalNumberOfRowsCallbackState) ?? 0;
- if (isLastElementOfArray) {
- continue;
- }
+ const { overscanPageAtBottom, overscanPageAtTop } =
+ getVirtualizationOverscanWindow({
+ scrollPosition: store.get(lastScrollPositionCallbackState),
+ scrollWrapperHeight:
+ getScrollWrapperElement().scrollWrapperElement?.clientHeight ?? 0,
+ totalNumberOfRows,
+ });
- const nextPageNumberIsNotContiguous =
- pagesToFetch.at(index + 1) !== pageNumber + 1;
+ const pagesInOverscanWindow = getContiguousIncrementalValues(
+ overscanPageAtBottom - overscanPageAtTop,
+ overscanPageAtTop,
+ );
- if (nextPageNumberIsNotContiguous) {
- pagesAreContiguous = false;
- break;
+ const pagesToFetch = shouldRefetchLoadedPages
+ ? pagesInOverscanWindow
+ : getPagesWithUnloadedRows({
+ pages: pagesInOverscanWindow,
+ recordIdsByRowIndex: store.get(
+ recordIdsByGroupCallbackState(NO_RECORD_GROUP_FAMILY_KEY),
+ ),
+ totalNumberOfRows,
+ });
+
+ const firstPageToFetch = pagesToFetch.at(0);
+ const lastPageToFetch = pagesToFetch.at(-1);
+
+ if (!isDefined(firstPageToFetch) || !isDefined(lastPageToFetch)) {
+ return;
}
- }
- if (pagesAreContiguous) {
- const startingRealIndexToFetch =
- (pagesToFetch.at(0) ?? 0) *
- TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE;
+ const startingRowIndexToFetch =
+ firstPageToFetch * TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE;
- const endingRealIndexToFetch =
- (pagesToFetch.at(-1) ?? 0) *
- TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE +
+ const numberOfRecordsToFetch =
+ (lastPageToFetch - firstPageToFetch + 1) *
TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE;
- const numberOfRecordsToFetch =
- endingRealIndexToFetch - startingRealIndexToFetch;
+ store.set(numberOfPendingPageFetchesCallbackState, (count) => count + 1);
- if (numberOfRecordsToFetch > 0) {
- const fetchResult = await findManyRecordsLazyWithOffset(
+ try {
+ const { records } = await findManyRecordsLazyWithOffset(
numberOfRecordsToFetch,
- startingRealIndexToFetch,
+ startingRowIndexToFetch,
);
- const records = fetchResult.records;
-
- if (isDefined(records)) {
- const pagingForUIUpdate = PAGING_FOR_UI_UPDATE;
-
- const pages = Math.ceil(records.length / pagingForUIUpdate);
-
- for (let page = 0; page < pages; page++) {
- await new Promise<void>((res) =>
- setTimeout(() => res(), TIME_BETWEEN_UI_BATCH_UPDATE),
- );
-
- const startingRealIndexInThisUIPage =
- startingRealIndexToFetch + page * pagingForUIUpdate;
-
- const startingSliceIndexInThisPage = page * pagingForUIUpdate;
- const endingSliceIndexInThisPage =
- startingSliceIndexInThisPage + pagingForUIUpdate;
-
- const recordsSlice = records.slice(
- startingSliceIndexInThisPage,
- endingSliceIndexInThisPage,
- );
-
- loadRecordsToVirtualRows({
- records: recordsSlice,
- startingRealIndex: startingRealIndexInThisUIPage,
- });
-
- upsertRecordsInStore({ partialRecords: recordsSlice });
- }
+ if (!isDefined(records)) {
+ return;
}
- store.set(dataPagesLoadedCallbackState, (currentLoadedPages) =>
- currentLoadedPages.concat(pagesToFetch).toSorted(),
+ for (
+ let sliceStart = 0;
+ sliceStart < records.length;
+ sliceStart += PAGING_FOR_UI_UPDATE
+ ) {
+ await new Promise<void>((resolve) =>
+ setTimeout(resolve, TIME_BETWEEN_UI_BATCH_UPDATE),
+ );
+
+ const recordsSlice = records.slice(
+ sliceStart,
+ sliceStart + PAGING_FOR_UI_UPDATE,
+ );
+
+ upsertRecordsInStore({ partialRecords: recordsSlice });
+
+ loadRecordsAtRowIndexes({
+ records: recordsSlice,
+ startingRowIndex: startingRowIndexToFetch + sliceStart,
+ });
+ }
+ } finally {
+ store.set(
+ numberOfPendingPageFetchesCallbackState,
+ (count) => count - 1,
);
}
- } else {
- // TODO fetch page by page (will be optmized later)
- }
- }, [
- dataPagesLoadedCallbackState,
- findManyRecordsLazyWithOffset,
- totalNumberOfRecordsToVirtualizeCallbackState,
- loadRecordsToVirtualRows,
- upsertRecordsInStore,
- lastScrollPositionCallbackState,
- scrollWrapperHTMLElement,
- lowDetailsActivatedCallbackState,
- store,
- ]);
+ },
+ [
+ findManyRecordsLazyWithOffset,
+ lastScrollPositionCallbackState,
+ loadRecordsAtRowIndexes,
+ lowDetailsActivatedCallbackState,
+ numberOfPendingPageFetchesCallbackState,
+ recordIdsByGroupCallbackState,
+ getScrollWrapperElement,
+ store,
+ totalNumberOfRowsCallbackState,
+ upsertRecordsInStore,
+ ],
+ );
const triggerFetchPages = useDebouncedCallback(
triggerFetchPagesWithoutDebounce,
- TIME_BETWEEN_TWO_REQUETS,
- {
- maxWait: 2000,
- },
+ TIME_BETWEEN_TWO_REQUESTS,
+ { maxWait: 2000 },
);
- return {
- triggerFetchPages,
- triggerFetchPagesWithoutDebounce,
- };
+ return { triggerFetchPages, triggerFetchPagesWithoutDebounce };
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerInitialRecordTableDataLoad.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerInitialRecordTableDataLoad.ts
index 0aad1bcc627..000ce8d1e51 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerInitialRecordTableDataLoad.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/hooks/useTriggerInitialRecordTableDataLoad.ts
@@ -17,19 +17,15 @@ import { isRecordTableInitialLoadingComponentState } from '@/object-record/recor
import { isRecordTableScrolledHorizontallyComponentState } from '@/object-record/record-table/states/isRecordTableScrolledHorizontallyComponentState';
import { isRecordTableScrolledVerticallyComponentState } from '@/object-record/record-table/states/isRecordTableScrolledVerticallyComponentState';
import { updateRecordTableCSSVariable } from '@/object-record/record-table/utils/updateRecordTableCSSVariable';
-import { useLoadRecordsToVirtualRows } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsToVirtualRows';
+import { useLoadRecordsAtRowIndexes } from '@/object-record/record-table/virtualization/hooks/useLoadRecordsAtRowIndexes';
import { useReapplyRowSelection } from '@/object-record/record-table/virtualization/hooks/useReapplyRowSelection';
+import { useAllocateRowSlots } from '@/object-record/record-table/virtualization/hooks/useAllocateRowSlots';
import { useResetTableFocuses } from '@/object-record/record-table/virtualization/hooks/useResetTableFocuses';
-import { useResetVirtualizedRowTreadmill } from '@/object-record/record-table/virtualization/hooks/useResetVirtualizedRowTreadmill';
-import { dataLoadingStatusByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentState';
-import { dataPagesLoadedComponentState } from '@/object-record/record-table/virtualization/states/dataPagesLoadedComponentState';
import { isInitializingVirtualTableDataLoadingComponentState } from '@/object-record/record-table/virtualization/states/isInitializingVirtualTableDataLoadingComponentState';
-import { lastRealIndexSetComponentState } from '@/object-record/record-table/virtualization/states/lastRealIndexSetComponentState';
import { lastScrollPositionComponentState } from '@/object-record/record-table/virtualization/states/lastScrollPositionComponentState';
-import { recordIdByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState';
-import { scrollAtRealIndexComponentState } from '@/object-record/record-table/virtualization/states/scrollAtRealIndexComponentState';
-import { totalNumberOfRecordsToVirtualizeComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState';
+import { scrollAtRowIndexComponentState } from '@/object-record/record-table/virtualization/states/scrollAtRowIndexComponentState';
+import { totalNumberOfRowsComponentState } from '@/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState';
import { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';
import { useAtomComponentSelectorCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentSelectorCallbackState';
import { useAtomComponentStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateCallbackState';
@@ -49,10 +45,6 @@ export const useTriggerInitialRecordTableDataLoad = () => {
isInitializingVirtualTableDataLoadingComponentState,
);
- const dataPagesLoadedCallbackState = useAtomComponentStateCallbackState(
- dataPagesLoadedComponentState,
- );
-
const isRecordTableInitialLoading = useAtomComponentStateCallbackState(
isRecordTableInitialLoadingComponentState,
recordTableId,
@@ -70,15 +62,6 @@ export const useTriggerInitialRecordTableDataLoad = () => {
recordIndexRecordIdsByGroupComponentFamilyState,
);
- const recordIdByRealIndexCallbackState = useAtomComponentStateCallbackState(
- recordIdByRealIndexComponentState,
- );
-
- const dataLoadingStatusByRealIndexCallbackState =
- useAtomComponentStateCallbackState(
- dataLoadingStatusByRealIndexComponentState,
- );
-
const setIsRecordTableScrolledHorizontally = useSetAtomComponentState(
isRecordTableScrolledHorizontallyComponentState,
);
@@ -91,29 +74,24 @@ export const useTriggerInitialRecordTableDataLoad = () => {
lastScrollPositionComponentState,
);
- const lastRealIndexSetCallbackState = useAtomComponentStateCallbackState(
- lastRealIndexSetComponentState,
- );
-
- const scrollAtRealIndexCallbackState = useAtomComponentStateCallbackState(
- scrollAtRealIndexComponentState,
+ const scrollAtRowIndexCallbackState = useAtomComponentStateCallbackState(
+ scrollAtRowIndexComponentState,
);
const { scrollTableToPosition } = useScrollTableToPosition();
- const { resetVirtualizedRowTreadmill } = useResetVirtualizedRowTreadmill();
+ const { allocateRowSlots } = useAllocateRowSlots();
const { resetTableFocuses } = useResetTableFocuses(recordTableId);
const { upsertRecordsInStore } = useUpsertRecordsInStore();
- const { loadRecordsToVirtualRows } = useLoadRecordsToVirtualRows();
+ const { loadRecordsAtRowIndexes } = useLoadRecordsAtRowIndexes();
const { reapplyRowSelection } = useReapplyRowSelection();
- const totalNumberOfRecordsToVirtualizeCallbackState =
- useAtomComponentStateCallbackState(
- totalNumberOfRecordsToVirtualizeComponentState,
- );
+ const totalNumberOfRowsCallbackState = useAtomComponentStateCallbackState(
+ totalNumberOfRowsComponentState,
+ );
const triggerInitialRecordTableDataLoad = useCallback(
async ({
@@ -153,26 +131,6 @@ export const useTriggerInitialRecordTableDataLoad = () => {
);
const clearLoadedRecords = () => {
- const currentRecordIds = store.get(recordIndexAllRecordIds);
-
- const newRecordIdByRealIndex = new Map(
- store.get(recordIdByRealIndexCallbackState),
- );
- const newDataLoadingStatusByRealIndex = new Map(
- store.get(dataLoadingStatusByRealIndexCallbackState),
- );
-
- for (const [realIndex] of currentRecordIds.entries()) {
- newDataLoadingStatusByRealIndex.set(realIndex, 'not-loaded');
- newRecordIdByRealIndex.delete(realIndex);
- }
-
- store.set(recordIdByRealIndexCallbackState, newRecordIdByRealIndex);
- store.set(
- dataLoadingStatusByRealIndexCallbackState,
- newDataLoadingStatusByRealIndex,
- );
-
store.set(
recordIndexRecordIdsByGroupFamilyState(NO_RECORD_GROUP_FAMILY_KEY),
[],
@@ -187,13 +145,12 @@ export const useTriggerInitialRecordTableDataLoad = () => {
findManyRecordsResult = await findManyRecordsLazy();
} finally {
clearLoadedRecords();
- resetVirtualizedRowTreadmill();
}
const { records, totalCount } = findManyRecordsResult;
store.set(
- totalNumberOfRecordsToVirtualizeCallbackState,
+ totalNumberOfRowsCallbackState,
isDefined(recordLimit)
? Math.min(totalCount, recordLimit)
: totalCount,
@@ -202,19 +159,18 @@ export const useTriggerInitialRecordTableDataLoad = () => {
if (isDefined(records)) {
upsertRecordsInStore({ partialRecords: records });
- loadRecordsToVirtualRows({
+ loadRecordsAtRowIndexes({
records,
- startingRealIndex: 0,
+ startingRowIndex: 0,
});
reapplyRowSelection();
}
- store.set(dataPagesLoadedCallbackState, []);
-
store.set(lastScrollPositionCallbackState, 0);
- store.set(lastRealIndexSetCallbackState, null);
- store.set(scrollAtRealIndexCallbackState, 0);
+ store.set(scrollAtRowIndexCallbackState, 0);
+
+ allocateRowSlots();
setIsRecordTableScrolledHorizontally(false);
setIsRecordTableScrolledVertically(false);
@@ -234,24 +190,20 @@ export const useTriggerInitialRecordTableDataLoad = () => {
[
isInitializingVirtualTableDataLoadingCallbackState,
resetTableFocuses,
- resetVirtualizedRowTreadmill,
+ allocateRowSlots,
recordIndexAllRecordIds,
recordIndexRecordIdsByGroupFamilyState,
store,
- dataPagesLoadedCallbackState,
isRecordTableInitialLoading,
lastScrollPositionCallbackState,
- lastRealIndexSetCallbackState,
- scrollAtRealIndexCallbackState,
+ scrollAtRowIndexCallbackState,
setIsRecordTableScrolledHorizontally,
setIsRecordTableScrolledVertically,
scrollTableToPosition,
findManyRecordsLazy,
- dataLoadingStatusByRealIndexCallbackState,
- recordIdByRealIndexCallbackState,
- totalNumberOfRecordsToVirtualizeCallbackState,
+ totalNumberOfRowsCallbackState,
upsertRecordsInStore,
- loadRecordsToVirtualRows,
+ loadRecordsAtRowIndexes,
reapplyRowSelection,
recordTableId,
recordLimit,
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/appliedRecordOperationKeysComponentState.ts
similarity index 62%
rename from packages/twenty-front/src/modules/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState.ts
rename to packages/twenty-front/src/modules/object-record/record-table/virtualization/states/appliedRecordOperationKeysComponentState.ts
index ac671dab942..1ffc0ed7719 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/totalNumberOfRecordsToVirtualizeComponentState.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/appliedRecordOperationKeysComponentState.ts
@@ -1,9 +1,9 @@
import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
import { createAtomComponentState } from '@/ui/utilities/state/jotai/utils/createAtomComponentState';
-export const totalNumberOfRecordsToVirtualizeComponentState =
- createAtomComponentState<number | null>({
- key: 'totalNumberOfRecordsToVirtualizeComponentState',
+export const appliedRecordOperationKeysComponentState =
+ createAtomComponentState<Set<string>>({
+ key: 'appliedRecordOperationKeysComponentState',
componentInstanceContext: RecordTableComponentInstanceContext,
- defaultValue: null,
+ defaultValue: new Set<string>(),
});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentFamilySelector.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentFamilySelector.ts
deleted file mode 100644
index 795987bc4a8..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentFamilySelector.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
-import { dataLoadingStatusByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentState';
-import { createAtomComponentFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilySelector';
-import { type Nullable } from 'twenty-shared/types';
-import { isDefined } from 'twenty-shared/utils';
-
-export const dataLoadingStatusByRealIndexComponentFamilySelector =
- createAtomComponentFamilySelector<
- 'loaded' | 'not-loaded' | undefined,
- Nullable<number>
- >({
- key: 'dataLoadingStatusByRealIndexComponentFamilySelector',
- componentInstanceContext: RecordTableComponentInstanceContext,
- get:
- ({ instanceId, familyKey }) =>
- ({ get }) => {
- const realIndex = familyKey;
-
- const dataLoadingStatusByRealIndex = get(
- dataLoadingStatusByRealIndexComponentState,
- { instanceId },
- );
-
- if (!isDefined(realIndex)) {
- return undefined;
- }
-
- const dataLoadingStatus = dataLoadingStatusByRealIndex.get(realIndex);
-
- return dataLoadingStatus;
- },
- });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentState.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentState.ts
deleted file mode 100644
index 22864d30551..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataLoadingStatusByRealIndexComponentState.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
-import { createAtomComponentState } from '@/ui/utilities/state/jotai/utils/createAtomComponentState';
-
-export const dataLoadingStatusByRealIndexComponentState =
- createAtomComponentState<Map<number, 'loaded' | 'not-loaded'>>({
- key: 'dataLoadingStatusByRealIndexComponentState',
- componentInstanceContext: RecordTableComponentInstanceContext,
- defaultValue: new Map(),
- });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataPagesLoadedComponentState.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataPagesLoadedComponentState.ts
deleted file mode 100644
index 11892896708..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/dataPagesLoadedComponentState.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
-import { createAtomComponentState } from '@/ui/utilities/state/jotai/utils/createAtomComponentState';
-
-export const dataPagesLoadedComponentState = createAtomComponentState<number[]>(
- {
- key: 'dataPagesLoadedComponentState',
- componentInstanceContext: RecordTableComponentInstanceContext,
- defaultValue: [],
- },
-);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/scrollAtRealIndexComponentState.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/numberOfPendingPageFetchesComponentState.ts
similarity index 68%
rename from packages/twenty-front/src/modules/object-record/record-table/virtualization/states/scrollAtRealIndexComponentState.ts
rename to packages/twenty-front/src/modules/object-record/record-table/virtualization/states/numberOfPendingPageFetchesComponentState.ts
index 7194dfb6eba..e120fbd51b5 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/scrollAtRealIndexComponentState.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/numberOfPendingPageFetchesComponentState.ts
@@ -1,10 +1,9 @@
import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
import { createAtomComponentState } from '@/ui/utilities/state/jotai/utils/createAtomComponentState';
-export const scrollAtRealIndexComponentState = createAtomComponentState<number>(
- {
- key: 'scrollAtRealIndexComponentState',
+export const numberOfPendingPageFetchesComponentState =
+ createAtomComponentState<number>({
+ key: 'numberOfPendingPageFetchesComponentState',
componentInstanceContext: RecordTableComponentInstanceContext,
defaultValue: 0,
- },
-);
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/recordIdByRealIndexComponentFamilySelector.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/recordIdByRealIndexComponentFamilySelector.ts
deleted file mode 100644
index ce097558d76..00000000000
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/recordIdByRealIndexComponentFamilySelector.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
-import { recordIdByRealIndexComponentState } from '@/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState';
-import { createAtomComponentFamilySelector } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilySelector';
-import { type Nullable } from 'twenty-shared/types';
-import { isDefined } from 'twenty-shared/utils';
-
-export const recordIdByRealIndexComponentFamilySelector =
- createAtomComponentFamilySelector<Nullable<string>, Nullable<number>>({
- key: 'recordIdByRealIndexComponentFamilySelector',
- componentInstanceContext: RecordTableComponentInstanceContext,
- get:
- ({ instanceId, familyKey }) =>
- ({ get }) => {
- const realIndex = familyKey;
-
- const recordIdByRealIndex = get(recordIdByRealIndexComponentState, {
- instanceId,
- });
-
- if (!isDefined(realIndex)) {
- return null;
- }
-
- const recordId = recordIdByRealIndex.get(realIndex);
-
- return recordId;
- },
- });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/realIndexByVirtualIndexComponentFamilyState.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/rowSlotBindingComponentFamilyState.ts
similarity index 56%
rename from packages/twenty-front/src/modules/object-record/record-table/virtualization/states/realIndexByVirtualIndexComponentFamilyState.ts
rename to packages/twenty-front/src/modules/object-record/record-table/virtualization/states/rowSlotBindingComponentFamilyState.ts
index ed7050bfb59..6efc3a1cfd1 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/realIndexByVirtualIndexComponentFamilyState.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/rowSlotBindingComponentFamilyState.ts
@@ -1,9 +1,10 @@
import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
+import { type RowSlotBinding } from '@/object-record/record-table/virtualization/types/RowSlotBinding';
import { createAtomComponentFamilyState } from '@/ui/utilities/state/jotai/utils/createAtomComponentFamilyState';
-export const realIndexByVirtualIndexComponentFamilyState =
- createAtomComponentFamilyState<number | null, { virtualIndex: number }>({
- key: 'realIndexByVirtualIndexComponentFamilyState',
+export const rowSlotBindingComponentFamilyState =
+ createAtomComponentFamilyState<RowSlotBinding | null, number>({
+ key: 'rowSlotBindingComponentFamilyState',
componentInstanceContext: RecordTableComponentInstanceContext,
defaultValue: null,
});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/scrollAtRowIndexComponentState.ts
similarity index 64%
rename from packages/twenty-front/src/modules/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState.ts
rename to packages/twenty-front/src/modules/object-record/record-table/virtualization/states/scrollAtRowIndexComponentState.ts
index c59bd1a7a93..35b88d99552 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/recordIdByRealIndexComponentState.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/scrollAtRowIndexComponentState.ts
@@ -1,10 +1,8 @@
import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
import { createAtomComponentState } from '@/ui/utilities/state/jotai/utils/createAtomComponentState';
-export const recordIdByRealIndexComponentState = createAtomComponentState<
- Map<number, string>
->({
- key: 'recordIdByRealIndexComponentState',
+export const scrollAtRowIndexComponentState = createAtomComponentState<number>({
+ key: 'scrollAtRowIndexComponentState',
componentInstanceContext: RecordTableComponentInstanceContext,
- defaultValue: new Map(),
+ defaultValue: 0,
});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/lastRealIndexSetComponentState.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState.ts
similarity index 75%
rename from packages/twenty-front/src/modules/object-record/record-table/virtualization/states/lastRealIndexSetComponentState.ts
rename to packages/twenty-front/src/modules/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState.ts
index f544ffcd858..bb3e161b69a 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/lastRealIndexSetComponentState.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/states/totalNumberOfRowsComponentState.ts
@@ -1,10 +1,10 @@
import { RecordTableComponentInstanceContext } from '@/object-record/record-table/states/context/RecordTableComponentInstanceContext';
import { createAtomComponentState } from '@/ui/utilities/state/jotai/utils/createAtomComponentState';
-export const lastRealIndexSetComponentState = createAtomComponentState<
+export const totalNumberOfRowsComponentState = createAtomComponentState<
number | null
>({
- key: 'lastRealIndexSetComponentState',
+ key: 'totalNumberOfRowsComponentState',
componentInstanceContext: RecordTableComponentInstanceContext,
defaultValue: null,
});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/types/RowSlotBinding.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/types/RowSlotBinding.ts
new file mode 100644
index 00000000000..23364a72b49
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/types/RowSlotBinding.ts
@@ -0,0 +1,3 @@
+export type RowSlotBinding =
+ | { type: 'record'; recordId: string }
+ | { type: 'skeleton'; rowIndex: number };
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/computeRowSlotBindings.test.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/computeRowSlotBindings.test.ts
new file mode 100644
index 00000000000..d1712385e4b
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/computeRowSlotBindings.test.ts
@@ -0,0 +1,48 @@
+import { computeRowSlotBindings } from '@/object-record/record-table/virtualization/utils/computeRowSlotBindings';
+
+describe('computeRowSlotBindings', () => {
+ it('keeps every record in its slot when a record is inserted before it', () => {
+ const currentBindings = computeRowSlotBindings({
+ currentBindings: [null, null, null, null],
+ recordIdsByRowIndex: ['a', 'b', 'c'],
+ firstRowIndex: 0,
+ lastRowIndex: 2,
+ });
+
+ const nextBindings = computeRowSlotBindings({
+ currentBindings,
+ recordIdsByRowIndex: ['new', 'a', 'b', 'c'],
+ firstRowIndex: 0,
+ lastRowIndex: 3,
+ });
+
+ expect(nextBindings.slice(0, 3)).toEqual(currentBindings.slice(0, 3));
+ expect(nextBindings[0]).toBe(currentBindings[0]);
+ expect(nextBindings[3]).toEqual({ type: 'record', recordId: 'new' });
+ });
+
+ it('frees the slots of records that left the window and shows skeletons for unloaded rows', () => {
+ const currentBindings = computeRowSlotBindings({
+ currentBindings: [null, null, null],
+ recordIdsByRowIndex: ['a', 'b', 'c'],
+ firstRowIndex: 0,
+ lastRowIndex: 2,
+ });
+
+ const recordIdsByRowIndex: string[] = [];
+
+ recordIdsByRowIndex[1] = 'b';
+ recordIdsByRowIndex[2] = 'c';
+
+ const nextBindings = computeRowSlotBindings({
+ currentBindings,
+ recordIdsByRowIndex,
+ firstRowIndex: 1,
+ lastRowIndex: 3,
+ });
+
+ expect(nextBindings[0]).toEqual({ type: 'skeleton', rowIndex: 3 });
+ expect(nextBindings[1]).toBe(currentBindings[1]);
+ expect(nextBindings[2]).toBe(currentBindings[2]);
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getPagesWithUnloadedRows.test.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getPagesWithUnloadedRows.test.ts
new file mode 100644
index 00000000000..94f9077566f
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getPagesWithUnloadedRows.test.ts
@@ -0,0 +1,28 @@
+import { getPagesWithUnloadedRows } from '@/object-record/record-table/virtualization/utils/getPagesWithUnloadedRows';
+
+const buildRecordIds = (rowIndexes: number[]) => {
+ const recordIds: string[] = [];
+
+ rowIndexes.forEach((rowIndex) => {
+ recordIds[rowIndex] = `record-${rowIndex}`;
+ });
+
+ return recordIds;
+};
+
+describe('getPagesWithUnloadedRows', () => {
+ it('keeps only the pages that have a row without a record id', () => {
+ const loadedRowIndexes = [
+ ...Array.from({ length: 15 }, (_, rowIndex) => rowIndex),
+ ...Array.from({ length: 5 }, (_, offset) => 20 + offset),
+ ];
+
+ expect(
+ getPagesWithUnloadedRows({
+ pages: [0, 1, 2],
+ recordIdsByRowIndex: buildRecordIds(loadedRowIndexes),
+ totalNumberOfRows: 25,
+ }),
+ ).toEqual([1]);
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getRowIndexToInsertRecord.test.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getRowIndexToInsertRecord.test.ts
new file mode 100644
index 00000000000..573ddaae61c
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getRowIndexToInsertRecord.test.ts
@@ -0,0 +1,125 @@
+import { getRowIndexToInsertRecord } from '@/object-record/record-table/virtualization/utils/getRowIndexToInsertRecord';
+
+const orderBy = [{ position: 'AscNullsFirst' as const }];
+
+const loadedRow = ({
+ rowIndex,
+ position,
+}: {
+ rowIndex: number;
+ position: number;
+}) => ({ rowIndex, position });
+
+const loadedRows = [
+ loadedRow({ rowIndex: 10, position: 100 }),
+ loadedRow({ rowIndex: 11, position: 110 }),
+ loadedRow({ rowIndex: 12, position: 120 }),
+ loadedRow({ rowIndex: 30, position: 300 }),
+];
+
+describe('getRowIndexToInsertRecord', () => {
+ it('returns the index of the first loaded row sorted after the record', () => {
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 5,
+ totalNumberOfRows: 500,
+ orderBy,
+ }),
+ ).toBe(10);
+
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 200,
+ totalNumberOfRows: 500,
+ orderBy,
+ }),
+ ).toBe(30);
+ });
+
+ it('appends at the total when the record sorts after every loaded row', () => {
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 999,
+ totalNumberOfRows: 500,
+ orderBy,
+ }),
+ ).toBe(500);
+ });
+
+ it('returns null on a tie or a missing sort value', () => {
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 110,
+ totalNumberOfRows: 500,
+ orderBy,
+ }),
+ ).toBeNull();
+
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: undefined,
+ totalNumberOfRows: 500,
+ orderBy,
+ }),
+ ).toBeNull();
+ });
+
+ it('places an optimistic first or last position at the top or the end', () => {
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 'first',
+ totalNumberOfRows: 500,
+ orderBy,
+ }),
+ ).toBe(0);
+
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 'last',
+ totalNumberOfRows: 500,
+ orderBy,
+ }),
+ ).toBe(500);
+ });
+
+ it('mirrors the placement for a descending position sort', () => {
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows: [
+ loadedRow({ rowIndex: 0, position: 300 }),
+ loadedRow({ rowIndex: 1, position: 200 }),
+ ],
+ position: 250,
+ totalNumberOfRows: 2,
+ orderBy: [{ position: 'DescNullsLast' }],
+ }),
+ ).toBe(1);
+
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 'first',
+ totalNumberOfRows: 500,
+ orderBy: [{ position: 'DescNullsLast' }],
+ }),
+ ).toBe(500);
+ });
+
+ it('returns null when the view is not sorted by position', () => {
+ expect(
+ getRowIndexToInsertRecord({
+ loadedRows,
+ position: 5,
+ totalNumberOfRows: 500,
+ orderBy: [{ name: 'AscNullsFirst' }],
+ }),
+ ).toBeNull();
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getRowSlotWindow.test.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getRowSlotWindow.test.ts
new file mode 100644
index 00000000000..9436c85afc5
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/getRowSlotWindow.test.ts
@@ -0,0 +1,47 @@
+import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
+import { getRowSlotWindow } from '@/object-record/record-table/virtualization/utils/getRowSlotWindow';
+
+const ROW_HEIGHT = RECORD_TABLE_ROW_HEIGHT + 1;
+const VIEWPORT_HEIGHT = 20 * ROW_HEIGHT;
+
+describe('getRowSlotWindow', () => {
+ it('starts at the first row when scrolled to the top', () => {
+ expect(
+ getRowSlotWindow({
+ scrollTop: 0,
+ viewportHeight: VIEWPORT_HEIGHT,
+ totalNumberOfRows: 1000,
+ }),
+ ).toEqual({ firstRowIndex: 0, lastRowIndex: 239 });
+ });
+
+ it('centers the window on the middle of the viewport', () => {
+ expect(
+ getRowSlotWindow({
+ scrollTop: 500 * ROW_HEIGHT,
+ viewportHeight: VIEWPORT_HEIGHT,
+ totalNumberOfRows: 1000,
+ }),
+ ).toEqual({ firstRowIndex: 390, lastRowIndex: 629 });
+ });
+
+ it('ends at the last row when scrolled to the bottom', () => {
+ expect(
+ getRowSlotWindow({
+ scrollTop: 980 * ROW_HEIGHT,
+ viewportHeight: VIEWPORT_HEIGHT,
+ totalNumberOfRows: 1000,
+ }),
+ ).toEqual({ firstRowIndex: 760, lastRowIndex: 999 });
+ });
+
+ it('covers every row when there are fewer rows than slots', () => {
+ expect(
+ getRowSlotWindow({
+ scrollTop: 0,
+ viewportHeight: VIEWPORT_HEIGHT,
+ totalNumberOfRows: 50,
+ }),
+ ).toEqual({ firstRowIndex: 0, lastRowIndex: 49 });
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/insertEmptyRowAtIndex.test.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/insertEmptyRowAtIndex.test.ts
new file mode 100644
index 00000000000..b609e67f49a
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/insertEmptyRowAtIndex.test.ts
@@ -0,0 +1,22 @@
+import { insertEmptyRowAtIndex } from '@/object-record/record-table/virtualization/utils/insertEmptyRowAtIndex';
+
+describe('insertEmptyRowAtIndex', () => {
+ it('moves the rows at and after the index down by one and leaves the index empty', () => {
+ const recordIds: string[] = [];
+
+ recordIds[0] = 'a';
+ recordIds[1] = 'b';
+ recordIds[3] = 'c';
+
+ const nextRecordIds = insertEmptyRowAtIndex({
+ recordIdsByRowIndex: recordIds,
+ rowIndex: 1,
+ });
+
+ expect(nextRecordIds[0]).toBe('a');
+ expect(1 in nextRecordIds).toBe(false);
+ expect(nextRecordIds[2]).toBe('b');
+ expect(3 in nextRecordIds).toBe(false);
+ expect(nextRecordIds[4]).toBe('c');
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/keepRecordIdsInRowIndexRange.test.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/keepRecordIdsInRowIndexRange.test.ts
new file mode 100644
index 00000000000..30adbf0f1c8
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/keepRecordIdsInRowIndexRange.test.ts
@@ -0,0 +1,23 @@
+import { keepRecordIdsInRowIndexRange } from '@/object-record/record-table/virtualization/utils/keepRecordIdsInRowIndexRange';
+
+describe('keepRecordIdsInRowIndexRange', () => {
+ it('keeps the record ids inside the range at their row index', () => {
+ const recordIds: string[] = [];
+
+ recordIds[0] = 'a';
+ recordIds[2] = 'b';
+ recordIds[3] = 'c';
+ recordIds[5] = 'd';
+
+ const recordIdsInRange = keepRecordIdsInRowIndexRange({
+ recordIdsByRowIndex: recordIds,
+ firstRowIndex: 1,
+ lastRowIndex: 3,
+ });
+
+ expect(0 in recordIdsInRange).toBe(false);
+ expect(recordIdsInRange[2]).toBe('b');
+ expect(recordIdsInRange[3]).toBe('c');
+ expect(5 in recordIdsInRange).toBe(false);
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/placeRecordIdsAtRowIndexes.test.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/placeRecordIdsAtRowIndexes.test.ts
new file mode 100644
index 00000000000..3a43266f562
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/__tests__/placeRecordIdsAtRowIndexes.test.ts
@@ -0,0 +1,22 @@
+import { placeRecordIdsAtRowIndexes } from '@/object-record/record-table/virtualization/utils/placeRecordIdsAtRowIndexes';
+
+describe('placeRecordIdsAtRowIndexes', () => {
+ it('writes the records from the starting row index and clears their previous index', () => {
+ const recordIdsByRowIndex: string[] = [];
+
+ recordIdsByRowIndex[0] = 'a';
+ recordIdsByRowIndex[1] = 'b';
+ recordIdsByRowIndex[5] = 'c';
+
+ const nextRecordIdsByRowIndex = placeRecordIdsAtRowIndexes({
+ recordIdsByRowIndex,
+ recordIds: ['c', 'd'],
+ startingRowIndex: 1,
+ });
+
+ expect(nextRecordIdsByRowIndex[0]).toBe('a');
+ expect(nextRecordIdsByRowIndex[1]).toBe('c');
+ expect(nextRecordIdsByRowIndex[2]).toBe('d');
+ expect(5 in nextRecordIdsByRowIndex).toBe(false);
+ });
+});
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/computeRowSlotBindings.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/computeRowSlotBindings.ts
new file mode 100644
index 00000000000..fa4a4b01c56
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/computeRowSlotBindings.ts
@@ -0,0 +1,63 @@
+import { isDefined } from 'twenty-shared/utils';
+
+import { type RowSlotBinding } from '@/object-record/record-table/virtualization/types/RowSlotBinding';
+
+const getRowSlotBindingKey = (binding: RowSlotBinding) =>
+ binding.type === 'record'
+ ? `record-${binding.recordId}`
+ : `skeleton-${binding.rowIndex}`;
+
+export const computeRowSlotBindings = ({
+ currentBindings,
+ recordIdsByRowIndex,
+ firstRowIndex,
+ lastRowIndex,
+}: {
+ currentBindings: (RowSlotBinding | null)[];
+ recordIdsByRowIndex: string[];
+ firstRowIndex: number;
+ lastRowIndex: number;
+}): (RowSlotBinding | null)[] => {
+ const wantedBindingByKey = new Map<string, RowSlotBinding>();
+
+ for (let rowIndex = firstRowIndex; rowIndex <= lastRowIndex; rowIndex++) {
+ const recordId = recordIdsByRowIndex[rowIndex];
+
+ const binding: RowSlotBinding = isDefined(recordId)
+ ? { type: 'record', recordId }
+ : { type: 'skeleton', rowIndex };
+
+ wantedBindingByKey.set(getRowSlotBindingKey(binding), binding);
+ }
+
+ const nextBindings = currentBindings.map((binding) =>
+ isDefined(binding) && wantedBindingByKey.has(getRowSlotBindingKey(binding))
+ ? binding
+ : null,
+ );
+
+ for (const binding of nextBindings) {
+ if (isDefined(binding)) {
+ wantedBindingByKey.delete(getRowSlotBindingKey(binding));
+ }
+ }
+
+ let freeRowSlot = 0;
+
+ for (const binding of wantedBindingByKey.values()) {
+ while (
+ freeRowSlot < nextBindings.length &&
+ isDefined(nextBindings[freeRowSlot])
+ ) {
+ freeRowSlot++;
+ }
+
+ if (freeRowSlot >= nextBindings.length) {
+ break;
+ }
+
+ nextBindings[freeRowSlot] = binding;
+ }
+
+ return nextBindings;
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getAppliedRecordOperationKey.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getAppliedRecordOperationKey.ts
new file mode 100644
index 00000000000..3996daa7b7a
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getAppliedRecordOperationKey.ts
@@ -0,0 +1,7 @@
+export const getAppliedRecordOperationKey = ({
+ operationType,
+ recordId,
+}: {
+ operationType: 'create' | 'delete';
+ recordId: string;
+}): string => `${operationType}-${recordId}`;
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getPagesWithUnloadedRows.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getPagesWithUnloadedRows.ts
new file mode 100644
index 00000000000..382964d9906
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getPagesWithUnloadedRows.ts
@@ -0,0 +1,28 @@
+import { getContiguousIncrementalValues, isDefined } from 'twenty-shared/utils';
+
+import { TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE } from '@/object-record/record-table/virtualization/constants/TableVirtualizationNumberOfRecordsPerPage';
+
+export const getPagesWithUnloadedRows = ({
+ pages,
+ recordIdsByRowIndex,
+ totalNumberOfRows,
+}: {
+ pages: number[];
+ recordIdsByRowIndex: string[];
+ totalNumberOfRows: number;
+}): number[] =>
+ pages.filter((page) => {
+ const firstRowIndex =
+ page * TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE;
+
+ const numberOfRowsInPage =
+ Math.min(
+ firstRowIndex + TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE,
+ totalNumberOfRows,
+ ) - firstRowIndex;
+
+ return getContiguousIncrementalValues(
+ Math.max(0, numberOfRowsInPage),
+ firstRowIndex,
+ ).some((rowIndex) => !isDefined(recordIdsByRowIndex[rowIndex]));
+ });
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowIndexAtViewportMiddle.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowIndexAtViewportMiddle.ts
new file mode 100644
index 00000000000..52ca879d706
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowIndexAtViewportMiddle.ts
@@ -0,0 +1,16 @@
+import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
+
+export const getRowIndexAtViewportMiddle = ({
+ scrollTop,
+ viewportHeight,
+}: {
+ scrollTop: number;
+ viewportHeight: number;
+}): number => {
+ const rowHeight = RECORD_TABLE_ROW_HEIGHT + 1;
+
+ return (
+ Math.floor(scrollTop / rowHeight) +
+ Math.floor(viewportHeight / rowHeight / 2)
+ );
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowIndexToInsertRecord.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowIndexToInsertRecord.ts
new file mode 100644
index 00000000000..d73e94cef1a
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowIndexToInsertRecord.ts
@@ -0,0 +1,60 @@
+import { isNumber } from '@sniptt/guards';
+import { type RecordGqlOperationOrderBy } from 'twenty-shared/types';
+
+import { isAscendingOrder } from '@/object-record/graphql/utils/isAscendingOrder';
+import { isOrderByDirection } from '@/object-record/graphql/utils/isOrderByDirection';
+
+export const getRowIndexToInsertRecord = ({
+ loadedRows,
+ position,
+ totalNumberOfRows,
+ orderBy,
+}: {
+ loadedRows: { rowIndex: number; position: number | null }[];
+ position: number | 'first' | 'last' | null | undefined;
+ totalNumberOfRows: number;
+ orderBy: RecordGqlOperationOrderBy;
+}): number | null => {
+ const positionDirection = orderBy[0]?.position;
+
+ if (!isOrderByDirection(positionDirection)) {
+ return null;
+ }
+
+ const isAscending = isAscendingOrder(positionDirection);
+
+ // An optimistic create carries the position input ('first' or 'last') until the server resolves it
+ if (position === 'first' || position === 'last') {
+ const isInsertedAtTop = (position === 'first') === isAscending;
+
+ if (isInsertedAtTop) {
+ return 0;
+ }
+
+ return totalNumberOfRows;
+ }
+
+ if (!isNumber(position) || loadedRows.length === 0) {
+ return null;
+ }
+
+ let low = 0;
+ let high = loadedRows.length;
+
+ while (low < high) {
+ const middle = Math.floor((low + high) / 2);
+ const loadedPosition = loadedRows[middle].position;
+
+ if (!isNumber(loadedPosition) || loadedPosition === position) {
+ return null;
+ }
+
+ if (position < loadedPosition === isAscending) {
+ high = middle;
+ } else {
+ low = middle + 1;
+ }
+ }
+
+ return loadedRows[low]?.rowIndex ?? totalNumberOfRows;
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowSlotTop.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowSlotTop.ts
new file mode 100644
index 00000000000..df5b17e8827
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowSlotTop.ts
@@ -0,0 +1,4 @@
+import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
+
+export const getRowSlotTop = (rowIndex: number) =>
+ (rowIndex + 1) * (RECORD_TABLE_ROW_HEIGHT + 1);
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowSlotWindow.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowSlotWindow.ts
new file mode 100644
index 00000000000..3ef3dc51ac5
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getRowSlotWindow.ts
@@ -0,0 +1,27 @@
+import { getRowIndexAtViewportMiddle } from '@/object-record/record-table/virtualization/utils/getRowIndexAtViewportMiddle';
+import { ROW_SLOT_COUNT } from '@/object-record/record-table/virtualization/constants/RowSlotCount';
+
+export const getRowSlotWindow = ({
+ scrollTop,
+ viewportHeight,
+ totalNumberOfRows,
+}: {
+ scrollTop: number;
+ viewportHeight: number;
+ totalNumberOfRows: number;
+}): { firstRowIndex: number; lastRowIndex: number } => {
+ const middleRowIndex = getRowIndexAtViewportMiddle({
+ scrollTop,
+ viewportHeight,
+ });
+
+ const lastRowIndex = Math.min(
+ totalNumberOfRows - 1,
+ Math.max(0, middleRowIndex - ROW_SLOT_COUNT / 2) + ROW_SLOT_COUNT - 1,
+ );
+
+ return {
+ firstRowIndex: Math.max(0, lastRowIndex - ROW_SLOT_COUNT + 1),
+ lastRowIndex,
+ };
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getVirtualizationOverscanWindow.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getVirtualizationOverscanWindow.ts
index 041a97b11e8..50b287f676e 100644
--- a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getVirtualizationOverscanWindow.ts
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/getVirtualizationOverscanWindow.ts
@@ -1,53 +1,54 @@
import { RECORD_TABLE_ROW_HEIGHT } from '@/object-record/record-table/constants/RecordTableRowHeight';
+import { getRowIndexAtViewportMiddle } from '@/object-record/record-table/virtualization/utils/getRowIndexAtViewportMiddle';
import { TABLE_VIRTUALIZATION_NUMBER_OF_OVERSCAN_PAGES } from '@/object-record/record-table/virtualization/constants/TableVirtualizationNumberOfOverscanPages';
import { TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE } from '@/object-record/record-table/virtualization/constants/TableVirtualizationNumberOfRecordsPerPage';
-export const getVirtualizationOverscanWindow = (
- scrollPosition: number,
- scrollWrapperHeight: number,
- totalNumberOfRecordsToVirtualize: number,
-) => {
- const numberOfRowsDisplayedInTable = Math.min(
- Math.floor(scrollWrapperHeight / (RECORD_TABLE_ROW_HEIGHT + 1)),
- 40,
- );
-
- const halfNumberOfRowsVisible = Math.floor(numberOfRowsDisplayedInTable / 2);
-
- const realIndexAtTheMiddleOfTheTable =
- Math.floor(scrollPosition / (RECORD_TABLE_ROW_HEIGHT + 1)) +
- halfNumberOfRowsVisible;
-
- const pageForRealIndex = Math.ceil(
- realIndexAtTheMiddleOfTheTable /
+export const getVirtualizationOverscanWindow = ({
+ scrollPosition,
+ scrollWrapperHeight,
+ totalNumberOfRows,
+}: {
+ scrollPosition: number;
+ scrollWrapperHeight: number;
+ totalNumberOfRows: number;
+}) => {
+ const rowIndexAtTheMiddleOfTheTable = getRowIndexAtViewportMiddle({
+ scrollTop: scrollPosition,
+ viewportHeight: Math.min(
+ scrollWrapperHeight,
+ 40 * (RECORD_TABLE_ROW_HEIGHT + 1),
+ ),
+ });
+
+ const pageForRowIndex = Math.ceil(
+ rowIndexAtTheMiddleOfTheTable /
TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE,
);
const maxPage = Math.ceil(
- totalNumberOfRecordsToVirtualize /
- TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE,
+ totalNumberOfRows / TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE,
);
const overscanPageAtTop = Math.max(
0,
- pageForRealIndex - TABLE_VIRTUALIZATION_NUMBER_OF_OVERSCAN_PAGES,
+ pageForRowIndex - TABLE_VIRTUALIZATION_NUMBER_OF_OVERSCAN_PAGES,
);
const overscanPageAtBottom = Math.min(
maxPage,
- pageForRealIndex + TABLE_VIRTUALIZATION_NUMBER_OF_OVERSCAN_PAGES,
+ pageForRowIndex + TABLE_VIRTUALIZATION_NUMBER_OF_OVERSCAN_PAGES,
);
- const firstRealIndexInOverscanWindow =
+ const firstRowIndexInOverscanWindow =
overscanPageAtTop * TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE;
- const lastRealIndexInOverscanWindow =
+ const lastRowIndexInOverscanWindow =
overscanPageAtBottom * TABLE_VIRTUALIZATION_NUMBER_OF_RECORDS_PER_PAGE;
return {
overscanPageAtTop,
overscanPageAtBottom,
- firstRealIndexInOverscanWindow,
- lastRealIndexInOverscanWindow,
+ firstRowIndexInOverscanWindow,
+ lastRowIndexInOverscanWindow,
};
};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/insertEmptyRowAtIndex.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/insertEmptyRowAtIndex.ts
new file mode 100644
index 00000000000..f17cd9cc418
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/insertEmptyRowAtIndex.ts
@@ -0,0 +1,17 @@
+export const insertEmptyRowAtIndex = ({
+ recordIdsByRowIndex,
+ rowIndex,
+}: {
+ recordIdsByRowIndex: string[];
+ rowIndex: number;
+}): string[] => {
+ const nextRecordIdsByRowIndex: string[] = [];
+
+ recordIdsByRowIndex.forEach((recordId, currentRowIndex) => {
+ nextRecordIdsByRowIndex[
+ currentRowIndex < rowIndex ? currentRowIndex : currentRowIndex + 1
+ ] = recordId;
+ });
+
+ return nextRecordIdsByRowIndex;
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/keepRecordIdsInRowIndexRange.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/keepRecordIdsInRowIndexRange.ts
new file mode 100644
index 00000000000..798e96f416d
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/keepRecordIdsInRowIndexRange.ts
@@ -0,0 +1,19 @@
+export const keepRecordIdsInRowIndexRange = ({
+ recordIdsByRowIndex,
+ firstRowIndex,
+ lastRowIndex,
+}: {
+ recordIdsByRowIndex: string[];
+ firstRowIndex: number;
+ lastRowIndex: number;
+}): string[] => {
+ const recordIdsInRange: string[] = [];
+
+ recordIdsByRowIndex.forEach((recordId, rowIndex) => {
+ if (rowIndex >= firstRowIndex && rowIndex <= lastRowIndex) {
+ recordIdsInRange[rowIndex] = recordId;
+ }
+ });
+
+ return recordIdsInRange;
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/placeRecordIdsAtRowIndexes.ts b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/placeRecordIdsAtRowIndexes.ts
new file mode 100644
index 00000000000..0b152f92f4f
--- /dev/null
+++ b/packages/twenty-front/src/modules/object-record/record-table/virtualization/utils/placeRecordIdsAtRowIndexes.ts
@@ -0,0 +1,24 @@
+export const placeRecordIdsAtRowIndexes = ({
+ recordIdsByRowIndex,
+ recordIds,
+ startingRowIndex,
+}: {
+ recordIdsByRowIndex: string[];
+ recordIds: string[];
+ startingRowIndex: number;
+}): string[] => {
+ const placedRecordIds = new Set(recordIds);
+ const nextRecordIdsByRowIndex: string[] = [];
+
+ recordIdsByRowIndex.forEach((recordId, rowIndex) => {
+ if (!placedRecordIds.has(recordId)) {
+ nextRecordIdsByRowIndex[rowIndex] = recordId;
+ }
+ });
+
+ recordIds.forEach((recordId, offset) => {
+ nextRecordIdsByRowIndex[startingRowIndex + offset] = recordId;
+ });
+
+ return nextRecordIdsByRowIndex;
+};
diff --git a/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/LinkChip.tsx b/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/LinkChip.tsx
index 04bf63496c5..6c7daeb59ad 100644
--- a/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/LinkChip.tsx
+++ b/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/LinkChip.tsx
@@ -1,5 +1,4 @@
import { css } from '@linaria/core';
-import { Link } from 'react-router-dom';
import { type LinkChipProps } from './types/LinkChipProps';
import { LINK_CHIP_CLICK_OUTSIDE_ID } from '@/ui/navigation/link/constants/LinkChipClickOutsideId';
@@ -34,8 +33,8 @@ export const LinkChip = ({
clickable
{...props}
render={
- <Link
- to={to}
+ <a
+ href={to}
ref={ref}
onClick={(event) => {
event.stopPropagation();
diff --git a/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/types/LinkChipProps.ts b/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/types/LinkChipProps.ts
index 2f706d12457..50d68bce6f1 100644
--- a/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/types/LinkChipProps.ts
+++ b/packages/twenty-front/src/modules/ui/navigation/link/components/LinkChip/types/LinkChipProps.ts
@@ -1,13 +1,12 @@
import { type TriggerEventType } from '@/ui/navigation/types/TriggerEventType';
import { type ComponentPropsWithRef, type MouseEvent } from 'react';
-import { type Link } from 'react-router-dom';
import { type ChipProps } from 'twenty-ui/primitives/data-display';
export type LinkChipProps = Omit<
ChipProps,
'render' | 'ref' | 'onClick' | 'onMouseDown'
> &
- Pick<ComponentPropsWithRef<typeof Link>, 'target' | 'ref'> & {
+ Pick<ComponentPropsWithRef<'a'>, 'target' | 'ref'> & {
to: string;
onClick?: (event: MouseEvent<HTMLElement>) => void;
onMouseDown?: (event: MouseEvent<HTMLElement>) => void;