getSortValueFromQuery.ts 442 Bytes
Newer Older
贾浩@五瓣科技's avatar
贾浩@五瓣科技 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import type { Query } from 'nextjs-routes';

import type { Option } from 'ui/shared/sort/Sort';

export default function getSortValueFromQuery<SortValue extends string>(query: Query, sortOptions: Array<Option<SortValue>>) {
  if (!query.sort || !query.order) {
    return undefined;
  }

  const str = query.sort + '-' + query.order;
  if (sortOptions.map(option => option.id).includes(str as SortValue)) {
    return str as SortValue;
  }
}