|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { ArrowRightIcon, Download } from "lucide-react"; |
| 6 | +import { toast } from "sonner"; |
| 7 | + |
| 8 | +import { cn } from "@/lib/utils"; |
| 9 | +import { Button, buttonVariants } from "@/components/ui/button"; |
| 10 | +import { Icons } from "@/components/icons"; |
| 11 | + |
| 12 | +interface RepoDownloadProps { |
| 13 | + repo: string; |
| 14 | + owner: string; |
| 15 | + free?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +export default function RepoDownload({ |
| 19 | + repo, |
| 20 | + owner, |
| 21 | + free = false, |
| 22 | +}: RepoDownloadProps) { |
| 23 | + const [loading, setLoading] = useState(false); |
| 24 | + |
| 25 | + const handleDownload = async () => { |
| 26 | + setLoading(true); |
| 27 | + |
| 28 | + try { |
| 29 | + const response = await fetch( |
| 30 | + `${process.env.NEXT_PUBLIC_APP_URL}/api/repo/download`, |
| 31 | + { |
| 32 | + method: "POST", |
| 33 | + headers: { |
| 34 | + "Content-Type": "application/json", |
| 35 | + }, |
| 36 | + body: JSON.stringify({ repo, owner }), |
| 37 | + }, |
| 38 | + ); |
| 39 | + if (!response.ok) { |
| 40 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 41 | + } |
| 42 | + const data = await response.json(); |
| 43 | + const downloadUrl = data.downloadUrl; |
| 44 | + /* window.open(downloadUrl, "_blank"); */ |
| 45 | + window.location.href = downloadUrl; |
| 46 | + } catch (error) { |
| 47 | + toast.error("Error occured while downloading. Please try again."); |
| 48 | + console.error("error", error); |
| 49 | + } finally { |
| 50 | + setLoading(false); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + if (free) { |
| 55 | + return ( |
| 56 | + <Button |
| 57 | + onClick={handleDownload} |
| 58 | + disabled={loading} |
| 59 | + className="not-prose group relative w-full gap-2" |
| 60 | + > |
| 61 | + {loading ? "Downloading" : "Free Download"} |
| 62 | + {!loading && <Download className="size-4" />} |
| 63 | + {loading && <Icons.spinner className="size-4 animate-spin" />} |
| 64 | + </Button> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <Link |
| 70 | + href="https://pro.magicui.design/pricing" |
| 71 | + target="_blank" |
| 72 | + className={cn( |
| 73 | + buttonVariants({ |
| 74 | + variant: "default", |
| 75 | + }), |
| 76 | + "not-prose group relative w-full gap-1", |
| 77 | + )} |
| 78 | + > |
| 79 | + Buy Now |
| 80 | + <ArrowRightIcon className="size-4 transition-all duration-300 ease-out group-hover:translate-x-1" /> |
| 81 | + </Link> |
| 82 | + ); |
| 83 | +} |
0 commit comments