Optimize app outlet
This commit is contained in:
parent
791088deb6
commit
fa0a21a021
1 changed files with 15 additions and 22 deletions
|
@ -1,41 +1,34 @@
|
|||
import { useState, useEffect } from 'react';
|
||||
import { useMemo } from 'react';
|
||||
import isElectron from 'is-electron';
|
||||
import { Navigate, Outlet, useLocation } from 'react-router-dom';
|
||||
import { Navigate, Outlet } from 'react-router-dom';
|
||||
import { AppRoute } from '/@/renderer/router/routes';
|
||||
import { useCurrentServer } from '/@/renderer/store';
|
||||
|
||||
const localSettings = isElectron() ? window.electron.localSettings : null;
|
||||
|
||||
export const AppOutlet = () => {
|
||||
const location = useLocation();
|
||||
const currentServer = useCurrentServer();
|
||||
|
||||
const [isMpvRequired, setIsMpvRequired] = useState(false);
|
||||
const isServerRequired = !currentServer;
|
||||
|
||||
useEffect(() => {
|
||||
const getMpvPath = async () => {
|
||||
if (!isElectron()) return setIsMpvRequired(false);
|
||||
const mpvPath = await localSettings.get('mpv_path');
|
||||
|
||||
if (mpvPath) {
|
||||
return setIsMpvRequired(false);
|
||||
}
|
||||
|
||||
return setIsMpvRequired(true);
|
||||
const isActionsRequired = useMemo(() => {
|
||||
const isMpvRequired = () => {
|
||||
if (!isElectron()) return false;
|
||||
const mpvPath = localSettings.get('mpv_path');
|
||||
if (mpvPath) return false;
|
||||
return true;
|
||||
};
|
||||
|
||||
getMpvPath();
|
||||
}, []);
|
||||
const isServerRequired = !currentServer;
|
||||
|
||||
const actions = [isServerRequired, isMpvRequired];
|
||||
const actionRequired = actions.some((c) => c);
|
||||
const actions = [isServerRequired, isMpvRequired()];
|
||||
const isActionRequired = actions.some((c) => c);
|
||||
|
||||
if (actionRequired) {
|
||||
return isActionRequired;
|
||||
}, [currentServer]);
|
||||
|
||||
if (isActionsRequired) {
|
||||
return (
|
||||
<Navigate
|
||||
replace
|
||||
state={{ from: location }}
|
||||
to={AppRoute.ACTION_REQUIRED}
|
||||
/>
|
||||
);
|
||||
|
|
Reference in a new issue