mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
Merge pull request #4083 from thornbill/connection-state-refactor
Refactor ConnectionState to a shared enum
This commit is contained in:
commit
feb35ede21
6 changed files with 39 additions and 25 deletions
9
src/apiclient.d.ts
vendored
9
src/apiclient.d.ts
vendored
|
@ -68,6 +68,7 @@ declare module 'jellyfin-apiclient' {
|
||||||
UtcTimeResponse,
|
UtcTimeResponse,
|
||||||
VirtualFolderInfo
|
VirtualFolderInfo
|
||||||
} from '@jellyfin/sdk/lib/generated-client';
|
} from '@jellyfin/sdk/lib/generated-client';
|
||||||
|
import { ConnectionState } from './utils/jellyfin-apiclient/ConnectionState';
|
||||||
|
|
||||||
class ApiClient {
|
class ApiClient {
|
||||||
constructor(serverAddress: string, appName: string, appVersion: string, deviceName: string, deviceId: string);
|
constructor(serverAddress: string, appName: string, appVersion: string, deviceName: string, deviceId: string);
|
||||||
|
@ -310,12 +311,18 @@ declare module 'jellyfin-apiclient' {
|
||||||
setItem(name: string, value: string): void;
|
setItem(name: string, value: string): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ConnectResponse {
|
||||||
|
ApiClient: ApiClient
|
||||||
|
Servers: any[]
|
||||||
|
State: ConnectionState
|
||||||
|
}
|
||||||
|
|
||||||
class ConnectionManager {
|
class ConnectionManager {
|
||||||
constructor(credentialProvider: Credentials, appName: string, appVersion: string, deviceName: string, deviceId: string, capabilities: ClientCapabilities);
|
constructor(credentialProvider: Credentials, appName: string, appVersion: string, deviceName: string, deviceId: string, capabilities: ClientCapabilities);
|
||||||
|
|
||||||
addApiClient(apiClient: ApiClient): void;
|
addApiClient(apiClient: ApiClient): void;
|
||||||
clearData(): void;
|
clearData(): void;
|
||||||
connect(options?: any): Promise<any>;
|
connect(options?: any): Promise<ConnectResponse>;
|
||||||
connectToAddress(address: string, options?: any): Promise<any>;
|
connectToAddress(address: string, options?: any): Promise<any>;
|
||||||
connectToServer(server: any, options?: any): Promise<any>;
|
connectToServer(server: any, options?: any): Promise<any>;
|
||||||
connectToServers(servers: any[], options?: any): Promise<any>;
|
connectToServers(servers: any[], options?: any): Promise<any>;
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
import React, { FunctionComponent, useEffect, useState } from 'react';
|
import React, { FunctionComponent, useEffect, useState } from 'react';
|
||||||
import { Outlet, useNavigate } from 'react-router-dom';
|
import { Outlet, useNavigate } from 'react-router-dom';
|
||||||
|
import type { ConnectResponse } from 'jellyfin-apiclient';
|
||||||
|
|
||||||
import alert from './alert';
|
import alert from './alert';
|
||||||
import { appRouter } from './appRouter';
|
import { appRouter } from './appRouter';
|
||||||
import Loading from './loading/LoadingComponent';
|
import Loading from './loading/LoadingComponent';
|
||||||
import ServerConnections from './ServerConnections';
|
import ServerConnections from './ServerConnections';
|
||||||
import globalize from '../scripts/globalize';
|
import globalize from '../scripts/globalize';
|
||||||
|
import { ConnectionState } from '../utils/jellyfin-apiclient/ConnectionState';
|
||||||
|
|
||||||
enum BounceRoutes {
|
enum BounceRoutes {
|
||||||
Home = '/home.html',
|
Home = '/home.html',
|
||||||
|
@ -14,14 +16,6 @@ enum BounceRoutes {
|
||||||
StartWizard = '/wizardstart.html'
|
StartWizard = '/wizardstart.html'
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This should probably be in the SDK
|
|
||||||
enum ConnectionState {
|
|
||||||
SignedIn = 'SignedIn',
|
|
||||||
ServerSignIn = 'ServerSignIn',
|
|
||||||
ServerSelection = 'ServerSelection',
|
|
||||||
ServerUpdateNeeded = 'ServerUpdateNeeded'
|
|
||||||
}
|
|
||||||
|
|
||||||
type ConnectionRequiredProps = {
|
type ConnectionRequiredProps = {
|
||||||
isAdminRequired?: boolean,
|
isAdminRequired?: boolean,
|
||||||
isUserRequired?: boolean
|
isUserRequired?: boolean
|
||||||
|
@ -42,7 +36,7 @@ const ConnectionRequired: FunctionComponent<ConnectionRequiredProps> = ({
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const bounce = async (connectionResponse: any) => {
|
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
|
||||||
|
|
|
@ -9,6 +9,7 @@ import loading from './loading/loading';
|
||||||
import viewManager from './viewManager/viewManager';
|
import viewManager from './viewManager/viewManager';
|
||||||
import ServerConnections from './ServerConnections';
|
import ServerConnections from './ServerConnections';
|
||||||
import alert from './alert';
|
import alert from './alert';
|
||||||
|
import { ConnectionState } from '../utils/jellyfin-apiclient/ConnectionState.ts';
|
||||||
|
|
||||||
export const history = createHashHistory();
|
export const history = createHashHistory();
|
||||||
|
|
||||||
|
@ -201,17 +202,17 @@ class AppRouter {
|
||||||
|
|
||||||
#handleConnectionResult(result) {
|
#handleConnectionResult(result) {
|
||||||
switch (result.State) {
|
switch (result.State) {
|
||||||
case 'SignedIn':
|
case ConnectionState.SignedIn:
|
||||||
loading.hide();
|
loading.hide();
|
||||||
this.goHome();
|
this.goHome();
|
||||||
break;
|
break;
|
||||||
case 'ServerSignIn':
|
case ConnectionState.ServerSignIn:
|
||||||
this.showLocalLogin(result.ApiClient.serverId());
|
this.showLocalLogin(result.ApiClient.serverId());
|
||||||
break;
|
break;
|
||||||
case 'ServerSelection':
|
case ConnectionState.ServerSelection:
|
||||||
this.showSelectServer();
|
this.showSelectServer();
|
||||||
break;
|
break;
|
||||||
case 'ServerUpdateNeeded':
|
case ConnectionState.ServerUpdateNeeded:
|
||||||
alert({
|
alert({
|
||||||
text: globalize.translate('ServerUpdateNeeded', 'https://github.com/jellyfin/jellyfin'),
|
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>')
|
html: globalize.translate('ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
||||||
|
@ -365,7 +366,7 @@ class AppRouter {
|
||||||
|
|
||||||
this.firstConnectionResult = null;
|
this.firstConnectionResult = null;
|
||||||
if (firstResult) {
|
if (firstResult) {
|
||||||
if (firstResult.State === 'ServerSignIn') {
|
if (firstResult.State === ConnectionState.ServerSignIn) {
|
||||||
const url = firstResult.ApiClient.serverAddress() + '/System/Info/Public';
|
const url = firstResult.ApiClient.serverAddress() + '/System/Info/Public';
|
||||||
fetch(url).then(response => {
|
fetch(url).then(response => {
|
||||||
if (!response.ok) return Promise.reject('fetch failed');
|
if (!response.ok) return Promise.reject('fetch failed');
|
||||||
|
@ -382,7 +383,7 @@ class AppRouter {
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else if (firstResult.State !== 'SignedIn') {
|
} else if (firstResult.State !== ConnectionState.SignedIn) {
|
||||||
this.#handleConnectionResult(firstResult);
|
this.#handleConnectionResult(firstResult);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,30 +4,31 @@ import globalize from '../../../scripts/globalize';
|
||||||
import '../../../elements/emby-button/emby-button';
|
import '../../../elements/emby-button/emby-button';
|
||||||
import Dashboard from '../../../utils/dashboard';
|
import Dashboard from '../../../utils/dashboard';
|
||||||
import ServerConnections from '../../../components/ServerConnections';
|
import ServerConnections from '../../../components/ServerConnections';
|
||||||
|
import { ConnectionState } from '../../../utils/jellyfin-apiclient/ConnectionState.ts';
|
||||||
|
|
||||||
/* eslint-disable indent */
|
/* eslint-disable indent */
|
||||||
|
|
||||||
function handleConnectionResult(page, result) {
|
function handleConnectionResult(page, result) {
|
||||||
loading.hide();
|
loading.hide();
|
||||||
switch (result.State) {
|
switch (result.State) {
|
||||||
case 'SignedIn': {
|
case ConnectionState.SignedIn: {
|
||||||
const apiClient = result.ApiClient;
|
const apiClient = result.ApiClient;
|
||||||
Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient);
|
Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient);
|
||||||
Dashboard.navigate('home.html');
|
Dashboard.navigate('home.html');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'ServerSignIn':
|
case ConnectionState.ServerSignIn:
|
||||||
Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id, false, 'none');
|
Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id, false, 'none');
|
||||||
break;
|
break;
|
||||||
case 'ServerSelection':
|
case ConnectionState.ServerSelection:
|
||||||
Dashboard.navigate('selectserver.html', false, 'none');
|
Dashboard.navigate('selectserver.html', false, 'none');
|
||||||
break;
|
break;
|
||||||
case 'ServerUpdateNeeded':
|
case ConnectionState.ServerUpdateNeeded:
|
||||||
Dashboard.alert({
|
Dashboard.alert({
|
||||||
message: globalize.translate('ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
message: globalize.translate('ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case 'Unavailable':
|
case ConnectionState.Unavailable:
|
||||||
Dashboard.alert({
|
Dashboard.alert({
|
||||||
message: globalize.translate('MessageUnableToConnectToServer'),
|
message: globalize.translate('MessageUnableToConnectToServer'),
|
||||||
title: globalize.translate('HeaderConnectionFailure')
|
title: globalize.translate('HeaderConnectionFailure')
|
||||||
|
@ -44,7 +45,7 @@ import ServerConnections from '../../../components/ServerConnections';
|
||||||
handleConnectionResult(page, result);
|
handleConnectionResult(page, result);
|
||||||
}, function() {
|
}, function() {
|
||||||
handleConnectionResult(page, {
|
handleConnectionResult(page, {
|
||||||
State: 'Unavailable'
|
State: ConnectionState.Unavailable
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import Dashboard from '../../../utils/dashboard';
|
||||||
import ServerConnections from '../../../components/ServerConnections';
|
import ServerConnections from '../../../components/ServerConnections';
|
||||||
import alert from '../../../components/alert';
|
import alert from '../../../components/alert';
|
||||||
import cardBuilder from '../../../components/cardbuilder/cardBuilder';
|
import cardBuilder from '../../../components/cardbuilder/cardBuilder';
|
||||||
|
import { ConnectionState } from '../../../utils/jellyfin-apiclient/ConnectionState.ts';
|
||||||
|
|
||||||
/* eslint-disable indent */
|
/* eslint-disable indent */
|
||||||
|
|
||||||
|
@ -113,17 +114,17 @@ import cardBuilder from '../../../components/cardbuilder/cardBuilder';
|
||||||
const apiClient = result.ApiClient;
|
const apiClient = result.ApiClient;
|
||||||
|
|
||||||
switch (result.State) {
|
switch (result.State) {
|
||||||
case 'SignedIn':
|
case ConnectionState.SignedIn:
|
||||||
Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient);
|
Dashboard.onServerChanged(apiClient.getCurrentUserId(), apiClient.accessToken(), apiClient);
|
||||||
Dashboard.navigate('home.html');
|
Dashboard.navigate('home.html');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'ServerSignIn':
|
case ConnectionState.ServerSignIn:
|
||||||
Dashboard.onServerChanged(null, null, apiClient);
|
Dashboard.onServerChanged(null, null, apiClient);
|
||||||
Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id);
|
Dashboard.navigate('login.html?serverid=' + result.Servers[0].Id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'ServerUpdateNeeded':
|
case ConnectionState.ServerUpdateNeeded:
|
||||||
alertTextWithOptions({
|
alertTextWithOptions({
|
||||||
text: globalize.translate('core#ServerUpdateNeeded', 'https://github.com/jellyfin/jellyfin'),
|
text: globalize.translate('core#ServerUpdateNeeded', 'https://github.com/jellyfin/jellyfin'),
|
||||||
html: globalize.translate('core#ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
html: globalize.translate('core#ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
||||||
|
|
10
src/utils/jellyfin-apiclient/ConnectionState.ts
Normal file
10
src/utils/jellyfin-apiclient/ConnectionState.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
/**
|
||||||
|
* Server state values for a connected server used by jellyfin-apiclient.
|
||||||
|
*/
|
||||||
|
export enum ConnectionState {
|
||||||
|
SignedIn = 'SignedIn',
|
||||||
|
ServerSignIn = 'ServerSignIn',
|
||||||
|
ServerSelection = 'ServerSelection',
|
||||||
|
ServerUpdateNeeded = 'ServerUpdateNeeded',
|
||||||
|
Unavailable = 'Unavailable'
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue