import { useMutation, useQuery, useQueryClient } from "react-query";
import { classNames, IsEmptyDate, simplifyDate } from "../../utils";
import { IrcNetworkAddForm, IrcNetworkUpdateForm } from "../../forms";
import { useToggle } from "../../hooks/hooks";
import { APIClient } from "../../api/APIClient";
import { EmptySimple } from "../../components/emptystates";
import { LockClosedIcon, LockOpenIcon } from "@heroicons/react/24/solid";
import { Menu, Switch, Transition } from "@headlessui/react";
import { Fragment, useRef } from "react";
import { DeleteModal } from "../../components/modals";
import { toast } from "react-hot-toast";
import Toast from "../../components/notifications/Toast";
import {
ArrowsPointingInIcon,
ArrowsPointingOutIcon,
EllipsisHorizontalIcon,
ExclamationCircleIcon,
PencilSquareIcon,
TrashIcon
} from "@heroicons/react/24/outline";
export const IrcSettings = () => {
const [expandNetworks, toggleExpand] = useToggle(false);
const [addNetworkIsOpen, toggleAddNetwork] = useToggle(false);
const { data } = useQuery("networks", () => APIClient.irc.getNetworks(), {
refetchOnWindowFocus: false,
// Refetch every 3 seconds
refetchInterval: 3000
});
return (
IRC
IRC networks and channels. Click on a network to view channel
status.
-
Network healthy
-
Network unhealthy
-
Network disabled
{data && data.length > 0 ? (
-
Enabled
Network
Server
Nick
{data &&
data.map((network, idx) => (
))}
) : (
)}
);
};
interface ListItemProps {
idx: number;
network: IrcNetworkWithHealth;
expanded: boolean;
}
const ListItem = ({ idx, network, expanded }: ListItemProps) => {
const [updateIsOpen, toggleUpdate] = useToggle(false);
const [edit, toggleEdit] = useToggle(false);
const queryClient = useQueryClient();
const mutation = useMutation(
(network: IrcNetwork) => APIClient.irc.updateNetwork(network),
{
onSuccess: () => {
queryClient.invalidateQueries(["networks"]);
toast.custom((t) => );
}
}
);
const onToggleMutation = (newState: boolean) => {
mutation.mutate({
...network,
enabled: newState
});
};
return (
Enable
{network.enabled ? (
network.healthy ? (
) : (
)
) : (
)}
{network.name}
{network.tls ? (
) : (
)}
{network.server}:{network.port}
{(edit || expanded) && (
{network.channels.length > 0 ? (
-
Channel
Monitoring since
Last announce
{network.channels.map((c) => (
-
{network.enabled ? (
c.monitoring ? (
) : (
)
) : (
)}
{c.name}
{IsEmptyDate(c.monitoring_since)}
{IsEmptyDate(c.last_announce)}
))}
) : (
)}
)}
);
};
interface ListItemDropdownProps {
network: IrcNetwork;
toggleUpdate: () => void;
}
const ListItemDropdown = ({
network,
toggleUpdate
}: ListItemDropdownProps) => {
const cancelModalButtonRef = useRef(null);
const queryClient = useQueryClient();
const [deleteModalIsOpen, toggleDeleteModal] = useToggle(false);
const deleteMutation = useMutation(
(id: number) => APIClient.irc.deleteNetwork(id),
{
onSuccess: () => {
queryClient.invalidateQueries(["networks"]);
queryClient.invalidateQueries(["networks", network.id]);
toast.custom((t) => );
toggleDeleteModal();
}
}
);
const restartMutation = useMutation(
(id: number) => APIClient.irc.restartNetwork(id),
{
onSuccess: () => {
toast.custom((t) => );
queryClient.invalidateQueries(["networks"]);
queryClient.invalidateQueries(["networks", network.id]);
}
}
);
const restart = (id: number) => {
restartMutation.mutate(id);
};
return (
);
};