42 lines
1.2 KiB
TypeScript
Raw Normal View History

// React component for download button
2024-10-31 10:27:23 +03:00
import { Button } from "@/components/ui/button"
import { useSocket } from "@/context/SocketContext"
2024-10-31 10:27:23 +03:00
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",
{ timestamp: Date.now() },
async (response: { zipBlob: string }) => {
const { zipBlob } = response
2024-10-31 10:27:23 +03:00
// Decode Base64 back to binary data
const binary = atob(zipBlob)
const bytes = new Uint8Array(binary.length)
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i)
}
const blob = new Blob([bytes], { type: "application/zip" })
// Create URL and download
const url = window.URL.createObjectURL(blob)
const a = document.createElement("a")
a.href = url
a.download = `${name}.zip`
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>
)
}