Spaces:
Running
Running
File size: 6,877 Bytes
ea6c2a8 b3c2102 ea6c2a8 c2c7576 ea6c2a8 22916b8 ea6c2a8 22916b8 7c248a2 ea6c2a8 a582fa0 ea6c2a8 b3c2102 0e43d69 b3c2102 ea6c2a8 b3c2102 ea6c2a8 a582fa0 ea6c2a8 f91ca77 ab80ab7 f91ca77 ea6c2a8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useState } from "react";
import classNames from "classnames";
import { toast } from "react-toastify";
import { FaPowerOff } from "react-icons/fa6";
import SpaceIcon from "@/assets/space.svg";
import Loading from "../loading/loading";
import Login from "../login/login";
import { Auth } from "./../../../utils/types";
const MsgToast = ({ url }: { url: string }) => (
<div className="w-full flex items-center justify-center gap-3">
Your space is live!
<button
className="bg-black text-sm block text-white rounded-md px-3 py-1.5 hover:bg-gray-900 cursor-pointer"
onClick={() => {
window.open(url, "_blank");
}}
>
See Space
</button>
</div>
);
function DeployButton({
html,
error = false,
auth,
}: {
html: string;
error: boolean;
auth?: Auth;
}) {
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);
const [path, setPath] = useState<string | undefined>(undefined);
const [config, setConfig] = useState({
title: "",
});
const createSpace = async () => {
setLoading(true);
try {
const request = await fetch("/api/deploy", {
method: "POST",
body: JSON.stringify({
title: config.title,
path,
html,
}),
headers: {
"Content-Type": "application/json",
},
});
const response = await request.json();
if (response.ok) {
toast.success(
<MsgToast
url={`https://huggingface.co/spaces/${response.path ?? path}`}
/>,
{
autoClose: 10000,
}
);
setPath(response.path);
} else {
toast.error(response.message);
}
} catch (err: any) {
toast.error(err.message);
} finally {
setLoading(false);
setOpen(false);
}
};
return (
<div className="relative flex items-center justify-end">
{auth && (
<>
<button
className="mr-2 cursor-pointer"
onClick={() => {
if (confirm("Are you sure you want to log out?")) {
// go to /auth/logout page
window.location.href = "/auth/logout";
}
}}
>
<FaPowerOff className="text-lg text-red-500" />
</button>
<p className="mr-3 text-xs lg:text-sm text-gray-300">
<span className="max-lg:hidden">Connected as </span>
<a
href={`https://huggingface.co/${auth.preferred_username}`}
target="_blank"
className="underline hover:text-white"
>
{auth.preferred_username}
</a>
</p>
</>
)}
<button
className={classNames(
"relative cursor-pointer flex-none flex items-center justify-center rounded-md text-xs lg:text-sm font-semibold leading-5 lg:leading-6 py-1.5 px-5 hover:bg-pink-400 text-white shadow-sm dark:shadow-highlight/20",
{
"bg-pink-400": open,
"bg-pink-500": !open,
}
)}
onClick={() => setOpen(!open)}
>
{path ? "Update Space" : "Deploy to Space"}
</button>
<div
className={classNames(
"h-screen w-screen bg-black/20 fixed left-0 top-0 z-10",
{
"opacity-0 pointer-events-none": !open,
}
)}
onClick={() => setOpen(false)}
></div>
<div
className={classNames(
"absolute top-[calc(100%+8px)] right-0 z-10 w-80 bg-white border border-gray-200 rounded-lg shadow-lg transition-all duration-75 overflow-hidden",
{
"opacity-0 pointer-events-none": !open,
}
)}
>
{!auth ? (
<Login html={html}>
<p className="text-gray-500 text-sm mb-3">
Host this project for free and share it with your friends.
</p>
</Login>
) : (
<>
<header className="flex items-center text-sm px-4 py-2 border-b border-gray-200 gap-2 bg-gray-100 font-semibold text-gray-700">
<span className="text-xs bg-pink-500/10 text-pink-500 rounded-full pl-1.5 pr-2.5 py-0.5 flex items-center justify-start gap-1.5">
<img src={SpaceIcon} alt="Space Icon" className="size-4" />
Space
</span>
Configure Deployment
</header>
<main className="px-4 pt-3 pb-4 space-y-3">
<p className="text-xs text-amber-600 bg-amber-500/10 rounded-md p-2">
{path ? (
<span>
Your space is live at{" "}
<a
href={`https://huggingface.co/spaces/${path}`}
target="_blank"
className="underline hover:text-amber-700"
>
huggingface.co/{path}
</a>
. You can update it by deploying again.
</span>
) : (
"Deploy your project to a space on the Hub. Spaces are a way to share your project with the world."
)}
</p>
{!path && (
<label className="block">
<p className="text-gray-600 text-sm font-medium mb-1.5">
Space Title
</p>
<input
type="text"
value={config.title}
className="mr-2 border rounded-md px-3 py-1.5 border-gray-300 w-full text-sm"
placeholder="My Awesome Space"
onChange={(e) =>
setConfig({ ...config, title: e.target.value })
}
/>
</label>
)}
{error && (
<p className="text-red-500 text-xs bg-red-500/10 rounded-md p-2">
Your code has errors. Fix them before deploying.
</p>
)}
<div className="pt-2 text-right">
<button
disabled={error || loading || !config.title}
className="relative rounded-full bg-black px-5 py-2 text-white font-semibold text-xs hover:bg-black/90 transition-all duration-100 disabled:bg-gray-300 disabled:text-gray-500 disabled:cursor-not-allowed disabled:hover:bg-gray-300"
onClick={createSpace}
>
{path ? "Update Space" : "Create Space"}
{loading && <Loading />}
</button>
</div>
</main>
</>
)}
</div>
</div>
);
}
export default DeployButton;
|