/* 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(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(
File saved successfully! ({data.path})
, { autoClose: 3000 } ); } catch (err: any) { console.error('Download error:', err); toast.error(err.message || 'Error saving file'); } finally { setLoading(false); setOpen(false); } }; return (
{}} />
{auth && ( <>

Connected as {auth.preferred_username}

)} {destination && (
Destination: {destination}
)}
); } export default DownloadButton;