continuewatching route implemented with maximum code reuse

This commit is contained in:
nklhrstv 2020-03-28 13:04:40 +02:00
parent ce0d0082ef
commit 21e50d323a
4 changed files with 44 additions and 7 deletions

View file

@ -21,6 +21,10 @@ const routerViewsConfig = [
...routesRegexp.library, ...routesRegexp.library,
component: routes.Library component: routes.Library
}, },
{
...routesRegexp.continuewatching,
component: routes.Library
},
{ {
...routesRegexp.search, ...routesRegexp.search,
component: routes.Search component: routes.Search

View file

@ -15,6 +15,10 @@ const routesRegexp = {
regexp: /^\/library(?:\/([^/]*))?$/, regexp: /^\/library(?:\/([^/]*))?$/,
urlParamsNames: ['type'] urlParamsNames: ['type']
}, },
continuewatching: {
regexp: /^\/continuewatching(?:\/([^/]*))?$/,
urlParamsNames: ['type']
},
search: { search: {
regexp: /^\/search$/, regexp: /^\/search$/,
urlParamsNames: [] urlParamsNames: []

View file

@ -1,14 +1,15 @@
const React = require('react'); const React = require('react');
const PropTypes = require('prop-types'); const PropTypes = require('prop-types');
const classnames = require('classnames'); const classnames = require('classnames');
const { Button, Multiselect, MainNavBars, LibItem, useProfile } = require('stremio/common'); const NotFound = require('stremio/routes/NotFound');
const { Button, Multiselect, MainNavBars, LibItem, useProfile, routesRegexp } = require('stremio/common');
const useLibrary = require('./useLibrary'); const useLibrary = require('./useLibrary');
const useSelectableInputs = require('./useSelectableInputs'); const useSelectableInputs = require('./useSelectableInputs');
const styles = require('./styles'); const styles = require('./styles');
const Library = ({ urlParams, queryParams }) => { const Library = ({ model, urlParams, queryParams }) => {
const profile = useProfile(); const profile = useProfile();
const library = useLibrary(urlParams, queryParams); const library = useLibrary(model, urlParams, queryParams);
const [typeSelect, sortSelect] = useSelectableInputs(library); const [typeSelect, sortSelect] = useSelectableInputs(library);
return ( return (
<MainNavBars className={styles['library-container']} route={'library'}> <MainNavBars className={styles['library-container']} route={'library'}>
@ -58,10 +59,38 @@ const Library = ({ urlParams, queryParams }) => {
}; };
Library.propTypes = { Library.propTypes = {
urlParams: PropTypes.exact({ model: PropTypes.string,
urlParams: PropTypes.shape({
type: PropTypes.string type: PropTypes.string
}), }),
queryParams: PropTypes.instanceOf(URLSearchParams) queryParams: PropTypes.instanceOf(URLSearchParams)
}; };
module.exports = Library; module.exports = ({ urlParams, queryParams }) => {
const model = React.useMemo(() => {
return typeof urlParams.path === 'string' ?
urlParams.path.match(routesRegexp.library.regexp) ?
'library'
:
urlParams.path.match(routesRegexp.continuewatching.regexp) ?
'continue_watching'
:
null
:
null;
}, [urlParams.path]);
if (typeof model === 'string') {
return (
<Library
key={model}
model={model}
urlParams={urlParams}
queryParams={queryParams}
/>
);
} else {
return (
<NotFound />
);
}
};

View file

@ -26,7 +26,7 @@ const mapLibraryState = (library) => {
return { selected, type_names, lib_items }; return { selected, type_names, lib_items };
}; };
const useLibrary = (urlParams, queryParams) => { const useLibrary = (libraryModel, urlParams, queryParams) => {
const loadLibraryAction = React.useMemo(() => ({ const loadLibraryAction = React.useMemo(() => ({
action: 'Load', action: 'Load',
args: { args: {
@ -38,7 +38,7 @@ const useLibrary = (urlParams, queryParams) => {
} }
}), [urlParams, queryParams]); }), [urlParams, queryParams]);
return useModelState({ return useModelState({
model: 'library', model: libraryModel,
action: loadLibraryAction, action: loadLibraryAction,
map: mapLibraryState, map: mapLibraryState,
init: initLibraryState init: initLibraryState