feishin/src/renderer/router/app-outlet.tsx

44 lines
1.1 KiB
TypeScript
Raw Normal View History

import { useMemo, useEffect } from 'react';
2022-12-19 15:59:14 -08:00
import isElectron from 'is-electron';
2023-05-31 00:27:16 -07:00
import { Navigate, Outlet } from 'react-router-dom';
2022-12-19 15:59:14 -08:00
import { AppRoute } from '/@/renderer/router/routes';
import { useCurrentServer } from '/@/renderer/store';
import { toast } from '/@/renderer/components';
2022-12-19 15:59:14 -08:00
const ipc = isElectron() ? window.electron.ipc : null;
const utils = isElectron() ? window.electron.utils : null;
2022-12-19 15:59:14 -08:00
export const AppOutlet = () => {
2023-07-01 19:10:05 -07:00
const currentServer = useCurrentServer();
const isActionsRequired = useMemo(() => {
const isServerRequired = !currentServer;
const actions = [isServerRequired];
2023-07-01 19:10:05 -07:00
const isActionRequired = actions.some((c) => c);
return isActionRequired;
}, [currentServer]);
useEffect(() => {
utils?.mainMessageListener((_event, data) => {
toast.show(data);
});
return () => {
ipc?.removeAllListeners('toast-from-main');
};
}, []);
2023-07-01 19:10:05 -07:00
if (isActionsRequired) {
return (
<Navigate
replace
to={AppRoute.ACTION_REQUIRED}
/>
);
}
return <Outlet />;
2022-12-19 15:59:14 -08:00
};