From bf11195d0e67a719fbf62e151f45f16a16ed4a89 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 30 Jan 2024 04:05:36 +0100 Subject: [PATCH] refactor(App): use SearchParamsHandler --- src/App/App.js | 3 +- src/App/SearchParamsHandler.js | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/App/SearchParamsHandler.js diff --git a/src/App/App.js b/src/App/App.js index 79efaf0fd..b5f67a658 100644 --- a/src/App/App.js +++ b/src/App/App.js @@ -9,7 +9,7 @@ const { NotFound } = require('stremio/routes'); const { ToastProvider, TooltipProvider, CONSTANTS, withCoreSuspender } = require('stremio/common'); const ServicesToaster = require('./ServicesToaster'); const DeepLinkHandler = require('./DeepLinkHandler'); -const DefaultSettingsHandler = require('./DefaultSettingsHandler'); +const SearchParamsHandler = require('./SearchParamsHandler'); const ErrorDialog = require('./ErrorDialog'); const withProtectedRoutes = require('./withProtectedRoutes'); const routerViewsConfig = require('./routerViewsConfig'); @@ -165,6 +165,7 @@ const App = () => { + { + const { core } = useServices(); + const profile = useProfile(); + const toast = useToast(); + + const [searchParams, setSearchParams] = React.useState({}); + + const onLocationChange = () => { + const { origin, hash, search } = window.location; + const { searchParams } = new URL(`${origin}${search.length ? search : hash.replace('#', '')}`); + + setSearchParams((previousSearchParams) => { + const currentSearchParams = Object.fromEntries(searchParams.entries()); + return isEqual(previousSearchParams, currentSearchParams) ? previousSearchParams : currentSearchParams; + }); + }; + + React.useEffect(() => { + const { streamingServerUrl } = searchParams; + + if (streamingServerUrl) { + core.transport.dispatch({ + action: 'Ctx', + args: { + action: 'UpdateSettings', + args: { + ...profile.settings, + streamingServerUrl, + }, + }, + }); + + toast.show({ + type: 'success', + title: `Using streaming server at ${streamingServerUrl}`, + timeout: 4000, + }); + } + }, [searchParams]); + + React.useEffect(() => { + onLocationChange(); + window.addEventListener('hashchange', onLocationChange); + return () => window.removeEventListener('hashchange', onLocationChange); + }, []); + + return null; +}; + +module.exports = withCoreSuspender(SearchParamsHandler);