"use client"; import { useState } from "react"; import { Import } from "lucide-react"; import { Project } from "@/types"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import Loading from "@/components/loading"; import { Input } from "../ui/input"; import { toast } from "sonner"; import { api } from "@/lib/api"; import { useUser } from "@/hooks/useUser"; import { LoginModal } from "../login-modal"; import { useRouter } from "next/navigation"; export const LoadProject = ({ fullXsBtn = false, onSuccess, }: { fullXsBtn?: boolean; onSuccess: (project: Project) => void; }) => { const { user } = useUser(); const router = useRouter(); const [openLoginModal, setOpenLoginModal] = useState(false); const [open, setOpen] = useState(false); const [url, setUrl] = useState(""); const [isLoading, setIsLoading] = useState(false); const checkIfUrlIsValid = (url: string) => { // should match a hugging face spaces URL like: https://huggingface.co/spaces/username/project or https://hf.co/spaces/username/project const urlPattern = new RegExp( /^(https?:\/\/)?(huggingface\.co|hf\.co)\/spaces\/([\w-]+)\/([\w-]+)$/, "i" ); return urlPattern.test(url); }; const handleClick = async () => { if (isLoading) return; // Prevent multiple clicks while loading if (!url) { toast.error("Please enter a URL."); return; } if (!checkIfUrlIsValid(url)) { toast.error("Please enter a valid Hugging Face Spaces URL."); return; } const [username, namespace] = url .replace("https://huggingface.co/spaces/", "") .replace("https://hf.co/spaces/", "") .split("/"); setIsLoading(true); try { const response = await api.post(`/me/projects/${username}/${namespace}`); toast.success("Project imported successfully!"); setOpen(false); setUrl(""); onSuccess(response.data.project); // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error: any) { if (error?.response?.data?.redirect) { return router.push(error.response.data.redirect); } toast.error( error?.response?.data?.error ?? "Failed to import the project." ); } finally { setIsLoading(false); } }; return ( <> {!user ? ( <> ) : (
🎨
🥳
💎

Import a Project

Enter the URL of your Hugging Face Space to import an existing project.

Enter your Hugging Face Space

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); }} className="!bg-white !border-neutral-300 !text-neutral-800 !placeholder:text-neutral-400 selection:!bg-blue-100" />

Then, let's import it!

)} ); };