/* * Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors. * SPDX-License-Identifier: GPL-2.0-or-later */ import { FC, Fragment, MutableRefObject, useState } from "react"; import { Dialog, DialogPanel, DialogTitle, Transition, TransitionChild } from "@headlessui/react"; import { ExclamationTriangleIcon } from "@heroicons/react/24/solid"; import { RingResizeSpinner } from "@components/Icons"; interface ModalUpperProps { title: string; text: string; } interface ModalLowerProps { isOpen: boolean; isLoading: boolean; toggle: () => void; deleteAction?: () => void; forceRunAction?: () => void; } interface DeleteModalProps extends ModalUpperProps, ModalLowerProps { buttonRef: MutableRefObject | undefined; } interface ForceRunModalProps { isOpen: boolean; isLoading: boolean; toggle: () => void; buttonRef: MutableRefObject | undefined; forceRunAction: () => void; title: string; text: string; } const ModalUpper = ({ title, text }: ModalUpperProps) => (
); const ModalLower = ({ isOpen, isLoading, toggle, deleteAction }: ModalLowerProps) => (
{isLoading ? ( ) : ( <> )}
); export const DeleteModal: FC = (props: DeleteModalProps) => (
); export const ForceRunModal: FC = (props: ForceRunModalProps) => { const [inputValue, setInputValue] = useState(""); const isInputCorrect = inputValue.trim().toLowerCase() === "i understand"; // A function to reset the input and handle any necessary cleanup const resetAndClose = () => { setInputValue(""); props.toggle(); }; // The handleClose function will be passed to the onClose prop of the Dialog const handleClose = () => { setTimeout(() => { resetAndClose(); }, 200); }; const handleForceRun = (e: React.SyntheticEvent) => { e.preventDefault(); if (props.isOpen && isInputCorrect) { props.forceRunAction(); props.toggle(); // Delay the reset of the input until after the transition finishes setTimeout(() => { setInputValue(""); }, 400); } }; // When the 'Cancel' button is clicked const handleCancel = (e: React.MouseEvent) => { e.preventDefault(); resetAndClose(); }; return (
setInputValue(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleForceRun(e); } }} />
{props.isLoading ? ( ) : ( <> )}
); };