default type removed; check for type improved; get selected type from state

This commit is contained in:
svetlagasheva 2019-11-15 15:46:32 +02:00
parent f0078427fa
commit a6343c8395

View file

@ -2,31 +2,34 @@ const React = require('react');
const UrlUtils = require('url');
const { useServices } = require('stremio/services');
const DEFAULT_TYPE = 'movie';
const useLibrary = (urlParams) => {
const { core } = useServices();
const [library, setLibrary] = React.useState([[], null]);
React.useEffect(() => {
const type = typeof urlParams.type === 'string' && urlParams.type.length > 0 ? urlParams.type : DEFAULT_TYPE;
const state = core.getState();
const type = typeof urlParams.type === 'string' && urlParams.type.length > 0 ?
urlParams.type
:
state.library.types.length > 0 ?
state.library.types[0]
:
'';
const onNewState = () => {
const state = core.getState();
const selectInput = {
selected: [type],
selected: [state.library.selected],
options: state.library.types
.map((type) => ({
label: type,
label: type === '' ? '"Empty"' : type,
value: type
})),
onSelect: (event) => {
const { search } = UrlUtils.parse(window.location.hash.slice(1));
const queryParams = new URLSearchParams(search || { sort: 'recent' });
window.location.replace(`#/library/${event.value}?${queryParams}`);
window.location.replace(`#/library/${event.value}${search !== null ? search : ''}`);
}
};
const items = state.library.items.filter(item => !item.removed);
setLibrary([items, selectInput]);
}
setLibrary([state.library.items, selectInput]);
};
core.on('NewModel', onNewState);
core.dispatch({
action: 'Load',