2021-12-25 21:51:25 +03:00
|
|
|
import { isEmpty, pipe } from 'ramda';
|
2023-02-18 13:11:01 +03:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
2023-07-16 09:47:10 +03:00
|
|
|
import type { TagsFilteringMode } from '../../../api/types';
|
|
|
|
import { orderToString, stringToOrder } from '../../../utils/helpers/ordering';
|
|
|
|
import { parseQuery, stringifyQuery } from '../../../utils/helpers/query';
|
|
|
|
import type { BooleanString } from '../../../utils/utils';
|
|
|
|
import { parseOptionalBooleanToString } from '../../../utils/utils';
|
2023-02-18 13:11:01 +03:00
|
|
|
import type { ShortUrlsOrder, ShortUrlsOrderableFields } from '../data';
|
2021-11-09 01:23:45 +03:00
|
|
|
|
2021-12-25 21:51:25 +03:00
|
|
|
interface ShortUrlsQueryCommon {
|
2021-11-09 01:23:45 +03:00
|
|
|
search?: string;
|
2021-11-11 00:25:56 +03:00
|
|
|
startDate?: string;
|
|
|
|
endDate?: string;
|
2022-01-31 12:15:25 +03:00
|
|
|
tagsMode?: TagsFilteringMode;
|
2021-11-09 01:23:45 +03:00
|
|
|
}
|
|
|
|
|
2021-12-25 21:51:25 +03:00
|
|
|
interface ShortUrlsQuery extends ShortUrlsQueryCommon {
|
|
|
|
orderBy?: string;
|
2022-03-13 13:14:30 +03:00
|
|
|
tags?: string;
|
2022-12-23 22:00:59 +03:00
|
|
|
excludeBots?: BooleanString;
|
2022-12-29 21:03:17 +03:00
|
|
|
excludeMaxVisitsReached?: BooleanString;
|
|
|
|
excludePastValidUntil?: BooleanString;
|
2021-12-25 21:51:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ShortUrlsFiltering extends ShortUrlsQueryCommon {
|
|
|
|
orderBy?: ShortUrlsOrder;
|
2022-03-13 13:14:30 +03:00
|
|
|
tags: string[];
|
2022-12-23 22:00:59 +03:00
|
|
|
excludeBots?: boolean;
|
2022-12-29 21:03:17 +03:00
|
|
|
excludeMaxVisitsReached?: boolean;
|
|
|
|
excludePastValidUntil?: boolean;
|
2021-12-25 21:51:25 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 11:11:46 +03:00
|
|
|
type ToFirstPage = (extra: Partial<ShortUrlsFiltering>) => void;
|
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
export const useShortUrlsQuery = (): [ShortUrlsFiltering, ToFirstPage] => {
|
|
|
|
const navigate = useNavigate();
|
2022-11-26 11:11:46 +03:00
|
|
|
const { search } = useLocation();
|
|
|
|
const { serverId = '' } = useParams<{ serverId: string }>();
|
2022-02-06 22:07:18 +03:00
|
|
|
|
2022-11-26 11:11:46 +03:00
|
|
|
const filtering = useMemo(
|
2021-12-25 21:51:25 +03:00
|
|
|
pipe(
|
2022-11-26 11:11:46 +03:00
|
|
|
() => parseQuery<ShortUrlsQuery>(search),
|
2022-12-29 21:03:17 +03:00
|
|
|
({ orderBy, tags, excludeBots, excludeMaxVisitsReached, excludePastValidUntil, ...rest }): ShortUrlsFiltering => {
|
2022-03-13 13:14:30 +03:00
|
|
|
const parsedOrderBy = orderBy ? stringToOrder<ShortUrlsOrderableFields>(orderBy) : undefined;
|
|
|
|
const parsedTags = tags?.split(',') ?? [];
|
2022-12-23 22:00:59 +03:00
|
|
|
return {
|
|
|
|
...rest,
|
|
|
|
orderBy: parsedOrderBy,
|
|
|
|
tags: parsedTags,
|
|
|
|
excludeBots: excludeBots !== undefined ? excludeBots === 'true' : undefined,
|
2022-12-29 21:03:17 +03:00
|
|
|
excludeMaxVisitsReached: excludeMaxVisitsReached !== undefined ? excludeMaxVisitsReached === 'true' : undefined,
|
|
|
|
excludePastValidUntil: excludePastValidUntil !== undefined ? excludePastValidUntil === 'true' : undefined,
|
2022-12-23 22:00:59 +03:00
|
|
|
};
|
2021-12-25 21:51:25 +03:00
|
|
|
},
|
|
|
|
),
|
2022-11-26 11:11:46 +03:00
|
|
|
[search],
|
2021-12-25 21:51:25 +03:00
|
|
|
);
|
|
|
|
const toFirstPageWithExtra = (extra: Partial<ShortUrlsFiltering>) => {
|
2022-12-29 21:03:17 +03:00
|
|
|
const merged = { ...filtering, ...extra };
|
|
|
|
const { orderBy, tags, excludeBots, excludeMaxVisitsReached, excludePastValidUntil, ...mergedFiltering } = merged;
|
2022-11-26 11:11:46 +03:00
|
|
|
const query: ShortUrlsQuery = {
|
|
|
|
...mergedFiltering,
|
2022-03-13 13:14:30 +03:00
|
|
|
orderBy: orderBy && orderToString(orderBy),
|
|
|
|
tags: tags.length > 0 ? tags.join(',') : undefined,
|
2022-12-29 21:03:17 +03:00
|
|
|
excludeBots: parseOptionalBooleanToString(excludeBots),
|
|
|
|
excludeMaxVisitsReached: parseOptionalBooleanToString(excludeMaxVisitsReached),
|
|
|
|
excludePastValidUntil: parseOptionalBooleanToString(excludePastValidUntil),
|
2022-03-13 13:14:30 +03:00
|
|
|
};
|
2022-11-26 11:11:46 +03:00
|
|
|
const stringifiedQuery = stringifyQuery(query);
|
|
|
|
const queryString = isEmpty(stringifiedQuery) ? '' : `?${stringifiedQuery}`;
|
2021-11-09 01:23:45 +03:00
|
|
|
|
2022-11-26 11:11:46 +03:00
|
|
|
navigate(`/server/${serverId}/list-short-urls/1${queryString}`);
|
2021-11-09 01:23:45 +03:00
|
|
|
};
|
|
|
|
|
2022-11-26 11:11:46 +03:00
|
|
|
return [filtering, toFirstPageWithExtra];
|
2021-11-09 01:23:45 +03:00
|
|
|
};
|