|
@@ -15,12 +15,23 @@ export type ISearchConfigurations = {
|
|
|
includeUserPages?: boolean,
|
|
includeUserPages?: boolean,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type ISearchConfigurationsFixed = {
|
|
|
|
|
+ limit: number,
|
|
|
|
|
+ offset: number,
|
|
|
|
|
+ sort: SORT_AXIS,
|
|
|
|
|
+ order: SORT_ORDER,
|
|
|
|
|
+ includeTrashPages: boolean,
|
|
|
|
|
+ includeUserPages: boolean,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export type ISearchConditions = {
|
|
export type ISearchConditions = {
|
|
|
- q: string,
|
|
|
|
|
- configurations: ISearchConfigurations,
|
|
|
|
|
|
|
+ conditions: ISearchConfigurationsFixed & {
|
|
|
|
|
+ keyword: string,
|
|
|
|
|
+ rawQuery: string,
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-const createSearchQuery = (keyword: string, includeTrashPages = false, includeUserPages = false): string => {
|
|
|
|
|
|
|
+const createSearchQuery = (keyword: string, includeTrashPages: boolean, includeUserPages: boolean): string => {
|
|
|
let query = keyword;
|
|
let query = keyword;
|
|
|
|
|
|
|
|
// pages included in specific path are not retrived when prefix is added
|
|
// pages included in specific path are not retrived when prefix is added
|
|
@@ -39,25 +50,33 @@ export const useSWRxFullTextSearch = (
|
|
|
): SWRResponse<IFormattedSearchResult, Error> & ISearchConditions => {
|
|
): SWRResponse<IFormattedSearchResult, Error> & ISearchConditions => {
|
|
|
|
|
|
|
|
const {
|
|
const {
|
|
|
- includeTrashPages, includeUserPages,
|
|
|
|
|
|
|
+ limit, offset, sort, order, includeTrashPages, includeUserPages,
|
|
|
} = configurations;
|
|
} = configurations;
|
|
|
|
|
|
|
|
- const rawQuery = createSearchQuery(keyword, includeTrashPages, includeUserPages);
|
|
|
|
|
|
|
+ const fixedConfigurations: ISearchConfigurationsFixed = {
|
|
|
|
|
+ limit,
|
|
|
|
|
+ offset: offset ?? 0,
|
|
|
|
|
+ sort: sort ?? SORT_AXIS.RELATION_SCORE,
|
|
|
|
|
+ order: order ?? SORT_ORDER.DESC,
|
|
|
|
|
+ includeTrashPages: includeTrashPages ?? false,
|
|
|
|
|
+ includeUserPages: includeUserPages ?? false,
|
|
|
|
|
+ };
|
|
|
|
|
+ const rawQuery = createSearchQuery(keyword, fixedConfigurations.includeTrashPages, fixedConfigurations.includeUserPages);
|
|
|
|
|
|
|
|
const swrResult = useSWRImmutable(
|
|
const swrResult = useSWRImmutable(
|
|
|
- ['/search', keyword, configurations],
|
|
|
|
|
- (endpoint, keyword, configurations) => {
|
|
|
|
|
|
|
+ ['/search', keyword, fixedConfigurations],
|
|
|
|
|
+ (endpoint, keyword, fixedConfigurations) => {
|
|
|
const {
|
|
const {
|
|
|
limit, offset, sort, order,
|
|
limit, offset, sort, order,
|
|
|
- } = configurations;
|
|
|
|
|
|
|
+ } = fixedConfigurations;
|
|
|
|
|
|
|
|
return apiGet(
|
|
return apiGet(
|
|
|
endpoint, {
|
|
endpoint, {
|
|
|
q: encodeURIComponent(rawQuery),
|
|
q: encodeURIComponent(rawQuery),
|
|
|
limit,
|
|
limit,
|
|
|
- offset: offset ?? 0,
|
|
|
|
|
- sort: sort ?? SORT_AXIS.RELATION_SCORE,
|
|
|
|
|
- order: order ?? SORT_ORDER.DESC,
|
|
|
|
|
|
|
+ offset,
|
|
|
|
|
+ sort,
|
|
|
|
|
+ order,
|
|
|
},
|
|
},
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
).then(result => result as IFormattedSearchResult);
|
|
).then(result => result as IFormattedSearchResult);
|
|
@@ -66,7 +85,10 @@ export const useSWRxFullTextSearch = (
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
|
...swrResult,
|
|
...swrResult,
|
|
|
- q: rawQuery,
|
|
|
|
|
- configurations,
|
|
|
|
|
|
|
+ conditions: {
|
|
|
|
|
+ keyword,
|
|
|
|
|
+ rawQuery,
|
|
|
|
|
+ ...fixedConfigurations,
|
|
|
|
|
+ },
|
|
|
};
|
|
};
|
|
|
};
|
|
};
|