The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/client/components/GenericTable/hooks/useSort.ts

34 lines
745 B

import { useCallback, useState } from 'react';
type Direction = 'asc' | 'desc';
export const useSort = <T extends string>(
by: T,
initialDirection: Direction = 'asc',
): {
sortBy: T;
sortDirection: Direction;
setSort: (sortBy: T, direction?: Direction | undefined) => void;
} => {
const [sort, _setSort] = useState<[T, Direction]>(() => [by, initialDirection]);
const setSort = useCallback((id: T, direction?: Direction | undefined) => {
_setSort(([sortBy, sortDirection]) => {
if (direction) {
return [id, direction];
}
if (sortBy === id) {
return [id, sortDirection === 'asc' ? 'desc' : 'asc'];
}
return [id, 'asc'];
});
}, []);
return {
sortBy: sort[0],
sortDirection: sort[1],
setSort,
};
};