From c82a4563dfcc0db8dca41c32cc371d8ae8208a92 Mon Sep 17 00:00:00 2001 From: nklhrstv Date: Tue, 4 Feb 2020 16:07:46 +0200 Subject: [PATCH] implement component that show toast for every error in core --- src/App/App.js | 4 +++- src/App/CoreEventsToaster.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/App/CoreEventsToaster.js diff --git a/src/App/App.js b/src/App/App.js index c7d23c300..22a1bb92a 100644 --- a/src/App/App.js +++ b/src/App/App.js @@ -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 ? + { + 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;