Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added parallel code path to set new record sorts state #10345

Merged
merged 2 commits into from
Feb 20, 2025

Conversation

lucasbordeau
Copy link
Contributor

This PR implements a parallel code path to set record sorts without impacting the actual sort, like we did on record filter.

We add a ViewBarRecordSortEffect which mirrors ViewBarRecordFilterEffect.

It also adds availableFieldMetadataItemsForSortFamilySelector to replace sortDefinitions progressively.

- Implemented ViewBarRecordSortEffect
- Added availableFieldMetadataItemsForSortFamilySelector
- Renamed sort to recordSort where possible
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR implements a parallel state management system for record sorting, focusing on separating view sorts from record sorts for better state control. Here's a summary of the key changes:

  • Added availableFieldMetadataItemsForSortFamilySelector in /packages/twenty-front/src/modules/object-metadata/states/availableFieldMetadataItemsForSortFamilySelector.ts to manage sortable field metadata
  • Introduced ViewBarRecordSortEffect in /packages/twenty-front/src/modules/views/components/ViewBarRecordSortEffect.tsx to handle sort initialization and state synchronization
  • Replaced Sort type with RecordSort type across multiple components for better type safety and consistency
  • Added useUpsertRecordSort hook in /packages/twenty-front/src/modules/object-record/record-sort/hooks/useUpsertRecordSort.ts for managing record sort state updates
  • Implemented filterSortableFieldMetadataItems in /packages/twenty-front/src/modules/object-metadata/utils/filterSortableFieldMetadataItems.ts to determine which fields can be sorted

31 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +201 to 204
{RECORD_SORT_DIRECTIONS.map((sortDirection, index) => (
<MenuItem
key={index}
onClick={() => handleSortDirectionClick(sortDirection)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Using array index as key prop may cause issues with React reconciliation if items are reordered. Consider using sortDirection as the key instead

Suggested change
{RECORD_SORT_DIRECTIONS.map((sortDirection, index) => (
<MenuItem
key={index}
onClick={() => handleSortDirectionClick(sortDirection)}
{RECORD_SORT_DIRECTIONS.map((sortDirection) => (
<MenuItem
key={sortDirection}
onClick={() => handleSortDirectionClick(sortDirection)}


export const onSortSelectComponentState = createComponentStateV2<
((sort: Sort) => void) | undefined
((sort: RecordSort) => void) | undefined
>({
key: 'onSortSelectComponentState',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding a more descriptive key that includes 'record' to match the new type (e.g., 'onRecordSortSelectComponentState')

Comment on lines 21 to 24
const foundRecordSortInCurrentRecordSorts = currentRecordSorts.some(
(existingSort) =>
existingSort.fieldMetadataId === recordSortToSet.fieldMetadataId,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: foundRecordSortInCurrentRecordSorts is a boolean but the isDefined check on line 26 is redundant since .some() always returns a boolean

Suggested change
const foundRecordSortInCurrentRecordSorts = currentRecordSorts.some(
(existingSort) =>
existingSort.fieldMetadataId === recordSortToSet.fieldMetadataId,
);
const foundRecordSortInCurrentRecordSorts = currentRecordSorts.some(
(existingSort) =>
existingSort.fieldMetadataId === recordSortToSet.fieldMetadataId,
);
if (!foundRecordSortInCurrentRecordSorts) {

Comment on lines +35 to +39
const indexOfSortToUpdate = newCurrentRecordSorts.findIndex(
(existingSort) =>
existingSort.fieldMetadataId ===
recordSortToSet.fieldMetadataId,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: no bounds check before array access on line 41 - findIndex could return -1 if no match is found

Suggested change
const indexOfSortToUpdate = newCurrentRecordSorts.findIndex(
(existingSort) =>
existingSort.fieldMetadataId ===
recordSortToSet.fieldMetadataId,
);
const indexOfSortToUpdate = newCurrentRecordSorts.findIndex(
(existingSort) =>
existingSort.fieldMetadataId ===
recordSortToSet.fieldMetadataId,
);
if (indexOfSortToUpdate === -1) {
return newCurrentRecordSorts;
}

fields: sortableFieldMetadataItems,
});

if (isDefined(currentView)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Redundant isDefined check since it's already checked on line 54

Suggested change
if (isDefined(currentView)) {
const sortDefinitions = formatFieldMetadataItemsAsSortDefinitions({
fields: sortableFieldMetadataItems,
});
setCurrentRecordSorts(
mapViewSortsToSorts(currentView.viewSorts, sortDefinitions),
);
setHasInitializedCurrentRecordSorts(true);

});
export const isRecordSortDirectionMenuUnfoldedComponentState =
createComponentStateV2<boolean>({
key: 'isRecordSortDirectionMenuUnfoldedComponentState',
Copy link
Member

@charlesBochet charlesBochet Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: i'm just wondering if the naming should be even more "scoped": isRecordSortDirectionDropdownMenuUnfoldedComponentState

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sure

@charlesBochet charlesBochet merged commit 3f93aba into main Feb 20, 2025
47 checks passed
@charlesBochet charlesBochet deleted the refactor/record-sorts-upsert-and-effect branch February 20, 2025 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants