import { useEffect, useRef, useState } from 'react'; import { httpDelete } from '../lib/http'; import type { TeamDocument } from './CreateTeam/CreateTeamForm'; import { useTeamId } from '../hooks/use-team-id'; import { useOutsideClick } from '../hooks/use-outside-click'; import { useKeydown } from '../hooks/use-keydown'; import { useToast } from '../hooks/use-toast'; type DeleteTeamPopupProps = { onClose: () => void; }; export function DeleteTeamPopup(props: DeleteTeamPopupProps) { const { onClose } = props; const toast = useToast(); const popupBodyEl = useRef(null); const inputEl = useRef(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); const [confirmationText, setConfirmationText] = useState(''); const { teamId } = useTeamId(); useOutsideClick(popupBodyEl, () => { onClose(); }); useKeydown('Escape', () => { onClose(); }); useEffect(() => { inputEl.current?.focus(); }, []); const handleSubmit = async (e: Event) => { e.preventDefault(); setIsLoading(true); setError(''); if (confirmationText.toUpperCase() !== 'DELETE') { setError('Verification text does not match'); setIsLoading(false); return; } const { response, error } = await httpDelete( `${import.meta.env.PUBLIC_API_URL}/v1-delete-team/${teamId}` ); if (error || !response) { setIsLoading(false); setError(error?.message || 'Something went wrong'); return; } toast.success('Team deleted successfully'); window.location.href = '/account'; }; const handleClosePopup = () => { setIsLoading(false); setError(''); setConfirmationText(''); onClose(); }; return ( <>

Delete Team

This will permanently delete your team and all associated data.

Please type "delete" to confirm.

setConfirmationText((e.target as HTMLInputElement).value) } /> {error && (

{error}

)}
); }