import { Dialog, Transition } from "@headlessui/react";
import { Fragment } from "react";
import type { FieldProps } from "formik";
import { Field, Form, Formik, FormikErrors, FormikValues } from "formik";
import { XMarkIcon } from "@heroicons/react/24/solid";
import Select, { components, ControlProps, InputProps, MenuProps, OptionProps } from "react-select";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { toast } from "react-hot-toast";
import { PasswordFieldWide, SwitchGroupWide, TextFieldWide } from "@components/inputs";
import DEBUG from "@components/debug";
import { EventOptions, NotificationTypeOptions, SelectOption } from "@domain/constants";
import { APIClient } from "@api/APIClient";
import Toast from "@components/notifications/Toast";
import { SlideOver } from "@components/panels";
import { componentMapType } from "./DownloadClientForms";
import { notificationKeys } from "@screens/settings/Notifications";
const Input = (props: InputProps) => {
return (
);
};
const Control = (props: ControlProps) => {
return (
);
};
const Menu = (props: MenuProps) => {
return (
);
};
const Option = (props: OptionProps) => {
return (
);
};
function FormFieldsDiscord() {
return (
);
}
function FormFieldsNotifiarr() {
return (
Settings
Enable the autobrr integration and optionally create a new API Key.
);
}
function FormFieldsTelegram() {
return (
);
}
const componentMap: componentMapType = {
DISCORD: ,
NOTIFIARR: ,
TELEGRAM:
};
interface NotificationAddFormValues {
name: string;
enabled: boolean;
}
interface AddProps {
isOpen: boolean;
toggle: () => void;
}
export function NotificationAddForm({ isOpen, toggle }: AddProps) {
const queryClient = useQueryClient();
const createMutation = useMutation({
mutationFn: (notification: Notification) => APIClient.notifications.create(notification),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: notificationKeys.lists() });
toast.custom((t) => );
toggle();
},
onError: () => {
toast.custom((t) => );
}
});
const onSubmit = (formData: unknown) => createMutation.mutate(formData as Notification);
const testMutation = useMutation({
mutationFn: (n: Notification) => APIClient.notifications.test(n),
onError: (err) => {
console.error(err);
}
});
const testNotification = (data: unknown) => testMutation.mutate(data as Notification);
const validate = (values: NotificationAddFormValues) => {
const errors = {} as FormikErrors;
if (!values.name)
errors.name = "Required";
return errors;
};
return (
);
}
const EventCheckBoxes = () => (
);
interface UpdateProps {
isOpen: boolean;
toggle: () => void;
notification: Notification;
}
interface InitialValues {
id: number;
enabled: boolean;
type: NotificationType;
name: string;
webhook?: string;
token?: string;
api_key?: string;
channel?: string;
events: NotificationEvent[];
}
export function NotificationUpdateForm({ isOpen, toggle, notification }: UpdateProps) {
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: (notification: Notification) => APIClient.notifications.update(notification),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: notificationKeys.lists() });
toast.custom((t) => );
toggle();
}
});
const onSubmit = (formData: unknown) => mutation.mutate(formData as Notification);
const deleteMutation = useMutation({
mutationFn: (notificationID: number) => APIClient.notifications.delete(notificationID),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: notificationKeys.lists() });
toast.custom((t) => );
}
});
const deleteAction = () => deleteMutation.mutate(notification.id);
const testMutation = useMutation({
mutationFn: (n: Notification) => APIClient.notifications.test(n),
onError: (err) => {
console.error(err);
}
});
const testNotification = (data: unknown) => testMutation.mutate(data as Notification);
const initialValues: InitialValues = {
id: notification.id,
enabled: notification.enabled,
type: notification.type,
name: notification.name,
webhook: notification.webhook,
token: notification.token,
api_key: notification.api_key,
channel: notification.channel,
events: notification.events || []
};
return (
type="UPDATE"
title="Notification"
isOpen={isOpen}
toggle={toggle}
onSubmit={onSubmit}
deleteAction={deleteAction}
initialValues={initialValues}
testFn={testNotification}
>
{(values) => (
{({ field, form: { setFieldValue, resetForm } }: FieldProps) => (
Events
Select what events to trigger on
{componentMap[values.type]}
)}
);
}