2024-08-04 07:09:52 -07:00
|
|
|
import { getClient } from '../api';
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
import { fetchRelationships } from './accounts';
|
2023-05-04 06:48:39 -07:00
|
|
|
import { importFetchedAccounts, importFetchedStatuses } from './importer';
|
2022-06-18 11:58:42 -07:00
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
import type { Search } from 'pl-api';
|
2022-06-18 11:58:42 -07:00
|
|
|
import type { SearchFilter } from 'soapbox/reducers/search';
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const SEARCH_CLEAR = 'SEARCH_CLEAR' as const;
|
|
|
|
const SEARCH_SHOW = 'SEARCH_SHOW' as const;
|
|
|
|
const SEARCH_RESULTS_CLEAR = 'SEARCH_RESULTS_CLEAR' as const;
|
2022-06-18 11:58:42 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const SEARCH_FETCH_REQUEST = 'SEARCH_FETCH_REQUEST' as const;
|
|
|
|
const SEARCH_FETCH_SUCCESS = 'SEARCH_FETCH_SUCCESS' as const;
|
|
|
|
const SEARCH_FETCH_FAIL = 'SEARCH_FETCH_FAIL' as const;
|
2022-06-18 11:58:42 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const SEARCH_FILTER_SET = 'SEARCH_FILTER_SET' as const;
|
2022-06-18 11:58:42 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const SEARCH_EXPAND_REQUEST = 'SEARCH_EXPAND_REQUEST' as const;
|
|
|
|
const SEARCH_EXPAND_SUCCESS = 'SEARCH_EXPAND_SUCCESS' as const;
|
|
|
|
const SEARCH_EXPAND_FAIL = 'SEARCH_EXPAND_FAIL' as const;
|
2022-06-18 11:58:42 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const SEARCH_ACCOUNT_SET = 'SEARCH_ACCOUNT_SET' as const;
|
2022-08-08 04:40:41 -07:00
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
const clearSearch = () => ({
|
|
|
|
type: SEARCH_CLEAR,
|
|
|
|
});
|
|
|
|
|
2022-09-09 14:20:26 -07:00
|
|
|
const clearSearchResults = () => ({
|
|
|
|
type: SEARCH_RESULTS_CLEAR,
|
|
|
|
});
|
|
|
|
|
2024-08-22 08:40:50 -07:00
|
|
|
const submitSearch = (value: string, filter?: SearchFilter) =>
|
2022-06-18 11:58:42 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const type = filter || getState().search.filter || 'accounts';
|
2022-08-08 04:40:41 -07:00
|
|
|
const accountId = getState().search.accountId;
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
// An empty search doesn't return any results
|
|
|
|
if (value.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(fetchSearchRequest(value));
|
|
|
|
|
2022-08-08 04:40:41 -07:00
|
|
|
const params: Record<string, any> = {
|
|
|
|
resolve: true,
|
|
|
|
limit: 20,
|
2024-08-04 07:09:52 -07:00
|
|
|
type: type as any,
|
2022-08-08 04:40:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (accountId) params.account_id = accountId;
|
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
return getClient(getState()).search.search(value, params).then(response => {
|
|
|
|
if (response.accounts) {
|
|
|
|
dispatch(importFetchedAccounts(response.accounts));
|
2022-06-18 11:58:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
if (response.statuses) {
|
|
|
|
dispatch(importFetchedStatuses(response.statuses));
|
2022-06-18 11:58:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
dispatch(fetchSearchSuccess(response, value, type));
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(fetchRelationships(response.accounts.map((item) => item.id)));
|
2022-06-18 11:58:42 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch(fetchSearchFail(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchSearchRequest = (value: string) => ({
|
|
|
|
type: SEARCH_FETCH_REQUEST,
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
const fetchSearchSuccess = (results: Search, searchTerm: string, searchType: SearchFilter) => ({
|
2022-06-18 11:58:42 -07:00
|
|
|
type: SEARCH_FETCH_SUCCESS,
|
|
|
|
results,
|
|
|
|
searchTerm,
|
|
|
|
searchType,
|
|
|
|
});
|
|
|
|
|
2023-10-23 15:22:10 -07:00
|
|
|
const fetchSearchFail = (error: unknown) => ({
|
2022-06-18 11:58:42 -07:00
|
|
|
type: SEARCH_FETCH_FAIL,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
|
2024-08-22 08:40:50 -07:00
|
|
|
const setFilter = (value: string, filterType: SearchFilter) =>
|
2022-06-18 11:58:42 -07:00
|
|
|
(dispatch: AppDispatch) => {
|
2024-08-22 08:40:50 -07:00
|
|
|
dispatch(submitSearch(value, filterType));
|
2022-06-18 11:58:42 -07:00
|
|
|
|
2024-08-22 08:40:50 -07:00
|
|
|
return dispatch({
|
2022-06-18 11:58:42 -07:00
|
|
|
type: SEARCH_FILTER_SET,
|
|
|
|
path: ['search', 'filter'],
|
|
|
|
value: filterType,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const expandSearch = (type: SearchFilter) => (dispatch: AppDispatch, getState: () => RootState) => {
|
2024-08-20 03:42:06 -07:00
|
|
|
if (type === 'links') return;
|
2024-08-22 08:40:50 -07:00
|
|
|
const value = getState().search.submittedValue;
|
2024-08-11 01:48:58 -07:00
|
|
|
const offset = getState().search.results[type].size;
|
2023-05-31 01:45:33 -07:00
|
|
|
const accountId = getState().search.accountId;
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
dispatch(expandSearchRequest(type));
|
|
|
|
|
2024-08-06 14:19:00 -07:00
|
|
|
const params: Record<string, any> = {
|
2024-08-04 07:09:52 -07:00
|
|
|
type,
|
|
|
|
offset,
|
|
|
|
};
|
|
|
|
if (accountId) params.account_id = accountId;
|
2023-05-31 01:45:33 -07:00
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
return getClient(getState()).search.search(value, params).then(response => {
|
|
|
|
if (response.accounts) {
|
|
|
|
dispatch(importFetchedAccounts(response.accounts));
|
2022-06-18 11:58:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
if (response.statuses) {
|
|
|
|
dispatch(importFetchedStatuses(response.statuses));
|
2022-06-18 11:58:42 -07:00
|
|
|
}
|
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
dispatch(expandSearchSuccess(response, value, type));
|
2024-08-11 01:48:58 -07:00
|
|
|
dispatch(fetchRelationships(response.accounts.map((item) => item.id)));
|
2022-06-18 11:58:42 -07:00
|
|
|
}).catch(error => {
|
|
|
|
dispatch(expandSearchFail(error));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const expandSearchRequest = (searchType: SearchFilter) => ({
|
|
|
|
type: SEARCH_EXPAND_REQUEST,
|
|
|
|
searchType,
|
|
|
|
});
|
|
|
|
|
2024-08-22 16:21:30 -07:00
|
|
|
const expandSearchSuccess = (results: Search, searchTerm: string, searchType: Exclude<SearchFilter, 'links'>) => ({
|
2022-06-18 11:58:42 -07:00
|
|
|
type: SEARCH_EXPAND_SUCCESS,
|
|
|
|
results,
|
|
|
|
searchTerm,
|
|
|
|
searchType,
|
|
|
|
});
|
|
|
|
|
2023-10-23 15:22:10 -07:00
|
|
|
const expandSearchFail = (error: unknown) => ({
|
2022-06-18 11:58:42 -07:00
|
|
|
type: SEARCH_EXPAND_FAIL,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
|
|
|
|
const showSearch = () => ({
|
|
|
|
type: SEARCH_SHOW,
|
|
|
|
});
|
|
|
|
|
2022-08-08 08:36:11 -07:00
|
|
|
const setSearchAccount = (accountId: string | null) => ({
|
2022-08-08 04:40:41 -07:00
|
|
|
type: SEARCH_ACCOUNT_SET,
|
|
|
|
accountId,
|
|
|
|
});
|
|
|
|
|
2024-08-22 08:40:50 -07:00
|
|
|
type SearchAction =
|
|
|
|
| ReturnType<typeof clearSearch>
|
|
|
|
| ReturnType<typeof clearSearchResults>
|
|
|
|
| ReturnType<typeof fetchSearchRequest>
|
|
|
|
| ReturnType<typeof fetchSearchSuccess>
|
|
|
|
| ReturnType<typeof fetchSearchFail>
|
|
|
|
| ReturnType<typeof expandSearchRequest>
|
|
|
|
| ReturnType<typeof expandSearchSuccess>
|
|
|
|
| ReturnType<typeof expandSearchFail>
|
|
|
|
| {
|
|
|
|
type: typeof SEARCH_FILTER_SET;
|
|
|
|
path: (['search', 'filter']);
|
|
|
|
value: SearchFilter;
|
|
|
|
}
|
|
|
|
| ReturnType<typeof showSearch>
|
|
|
|
| ReturnType<typeof setSearchAccount>
|
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
export {
|
|
|
|
SEARCH_CLEAR,
|
|
|
|
SEARCH_SHOW,
|
2022-09-09 14:20:26 -07:00
|
|
|
SEARCH_RESULTS_CLEAR,
|
2022-06-18 11:58:42 -07:00
|
|
|
SEARCH_FETCH_REQUEST,
|
|
|
|
SEARCH_FETCH_SUCCESS,
|
|
|
|
SEARCH_FETCH_FAIL,
|
|
|
|
SEARCH_FILTER_SET,
|
|
|
|
SEARCH_EXPAND_REQUEST,
|
|
|
|
SEARCH_EXPAND_SUCCESS,
|
|
|
|
SEARCH_EXPAND_FAIL,
|
2022-08-08 04:40:41 -07:00
|
|
|
SEARCH_ACCOUNT_SET,
|
2022-06-18 11:58:42 -07:00
|
|
|
clearSearch,
|
2022-09-09 14:20:26 -07:00
|
|
|
clearSearchResults,
|
2022-06-18 11:58:42 -07:00
|
|
|
submitSearch,
|
|
|
|
fetchSearchRequest,
|
|
|
|
fetchSearchSuccess,
|
|
|
|
fetchSearchFail,
|
|
|
|
setFilter,
|
|
|
|
expandSearch,
|
|
|
|
expandSearchRequest,
|
|
|
|
expandSearchSuccess,
|
|
|
|
expandSearchFail,
|
|
|
|
showSearch,
|
2022-08-08 04:40:41 -07:00
|
|
|
setSearchAccount,
|
2024-08-22 08:40:50 -07:00
|
|
|
type SearchAction,
|
2022-06-18 11:58:42 -07:00
|
|
|
};
|