import { ofetch } from "ofetch"; import { useState } from "react"; import { useAsyncFn } from "react-use"; import { getAuthHeaders } from "@/backend/accounts/auth"; import { Button } from "@/components/buttons/Button"; import { Icon, Icons } from "@/components/Icon"; import { Box } from "@/components/layout/Box"; import { Heading2 } from "@/components/utils/Text"; import { useBackendUrl } from "@/hooks/auth/useBackendUrl"; import { AccountWithToken, useAuthStore } from "@/stores/auth"; interface CleanupResponse { deletedCount: number; message: string; } async function cleanupProgressItems( backendUrl: string, account: AccountWithToken, ) { return ofetch(`/users/${account.userId}/progress/cleanup`, { method: "DELETE", headers: getAuthHeaders(account.token), baseURL: backendUrl, }); } export function ProgressCleanupPart() { const backendUrl = useBackendUrl(); const account = useAuthStore((s) => s.account); const [status, setStatus] = useState<{ hasRun: boolean; success: boolean; errorText: string; result: CleanupResponse | null; }>({ hasRun: false, success: false, errorText: "", result: null, }); const [cleanupState, runCleanup] = useAsyncFn(async () => { setStatus({ hasRun: false, success: false, errorText: "", result: null, }); if (!backendUrl || !account) { return setStatus({ hasRun: true, success: false, errorText: "Backend URL or account not available", result: null, }); } try { const result = await cleanupProgressItems(backendUrl, account); return setStatus({ hasRun: true, success: true, errorText: "", result, }); } catch (err) { console.error("Progress cleanup failed:", err); return setStatus({ hasRun: true, success: false, errorText: "Failed to clean up progress items. Check console for details.", result: null, }); } }, [backendUrl, account]); return ( <> Progress Cleanup
{!status.hasRun ? (

Remove unwanted progress items from the database

) : status.success ? (

Cleanup completed

) : (

Cleanup failed

{status.errorText}

)}
); }