mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Fix complexity warnings in ConnectionRequired
This commit is contained in:
parent
a6c8c63d2e
commit
6fc90c1740
1 changed files with 101 additions and 98 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React, { FunctionComponent, useEffect, useState } from 'react';
|
import React, { FunctionComponent, useCallback, useEffect, useState } from 'react';
|
||||||
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
|
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
|
||||||
import type { ConnectResponse } from 'jellyfin-apiclient';
|
import type { ConnectResponse } from 'jellyfin-apiclient';
|
||||||
|
|
||||||
|
@ -35,116 +35,119 @@ const ConnectionRequired: FunctionComponent<ConnectionRequiredProps> = ({
|
||||||
|
|
||||||
const [ isLoading, setIsLoading ] = useState(true);
|
const [ isLoading, setIsLoading ] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
const bounce = useCallback(async (connectionResponse: ConnectResponse) => {
|
||||||
const bounce = async (connectionResponse: ConnectResponse) => {
|
switch (connectionResponse.State) {
|
||||||
switch (connectionResponse.State) {
|
case ConnectionState.SignedIn:
|
||||||
case ConnectionState.SignedIn:
|
// Already logged in, bounce to the home page
|
||||||
// Already logged in, bounce to the home page
|
console.debug('[ConnectionRequired] already logged in, redirecting to home');
|
||||||
console.debug('[ConnectionRequired] already logged in, redirecting to home');
|
navigate(BounceRoutes.Home);
|
||||||
navigate(BounceRoutes.Home);
|
return;
|
||||||
return;
|
case ConnectionState.ServerSignIn:
|
||||||
case ConnectionState.ServerSignIn:
|
// Bounce to the login page
|
||||||
// Bounce to the login page
|
if (location.pathname === BounceRoutes.Login) {
|
||||||
if (location.pathname === BounceRoutes.Login) {
|
setIsLoading(false);
|
||||||
setIsLoading(false);
|
} else {
|
||||||
} else {
|
console.debug('[ConnectionRequired] not logged in, redirecting to login page');
|
||||||
console.debug('[ConnectionRequired] not logged in, redirecting to login page');
|
navigate(`${BounceRoutes.Login}?serverid=${connectionResponse.ApiClient.serverId()}`);
|
||||||
navigate(`${BounceRoutes.Login}?serverid=${connectionResponse.ApiClient.serverId()}`);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
case ConnectionState.ServerSelection:
|
|
||||||
// Bounce to select server page
|
|
||||||
console.debug('[ConnectionRequired] redirecting to select server page');
|
|
||||||
navigate(BounceRoutes.SelectServer);
|
|
||||||
return;
|
|
||||||
case ConnectionState.ServerUpdateNeeded:
|
|
||||||
// Show update needed message and bounce to select server page
|
|
||||||
try {
|
|
||||||
await alert({
|
|
||||||
text: globalize.translate('ServerUpdateNeeded', 'https://github.com/jellyfin/jellyfin'),
|
|
||||||
html: globalize.translate('ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
|
||||||
});
|
|
||||||
} catch (ex) {
|
|
||||||
console.warn('[ConnectionRequired] failed to show alert', ex);
|
|
||||||
}
|
|
||||||
console.debug('[ConnectionRequired] server update required, redirecting to select server page');
|
|
||||||
navigate(BounceRoutes.SelectServer);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.warn('[ConnectionRequired] unhandled connection state', connectionResponse.State);
|
|
||||||
};
|
|
||||||
|
|
||||||
const validateConnection = async () => {
|
|
||||||
// Check connection status on initial page load
|
|
||||||
const firstConnection = appRouter.firstConnectionResult;
|
|
||||||
appRouter.firstConnectionResult = null;
|
|
||||||
|
|
||||||
if (firstConnection && firstConnection.State !== ConnectionState.SignedIn) {
|
|
||||||
if (firstConnection.State === ConnectionState.ServerSignIn) {
|
|
||||||
// Verify the wizard is complete
|
|
||||||
try {
|
|
||||||
const infoResponse = await fetch(`${firstConnection.ApiClient.serverAddress()}/System/Info/Public`);
|
|
||||||
if (!infoResponse.ok) {
|
|
||||||
throw new Error('Public system info request failed');
|
|
||||||
}
|
|
||||||
const systemInfo = await infoResponse.json();
|
|
||||||
if (!systemInfo?.StartupWizardCompleted) {
|
|
||||||
// Update the current ApiClient
|
|
||||||
// TODO: Is there a better place to handle this?
|
|
||||||
ServerConnections.setLocalApiClient(firstConnection.ApiClient);
|
|
||||||
// Bounce to the wizard
|
|
||||||
console.info('[ConnectionRequired] startup wizard is not complete, redirecting there');
|
|
||||||
navigate(BounceRoutes.StartWizard);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
|
||||||
console.error('[ConnectionRequired] checking wizard status failed', ex);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
case ConnectionState.ServerSelection:
|
||||||
|
// Bounce to select server page
|
||||||
|
console.debug('[ConnectionRequired] redirecting to select server page');
|
||||||
|
navigate(BounceRoutes.SelectServer);
|
||||||
|
return;
|
||||||
|
case ConnectionState.ServerUpdateNeeded:
|
||||||
|
// Show update needed message and bounce to select server page
|
||||||
|
try {
|
||||||
|
await alert({
|
||||||
|
text: globalize.translate('ServerUpdateNeeded', 'https://github.com/jellyfin/jellyfin'),
|
||||||
|
html: globalize.translate('ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
||||||
|
});
|
||||||
|
} catch (ex) {
|
||||||
|
console.warn('[ConnectionRequired] failed to show alert', ex);
|
||||||
|
}
|
||||||
|
console.debug('[ConnectionRequired] server update required, redirecting to select server page');
|
||||||
|
navigate(BounceRoutes.SelectServer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Bounce to the correct page in the login flow
|
console.warn('[ConnectionRequired] unhandled connection state', connectionResponse.State);
|
||||||
bounce(firstConnection);
|
}, [location.pathname, navigate]);
|
||||||
|
|
||||||
|
const handleIncompleteWizard = useCallback(async (firstConnection: ConnectResponse) => {
|
||||||
|
if (firstConnection.State === ConnectionState.ServerSignIn) {
|
||||||
|
// Verify the wizard is complete
|
||||||
|
try {
|
||||||
|
const infoResponse = await fetch(`${firstConnection.ApiClient.serverAddress()}/System/Info/Public`);
|
||||||
|
if (!infoResponse.ok) {
|
||||||
|
throw new Error('Public system info request failed');
|
||||||
|
}
|
||||||
|
const systemInfo = await infoResponse.json();
|
||||||
|
if (!systemInfo?.StartupWizardCompleted) {
|
||||||
|
// Update the current ApiClient
|
||||||
|
// TODO: Is there a better place to handle this?
|
||||||
|
ServerConnections.setLocalApiClient(firstConnection.ApiClient);
|
||||||
|
// Bounce to the wizard
|
||||||
|
console.info('[ConnectionRequired] startup wizard is not complete, redirecting there');
|
||||||
|
navigate(BounceRoutes.StartWizard);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
console.error('[ConnectionRequired] checking wizard status failed', ex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: appRouter will call appHost.exit() if navigating back when you are already at the default route.
|
// Bounce to the correct page in the login flow
|
||||||
// This case will need to be handled elsewhere before appRouter can be killed.
|
bounce(firstConnection);
|
||||||
|
}, [bounce, navigate]);
|
||||||
|
|
||||||
const client = ServerConnections.currentApiClient();
|
const validateUserAccess = useCallback(async () => {
|
||||||
|
const client = ServerConnections.currentApiClient();
|
||||||
|
|
||||||
// If this is a user route, ensure a user is logged in
|
// If this is a user route, ensure a user is logged in
|
||||||
if ((isAdminRequired || isUserRequired) && !client?.isLoggedIn()) {
|
if ((isAdminRequired || isUserRequired) && !client?.isLoggedIn()) {
|
||||||
try {
|
try {
|
||||||
console.warn('[ConnectionRequired] unauthenticated user attempted to access user route');
|
console.warn('[ConnectionRequired] unauthenticated user attempted to access user route');
|
||||||
|
bounce(await ServerConnections.connect());
|
||||||
|
} catch (ex) {
|
||||||
|
console.warn('[ConnectionRequired] error bouncing from user route', ex);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If this is an admin route, ensure the user has access
|
||||||
|
if (isAdminRequired) {
|
||||||
|
try {
|
||||||
|
const user = await client?.getCurrentUser();
|
||||||
|
if (!user?.Policy?.IsAdministrator) {
|
||||||
|
console.warn('[ConnectionRequired] normal user attempted to access admin route');
|
||||||
bounce(await ServerConnections.connect());
|
bounce(await ServerConnections.connect());
|
||||||
} catch (ex) {
|
|
||||||
console.warn('[ConnectionRequired] error bouncing from user route', ex);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If this is an admin route, ensure the user has access
|
|
||||||
if (isAdminRequired) {
|
|
||||||
try {
|
|
||||||
const user = await client?.getCurrentUser();
|
|
||||||
if (!user?.Policy?.IsAdministrator) {
|
|
||||||
console.warn('[ConnectionRequired] normal user attempted to access admin route');
|
|
||||||
bounce(await ServerConnections.connect());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
|
||||||
console.warn('[ConnectionRequired] error bouncing from admin route', ex);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
console.warn('[ConnectionRequired] error bouncing from admin route', ex);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
};
|
}, [bounce, isAdminRequired, isUserRequired]);
|
||||||
|
|
||||||
validateConnection();
|
useEffect(() => {
|
||||||
}, [ isAdminRequired, isUserRequired, location.pathname, navigate ]);
|
// TODO: appRouter will call appHost.exit() if navigating back when you are already at the default route.
|
||||||
|
// This case will need to be handled elsewhere before appRouter can be killed.
|
||||||
|
|
||||||
|
// Check connection status on initial page load
|
||||||
|
const firstConnection = appRouter.firstConnectionResult;
|
||||||
|
appRouter.firstConnectionResult = null;
|
||||||
|
|
||||||
|
if (firstConnection && firstConnection.State !== ConnectionState.SignedIn) {
|
||||||
|
handleIncompleteWizard(firstConnection);
|
||||||
|
} else {
|
||||||
|
validateUserAccess();
|
||||||
|
}
|
||||||
|
}, [handleIncompleteWizard, validateUserAccess]);
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return <Loading />;
|
return <Loading />;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue