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

Fix table sort does not work if table has formula column #1528

Merged
merged 1 commit into from
Jan 26, 2024

Conversation

bofeiw
Copy link
Contributor

@bofeiw bofeiw commented Jan 23, 2024

Issue

Sort does not work if table has formula column.

Example: in this table setup, the right-most column is a formula column. The table sort is set to num descending, however the table UI displays rows order incorrectly.

Screenshot 2024-01-23 at 07 26 43

Cause

In a commit related to formula field, it sets all rows to rowsLocal if there is formula column in the table.

const isLocalRow =
fieldName.startsWith("_rowy_formulaValue_") ||
Boolean(find(tableRowsLocal, ["_rowy_ref.path", path]));

rowsLocal is designed to hold our of order rows that are supposed to be a very small set, and they are always displayed at the top of the table UI. They are not sorted or ordered.

/** Combine tableRowsLocal and tableRowsDb */
export const tableRowsAtom = atom<TableRow[]>((get) => {
const rowsDb = get(tableRowsDbAtom);
const rowsLocal = get(tableRowsLocalAtom);
// Optimization: create Map of rowsDb by path to index for faster lookup
const rowsDbMap = new Map<string, number>();
rowsDb.forEach((row, i) => rowsDbMap.set(row._rowy_ref.path, i));
// Loop through rowsLocal, which is usually the smaller of the two arrays
const rowsLocalToMerge = rowsLocal.map((row, i) => {
// If row is in rowsDb, merge the two
// and remove from rowsDb to prevent duplication
if (rowsDbMap.has(row._rowy_ref.path)) {
const index = rowsDbMap.get(row._rowy_ref.path)!;
const merged = updateRowData({ ...rowsDb[index] }, row);
rowsDbMap.delete(row._rowy_ref.path);
return merged;
}
return row;
});
// Merge the two arrays
return [
...rowsLocalToMerge,
...rowsDb.filter((row) => rowsDbMap.has(row._rowy_ref.path)),
];
});

However, the commit would set all rows to rowsLocal, as a result, rows are displayed without going through filters or sorts.

Fix

Simply remove the condition for local row would fix this issue.

fieldName.startsWith("_rowy_formulaValue_")

After fix:
Screenshot 2024-01-23 at 07 25 17

@bofeiw bofeiw added the bug Something isn't working label Jan 23, 2024
Copy link

vercel bot commented Jan 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
rowy-os ✅ Ready (Inspect) Visit Preview Jan 23, 2024 0:00am
rowy-typedoc ✅ Ready (Inspect) Visit Preview Jan 23, 2024 0:00am

@shamsmosowi shamsmosowi merged commit 61c466f into develop Jan 26, 2024
3 checks passed
@bofeiw bofeiw deleted the feat/fix-sorting branch January 26, 2024 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants