mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 14:52:13 +00:00
search state mapped in the initial state
This commit is contained in:
parent
2ee8f3f1ea
commit
e3811c91e2
2 changed files with 19 additions and 8 deletions
|
|
@ -6,18 +6,15 @@ const styles = require('./styles');
|
|||
|
||||
const Search = ({ queryParams }) => {
|
||||
const search = useSearch(queryParams);
|
||||
const discoverUrls = React.useCallback((request) => {
|
||||
window.location.replace(`#/discover/${encodeURIComponent(request.base)}/${encodeURIComponent(request.path.id)}/${encodeURIComponent(request.path.type_name)}?search=${search.selected[0][1]}`);
|
||||
}, [search.selected]);
|
||||
return (
|
||||
<div className={styles['search-container']}>
|
||||
<MainNavBar className={styles['nav-bar']} />
|
||||
<div className={styles['search-content']}>
|
||||
{
|
||||
search.selected && search.selected[0][1] && search.selected[0][1].length > 0 ?
|
||||
search.selected ?
|
||||
search.items_groups && search.items_groups.length > 0 ?
|
||||
search.items_groups.some(group => group.content.type !== 'Err') ?
|
||||
search.items_groups.map(({ request, content }, index) => {
|
||||
search.items_groups.map(({ href, request, content }, index) => {
|
||||
switch (content.type) {
|
||||
case 'Ready':
|
||||
return (
|
||||
|
|
@ -26,7 +23,7 @@ const Search = ({ queryParams }) => {
|
|||
className={styles['search-row']}
|
||||
title={`${request.path.id} - ${request.path.type_name}`}
|
||||
items={content.content}
|
||||
onSeeAllButtonClicked={() => discoverUrls(request)}
|
||||
catalogHref={href}
|
||||
/>
|
||||
);
|
||||
case 'Err':
|
||||
|
|
|
|||
|
|
@ -1,13 +1,27 @@
|
|||
const React = require('react');
|
||||
const { useServices } = require('stremio/services');
|
||||
|
||||
const mapSearchState = (state) => {
|
||||
const selected = state.search.selected && state.search.selected[0] && state.search.selected[0][1] && state.search.selected[0][1].length > 0 ? state.search.selected[0][1] : '';
|
||||
const items_groups = state.search.items_groups.map((group) => {
|
||||
group.href = `#/discover/${encodeURIComponent(group.request.base)}/${encodeURIComponent(group.request.path.id)}/${encodeURIComponent(group.request.path.type_name)}?search=${state.search.selected[0][1]}`;
|
||||
return group;
|
||||
});
|
||||
return { selected, items_groups };
|
||||
};
|
||||
|
||||
const useSearch = (queryParams) => {
|
||||
const { core } = useServices();
|
||||
const [search, setSearch] = React.useState([]);
|
||||
const [search, setSearch] = React.useState(() => {
|
||||
const state = core.getState();
|
||||
const search = mapSearchState(state);
|
||||
return search;
|
||||
});
|
||||
React.useEffect(() => {
|
||||
const onNewState = () => {
|
||||
const state = core.getState();
|
||||
setSearch(state.search);
|
||||
const search = mapSearchState(state);
|
||||
setSearch(search);
|
||||
};
|
||||
core.on('NewModel', onNewState);
|
||||
if (queryParams.has('q')) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue