mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 14:52:13 +00:00
default type removed; check for type improved; get selected type from state
This commit is contained in:
parent
f0078427fa
commit
a6343c8395
1 changed files with 13 additions and 10 deletions
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Reference in a new issue