2024-08-05 11:14:39 -04:00
|
|
|
import { Action } from 'history';
|
2023-10-23 23:14:21 +03:30
|
|
|
import { FunctionComponent, useEffect } from 'react';
|
2024-08-05 11:14:39 -04:00
|
|
|
import { useLocation, useNavigationType } from 'react-router-dom';
|
2022-10-22 03:28:15 -04:00
|
|
|
|
2024-08-14 13:31:34 -04:00
|
|
|
import globalize from '../../lib/globalize';
|
2023-04-26 11:30:57 -04:00
|
|
|
import type { RestoreViewFailResponse } from '../../types/viewManager';
|
2022-10-22 03:28:15 -04:00
|
|
|
import viewManager from './viewManager';
|
|
|
|
|
2022-10-24 02:17:35 -04:00
|
|
|
export interface ViewManagerPageProps {
|
2022-10-22 03:28:15 -04:00
|
|
|
controller: string
|
|
|
|
view: string
|
|
|
|
type?: string
|
|
|
|
isFullscreen?: boolean
|
|
|
|
isNowPlayingBarEnabled?: boolean
|
|
|
|
isThemeMediaSupported?: boolean
|
|
|
|
transition?: string
|
|
|
|
}
|
|
|
|
|
2024-08-05 11:14:39 -04:00
|
|
|
interface ViewOptions {
|
|
|
|
url: string
|
|
|
|
type?: string
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
state: any
|
|
|
|
autoFocus: boolean
|
|
|
|
fullscreen?: boolean
|
|
|
|
transition?: string
|
|
|
|
options: {
|
|
|
|
supportsThemeMedia?: boolean
|
|
|
|
enableMediaControl?: boolean
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadView = async (controller: string, view: string, viewOptions: ViewOptions) => {
|
|
|
|
const [ controllerFactory, viewHtml ] = await Promise.all([
|
|
|
|
import(/* webpackChunkName: "[request]" */ `../../controllers/${controller}`),
|
|
|
|
import(/* webpackChunkName: "[request]" */ `../../controllers/${view}`)
|
|
|
|
.then(html => globalize.translateHtml(html))
|
|
|
|
]);
|
|
|
|
|
|
|
|
viewManager.loadView({
|
|
|
|
...viewOptions,
|
|
|
|
controllerFactory,
|
|
|
|
view: viewHtml
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-22 03:28:15 -04:00
|
|
|
/**
|
|
|
|
* Page component that renders legacy views via the ViewManager.
|
2022-10-25 12:39:12 -04:00
|
|
|
* NOTE: Any new pages should use the generic Page component instead.
|
2022-10-22 03:28:15 -04:00
|
|
|
*/
|
|
|
|
const ViewManagerPage: FunctionComponent<ViewManagerPageProps> = ({
|
|
|
|
controller,
|
|
|
|
view,
|
|
|
|
type,
|
|
|
|
isFullscreen = false,
|
|
|
|
isNowPlayingBarEnabled = true,
|
|
|
|
isThemeMediaSupported = false,
|
|
|
|
transition
|
|
|
|
}) => {
|
|
|
|
const location = useLocation();
|
2024-08-05 11:14:39 -04:00
|
|
|
const navigationType = useNavigationType();
|
2022-10-22 03:28:15 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-04 12:27:14 -04:00
|
|
|
const loadPage = () => {
|
2022-10-24 02:17:35 -04:00
|
|
|
const viewOptions = {
|
2022-10-22 03:28:15 -04:00
|
|
|
url: location.pathname + location.search,
|
|
|
|
type,
|
|
|
|
state: location.state,
|
|
|
|
autoFocus: false,
|
|
|
|
fullscreen: isFullscreen,
|
|
|
|
transition,
|
|
|
|
options: {
|
|
|
|
supportsThemeMedia: isThemeMediaSupported,
|
|
|
|
enableMediaControl: isNowPlayingBarEnabled
|
|
|
|
}
|
2022-10-24 02:17:35 -04:00
|
|
|
};
|
|
|
|
|
2024-08-05 11:14:39 -04:00
|
|
|
if (navigationType !== Action.Pop) {
|
|
|
|
console.debug('[ViewManagerPage] loading view [%s]', view);
|
|
|
|
return loadView(controller, view, viewOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.debug('[ViewManagerPage] restoring view [%s]', view);
|
|
|
|
return viewManager.tryRestoreView(viewOptions)
|
2023-04-26 11:30:57 -04:00
|
|
|
.catch(async (result?: RestoreViewFailResponse) => {
|
2023-07-06 13:39:48 -04:00
|
|
|
if (!result?.cancelled) {
|
2024-08-05 11:14:39 -04:00
|
|
|
console.debug('[ViewManagerPage] restore failed; loading view [%s]', view);
|
|
|
|
return loadView(controller, view, viewOptions);
|
2022-10-24 02:17:35 -04:00
|
|
|
}
|
|
|
|
});
|
2022-10-22 03:28:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
loadPage();
|
2023-04-26 16:53:31 -04:00
|
|
|
},
|
2024-08-05 11:14:39 -04:00
|
|
|
// location.state and navigationType are NOT included as dependencies here since dialogs will update state while the current view stays the same
|
2023-04-26 16:53:31 -04:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
[
|
2022-10-22 03:28:15 -04:00
|
|
|
controller,
|
|
|
|
view,
|
|
|
|
type,
|
|
|
|
isFullscreen,
|
|
|
|
isNowPlayingBarEnabled,
|
|
|
|
isThemeMediaSupported,
|
|
|
|
transition,
|
|
|
|
location.pathname,
|
2022-11-02 13:38:43 -04:00
|
|
|
location.search
|
2022-10-22 03:28:15 -04:00
|
|
|
]);
|
|
|
|
|
2023-10-23 23:14:21 +03:30
|
|
|
return null;
|
2022-10-22 03:28:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ViewManagerPage;
|