implement component that show toast for every error in core

This commit is contained in:
nklhrstv 2020-02-04 16:07:46 +02:00
parent 416328a19d
commit c82a4563df
2 changed files with 32 additions and 1 deletions

View file

@ -3,6 +3,7 @@ const React = require('react');
const { Router } = require('stremio-router');
const { Core, KeyboardNavigation, ServicesProvider, Shell } = require('stremio/services');
const { ToastProvider } = require('stremio/common');
const CoreEventsToaster = require('./CoreEventsToaster');
const routerViewsConfig = require('./routerViewsConfig');
const styles = require('./styles');
@ -23,13 +24,13 @@ const App = () => {
};
const onCoreStateChanged = () => {
if (services.core.active) {
window.core = services.core;
services.core.dispatch({
action: 'Load',
args: {
model: 'Ctx'
}
});
window.core = services.core;
}
setCoreInitialized(services.core.active || services.core.error instanceof Error);
};
@ -52,6 +53,7 @@ const App = () => {
{
shellInitialized && coreInitialized ?
<ToastProvider className={styles['toasts-container']}>
<CoreEventsToaster />
<Router
className={styles['router']}
homePath={'/'}

View file

@ -0,0 +1,29 @@
const React = require('react');
const { useServices } = require('stremio/services');
const { useToast } = require('stremio/common');
const CoreEventsToaster = () => {
const { core } = useServices();
const toast = useToast();
React.useEffect(() => {
const onEvent = ({ event, args }) => {
// UserAuthenticated are handled only in the /intro route
if (event === 'Error' && args.source.event !== 'UserAuthenticated') {
toast.show({
type: 'error',
title: args.source.event,
message: args.error.message,
icon: 'ic_warning',
timeout: 10000
});
}
};
core.on('Event', onEvent);
return () => {
core.off('Event', onEvent);
};
}, []);
return null;
};
module.exports = CoreEventsToaster;