2024-11-08 17:32:05 +02:00
|
|
|
import * as React from 'react';
|
2017-11-06 20:35:04 +02:00
|
|
|
const { connect } = require('react-redux');
|
2023-01-19 19:19:06 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
2024-11-08 17:32:05 +02:00
|
|
|
import { AppState, AppStateRoute } from '../app.reducer';
|
|
|
|
import bridge from '../services/bridge';
|
|
|
|
import { useContext, useEffect, useRef } from 'react';
|
|
|
|
import { WindowIdContext } from './NewWindowOrIFrame';
|
2017-11-06 20:35:04 +02:00
|
|
|
|
2023-01-19 19:19:06 +02:00
|
|
|
interface Props {
|
2024-11-08 17:32:05 +02:00
|
|
|
route: AppStateRoute;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2024-11-08 17:32:05 +02:00
|
|
|
screens: Record<string, any>;
|
|
|
|
|
|
|
|
style: React.CSSProperties;
|
|
|
|
className?: string;
|
2023-01-19 19:19:06 +02:00
|
|
|
}
|
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
const NavigatorComponent: React.FC<Props> = props => {
|
|
|
|
const windowId = useContext(WindowIdContext);
|
|
|
|
|
|
|
|
const route = props.route;
|
|
|
|
const screenInfo = props.screens[route?.routeName];
|
|
|
|
|
|
|
|
const screensRef = useRef(props.screens);
|
|
|
|
screensRef.current = props.screens;
|
|
|
|
|
|
|
|
const prevRoute = useRef<AppStateRoute|null>(null);
|
|
|
|
useEffect(() => {
|
|
|
|
const routeName = route?.routeName;
|
|
|
|
if (route) {
|
2021-08-12 17:54:10 +02:00
|
|
|
const devMarker = Setting.value('env') === 'dev' ? ` (DEV - ${Setting.value('profileDir')})` : '';
|
2021-05-04 10:34:18 +02:00
|
|
|
const windowTitle = [`Joplin${devMarker}`];
|
|
|
|
if (screenInfo.title) {
|
|
|
|
windowTitle.push(screenInfo.title());
|
|
|
|
}
|
2024-11-08 17:32:05 +02:00
|
|
|
bridge().windowById(windowId)?.setTitle(windowTitle.join(' - '));
|
2017-11-12 18:47:33 +02:00
|
|
|
}
|
2021-05-04 10:34:18 +02:00
|
|
|
|
2024-11-08 17:32:05 +02:00
|
|
|
// When a navigation happens in an unfocused window, show the window to the user.
|
|
|
|
// This might happen if, for example, a secondary window triggers a navigation in
|
|
|
|
// the main window.
|
|
|
|
if (routeName && routeName !== prevRoute.current?.routeName) {
|
|
|
|
bridge().switchToWindow(windowId);
|
2019-03-03 02:31:41 +02:00
|
|
|
}
|
2024-11-08 17:32:05 +02:00
|
|
|
|
|
|
|
prevRoute.current = route;
|
|
|
|
}, [route, screenInfo, windowId]);
|
|
|
|
|
|
|
|
if (!route) throw new Error('Route must not be null');
|
|
|
|
|
|
|
|
const screenProps = route.props ? route.props : {};
|
|
|
|
const Screen = screenInfo.screen;
|
|
|
|
|
|
|
|
const screenStyle = {
|
|
|
|
width: props.style.width,
|
|
|
|
height: props.style.height,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div style={props.style} className={props.className}>
|
|
|
|
<Screen style={screenStyle} {...screenProps} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2017-11-06 20:35:04 +02:00
|
|
|
|
2023-01-19 19:19:06 +02:00
|
|
|
const Navigator = connect((state: AppState) => {
|
2019-07-29 14:13:23 +02:00
|
|
|
return {
|
|
|
|
route: state.route,
|
|
|
|
};
|
|
|
|
})(NavigatorComponent);
|
2017-11-06 20:35:04 +02:00
|
|
|
|
2023-01-19 19:19:06 +02:00
|
|
|
export default Navigator;
|