feat(requests): Request Overrides & Request Editing (#653)
parent
d9919abb89
commit
bdb33722e6
@ -0,0 +1,18 @@
|
||||
import { RadarrProfile, RadarrRootFolder } from '../../api/radarr';
|
||||
|
||||
export interface ServiceCommonServer {
|
||||
id: number;
|
||||
name: string;
|
||||
is4k: boolean;
|
||||
isDefault: boolean;
|
||||
activeProfileId: number;
|
||||
activeDirectory: string;
|
||||
activeAnimeProfileId?: number;
|
||||
activeAnimeDirectory?: string;
|
||||
}
|
||||
|
||||
export interface ServiceCommonServerWithDetails {
|
||||
server: ServiceCommonServer;
|
||||
profiles: RadarrProfile[];
|
||||
rootFolders: Partial<RadarrRootFolder>[];
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
import { Router } from 'express';
|
||||
import RadarrAPI from '../api/radarr';
|
||||
import SonarrAPI from '../api/sonarr';
|
||||
import {
|
||||
ServiceCommonServer,
|
||||
ServiceCommonServerWithDetails,
|
||||
} from '../interfaces/api/serviceInterfaces';
|
||||
import { getSettings } from '../lib/settings';
|
||||
|
||||
const serviceRoutes = Router();
|
||||
|
||||
serviceRoutes.get('/radarr', async (req, res) => {
|
||||
const settings = getSettings();
|
||||
|
||||
const filteredRadarrServers: ServiceCommonServer[] = settings.radarr.map(
|
||||
(radarr) => ({
|
||||
id: radarr.id,
|
||||
name: radarr.name,
|
||||
is4k: radarr.is4k,
|
||||
isDefault: radarr.isDefault,
|
||||
activeDirectory: radarr.activeDirectory,
|
||||
activeProfileId: radarr.activeProfileId,
|
||||
})
|
||||
);
|
||||
|
||||
return res.status(200).json(filteredRadarrServers);
|
||||
});
|
||||
|
||||
serviceRoutes.get<{ radarrId: string }>(
|
||||
'/radarr/:radarrId',
|
||||
async (req, res, next) => {
|
||||
const settings = getSettings();
|
||||
|
||||
const radarrSettings = settings.radarr.find(
|
||||
(radarr) => radarr.id === Number(req.params.radarrId)
|
||||
);
|
||||
|
||||
if (!radarrSettings) {
|
||||
return next({
|
||||
status: 404,
|
||||
message: 'Radarr server with provided ID does not exist.',
|
||||
});
|
||||
}
|
||||
|
||||
const radarr = new RadarrAPI({
|
||||
apiKey: radarrSettings.apiKey,
|
||||
url: `${radarrSettings.useSsl ? 'https' : 'http'}://${
|
||||
radarrSettings.hostname
|
||||
}:${radarrSettings.port}${radarrSettings.baseUrl ?? ''}/api`,
|
||||
});
|
||||
|
||||
const profiles = await radarr.getProfiles();
|
||||
const rootFolders = await radarr.getRootFolders();
|
||||
|
||||
return res.status(200).json({
|
||||
server: {
|
||||
id: radarrSettings.id,
|
||||
name: radarrSettings.name,
|
||||
is4k: radarrSettings.is4k,
|
||||
isDefault: radarrSettings.isDefault,
|
||||
activeDirectory: radarrSettings.activeDirectory,
|
||||
activeProfileId: radarrSettings.activeProfileId,
|
||||
},
|
||||
profiles: profiles.map((profile) => ({
|
||||
id: profile.id,
|
||||
name: profile.name,
|
||||
})),
|
||||
rootFolders: rootFolders.map((folder) => ({
|
||||
id: folder.id,
|
||||
freeSpace: folder.freeSpace,
|
||||
path: folder.path,
|
||||
totalSpace: folder.totalSpace,
|
||||
})),
|
||||
} as ServiceCommonServerWithDetails);
|
||||
}
|
||||
);
|
||||
|
||||
serviceRoutes.get('/sonarr', async (req, res) => {
|
||||
const settings = getSettings();
|
||||
|
||||
const filteredSonarrServers: ServiceCommonServer[] = settings.sonarr.map(
|
||||
(sonarr) => ({
|
||||
id: sonarr.id,
|
||||
name: sonarr.name,
|
||||
is4k: sonarr.is4k,
|
||||
isDefault: sonarr.isDefault,
|
||||
activeDirectory: sonarr.activeDirectory,
|
||||
activeProfileId: sonarr.activeProfileId,
|
||||
activeAnimeProfileId: sonarr.activeAnimeProfileId,
|
||||
activeAnimeDirectory: sonarr.activeAnimeDirectory,
|
||||
})
|
||||
);
|
||||
|
||||
return res.status(200).json(filteredSonarrServers);
|
||||
});
|
||||
|
||||
serviceRoutes.get<{ sonarrId: string }>(
|
||||
'/sonarr/:sonarrId',
|
||||
async (req, res, next) => {
|
||||
const settings = getSettings();
|
||||
|
||||
const sonarrSettings = settings.sonarr.find(
|
||||
(radarr) => radarr.id === Number(req.params.sonarrId)
|
||||
);
|
||||
|
||||
if (!sonarrSettings) {
|
||||
return next({
|
||||
status: 404,
|
||||
message: 'Radarr server with provided ID does not exist.',
|
||||
});
|
||||
}
|
||||
|
||||
const sonarr = new SonarrAPI({
|
||||
apiKey: sonarrSettings.apiKey,
|
||||
url: `${sonarrSettings.useSsl ? 'https' : 'http'}://${
|
||||
sonarrSettings.hostname
|
||||
}:${sonarrSettings.port}${sonarrSettings.baseUrl ?? ''}/api`,
|
||||
});
|
||||
|
||||
const profiles = await sonarr.getProfiles();
|
||||
const rootFolders = await sonarr.getRootFolders();
|
||||
|
||||
return res.status(200).json({
|
||||
server: {
|
||||
id: sonarrSettings.id,
|
||||
name: sonarrSettings.name,
|
||||
is4k: sonarrSettings.is4k,
|
||||
isDefault: sonarrSettings.isDefault,
|
||||
activeDirectory: sonarrSettings.activeDirectory,
|
||||
activeProfileId: sonarrSettings.activeProfileId,
|
||||
activeAnimeProfileId: sonarrSettings.activeAnimeProfileId,
|
||||
activeAnimeDirectory: sonarrSettings.activeAnimeDirectory,
|
||||
},
|
||||
profiles: profiles.map((profile) => ({
|
||||
id: profile.id,
|
||||
name: profile.name,
|
||||
})),
|
||||
rootFolders: rootFolders.map((folder) => ({
|
||||
id: folder.id,
|
||||
freeSpace: folder.freeSpace,
|
||||
path: folder.path,
|
||||
totalSpace: folder.totalSpace,
|
||||
})),
|
||||
} as ServiceCommonServerWithDetails);
|
||||
}
|
||||
);
|
||||
|
||||
export default serviceRoutes;
|
@ -0,0 +1,312 @@
|
||||
/* eslint-disable react-hooks/exhaustive-deps */
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import { SmallLoadingSpinner } from '../../Common/LoadingSpinner';
|
||||
import type {
|
||||
ServiceCommonServer,
|
||||
ServiceCommonServerWithDetails,
|
||||
} from '../../../../server/interfaces/api/serviceInterfaces';
|
||||
import { defineMessages, useIntl } from 'react-intl';
|
||||
|
||||
const formatBytes = (bytes: number, decimals = 2) => {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
|
||||
const k = 1024;
|
||||
const dm = decimals < 0 ? 0 : decimals;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||
};
|
||||
|
||||
const messages = defineMessages({
|
||||
advancedoptions: 'Advanced Options',
|
||||
destinationserver: 'Destination Server',
|
||||
qualityprofile: 'Quality Profile',
|
||||
rootfolder: 'Root Folder',
|
||||
animenote: '* This series is an anime.',
|
||||
default: '(Default)',
|
||||
loadingprofiles: 'Loading profiles…',
|
||||
loadingfolders: 'Loading folders…',
|
||||
});
|
||||
|
||||
export type RequestOverrides = {
|
||||
server?: number;
|
||||
profile?: number;
|
||||
folder?: string;
|
||||
};
|
||||
|
||||
interface AdvancedRequesterProps {
|
||||
type: 'movie' | 'tv';
|
||||
is4k: boolean;
|
||||
isAnime?: boolean;
|
||||
defaultOverrides?: RequestOverrides;
|
||||
onChange: (overrides: RequestOverrides) => void;
|
||||
}
|
||||
|
||||
const AdvancedRequester: React.FC<AdvancedRequesterProps> = ({
|
||||
type,
|
||||
is4k = false,
|
||||
isAnime = false,
|
||||
defaultOverrides,
|
||||
onChange,
|
||||
}) => {
|
||||
const intl = useIntl();
|
||||
const { data, error } = useSWR<ServiceCommonServer[]>(
|
||||
`/api/v1/service/${type === 'movie' ? 'radarr' : 'sonarr'}`,
|
||||
{
|
||||
refreshInterval: 0,
|
||||
refreshWhenHidden: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnMount: true,
|
||||
}
|
||||
);
|
||||
const [selectedServer, setSelectedServer] = useState<number | null>(
|
||||
defaultOverrides?.server !== undefined && defaultOverrides?.server >= 0
|
||||
? defaultOverrides?.server
|
||||
: null
|
||||
);
|
||||
const [selectedProfile, setSelectedProfile] = useState<number>(
|
||||
defaultOverrides?.profile ?? -1
|
||||
);
|
||||
const [selectedFolder, setSelectedFolder] = useState<string>(
|
||||
defaultOverrides?.folder ?? ''
|
||||
);
|
||||
const {
|
||||
data: serverData,
|
||||
isValidating,
|
||||
} = useSWR<ServiceCommonServerWithDetails>(
|
||||
selectedServer !== null
|
||||
? `/api/v1/service/${
|
||||
type === 'movie' ? 'radarr' : 'sonarr'
|
||||
}/${selectedServer}`
|
||||
: null,
|
||||
{
|
||||
refreshInterval: 0,
|
||||
refreshWhenHidden: false,
|
||||
revalidateOnFocus: false,
|
||||
}
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
let defaultServer = data?.find(
|
||||
(server) => server.isDefault && is4k === server.is4k
|
||||
);
|
||||
|
||||
if (!defaultServer && (data ?? []).length > 0) {
|
||||
defaultServer = data?.[0];
|
||||
}
|
||||
|
||||
if (
|
||||
defaultServer &&
|
||||
defaultServer.id !== selectedServer &&
|
||||
(!defaultOverrides || defaultOverrides.server === null)
|
||||
) {
|
||||
setSelectedServer(defaultServer.id);
|
||||
}
|
||||
}, [data]);
|
||||
|
||||
useEffect(() => {
|
||||
if (serverData) {
|
||||
const defaultProfile = serverData.profiles.find(
|
||||
(profile) =>
|
||||
profile.id ===
|
||||
(isAnime
|
||||
? serverData.server.activeAnimeProfileId
|
||||
: serverData.server.activeProfileId)
|
||||
);
|
||||
const defaultFolder = serverData.rootFolders.find(
|
||||
(folder) =>
|
||||
folder.path ===
|
||||
(isAnime
|
||||
? serverData.server.activeAnimeDirectory
|
||||
: serverData.server.activeDirectory)
|
||||
);
|
||||
|
||||
if (
|
||||
defaultProfile &&
|
||||
defaultProfile.id !== selectedProfile &&
|
||||
(!defaultOverrides || defaultOverrides.profile === null)
|
||||
) {
|
||||
setSelectedProfile(defaultProfile.id);
|
||||
}
|
||||
|
||||
if (
|
||||
defaultFolder &&
|
||||
defaultFolder.path !== selectedFolder &&
|
||||
(!defaultOverrides || defaultOverrides.folder === null)
|
||||
) {
|
||||
setSelectedFolder(defaultFolder?.path ?? '');
|
||||
}
|
||||
}
|
||||
}, [serverData]);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
defaultOverrides &&
|
||||
defaultOverrides.server !== null &&
|
||||
defaultOverrides.server !== undefined
|
||||
) {
|
||||
setSelectedServer(defaultOverrides.server);
|
||||
}
|
||||
|
||||
if (
|
||||
defaultOverrides &&
|
||||
defaultOverrides.profile !== null &&
|
||||
defaultOverrides.profile !== undefined
|
||||
) {
|
||||
setSelectedProfile(defaultOverrides.profile);
|
||||
}
|
||||
|
||||
if (
|
||||
defaultOverrides &&
|
||||
defaultOverrides.folder !== null &&
|
||||
defaultOverrides.folder !== undefined
|
||||
) {
|
||||
setSelectedFolder(defaultOverrides.folder);
|
||||
}
|
||||
}, [
|
||||
defaultOverrides?.server,
|
||||
defaultOverrides?.folder,
|
||||
defaultOverrides?.profile,
|
||||
]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedServer !== null) {
|
||||
onChange({
|
||||
folder: selectedFolder !== '' ? selectedFolder : undefined,
|
||||
profile: selectedProfile !== -1 ? selectedProfile : undefined,
|
||||
server: selectedServer ?? undefined,
|
||||
});
|
||||
}
|
||||
}, [selectedFolder, selectedServer, selectedProfile]);
|
||||
|
||||
if (!data && !error) {
|
||||
return (
|
||||
<div className="w-full mb-2">
|
||||
<SmallLoadingSpinner />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!data || selectedServer === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="flex items-center mb-2 font-bold tracking-wider">
|
||||
<svg
|
||||
className="w-4 h-4 mr-1"
|
||||
fill="currentColor"
|
||||
viewBox="0 0 20 20"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path d="M9.707 7.293a1 1 0 00-1.414 1.414l3 3a1 1 0 001.414 0l3-3a1 1 0 00-1.414-1.414L13 8.586V5h3a2 2 0 012 2v5a2 2 0 01-2 2H8a2 2 0 01-2-2V7a2 2 0 012-2h3v3.586L9.707 7.293zM11 3a1 1 0 112 0v2h-2V3z" />
|
||||
<path d="M4 9a2 2 0 00-2 2v5a2 2 0 002 2h8a2 2 0 002-2H4V9z" />
|
||||
</svg>
|
||||
{intl.formatMessage(messages.advancedoptions)}
|
||||
</div>
|
||||
<div className="p-4 bg-gray-600 rounded-md shadow">
|
||||
<div className="flex flex-col items-center justify-between md:flex-row">
|
||||
<div className="flex-grow flex-shrink-0 w-full mb-2 md:w-1/3 md:pr-4 md:mb-0">
|
||||
<label htmlFor="server" className="block text-sm font-medium">
|
||||
{intl.formatMessage(messages.destinationserver)}
|
||||
</label>
|
||||
<select
|
||||
id="server"
|
||||
name="server"
|
||||
onChange={(e) => setSelectedServer(Number(e.target.value))}
|
||||
onBlur={(e) => setSelectedServer(Number(e.target.value))}
|
||||
value={selectedServer}
|
||||
className="block w-full py-2 pl-3 pr-10 mt-1 text-base leading-6 text-white transition duration-150 ease-in-out bg-gray-800 border-gray-700 rounded-md form-select focus:outline-none focus:ring-blue focus:border-blue-300 sm:text-sm sm:leading-5"
|
||||
>
|
||||
{data.map((server) => (
|
||||
<option key={`server-list-${server.id}`} value={server.id}>
|
||||
{server.name}
|
||||
{server.isDefault && server.is4k === is4k
|
||||
? ` ${intl.formatMessage(messages.default)}`
|
||||
: ''}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="flex-grow flex-shrink-0 w-full mb-2 md:w-1/3 md:pr-4 md:mb-0">
|
||||
<label htmlFor="server" className="block text-sm font-medium">
|
||||
{intl.formatMessage(messages.qualityprofile)}
|
||||
</label>
|
||||
<select
|
||||
id="profile"
|
||||
name="profile"
|
||||
value={selectedProfile}
|
||||
onChange={(e) => setSelectedProfile(Number(e.target.value))}
|
||||
onBlur={(e) => setSelectedProfile(Number(e.target.value))}
|
||||
className="block w-full py-2 pl-3 pr-10 mt-1 text-base leading-6 text-white transition duration-150 ease-in-out bg-gray-800 border-gray-700 rounded-md form-select focus:outline-none focus:ring-blue focus:border-blue-300 sm:text-sm sm:leading-5"
|
||||
>
|
||||
{isValidating && (
|
||||
<option value="">
|
||||
{intl.formatMessage(messages.loadingprofiles)}
|
||||
</option>
|
||||
)}
|
||||
{!isValidating &&
|
||||
serverData &&
|
||||
serverData.profiles.map((profile) => (
|
||||
<option key={`profile-list${profile.id}`} value={profile.id}>
|
||||
{profile.name}
|
||||
{isAnime &&
|
||||
serverData.server.activeAnimeProfileId === profile.id
|
||||
? ` ${intl.formatMessage(messages.default)}`
|
||||
: !isAnime &&
|
||||
serverData.server.activeProfileId === profile.id
|
||||
? ` ${intl.formatMessage(messages.default)}`
|
||||
: ''}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="flex-grow flex-shrink-0 w-full mb-2 md:w-1/3 md:mb-0">
|
||||
<label htmlFor="server" className="block text-sm font-medium">
|
||||
{intl.formatMessage(messages.rootfolder)}
|
||||
</label>
|
||||
<select
|
||||
id="folder"
|
||||
name="folder"
|
||||
value={selectedFolder}
|
||||
onChange={(e) => setSelectedFolder(e.target.value)}
|
||||
onBlur={(e) => setSelectedFolder(e.target.value)}
|
||||
className="block w-full py-2 pl-3 pr-10 mt-1 text-base leading-6 text-white transition duration-150 ease-in-out bg-gray-800 border-gray-700 rounded-md form-select focus:outline-none focus:ring-blue focus:border-blue-300 sm:text-sm sm:leading-5"
|
||||
>
|
||||
{isValidating && (
|
||||
<option value="">
|
||||
{intl.formatMessage(messages.loadingfolders)}
|
||||
</option>
|
||||
)}
|
||||
{!isValidating &&
|
||||
serverData &&
|
||||
serverData.rootFolders.map((folder) => (
|
||||
<option key={`folder-list${folder.id}`} value={folder.path}>
|
||||
{folder.path} ({formatBytes(folder.freeSpace ?? 0)})
|
||||
{isAnime &&
|
||||
serverData.server.activeAnimeDirectory === folder.path
|
||||
? ` ${intl.formatMessage(messages.default)}`
|
||||
: !isAnime &&
|
||||
serverData.server.activeDirectory === folder.path
|
||||
? ` ${intl.formatMessage(messages.default)}`
|
||||
: ''}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
{isAnime && (
|
||||
<div className="mt-4 italic">
|
||||
{intl.formatMessage(messages.animenote)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default AdvancedRequester;
|
@ -0,0 +1,46 @@
|
||||
import useSWR from 'swr';
|
||||
import { MediaRequest } from '../../server/entity/MediaRequest';
|
||||
import { ServiceCommonServer } from '../../server/interfaces/api/serviceInterfaces';
|
||||
|
||||
interface OverrideStatus {
|
||||
server: string | null;
|
||||
profile: number | null;
|
||||
rootFolder: string | null;
|
||||
}
|
||||
|
||||
const useRequestOverride = (request: MediaRequest): OverrideStatus => {
|
||||
const { data } = useSWR<ServiceCommonServer[]>(
|
||||
`/api/v1/service/${request.type === 'movie' ? 'radarr' : 'sonarr'}`
|
||||
);
|
||||
|
||||
if (!data) {
|
||||
return {
|
||||
server: null,
|
||||
profile: null,
|
||||
rootFolder: null,
|
||||
};
|
||||
}
|
||||
|
||||
const defaultServer = data.find(
|
||||
(server) => server.is4k === request.is4k && server.isDefault
|
||||
);
|
||||
|
||||
const activeServer = data.find((server) => server.id === request.serverId);
|
||||
|
||||
return {
|
||||
server:
|
||||
activeServer && request.serverId !== defaultServer?.id
|
||||
? activeServer.name
|
||||
: null,
|
||||
profile:
|
||||
defaultServer?.activeProfileId !== request.profileId
|
||||
? request.profileId
|
||||
: null,
|
||||
rootFolder:
|
||||
defaultServer?.activeDirectory !== request.rootFolder
|
||||
? request.rootFolder
|
||||
: null,
|
||||
};
|
||||
};
|
||||
|
||||
export default useRequestOverride;
|
Loading…
Reference in new issue