stremio-web/src/routes/Library/useLibrary.js
2020-02-18 17:13:11 +02:00

96 lines
3 KiB
JavaScript

const React = require('react');
const { useServices } = require('stremio/services');
const { useModelState } = require('stremio/common');
const initLibraryState = () => ({
selected: null,
type_names: [],
lib_items: []
});
const mapLibraryState = (library) => {
const selected = library.selected;
const type_names = library.type_names;
const lib_items = library.lib_items.map((lib_item) => ({
id: lib_item._id,
type: lib_item.type,
name: lib_item.name,
poster: lib_item.poster,
posterShape: lib_item.posterShape,
progress: lib_item.state.timeOffset > 0 && lib_item.state.duration > 0 ?
lib_item.state.timeOffset / lib_item.state.duration
:
null,
videoId: lib_item.state.video_id,
href: `#/metadetails/${encodeURIComponent(lib_item.type)}/${encodeURIComponent(lib_item._id)}${lib_item.state.video_id !== null ? `/${encodeURIComponent(lib_item.state.video_id)}` : ''}`
}));
return { selected, type_names, lib_items };
};
const onNewLibraryState = (library) => {
if (library.selected === null && library.type_names.length > 0) {
return {
action: 'Load',
args: {
model: 'LibraryWithFilters',
args: {
type_name: library.type_names[0]
}
}
};
}
};
const useLibrary = (urlParams) => {
const { core } = useServices();
const loadLibraryAction = React.useMemo(() => {
if (typeof urlParams.type === 'string' && typeof urlParams.sort === 'string') {
return {
action: 'Load',
args: {
model: 'LibraryWithFilters',
args: {
type_name: urlParams.type,
sort_prop: urlParams.sort
}
}
};
} else if (typeof urlParams.type === 'string') {
return {
action: 'Load',
args: {
model: 'LibraryWithFilters',
args: {
type_name: urlParams.type
}
}
};
} else {
const library = core.getState('library');
if (library.type_names.length > 0) {
return {
action: 'Load',
args: {
model: 'LibraryWithFilters',
args: {
type_name: library.type_names[0]
}
}
};
} else {
return {
action: 'Unload'
};
}
}
}, [urlParams]);
return useModelState({
model: 'library',
action: loadLibraryAction,
map: mapLibraryState,
init: initLibraryState,
onNewState: onNewLibraryState
});
};
module.exports = useLibrary;