addons adapted to stremio-core changes

This commit is contained in:
svetlagasheva 2019-10-23 16:09:04 +03:00
parent 82c7e09688
commit 836a2e1ad7
3 changed files with 59 additions and 54 deletions

View file

@ -25,7 +25,7 @@ const routesRegexp = {
}, },
addons: { addons: {
regexp: /^\/addons(?:\/([^\/]*?))?(?:\/([^\/]*?))?\/?$/i, regexp: /^\/addons(?:\/([^\/]*?))?(?:\/([^\/]*?))?\/?$/i,
urlParamsNames: ['category', 'type'] urlParamsNames: ['type', 'category']
}, },
settings: { settings: {
regexp: /^\/settings\/?$/i, regexp: /^\/settings\/?$/i,

View file

@ -13,7 +13,7 @@ const Addons = ({ urlParams, queryParams }) => {
const queryOnChange = React.useCallback((event) => { const queryOnChange = React.useCallback((event) => {
setQuery(event.currentTarget.value); setQuery(event.currentTarget.value);
}, []); }, []);
const [addons, dropdowns] = useAddons(urlParams.category, urlParams.type); const [addons, dropdowns] = useAddons(urlParams, queryParams);
const [selectedAddon, clearSelectedAddon] = useSelectedAddon(queryParams.get('addon')); const [selectedAddon, clearSelectedAddon] = useSelectedAddon(queryParams.get('addon'));
const addonPromptModalBackgroundOnClick = React.useCallback((event) => { const addonPromptModalBackgroundOnClick = React.useCallback((event) => {
if (!event.nativeEvent.clearSelectedAddonPrevented) { if (!event.nativeEvent.clearSelectedAddonPrevented) {

View file

@ -1,70 +1,75 @@
const React = require('react'); const React = require('react');
const { useServices } = require('stremio/services'); const { useServices } = require('stremio/services');
const CATEGORIES = ['official', 'community', 'my']; const DEFAULT_TYPE = 'movie';
const DEFAULT_CATEGORY = 'community'; const DEFAULT_CATEGORY = 'thirdparty';
const DEFAULT_TYPE = 'all';
const useAddons = (category, type) => { const useAddons = (urlParams, queryParams) => {
const { core } = useServices(); const { core } = useServices();
const [addons, setAddons] = React.useState([[], []]); const [addons, setAddons] = React.useState([[], []]);
React.useEffect(() => { React.useEffect(() => {
category = CATEGORIES.includes(category) ? category : DEFAULT_CATEGORY; const type = typeof urlParams.type === 'string' && urlParams.type.length > 0 ? urlParams.type : DEFAULT_TYPE;
type = typeof type === 'string' && type.length > 0 ? type : DEFAULT_TYPE; const category = typeof urlParams.category === 'string' && urlParams.category.length > 0 ? urlParams.category : DEFAULT_CATEGORY;
const onNewState = () => { const onNewState = () => {
const state = core.getState(); const state = core.getState();
const onSelect = (event) => { const selectInputs = [
const { value } = event.reactEvent.currentTarget.dataset; {
const name = event.dataset.name;
if (name === 'category') {
const nextCategory = CATEGORIES.includes(value) ? value : '';
window.location.replace(`#/addons/${nextCategory}/${DEFAULT_TYPE}`);
} else if (name === 'type') {
const nextType = typeof value === 'string' ? value : '';
window.location.replace(`#/addons/${category}/${nextType}`);
}
};
const categoryDropdown = () => {
const selected = CATEGORIES.includes(category) ? [category] : [];
const options = CATEGORIES
.map((category) => ({ label: category, value: category }));
return {
'data-name': 'category',
selected,
options,
onSelect
};
};
const typeDropdown = () => {
const selected = typeof type === 'string' && type.length > 0 ? [type] : [];
const options = [...new Set(
['all'].concat(
...state.ctx.content.addons.map(addon => addon.manifest.types),
selected
)
)]
.map((type) => ({ label: type, value: type }));
return {
'data-name': 'type', 'data-name': 'type',
selected, selected: state.addons.types
options, .filter(({ is_selected }) => is_selected)
onSelect .map(({ load }) => JSON.stringify(load)),
}; options: state.addons.types
}; .map(({ type_name, load }) => ({
setAddons([ value: JSON.stringify(load),
state.ctx.content.addons.filter((addon) => { label: type_name
return (categoryDropdown().selected.join('') === 'official' ? addon.flags.official : !addon.flags.official) && })),
(typeDropdown().selected.join('') === 'all' || addon.manifest.types.includes(typeDropdown().selected.join(''))); onSelect: (event) => {
}), const load = JSON.parse(event.reactEvent.currentTarget.dataset.value);
[categoryDropdown(), typeDropdown()] window.location = `#/addons/${encodeURIComponent(load.path.type_name)}/${encodeURIComponent(load.path.id)}`;
]); }
},
{
'data-name': 'category',
selected: state.addons.catalogs
.filter(({ is_selected }) => is_selected)
.map(({ load }) => JSON.stringify(load)),
options: state.addons.catalogs
.filter(({ load: { path: { type_name } } }) => {
return type_name === type;
})
.map(({ name, load }) => ({
value: JSON.stringify(load),
label: name
})),
onSelect: (event) => {
const load = JSON.parse(event.reactEvent.currentTarget.dataset.value);
window.location = `#/addons/${encodeURIComponent(load.path.type_name)}/${encodeURIComponent(load.path.id)}`
}
}
];
const addonsItems = state.addons.content.type === 'Ready' ? state.addons.content.content : [];
setAddons([addonsItems, selectInputs]);
}; };
core.on('NewModel', onNewState); core.on('NewModel', onNewState);
onNewState(); core.dispatch({
action: 'Load',
args: {
load: 'CatalogFiltered',
args: {
base: 'https://v3-cinemeta.strem.io/manifest.json',
path: {
resource: 'addon_catalog',
type_name: type,
id: category,
extra: [] // TODO
}
}
}
});
return () => { return () => {
core.off('NewModel', onNewState); core.off('NewModel', onNewState);
}; };
}, [category, type]); }, [urlParams, queryParams]);
return addons; return addons;
}; };