2020-07-27 22:04:12 +02:00
|
|
|
// @flow
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
import { handleActions } from 'util/redux-utils';
|
2021-03-26 09:33:30 +01:00
|
|
|
import { SEARCH_OPTIONS, SEARCH_PAGE_SIZE } from 'constants/search';
|
|
|
|
import { createNormalizedSearchKey } from 'util/search';
|
2021-07-22 22:04:46 +02:00
|
|
|
import { LIGHTHOUSE_DEFAULT_TYPES } from 'config';
|
|
|
|
const defaultSearchTypes = LIGHTHOUSE_DEFAULT_TYPES && LIGHTHOUSE_DEFAULT_TYPES.split(',');
|
2020-07-27 22:04:12 +02:00
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
const defaultState: SearchState = {
|
|
|
|
// $FlowFixMe
|
2020-07-27 22:04:12 +02:00
|
|
|
options: {
|
2021-03-26 09:33:30 +01:00
|
|
|
[SEARCH_OPTIONS.RESULT_COUNT]: SEARCH_PAGE_SIZE,
|
2020-07-27 22:04:12 +02:00
|
|
|
[SEARCH_OPTIONS.CLAIM_TYPE]: SEARCH_OPTIONS.INCLUDE_FILES_AND_CHANNELS,
|
2021-07-22 22:04:46 +02:00
|
|
|
[SEARCH_OPTIONS.MEDIA_AUDIO]: defaultSearchTypes.includes(SEARCH_OPTIONS.MEDIA_AUDIO),
|
|
|
|
[SEARCH_OPTIONS.MEDIA_VIDEO]: defaultSearchTypes.includes(SEARCH_OPTIONS.MEDIA_VIDEO),
|
|
|
|
[SEARCH_OPTIONS.MEDIA_TEXT]: defaultSearchTypes.includes(SEARCH_OPTIONS.MEDIA_TEXT),
|
|
|
|
[SEARCH_OPTIONS.MEDIA_IMAGE]: defaultSearchTypes.includes(SEARCH_OPTIONS.MEDIA_IMAGE),
|
|
|
|
[SEARCH_OPTIONS.MEDIA_APPLICATION]: defaultSearchTypes.includes(SEARCH_OPTIONS.MEDIA_APPLICATION),
|
2020-07-27 22:04:12 +02:00
|
|
|
},
|
2021-08-17 16:03:25 +02:00
|
|
|
resultsByQuery: {},
|
2021-03-26 09:33:30 +01:00
|
|
|
hasReachedMaxResultsLength: {},
|
2020-12-03 18:29:47 +01:00
|
|
|
searching: false,
|
2021-12-06 20:39:39 +01:00
|
|
|
results: [],
|
2021-12-07 19:17:29 +01:00
|
|
|
mentionQuery: '',
|
2022-03-24 02:07:18 +01:00
|
|
|
personalRecommendations: { gid: '', uris: [], fetched: false },
|
2020-07-27 22:04:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default handleActions(
|
|
|
|
{
|
|
|
|
[ACTIONS.SEARCH_START]: (state: SearchState): SearchState => ({
|
|
|
|
...state,
|
|
|
|
searching: true,
|
|
|
|
}),
|
|
|
|
[ACTIONS.SEARCH_SUCCESS]: (state: SearchState, action: SearchSuccess): SearchState => {
|
2022-06-22 15:43:54 +02:00
|
|
|
const { query, uris, from, size, poweredBy: recsys, uuid } = action.data;
|
2021-03-26 09:33:30 +01:00
|
|
|
const normalizedQuery = createNormalizedSearchKey(query);
|
2021-08-17 16:03:25 +02:00
|
|
|
const urisForQuery = state.resultsByQuery[normalizedQuery] && state.resultsByQuery[normalizedQuery]['uris'];
|
2021-03-26 09:33:30 +01:00
|
|
|
|
|
|
|
let newUris = uris;
|
2021-08-17 16:03:25 +02:00
|
|
|
if (from !== 0 && urisForQuery) {
|
|
|
|
newUris = Array.from(new Set(urisForQuery.concat(uris)));
|
2021-03-26 09:33:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The returned number of urls is less than the page size, so we're on the last page
|
|
|
|
const noMoreResults = size && uris.length < size;
|
2020-07-27 22:04:12 +02:00
|
|
|
|
2022-06-22 15:43:54 +02:00
|
|
|
const results = { uris: newUris, recsys, uuid };
|
2020-07-27 22:04:12 +02:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
searching: false,
|
2021-08-17 16:03:25 +02:00
|
|
|
resultsByQuery: Object.assign({}, state.resultsByQuery, { [normalizedQuery]: results }),
|
2021-03-26 09:33:30 +01:00
|
|
|
hasReachedMaxResultsLength: Object.assign({}, state.hasReachedMaxResultsLength, {
|
|
|
|
[normalizedQuery]: noMoreResults,
|
|
|
|
}),
|
2020-07-27 22:04:12 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
[ACTIONS.SEARCH_FAIL]: (state: SearchState): SearchState => ({
|
|
|
|
...state,
|
|
|
|
searching: false,
|
|
|
|
}),
|
|
|
|
|
|
|
|
[ACTIONS.UPDATE_SEARCH_OPTIONS]: (state: SearchState, action: UpdateSearchOptions): SearchState => {
|
|
|
|
const { options: oldOptions } = state;
|
|
|
|
const newOptions = action.data;
|
|
|
|
const options = { ...oldOptions, ...newOptions };
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
options,
|
|
|
|
};
|
|
|
|
},
|
2021-12-06 20:39:39 +01:00
|
|
|
|
2021-12-07 15:51:55 +01:00
|
|
|
[ACTIONS.SET_MENTION_SEARCH_RESULTS]: (state: SearchState, action: SearchSuccess): SearchState => ({
|
2021-12-06 20:39:39 +01:00
|
|
|
...state,
|
|
|
|
results: action.data.uris,
|
2021-12-07 19:17:29 +01:00
|
|
|
mentionQuery: action.data.query,
|
2021-12-06 20:39:39 +01:00
|
|
|
}),
|
2022-03-15 20:07:31 +01:00
|
|
|
|
|
|
|
[ACTIONS.FYP_FETCH_SUCCESS]: (state: SearchState, action: any): SearchState => {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
personalRecommendations: {
|
|
|
|
gid: action.data.gid,
|
|
|
|
uris: action.data.uris,
|
2022-03-24 02:07:18 +01:00
|
|
|
fetched: true,
|
2022-03-15 20:07:31 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
[ACTIONS.FYP_FETCH_FAILED]: (state: SearchState, action: any): SearchState => ({
|
|
|
|
...state,
|
2022-03-24 02:07:18 +01:00
|
|
|
personalRecommendations: {
|
|
|
|
...defaultState.personalRecommendations,
|
|
|
|
fetched: true,
|
|
|
|
},
|
2022-03-15 20:07:31 +01:00
|
|
|
}),
|
|
|
|
|
|
|
|
[ACTIONS.FYP_HIDE_URI]: (state: SearchState, action: any): SearchState => {
|
|
|
|
const { uri } = action.data;
|
|
|
|
const uris = state.personalRecommendations.uris.slice();
|
|
|
|
const index = uris.findIndex((x) => x === uri);
|
|
|
|
if (index !== -1) {
|
|
|
|
uris.splice(index, 1);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
personalRecommendations: {
|
2022-03-24 02:07:18 +01:00
|
|
|
...state.personalRecommendations,
|
2022-03-15 20:07:31 +01:00
|
|
|
gid: state.personalRecommendations.gid,
|
|
|
|
uris,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
},
|
2020-07-27 22:04:12 +02:00
|
|
|
},
|
|
|
|
defaultState
|
|
|
|
);
|