import { useState } from "react"; import { Paintbrush } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Input } from "@/components/ui/input"; import Loading from "@/components/loading"; export function Redesign({ onRedesign, }: { onRedesign: (md: string, url: string) => void; }) { const [url, setUrl] = useState(""); const [open, setOpen] = useState(false); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const checkIfUrlIsValid = (url: string) => { const urlPattern = new RegExp( /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/, "i" ); return urlPattern.test(url); }; const handleClick = async () => { setError(null); if (isLoading) return; if (!url) { toast.error("Please enter a URL."); return; } if (!checkIfUrlIsValid(url)) { toast.error("Please enter a valid URL."); return; } setIsLoading(true); const response = await fetch("/api/redesign", { method: "PUT", body: JSON.stringify({ url: url.trim() }), headers: { "Content-Type": "application/json", }, }) .then(async (response) => { if (response?.ok) { const data = await response.json(); return data; } }) .catch((error) => { setError(error.message); }); if (response.ok) { onRedesign(response.markdown, url); } setIsLoading(false); }; return (
🎨
🥳
💎

Redesign your Site!

Try our new Redesign feature to give your site a fresh look.

{error &&

{error}

}

Enter your website URL to get started:

setUrl(e.target.value)} onBlur={(e) => { const inputUrl = e.target.value.trim(); if (!inputUrl) { setUrl(""); return; } if (!checkIfUrlIsValid(inputUrl)) { toast.error("Please enter a valid URL."); return; } setUrl(inputUrl); }} onKeyDown={(e) => { if (e.key === "Enter") { handleClick(); } }} />

Then, let's redesign it!

); }