lbry-desktop/ui/redux/actions/search.js

117 lines
3 KiB
JavaScript
Raw Normal View History

2020-07-27 22:04:12 +02:00
// @flow
import * as ACTIONS from 'constants/action_types';
2021-01-28 20:03:37 +01:00
import { buildURI, doResolveUris, batchActions, isURIValid } from 'lbry-redux';
2020-12-03 18:29:47 +01:00
import { makeSelectSearchUris, makeSelectQueryWithOptions, selectSearchValue } from 'redux/selectors/search';
2020-07-27 22:04:12 +02:00
import handleFetchResponse from 'util/handle-fetch';
type Dispatch = (action: any) => any;
type GetState = () => { search: SearchState };
type SearchOptions = {
size?: number,
from?: number,
related_to?: string,
nsfw?: boolean,
isBackgroundSearch?: boolean,
};
2020-12-03 18:29:47 +01:00
let lighthouse = {
CONNECTION_STRING: 'https://lighthouse.lbry.com/search',
search: (queryString: string) => fetch(`${lighthouse.CONNECTION_STRING}?${queryString}`).then(handleFetchResponse),
2020-07-27 22:04:12 +02:00
};
2020-12-03 18:29:47 +01:00
export const setSearchApi = (endpoint: string) => {
lighthouse.CONNECTION_STRING = endpoint.replace(/\/*$/, '/'); // exactly one slash at the end;
2020-07-27 22:04:12 +02:00
};
export const doSearch = (rawQuery: string, searchOptions: SearchOptions) => (
dispatch: Dispatch,
getState: GetState
) => {
const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
if (!query) {
dispatch({
type: ACTIONS.SEARCH_FAIL,
});
return;
}
const state = getState();
let queryWithOptions = makeSelectQueryWithOptions(query, searchOptions)(state);
// If we have already searched for something, we don't need to do anything
const urisForQuery = makeSelectSearchUris(queryWithOptions)(state);
if (urisForQuery && !!urisForQuery.length) {
return;
}
dispatch({
type: ACTIONS.SEARCH_START,
});
2020-12-03 18:29:47 +01:00
lighthouse
.search(queryWithOptions)
2020-07-27 22:04:12 +02:00
.then((data: Array<{ name: string, claimId: string }>) => {
const uris = [];
const actions = [];
data.forEach(result => {
if (result) {
const { name, claimId } = result;
const urlObj: LbryUrlObj = {};
if (name.startsWith('@')) {
urlObj.channelName = name;
urlObj.channelClaimId = claimId;
} else {
urlObj.streamName = name;
urlObj.streamClaimId = claimId;
}
const url = buildURI(urlObj);
2021-01-28 20:03:37 +01:00
if (isURIValid(url)) {
uris.push(url);
}
2020-07-27 22:04:12 +02:00
}
});
2020-08-12 19:02:19 +02:00
actions.push(doResolveUris(uris));
2020-07-27 22:04:12 +02:00
actions.push({
type: ACTIONS.SEARCH_SUCCESS,
data: {
query: queryWithOptions,
uris,
},
});
dispatch(batchActions(...actions));
})
.catch(e => {
dispatch({
type: ACTIONS.SEARCH_FAIL,
});
});
};
export const doUpdateSearchOptions = (newOptions: SearchOptions, additionalOptions: SearchOptions) => (
dispatch: Dispatch,
getState: GetState
) => {
const state = getState();
const searchValue = selectSearchValue(state);
dispatch({
type: ACTIONS.UPDATE_SEARCH_OPTIONS,
data: newOptions,
});
if (searchValue) {
// After updating, perform a search with the new options
dispatch(doSearch(searchValue, additionalOptions));
}
};
2020-12-03 18:29:47 +01:00
export { lighthouse };