Files
requestarr/src/hooks/useRequestOverride.ts
Ryan Cohen f5864b49de refactor: update a few dev deps and convert to using type imports where possible (#2886)
* build: bump deps and add some new eslint rules

* refactor: run eslint --fix on code to convert to type imports where possible
2022-08-03 12:57:51 +09:00

63 lines
1.7 KiB
TypeScript

import useSWR from 'swr';
import type { MediaRequest } from '../../server/entity/MediaRequest';
import type {
ServiceCommonServer,
ServiceCommonServerWithDetails,
} from '../../server/interfaces/api/serviceInterfaces';
interface OverrideStatus {
server?: string;
profile?: string;
rootFolder?: string;
languageProfile?: string;
}
const useRequestOverride = (request: MediaRequest): OverrideStatus => {
const { data: allServers } = useSWR<ServiceCommonServer[]>(
`/api/v1/service/${request.type === 'movie' ? 'radarr' : 'sonarr'}`
);
const { data } = useSWR<ServiceCommonServerWithDetails>(
`/api/v1/service/${request.type === 'movie' ? 'radarr' : 'sonarr'}/${
request.serverId
}`
);
if (!data || !allServers) {
return {};
}
const defaultServer = allServers.find(
(server) => server.is4k === request.is4k && server.isDefault
);
const activeServer = allServers.find(
(server) => server.id === request.serverId
);
return {
server:
activeServer && request.serverId !== defaultServer?.id
? activeServer.name
: undefined,
profile:
defaultServer?.activeProfileId !== request.profileId
? data.profiles.find((profile) => profile.id === request.profileId)
?.name
: undefined,
rootFolder:
defaultServer?.activeDirectory !== request.rootFolder
? request.rootFolder
: undefined,
languageProfile:
request.type === 'tv' &&
defaultServer?.activeLanguageProfileId !== request.languageProfileId
? data.languageProfiles?.find(
(profile) => profile.id === request.languageProfileId
)?.name
: undefined,
};
};
export default useRequestOverride;