Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/real-suns-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/meteor': patch
'@rocket.chat/ui-client': patch
---

Fixes pagination not resetting to the first page when searching the Custom Sounds and Custom Emojis admin tables. Searching from a later page previously kept the stale offset, so the filtered request ran with an out-of-range offset and returned an empty result set.
4 changes: 2 additions & 2 deletions apps/meteor/client/views/admin/customEmoji/CustomEmoji.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const CustomEmoji = ({ onClick, reload }: CustomEmojiProps) => {

const [text, setText] = useState('');
const { sortBy, sortDirection, setSort } = useSort<'name'>('name');
const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();
const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination(text);

const query = useDebouncedValue(
useMemo(
Expand Down Expand Up @@ -114,7 +114,7 @@ const CustomEmoji = ({ onClick, reload }: CustomEmojiProps) => {
/>
</>
)}
{isSuccess && data && data.emojis.length === 0 && <GenericNoResults />}
{isSuccess && data?.emojis.length === 0 && <GenericNoResults />}
{isError && (
<States>
<StatesIcon name='warning' variation='danger' />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ export type CustomSoundsTableProps = {
const CustomSoundsTable = ({ reload, onClick }: CustomSoundsTableProps) => {
const t = useTranslation();
const { sortBy, sortDirection, setSort } = useSort<'name'>('name');
const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination();

const [text, setText] = useState('');
const { current, itemsPerPage, setItemsPerPage: onSetItemsPerPage, setCurrent: onSetCurrent, ...paginationProps } = usePagination(text);

Comment thread
ricardogarim marked this conversation as resolved.
Resolved
const query = useDebouncedValue(
useMemo(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { act, renderHook } from '@testing-library/react';

import { usePagination } from './usePagination';

it('should reset to the first page when the reset key changes', () => {
const { result, rerender } = renderHook(({ resetKey }) => usePagination(resetKey), {
wrapper: mockAppRoot().build(),
initialProps: { resetKey: 'initial' },
});

act(() => {
result.current.setCurrent(3);
});
expect(result.current.current).toBe(3);

rerender({ resetKey: 'changed' });

expect(result.current.current).toBe(0);
});

it('should keep the current page when the reset key does not change', () => {
const { result, rerender } = renderHook(({ resetKey }) => usePagination(resetKey), {
wrapper: mockAppRoot().build(),
initialProps: { resetKey: 'same' },
});

act(() => {
result.current.setCurrent(2);
});
expect(result.current.current).toBe(2);

rerender({ resetKey: 'same' });

expect(result.current.current).toBe(2);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { useShowingResultsLabel } from './useShowingResultsLabel';
/**
* TODO: Move `usePagination` outside from `GenericTable` folder
*/
export const usePagination = (): {
export const usePagination = (
resetKey?: unknown,
): {
current: ReturnType<typeof useCurrent>[0];
setCurrent: ReturnType<typeof useCurrent>[1];
itemsPerPage: ReturnType<typeof useItemsPerPage>[0];
Expand All @@ -21,10 +23,9 @@ export const usePagination = (): {
const itemsPerPageLabel = useItemsPerPageLabel();
const showingResultsLabel = useShowingResultsLabel();

// Reset to first page when itemsPerPage changes
useEffect(() => {
setCurrent(0);
}, [itemsPerPage, setCurrent]);
}, [itemsPerPage, setCurrent, resetKey]);

return useMemo(
() => ({
Expand Down
Loading