import {Fragment, useState} from "react";
import { toast } from "react-hot-toast";
import { useMutation, useQuery } from "react-query";
import Select, { components } from "react-select";
import { Field, Form, Formik } from "formik";
import type { FieldProps } from "formik";
import { XIcon } from "@heroicons/react/solid";
import { Dialog, Transition } from "@headlessui/react";
import { sleep } from "../../utils";
import { queryClient } from "../../App";
import DEBUG from "../../components/debug";
import { APIClient } from "../../api/APIClient";
import {
TextFieldWide,
PasswordFieldWide,
SwitchGroupWide
} from "../../components/inputs";
import { SlideOver } from "../../components/panels";
import Toast from '../../components/notifications/Toast';
const Input = (props: any) => {
return (
);
}
const Control = (props: any) => {
return (
);
}
const Menu = (props: any) => {
return (
);
}
const Option = (props: any) => {
return (
);
}
const IrcSettingFields = (ind: IndexerDefinition, indexer: string) => {
if (indexer !== "") {
return (
{ind && ind.irc && ind.irc.settings && (
IRC
Networks, channels and invite commands are configured automatically.
{ind.irc.settings.map((f: IndexerSetting, idx: number) => {
switch (f.type) {
case "text":
return
case "secret":
if (f.name === "invite_command") {
return
}
return
}
return null
})}
{/*
*/}
)}
)
}
}
const SettingFields = (ind: IndexerDefinition, indexer: string) => {
if (indexer !== "") {
return (
{ind && ind.settings && ind.settings.map((f: any, idx: number) => {
switch (f.type) {
case "text":
return (
)
case "secret":
return (
)
}
return null
})}
)
}
}
interface AddProps {
isOpen: boolean;
toggle: any;
}
export function IndexerAddForm({ isOpen, toggle }: AddProps) {
const [indexer, setIndexer] = useState({} as IndexerDefinition)
const { data } = useQuery('indexerDefinition', APIClient.indexers.getSchema,
{
enabled: isOpen,
refetchOnWindowFocus: false
}
)
const mutation = useMutation(
(indexer: Indexer) => APIClient.indexers.create(indexer), {
onSuccess: () => {
queryClient.invalidateQueries(['indexer']);
toast.custom((t) => )
sleep(1500)
toggle()
},
onError: () => {
toast.custom((t) => )
}
})
const ircMutation = useMutation(
(network: IrcNetworkCreate) => APIClient.irc.createNetwork(network)
);
const onSubmit = (formData: any) => {
const ind = data && data.find(i => i.identifier === formData.identifier);
if (!ind)
return;
const channels: IrcChannel[] = [];
if (ind.irc.channels.length) {
ind.irc.channels.forEach(element => {
channels.push({
id: 0,
enabled: true,
name: element,
password: "",
detached: false,
monitoring: false
});
});
}
const network: IrcNetworkCreate = {
name: ind.irc.network,
pass: "",
enabled: false,
connected: false,
server: ind.irc.server,
port: ind.irc.port,
tls: ind.irc.tls,
nickserv: formData.irc.nickserv,
invite_command: formData.irc.invite_command,
channels: channels,
}
mutation.mutate(formData, {
onSuccess: () => ircMutation.mutate(network)
});
};
const renderSettingFields = (indexer: string) => {
if (indexer !== "") {
const ind = data && data.find(i => i.identifier === indexer);
return (
{ind && ind.settings && ind.settings.map((f: any, idx: number) => {
switch (f.type) {
case "text":
return (
)
case "secret":
return (
)
}
return null
})}
)
}
}
const renderIrcSettingFields = (indexer: string) => {
if (indexer !== "") {
const ind = data && data.find(i => i.identifier === indexer);
return (
{ind && ind.irc && ind.irc.settings && (
IRC
Networks, channels and invite commands are configured automatically.
{ind.irc.settings.map((f: IndexerSetting, idx: number) => {
switch (f.type) {
case "text":
return
case "secret":
return
}
return null
})}
{/*
*/}
)}
)
}
}
return (
)
}
interface UpdateProps {
isOpen: boolean;
toggle: any;
indexer: Indexer;
}
export function IndexerUpdateForm({ isOpen, toggle, indexer }: UpdateProps) {
const mutation = useMutation((indexer: Indexer) => APIClient.indexers.update(indexer), {
onSuccess: () => {
queryClient.invalidateQueries(['indexer']);
toast.custom((t) => )
sleep(1500)
toggle()
}
})
const deleteMutation = useMutation((id: number) => APIClient.indexers.delete(id), {
onSuccess: () => {
queryClient.invalidateQueries(['indexer']);
toast.custom((t) => )
}
})
const onSubmit = (data: any) => {
// TODO clear data depending on type
mutation.mutate(data)
};
const deleteAction = () => {
deleteMutation.mutate(indexer.id)
}
const renderSettingFields = (settings: IndexerSetting[]) => {
if (settings === undefined) {
return null
}
return (
{settings.map((f: IndexerSetting, idx: number) => {
switch (f.type) {
case "text":
return (
)
case "secret":
return (
)
}
return null
})}
)
}
const initialValues = {
id: indexer.id,
name: indexer.name,
enabled: indexer.enabled,
identifier: indexer.identifier,
settings: indexer.settings?.reduce(
(o: Record, obj: IndexerSetting) => ({
...o,
[obj.name]: obj.value
} as Record),
{} as Record
),
}
return (
{() => (
{renderSettingFields(indexer.settings)}
)}
)
}