mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-04-20 23:12:13 +00:00
useLibrary implemented with useModelState
This commit is contained in:
parent
46e7e0c21a
commit
dfebba9e1c
1 changed files with 55 additions and 45 deletions
|
|
@ -1,46 +1,50 @@
|
||||||
const React = require('react');
|
const React = require('react');
|
||||||
const { useServices } = require('stremio/services');
|
const { useServices } = require('stremio/services');
|
||||||
|
const { useModelState } = require('stremio/common');
|
||||||
|
|
||||||
const mapLibraryState = (state) => {
|
const initLibraryState = () => ({
|
||||||
const library_state = state.library.library_state;
|
library_state: {
|
||||||
const selected = state.library.selected;
|
type: 'NotLoaded'
|
||||||
const lib_items = state.library.lib_items.map((lib_item) => {
|
},
|
||||||
|
selected: {
|
||||||
|
type_name: null
|
||||||
|
},
|
||||||
|
type_names: [],
|
||||||
|
lib_items: []
|
||||||
|
});
|
||||||
|
|
||||||
|
const mapLibraryState = (library) => {
|
||||||
|
const library_state = library.library_state;
|
||||||
|
const selected = library.selected;
|
||||||
|
const type_names = library.type_names;
|
||||||
|
const lib_items = library.lib_items.map((lib_item) => {
|
||||||
|
// TODO what else
|
||||||
lib_item._ctime = new Date(lib_item._ctime);
|
lib_item._ctime = new Date(lib_item._ctime);
|
||||||
|
lib_item.href = `#/metadetails/${encodeURIComponent(lib_item.type)}/${encodeURIComponent(lib_item._id)}${lib_item.state.video_id !== null ? `/${encodeURIComponent(lib_item.state.video_id)}` : ''}`;
|
||||||
return lib_item;
|
return lib_item;
|
||||||
});
|
});
|
||||||
const type_names = state.library.type_names;
|
return { library_state, selected, type_names, lib_items };
|
||||||
return { library_state, selected, lib_items, type_names };
|
};
|
||||||
|
|
||||||
|
const onNewLibraryState = (library) => {
|
||||||
|
if (library.selected.type_name === null && library.type_names.length > 0) {
|
||||||
|
return {
|
||||||
|
action: 'Load',
|
||||||
|
args: {
|
||||||
|
load: 'LibraryFiltered',
|
||||||
|
args: {
|
||||||
|
type_name: library.type_names[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const useLibrary = (urlParams) => {
|
const useLibrary = (urlParams) => {
|
||||||
const { core } = useServices();
|
const { core } = useServices();
|
||||||
const [library, setLibrary] = React.useState(() => {
|
const loadLibraryAction = React.useMemo(() => {
|
||||||
const state = core.getState();
|
|
||||||
const library = mapLibraryState(state);
|
|
||||||
return library;
|
|
||||||
});
|
|
||||||
React.useEffect(() => {
|
|
||||||
const onNewState = () => {
|
|
||||||
const state = core.getState();
|
|
||||||
if (state.library.selected.type_name === null && state.library.type_names.length > 0) {
|
|
||||||
core.dispatch({
|
|
||||||
action: 'Load',
|
|
||||||
args: {
|
|
||||||
load: 'LibraryFiltered',
|
|
||||||
args: {
|
|
||||||
type_name: state.library.type_names[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const library = mapLibraryState(state);
|
|
||||||
setLibrary(library);
|
|
||||||
};
|
|
||||||
core.on('NewModel', onNewState);
|
|
||||||
const state = core.getState();
|
|
||||||
if (typeof urlParams.type === 'string') {
|
if (typeof urlParams.type === 'string') {
|
||||||
core.dispatch({
|
return {
|
||||||
action: 'Load',
|
action: 'Load',
|
||||||
args: {
|
args: {
|
||||||
load: 'LibraryFiltered',
|
load: 'LibraryFiltered',
|
||||||
|
|
@ -48,23 +52,29 @@ const useLibrary = (urlParams) => {
|
||||||
type_name: urlParams.type
|
type_name: urlParams.type
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
} else if (state.library.type_names.length > 0) {
|
} else {
|
||||||
core.dispatch({
|
const library = core.getState('library');
|
||||||
action: 'Load',
|
if (library.type_names.length > 0) {
|
||||||
args: {
|
return {
|
||||||
load: 'LibraryFiltered',
|
action: 'Load',
|
||||||
args: {
|
args: {
|
||||||
type_name: state.library.type_names[0]
|
load: 'LibraryFiltered',
|
||||||
|
args: {
|
||||||
|
type_name: library.type_names[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
return () => {
|
|
||||||
core.off('NewModel', onNewState);
|
|
||||||
};
|
|
||||||
}, [urlParams]);
|
}, [urlParams]);
|
||||||
return library;
|
return useModelState({
|
||||||
|
model: 'library',
|
||||||
|
action: loadLibraryAction,
|
||||||
|
map: mapLibraryState,
|
||||||
|
init: initLibraryState,
|
||||||
|
onNewState: onNewLibraryState
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = useLibrary;
|
module.exports = useLibrary;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue