2024-10-05 14:21:21 -07:00
|
|
|
import { Record as ImmutableRecord } from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
|
|
|
COMPOSE_MENTION,
|
|
|
|
COMPOSE_REPLY,
|
|
|
|
COMPOSE_DIRECT,
|
2022-01-23 09:44:17 -08:00
|
|
|
COMPOSE_QUOTE,
|
2024-08-22 08:40:50 -07:00
|
|
|
type ComposeAction,
|
2022-01-10 14:17:52 -08:00
|
|
|
} from '../actions/compose';
|
2020-03-27 13:59:38 -07:00
|
|
|
import {
|
|
|
|
SEARCH_CLEAR,
|
2021-03-21 14:09:50 -07:00
|
|
|
SEARCH_FETCH_REQUEST,
|
2020-03-27 13:59:38 -07:00
|
|
|
SEARCH_FETCH_SUCCESS,
|
|
|
|
SEARCH_SHOW,
|
2021-08-02 11:51:15 -07:00
|
|
|
SEARCH_FILTER_SET,
|
2021-10-24 10:54:51 -07:00
|
|
|
SEARCH_EXPAND_REQUEST,
|
2021-07-30 08:51:43 -07:00
|
|
|
SEARCH_EXPAND_SUCCESS,
|
2022-08-08 04:40:41 -07:00
|
|
|
SEARCH_ACCOUNT_SET,
|
2022-09-09 14:20:26 -07:00
|
|
|
SEARCH_RESULTS_CLEAR,
|
2024-08-22 08:40:50 -07:00
|
|
|
type SearchAction,
|
2020-03-27 13:59:38 -07:00
|
|
|
} from '../actions/search';
|
|
|
|
|
2024-08-06 14:19:00 -07:00
|
|
|
import type { Search, Tag } from 'pl-api';
|
2022-06-07 09:25:53 -07:00
|
|
|
|
|
|
|
const ResultsRecord = ImmutableRecord({
|
2024-10-05 14:21:21 -07:00
|
|
|
accounts: Array<string>(),
|
|
|
|
statuses: Array<string>(),
|
|
|
|
groups: Array<string>(),
|
|
|
|
hashtags: Array<Tag>(), // it's a list of maps
|
2022-06-07 09:25:53 -07:00
|
|
|
accountsHasMore: false,
|
|
|
|
statusesHasMore: false,
|
2022-12-11 12:37:00 -08:00
|
|
|
groupsHasMore: false,
|
2022-06-07 09:25:53 -07:00
|
|
|
hashtagsHasMore: false,
|
|
|
|
accountsLoaded: false,
|
|
|
|
statusesLoaded: false,
|
2022-12-11 12:37:00 -08:00
|
|
|
groupsLoaded: false,
|
2022-06-07 09:25:53 -07:00
|
|
|
hashtagsLoaded: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const ReducerRecord = ImmutableRecord({
|
2020-03-27 13:59:38 -07:00
|
|
|
submitted: false,
|
2021-08-07 11:42:39 -07:00
|
|
|
submittedValue: '',
|
2020-03-27 13:59:38 -07:00
|
|
|
hidden: false,
|
2022-06-07 09:25:53 -07:00
|
|
|
results: ResultsRecord(),
|
|
|
|
filter: 'accounts' as SearchFilter,
|
2022-08-08 04:40:41 -07:00
|
|
|
accountId: null as string | null,
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
|
|
|
|
2022-06-07 09:25:53 -07:00
|
|
|
type State = ReturnType<typeof ReducerRecord>;
|
2024-08-20 03:42:06 -07:00
|
|
|
type SearchFilter = 'accounts' | 'statuses' | 'groups' | 'hashtags' | 'links';
|
2022-06-07 09:25:53 -07:00
|
|
|
|
2024-10-05 14:21:21 -07:00
|
|
|
const toIds = (items: Array<{ id: string }> = []) => items.map(item => item.id);
|
2021-10-28 11:23:54 -07:00
|
|
|
|
2024-08-22 08:40:50 -07:00
|
|
|
const importResults = (state: State, results: Search, searchTerm: string, searchType: SearchFilter) =>
|
2024-05-12 16:18:04 -07:00
|
|
|
state.withMutations(state => {
|
2024-08-22 08:40:50 -07:00
|
|
|
if (state.submittedValue === searchTerm && state.filter === searchType) {
|
2022-06-07 09:25:53 -07:00
|
|
|
state.set('results', ResultsRecord({
|
2021-12-12 11:34:29 -08:00
|
|
|
accounts: toIds(results.accounts),
|
|
|
|
statuses: toIds(results.statuses),
|
2022-12-11 12:37:00 -08:00
|
|
|
groups: toIds(results.groups),
|
2024-10-05 14:21:21 -07:00
|
|
|
hashtags: results.hashtags, // it's a list of records
|
2024-08-22 08:40:50 -07:00
|
|
|
accountsHasMore: results.accounts.length !== 0,
|
|
|
|
statusesHasMore: results.statuses.length !== 0,
|
|
|
|
groupsHasMore: results.groups?.length !== 0,
|
|
|
|
hashtagsHasMore: results.hashtags.length !== 0,
|
2021-12-12 11:34:29 -08:00
|
|
|
accountsLoaded: true,
|
|
|
|
statusesLoaded: true,
|
2022-12-11 12:37:00 -08:00
|
|
|
groupsLoaded: true,
|
2021-12-12 11:34:29 -08:00
|
|
|
hashtagsLoaded: true,
|
|
|
|
}));
|
2021-10-28 11:23:54 -07:00
|
|
|
|
2021-12-12 11:34:29 -08:00
|
|
|
state.set('submitted', true);
|
|
|
|
}
|
2021-10-28 11:23:54 -07:00
|
|
|
});
|
|
|
|
|
2024-08-22 16:21:30 -07:00
|
|
|
const paginateResults = (state: State, searchType: Exclude<SearchFilter, 'links'>, results: Search, searchTerm: string) =>
|
2024-05-12 16:18:04 -07:00
|
|
|
state.withMutations(state => {
|
2024-08-22 08:40:50 -07:00
|
|
|
if (state.submittedValue === searchTerm) {
|
2021-12-12 11:34:29 -08:00
|
|
|
state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20);
|
|
|
|
state.setIn(['results', `${searchType}Loaded`], true);
|
2022-05-06 09:36:55 -07:00
|
|
|
state.updateIn(['results', searchType], items => {
|
|
|
|
const data = results[searchType];
|
|
|
|
// Hashtags are a list of maps. Others are IDs.
|
|
|
|
if (searchType === 'hashtags') {
|
2024-10-05 14:21:21 -07:00
|
|
|
return (items as Array<Tag>).concat(data as Search['hashtags']);
|
2022-05-06 09:36:55 -07:00
|
|
|
} else {
|
2024-10-05 14:21:21 -07:00
|
|
|
return (items as Array<string>).concat(toIds(data as Search['accounts']));
|
2022-05-06 09:36:55 -07:00
|
|
|
}
|
|
|
|
});
|
2021-12-12 11:34:29 -08:00
|
|
|
}
|
2021-10-28 11:23:54 -07:00
|
|
|
});
|
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const handleSubmitted = (state: State, value: string) =>
|
|
|
|
state.withMutations(state => {
|
2022-06-07 09:25:53 -07:00
|
|
|
state.set('results', ResultsRecord());
|
2021-10-28 11:23:54 -07:00
|
|
|
state.set('submitted', true);
|
|
|
|
state.set('submittedValue', value);
|
|
|
|
});
|
|
|
|
|
2024-08-22 08:40:50 -07:00
|
|
|
const search = (state = ReducerRecord(), action: SearchAction | ComposeAction) => {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case SEARCH_CLEAR:
|
2022-06-07 09:25:53 -07:00
|
|
|
return ReducerRecord();
|
2022-09-09 14:20:26 -07:00
|
|
|
case SEARCH_RESULTS_CLEAR:
|
|
|
|
return state.merge({
|
|
|
|
results: ResultsRecord(),
|
|
|
|
submitted: false,
|
|
|
|
submittedValue: '',
|
|
|
|
});
|
2022-05-11 14:06:35 -07:00
|
|
|
case SEARCH_SHOW:
|
|
|
|
return state.set('hidden', false);
|
|
|
|
case COMPOSE_REPLY:
|
|
|
|
case COMPOSE_MENTION:
|
|
|
|
case COMPOSE_DIRECT:
|
|
|
|
case COMPOSE_QUOTE:
|
|
|
|
return state.set('hidden', true);
|
|
|
|
case SEARCH_FETCH_REQUEST:
|
|
|
|
return handleSubmitted(state, action.value);
|
|
|
|
case SEARCH_FETCH_SUCCESS:
|
2024-08-22 08:40:50 -07:00
|
|
|
return importResults(state, action.results, action.searchTerm, action.searchType);
|
2022-05-11 14:06:35 -07:00
|
|
|
case SEARCH_FILTER_SET:
|
|
|
|
return state.set('filter', action.value);
|
|
|
|
case SEARCH_EXPAND_REQUEST:
|
|
|
|
return state.setIn(['results', `${action.searchType}Loaded`], false);
|
|
|
|
case SEARCH_EXPAND_SUCCESS:
|
2024-08-22 08:40:50 -07:00
|
|
|
return paginateResults(state, action.searchType, action.results, action.searchTerm);
|
2022-08-08 04:40:41 -07:00
|
|
|
case SEARCH_ACCOUNT_SET:
|
2022-09-09 14:20:26 -07:00
|
|
|
if (!action.accountId) return state.merge({
|
|
|
|
results: ResultsRecord(),
|
|
|
|
submitted: false,
|
|
|
|
submittedValue: '',
|
|
|
|
filter: 'accounts',
|
|
|
|
accountId: null,
|
|
|
|
});
|
2022-08-08 04:40:41 -07:00
|
|
|
return ReducerRecord({ accountId: action.accountId, filter: 'statuses' });
|
2022-05-11 14:06:35 -07:00
|
|
|
default:
|
|
|
|
return state;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
2024-05-12 16:18:04 -07:00
|
|
|
};
|
|
|
|
|
2024-05-13 10:00:42 -07:00
|
|
|
export {
|
|
|
|
type SearchFilter,
|
|
|
|
search as default,
|
|
|
|
};
|