import { useQueryClient } from '@tanstack/react-query' import { useParams } from 'common' import { useEffect, useRef, useState } from 'react' import { Button, IconCheckCircle, IconLoader } from 'ui' import { projectKeys } from 'data/projects/keys' import { invalidateProjectDetailsQuery } from 'data/projects/project-detail-query' import { getWithTimeout } from 'lib/common/fetch' import { API_URL, PROJECT_STATUS } from 'lib/constants' import { useProjectContext } from './ProjectContext' const RestoringState = () => { const { ref } = useParams() const queryClient = useQueryClient() const { project } = useProjectContext() const checkServerInterval = useRef() const [loading, setLoading] = useState(false) const [isCompleted, setIsCompleted] = useState(false) useEffect(() => { checkServerInterval.current = window.setInterval(checkServer, 4000) return () => clearInterval(checkServerInterval.current) }, []) async function checkServer() { if (!project) return const projectStatus = await getWithTimeout(`${API_URL}/projects/${project.ref}/status`, { timeout: 2000, }) if (projectStatus && !projectStatus.error) { const { status } = projectStatus if (status === PROJECT_STATUS.ACTIVE_HEALTHY) { clearInterval(checkServerInterval.current) setIsCompleted(true) } else { queryClient.invalidateQueries(projectKeys.detail(ref)) } } } const onConfirm = async () => { if (!project) return console.error('Project is required') setLoading(true) if (ref) await invalidateProjectDetailsQuery(queryClient, ref) } return (
{isCompleted ? (

Restoration complete!

Your project has been successfully restored and is now back online.

) : (

Restoration in progress

Restoration can take from a few minutes up to several hours depending on the size of your database. Your project will be offline while the restoration is running.

)}
) } export default RestoringState