-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[On-week] Add persisting page size and sorting for Management tables #186997
[On-week] Add persisting page size and sorting for Management tables #186997
Conversation
/ci |
/ci |
/ci |
…om/ElenaStoeva/kibana into on-week/persisting-table-page-size
/ci |
/ci |
/ci |
Pinging @elastic/kibana-management (Team:Kibana Management) |
Pinging @elastic/appex-sharedux (Team:SharedUX) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a great improvement to the tables, @ElenaStoeva!
I verified locally that the page size is persisted between page loads. Code changes LGTM, I would just recommend not naming the function a hook if it doesn't use any react hooks. Also I think some unit tests for the useEuiTablePersist
could be useful as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @ElenaStoeva ! I left a few comments regarding code style and readability.
As @yuliacech mentioned currently it is not a hook and I think we could return at least the onTableChange
in a useCallback
and probably make the 2 other value a state so those are reactive values for consumers.
const newPersistData: PersistData<T> = {}; | ||
newPersistData.pageSize = nextValues.page?.size ?? storagePageSize; | ||
const newSort = nextValues?.sort; | ||
newPersistData.sort = newSort?.field |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not easy to parse the double ternary, could we make it more explicit like the below suggestion? Also I would keep the "next" prefix everywhere instead of "new" and "next"
let nextSort: Criteria<T>['sort'];
if (nextValues.sort?.field) {
nextSort = nextValues.sort;
} else if (!nextValues.sort?.direction) {
// If both field and direction are undefined, there is no change on sort so use the stored one
nextSort = storageSort;
}
const nextPageSize = nextValues.page?.size ?? storagePageSize;
const nextPersistData: PersistData<T> = {
pageSize: nextPageSize,
sort: nextSort,
};
if (nextPersistData.pageSize !== storagePageSize || nextPersistData.sort !== storageSort) {
storage.set(tableId, nextPersistData);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I updated it as suggested, with some modifications due to some recent changes.
On a side note, I just want to note that with the current logic, if the consumer provides an initial sort, when the user removes a sort (the change criteria would contain just the direction
property, with undefined
field), then we store undefined
sort in local storage, so next time when the user visits this page, the initial sort will be restored (rather than no sort to be preserved). Just wanted to make sure this is the desired behaviour.
...nagement/public/application/components/component_templates/component_template_list/table.tsx
Outdated
Show resolved
Hide resolved
...nt/public/application/sections/home/data_stream_list/data_stream_table/data_stream_table.tsx
Outdated
Show resolved
Hide resolved
..._management/public/application/sections/home/template_list/template_table/template_table.tsx
Outdated
Show resolved
Hide resolved
x-pack/plugins/ingest_pipelines/public/application/sections/pipelines_list/table.tsx
Outdated
Show resolved
Hide resolved
Many thanks for the thorough reviews, @sebelga and @yuliacech! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! I left one more comment to avoid having the state as a dependency of a callback setting the state.
I'll approve to unblock the PR but it'd be great to apply my suggestion 🙏 Thanks!
storage.set(tableId, nextPersistData); | ||
} | ||
}, | ||
[customOnTableChange, pageSize, sort, storage, storagePageSize, storageSort, tableId] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a callback that it setting the state (setPageSize
, setSort
), it is usually better to avoid having the state being set as a dependency. It could create an infinite loop.
Here I checked and there are no infinite loop but I think we could avoid it this way
const onTableChange = useCallback(
(nextValues: Criteria<T>) => {
if (customOnTableChange) {
customOnTableChange(nextValues);
}
let nextSort: PropertySort<T> | undefined;
if (nextValues.sort?.field && nextValues.sort?.direction) {
// Both field and direction are needed for a valid sort criteria
nextSort = nextValues.sort;
}
if (nextValues.sort?.field || nextValues.sort?.direction) {
setSort(nextSort);
}
const nextPageSize = nextValues.page?.size;
if (nextPageSize) {
setPageSize(nextPageSize);
}
if (
(nextPageSize && nextPageSize !== storagePageSize) ||
(nextSort && nextSort !== storageSort)
) {
const nextPersistData: PersistData<T> = {
pageSize: nextPageSize,
sort: nextSort,
};
storage.set(tableId, nextPersistData);
}
},
[customOnTableChange, storage, storagePageSize, storageSort, tableId]
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! I added your suggestion with a small modification to ensure that sort would be cleaned up in local storage if the user removes the sort on a specific field (when this happens, field
is an empty string). 7754207
💛 Build succeeded, but was flaky
Failed CI StepsTest Failures
Metrics [docs]Module Count
Public APIs missing comments
Async chunks
Public APIs missing exports
Unknown metric groupsAPI count
History
To update your PR or re-run it, just comment with: cc @ElenaStoeva |
Addresses #56406
Summary
This PR is part of my June 2024 On-week project. It adds functionality for persisting table page size (rows per page) and sorting in EUI tables. When a user changes the size or sort, the new values are saved in local storage, so that when the user navigates out of the page and comes back, the page size and sort will be the same.
In this PR, the functionality is added to the following tables:
Screen.Recording.2024-06-28.at.16.05.48.mov