import { useEffect, useState } from "react"; import { Settings } from "../../types"; import { Button } from "../ui/button"; import { Input } from "../ui/input"; import { Label } from "../ui/label"; import useThrottle from "../../hooks/useThrottle"; import { Progress } from "../ui/progress"; import { PICO_BACKEND_FORM_SECRET } from "../../config"; interface Props { settings: Settings; setSettings: React.Dispatch>; } interface UsageResponse { used_credits: number; total_credits: number; is_valid: boolean; } enum FetchState { EMPTY = "EMPTY", LOADING = "LOADING", INVALID = "INVALID", VALID = "VALID", } function AccessCodeSection({ settings, setSettings }: Props) { const [isLoading, setIsLoading] = useState(false); const [isValid, setIsValid] = useState(false); const [usedCredits, setUsedCredits] = useState(0); const [totalCredits, setTotalCredits] = useState(0); const throttledAccessCode = useThrottle(settings.accessCode || "", 500); const fetchState = (() => { if (!settings.accessCode) return FetchState.EMPTY; if (isLoading) return FetchState.LOADING; if (!isValid) return FetchState.INVALID; return FetchState.VALID; })(); async function fetchUsage(accessCode: string) { const res = await fetch( "https://backend.buildpicoapps.com/screenshot_to_code/get_access_code_usage", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ access_code: accessCode, secret: PICO_BACKEND_FORM_SECRET, }), } ); const usage = (await res.json()) as UsageResponse; if (!usage.is_valid) { setIsValid(false); } else { setIsValid(true); setUsedCredits(usage.used_credits); setTotalCredits(usage.total_credits); } setIsLoading(false); } useEffect(() => { // Don't do anything if access code is empty if (!throttledAccessCode) return; setIsLoading(true); setIsValid(true); // Wait for 500 ms before fetching usage setTimeout(async () => { await fetchUsage(throttledAccessCode); }, 500); }, [throttledAccessCode]); return (
setSettings((s) => ({ ...s, accessCode: e.target.value, })) } /> {fetchState === "EMPTY" && (
)} {fetchState === "LOADING" && (
Loading...
)} {fetchState === "INVALID" && ( <>
Invalid access code
)} {fetchState === "VALID" && ( <>
{usedCredits} out of {totalCredits} credits used
)}
); } export default AccessCodeSection;