Skip to content

Commit ec6dda2

Browse files
committed
refactor: replace download handling with ButtonLink for direct file downloads
1 parent 54a207d commit ec6dda2

File tree

1 file changed

+15
-40
lines changed

1 file changed

+15
-40
lines changed

src/components/AssetDownload/index.tsx

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ImageProps, StaticImageData } from "next/image"
55

66
import AssetDownloadArtist from "@/components/AssetDownload/AssetDownloadArtist"
77
import AssetDownloadImage from "@/components/AssetDownload/AssetDownloadImage"
8-
import { Button } from "@/components/ui/buttons/Button"
8+
import { ButtonLink } from "@/components/ui/buttons/Button"
99
import { Flex, Stack } from "@/components/ui/flex"
1010

1111
import { cn } from "@/lib/utils/cn"
@@ -41,40 +41,8 @@ const AssetDownload = ({
4141
})
4242
}
4343

44-
const handleDownload = async (url: string, fileExtension: string) => {
45-
if (!url) return
46-
47-
matomoHandler()
48-
49-
try {
50-
const response = await fetch(url)
51-
const blob = await response.blob()
52-
const blobUrl = window.URL.createObjectURL(blob)
53-
const link = document.createElement("a")
54-
link.href = blobUrl
55-
link.download = `${title.replace(/\s+/g, "-").toLowerCase()}.${fileExtension}`
56-
document.body.appendChild(link)
57-
link.click()
58-
document.body.removeChild(link)
59-
window.URL.revokeObjectURL(blobUrl)
60-
} catch (error) {
61-
console.error("Failed to download file:", error)
62-
}
63-
}
64-
65-
const handleSvgDownload = () => {
66-
if (!svgUrl) return
67-
handleDownload(svgUrl, "svg")
68-
}
69-
70-
const handleImageDownload = () => {
71-
const imgSrc = (image as StaticImageData).src
72-
if (!imgSrc) return
73-
const fileExt = extname(imgSrc).slice(1)
74-
handleDownload(imgSrc, fileExt)
75-
}
76-
7744
const imgSrc = (image as StaticImageData).src
45+
const fileExtension = extname(imgSrc).slice(1)
7846

7947
return (
8048
<Stack
@@ -89,14 +57,21 @@ const AssetDownload = ({
8957
)}
9058
</div>
9159
<Flex className="mt-4 gap-5">
92-
<Button onClick={handleImageDownload}>
93-
{t("page-assets-download-download")} (
94-
{extname(imgSrc).slice(1).toUpperCase()})
95-
</Button>
60+
<ButtonLink
61+
href={imgSrc}
62+
onClick={matomoHandler}
63+
download={`${title.replace(/\s+/g, "-").toLowerCase()}.${fileExtension}`}
64+
>
65+
{t("page-assets-download-download")} ({fileExtension.toUpperCase()})
66+
</ButtonLink>
9667
{svgUrl && (
97-
<Button onClick={handleSvgDownload}>
68+
<ButtonLink
69+
href={svgUrl}
70+
onClick={matomoHandler}
71+
download={`${title.replace(/\s+/g, "-").toLowerCase()}.svg`}
72+
>
9873
{t("page-assets-download-download")} (SVG)
99-
</Button>
74+
</ButtonLink>
10075
)}
10176
</Flex>
10277
</Stack>

0 commit comments

Comments
 (0)