parent
d00e470b55
commit
8af6a1f566
@ -0,0 +1,70 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { NotificationItem, hasNotificationType } from '..';
|
||||||
|
|
||||||
|
interface NotificationTypeProps {
|
||||||
|
option: NotificationItem;
|
||||||
|
currentTypes: number;
|
||||||
|
parent?: NotificationItem;
|
||||||
|
onUpdate: (newTypes: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const NotificationType: React.FC<NotificationTypeProps> = ({
|
||||||
|
option,
|
||||||
|
currentTypes,
|
||||||
|
onUpdate,
|
||||||
|
parent,
|
||||||
|
}) => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div
|
||||||
|
className={`relative flex items-start first:mt-0 mt-4 ${
|
||||||
|
!!parent?.value && hasNotificationType(parent.value, currentTypes)
|
||||||
|
? 'opacity-50'
|
||||||
|
: ''
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<div className="flex items-center h-5">
|
||||||
|
<input
|
||||||
|
id={option.id}
|
||||||
|
name="permissions"
|
||||||
|
type="checkbox"
|
||||||
|
className="w-4 h-4 text-indigo-600 transition duration-150 ease-in-out rounded-md form-checkbox"
|
||||||
|
disabled={
|
||||||
|
!!parent?.value && hasNotificationType(parent.value, currentTypes)
|
||||||
|
}
|
||||||
|
onClick={() => {
|
||||||
|
onUpdate(
|
||||||
|
hasNotificationType(option.value, currentTypes)
|
||||||
|
? currentTypes - option.value
|
||||||
|
: currentTypes + option.value
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
defaultChecked={
|
||||||
|
hasNotificationType(option.value, currentTypes) ||
|
||||||
|
(!!parent?.value &&
|
||||||
|
hasNotificationType(parent.value, currentTypes))
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="ml-3 text-sm leading-5">
|
||||||
|
<label htmlFor={option.id} className="font-medium">
|
||||||
|
{option.name}
|
||||||
|
</label>
|
||||||
|
<p className="text-gray-500">{option.description}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{(option.children ?? []).map((child) => (
|
||||||
|
<div key={`notification-type-child-${child.id}`} className="pl-6 mt-4">
|
||||||
|
<NotificationType
|
||||||
|
option={child}
|
||||||
|
currentTypes={currentTypes}
|
||||||
|
onUpdate={(newTypes) => onUpdate(newTypes)}
|
||||||
|
parent={option}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default NotificationType;
|
@ -0,0 +1,106 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { defineMessages, useIntl } from 'react-intl';
|
||||||
|
import NotificationType from './NotificationType';
|
||||||
|
|
||||||
|
const messages = defineMessages({
|
||||||
|
mediarequested: 'Media Requested',
|
||||||
|
mediarequestedDescription:
|
||||||
|
'Sends a notification when new media is requested. For certain agents, this will only send the notification to admins or users with the "Manage Requests" permission.',
|
||||||
|
mediaapproved: 'Media Approved',
|
||||||
|
mediaapprovedDescription: 'Sends a notification when media is approved.',
|
||||||
|
mediaavailable: 'Media Available',
|
||||||
|
mediaavailableDescription:
|
||||||
|
'Sends a notification when media becomes available.',
|
||||||
|
mediafailed: 'Media Failed',
|
||||||
|
mediafailedDescription:
|
||||||
|
'Sends a notification when media fails to be added to services (Radarr/Sonarr). For certain agents, this will only send the notification to admins or users with the "Manage Requests" permission.',
|
||||||
|
});
|
||||||
|
|
||||||
|
export const hasNotificationType = (
|
||||||
|
types: Notification | Notification[],
|
||||||
|
value: number
|
||||||
|
): boolean => {
|
||||||
|
let total = 0;
|
||||||
|
|
||||||
|
if (types === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(types)) {
|
||||||
|
total = types.reduce((a, v) => a + v, 0);
|
||||||
|
} else {
|
||||||
|
total = types;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !!(value & total);
|
||||||
|
};
|
||||||
|
|
||||||
|
export enum Notification {
|
||||||
|
MEDIA_PENDING = 2,
|
||||||
|
MEDIA_APPROVED = 4,
|
||||||
|
MEDIA_AVAILABLE = 8,
|
||||||
|
MEDIA_FAILED = 16,
|
||||||
|
TEST_NOTIFICATION = 32,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NotificationItem {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
value: Notification;
|
||||||
|
children?: NotificationItem[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NotificationTypeSelectorProps {
|
||||||
|
currentTypes: number;
|
||||||
|
onUpdate: (newTypes: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
const NotificationTypeSelector: React.FC<NotificationTypeSelectorProps> = ({
|
||||||
|
currentTypes,
|
||||||
|
onUpdate,
|
||||||
|
}) => {
|
||||||
|
const intl = useIntl();
|
||||||
|
|
||||||
|
const types: NotificationItem[] = [
|
||||||
|
{
|
||||||
|
id: 'media-requested',
|
||||||
|
name: intl.formatMessage(messages.mediarequested),
|
||||||
|
description: intl.formatMessage(messages.mediarequestedDescription),
|
||||||
|
value: Notification.MEDIA_PENDING,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'media-approved',
|
||||||
|
name: intl.formatMessage(messages.mediaapproved),
|
||||||
|
description: intl.formatMessage(messages.mediaapprovedDescription),
|
||||||
|
value: Notification.MEDIA_APPROVED,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'media-available',
|
||||||
|
name: intl.formatMessage(messages.mediaavailable),
|
||||||
|
description: intl.formatMessage(messages.mediaavailableDescription),
|
||||||
|
value: Notification.MEDIA_AVAILABLE,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'media-failed',
|
||||||
|
name: intl.formatMessage(messages.mediafailed),
|
||||||
|
description: intl.formatMessage(messages.mediafailedDescription),
|
||||||
|
value: Notification.MEDIA_FAILED,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{types.map((type) => (
|
||||||
|
<NotificationType
|
||||||
|
key={`notification-type-${type.id}`}
|
||||||
|
option={type}
|
||||||
|
currentTypes={currentTypes}
|
||||||
|
onUpdate={onUpdate}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default NotificationTypeSelector;
|
Loading…
Reference in new issue