2022-06-07 09:25:53 -07:00
|
|
|
import { OrderedSet as ImmutableOrderedSet, Record as ImmutableRecord, fromJS } from 'immutable';
|
|
|
|
|
2022-06-08 14:43:27 -07:00
|
|
|
import { normalizeTag } from 'soapbox/normalizers';
|
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,
|
2022-01-10 14:17:52 -08:00
|
|
|
} from '../actions/compose';
|
2020-03-27 13:59:38 -07:00
|
|
|
import {
|
|
|
|
SEARCH_CHANGE,
|
|
|
|
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,
|
2020-03-27 13:59:38 -07:00
|
|
|
} from '../actions/search';
|
|
|
|
|
2022-06-07 09:25:53 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
2022-06-08 14:43:27 -07:00
|
|
|
import type { APIEntity, Tag } from 'soapbox/types/entities';
|
2022-06-07 09:25:53 -07:00
|
|
|
|
|
|
|
const ResultsRecord = ImmutableRecord({
|
|
|
|
accounts: ImmutableOrderedSet<string>(),
|
|
|
|
statuses: ImmutableOrderedSet<string>(),
|
2022-12-11 12:37:00 -08:00
|
|
|
groups: ImmutableOrderedSet<string>(),
|
2022-06-08 14:43:27 -07:00
|
|
|
hashtags: ImmutableOrderedSet<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
|
|
|
value: '',
|
|
|
|
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,
|
2023-06-15 06:05:55 -07:00
|
|
|
next: 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>;
|
|
|
|
type APIEntities = Array<APIEntity>;
|
2022-12-11 12:37:00 -08:00
|
|
|
export type SearchFilter = 'accounts' | 'statuses' | 'groups' | 'hashtags';
|
2022-06-07 09:25:53 -07:00
|
|
|
|
2023-01-27 14:04:42 -08:00
|
|
|
const toIds = (items: APIEntities = []) => {
|
2021-10-28 11:23:54 -07:00
|
|
|
return ImmutableOrderedSet(items.map(item => item.id));
|
|
|
|
};
|
|
|
|
|
2023-06-15 06:05:55 -07:00
|
|
|
const importResults = (state: State, results: APIEntity, searchTerm: string, searchType: SearchFilter, next: string | null) => {
|
2021-10-28 11:23:54 -07:00
|
|
|
return state.withMutations(state => {
|
2022-06-07 09:25:53 -07:00
|
|
|
if (state.value === searchTerm && state.filter === searchType) {
|
|
|
|
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),
|
2022-06-08 14:43:27 -07:00
|
|
|
hashtags: ImmutableOrderedSet(results.hashtags.map(normalizeTag)), // it's a list of records
|
2021-12-12 11:34:29 -08:00
|
|
|
accountsHasMore: results.accounts.length >= 20,
|
|
|
|
statusesHasMore: results.statuses.length >= 20,
|
2023-01-27 14:04:42 -08:00
|
|
|
groupsHasMore: results.groups?.length >= 20,
|
2021-12-12 11:34:29 -08:00
|
|
|
hashtagsHasMore: results.hashtags.length >= 20,
|
|
|
|
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);
|
2023-06-15 06:05:55 -07:00
|
|
|
state.set('next', next);
|
2021-12-12 11:34:29 -08:00
|
|
|
}
|
2021-10-28 11:23:54 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-06-15 06:05:55 -07:00
|
|
|
const paginateResults = (state: State, searchType: SearchFilter, results: APIEntity, searchTerm: string, next: string | null) => {
|
2021-10-28 11:23:54 -07:00
|
|
|
return state.withMutations(state => {
|
2022-06-07 09:25:53 -07:00
|
|
|
if (state.value === searchTerm) {
|
2021-12-12 11:34:29 -08:00
|
|
|
state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20);
|
|
|
|
state.setIn(['results', `${searchType}Loaded`], true);
|
2023-06-15 06:05:55 -07:00
|
|
|
state.set('next', next);
|
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') {
|
2022-09-09 07:43:33 -07:00
|
|
|
return (items as ImmutableOrderedSet<string>).concat((fromJS(data) as Record<string, any>).map(normalizeTag));
|
2022-05-06 09:36:55 -07:00
|
|
|
} else {
|
2022-06-07 09:25:53 -07:00
|
|
|
return (items as ImmutableOrderedSet<string>).concat(toIds(data));
|
2022-05-06 09:36:55 -07:00
|
|
|
}
|
|
|
|
});
|
2021-12-12 11:34:29 -08:00
|
|
|
}
|
2021-10-28 11:23:54 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-07 09:25:53 -07:00
|
|
|
const handleSubmitted = (state: State, value: string) => {
|
2021-10-28 11:23:54 -07:00
|
|
|
return 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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-07 09:25:53 -07:00
|
|
|
export default function search(state = ReducerRecord(), action: AnyAction) {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case SEARCH_CHANGE:
|
|
|
|
return state.set('value', action.value);
|
|
|
|
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({
|
|
|
|
value: '',
|
|
|
|
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:
|
2023-06-15 06:05:55 -07:00
|
|
|
return importResults(state, action.results, action.searchTerm, action.searchType, action.next);
|
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:
|
2023-06-15 06:05:55 -07:00
|
|
|
return paginateResults(state, action.searchType, action.results, action.searchTerm, action.next);
|
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
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|