2024-10-31 10:27:23 +03:00
|
|
|
import JSZip from 'jszip'
|
|
|
|
import { useSocket } from "@/context/SocketContext"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
import { Download } from "lucide-react"
|
|
|
|
|
2024-11-01 17:48:05 +02:00
|
|
|
export default function DownloadButton({ name }: { name: string }) {
|
2024-10-31 10:27:23 +03:00
|
|
|
const { socket } = useSocket()
|
|
|
|
|
|
|
|
const handleDownload = async () => {
|
|
|
|
socket?.emit("downloadFiles", {}, async (response: {files: {path: string, content: string}[]}) => {
|
|
|
|
const zip = new JSZip()
|
|
|
|
|
|
|
|
response.files.forEach(file => {
|
|
|
|
zip.file(file.path, file.content)
|
|
|
|
})
|
|
|
|
|
|
|
|
const blob = await zip.generateAsync({type: "blob"})
|
|
|
|
const url = window.URL.createObjectURL(blob)
|
|
|
|
const a = document.createElement('a')
|
|
|
|
a.href = url
|
2024-11-01 17:48:05 +02:00
|
|
|
a.download = `${name}.zip`
|
2024-10-31 10:27:23 +03:00
|
|
|
a.click()
|
|
|
|
window.URL.revokeObjectURL(url)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-01 17:48:05 +02:00
|
|
|
|
2024-10-31 10:27:23 +03:00
|
|
|
return (
|
|
|
|
<Button variant="outline" onClick={handleDownload}>
|
|
|
|
<Download className="w-4 h-4 mr-2" />
|
|
|
|
Download
|
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
}
|