cc_site_maker / src /components /deploy-button /deploy-button.tsx
postApocalebtic
Download To Project Root
a3306be
Raw History Blame
3.8 kB
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useState, useEffect } from 'react';
import classNames from 'classnames';
import { toast } from 'react-toastify';
import { FaDownload } from 'react-icons/fa';
import { Auth } from './../../../utils/types';
import LoadButton from '../load-button/load-button';
function getUrlParam(paramName: string): string | null {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(paramName);
}
function DownloadButton({
html,
error = false,
auth,
setHtml
}: {
html: string;
error: boolean;
auth?: Auth;
setHtml: (html: string) => void;
}) {
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);
const [destination, setDestination] = useState<string | null>(null);
const [filename, setFilename] = useState('index.html');
// Get destination from URL parameters
useEffect(() => {
const fileDestination = getUrlParam('destination');
const customFilename = getUrlParam('filename');
if (fileDestination) {
setDestination(fileDestination);
}
if (customFilename) {
setFilename(customFilename);
}
}, []);
const downloadFile = async () => {
setLoading(true);
try {
if (!destination) {
throw new Error(
'No destination specified. Please provide a destination path in the URL.'
);
}
// Make a POST request to the server endpoint
const response = await fetch('/api/download', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
html,
filename,
destination
})
});
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || 'Failed to save file');
}
toast.success(
<div className="w-full flex items-center justify-center">
File saved successfully!
<span className="ml-1">({data.path})</span>
</div>,
{
autoClose: 3000
}
);
} catch (err: any) {
console.error('Download error:', err);
toast.error(err.message || 'Error saving file');
} finally {
setLoading(false);
setOpen(false);
}
};
return (
<div className="flex items-center justify-end gap-5">
<LoadButton auth={auth} setHtml={setHtml} setPath={() => {}} />
<div className="relative flex items-center justify-end">
{auth && (
<>
<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-green-400 text-white shadow-sm dark:shadow-highlight/20',
{
'bg-green-600': open,
'bg-green-500': !open
}
)}
onClick={downloadFile}
disabled={loading || error}
>
<FaDownload className="mr-2" />
Download
</button>
{destination && (
<div className="absolute top-full right-0 mt-2 text-xs text-gray-300 bg-gray-800 rounded p-1">
Destination: {destination}
</div>
)}
</div>
</div>
);
}
export default DownloadButton;