({ children }: { children: React.ReactNode })
| 9 | import * as Toast from "@radix-ui/react-toast"; |
| 10 | |
| 11 | export function DownloadDropdown({ children }: { children: React.ReactNode }) { |
| 12 | const hasProAccess = useHasProAccess(); |
| 13 | const filename = useDownloadFilename(); |
| 14 | const watermark = !hasProAccess; |
| 15 | const scale = hasProAccess ? AUTH_IMG_SCALE : UNAUTH_IMG_SCALE; |
| 16 | |
| 17 | // store a string for a recently completed action we will use to show a checkmark in certain cases |
| 18 | const [message, setMessage] = useState<string | null>(null); |
| 19 | |
| 20 | const handleDownload = async (format: string) => { |
| 21 | if (!window.__cy) return; |
| 22 | |
| 23 | if (format === "svg" && hasProAccess) { |
| 24 | const svg = await getSvg({ cy: window.__cy }); |
| 25 | downloadSvg({ svg, filename }); |
| 26 | } else { |
| 27 | try { |
| 28 | const { canvas } = await getCanvas({ |
| 29 | cy: window.__cy, |
| 30 | type: format as "png" | "jpg", |
| 31 | watermark, |
| 32 | scale, |
| 33 | }); |
| 34 | downloadCanvas({ |
| 35 | canvas, |
| 36 | filename, |
| 37 | type: format as "png" | "jpg", |
| 38 | cleanup: () => {}, |
| 39 | }); |
| 40 | } catch (err) { |
| 41 | console.error(`Failed to download ${format}:`, err); |
| 42 | } |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | const handleCopy = async (format: string) => { |
| 47 | if (!window.__cy) return; |
| 48 | |
| 49 | // if the type is svg, copy code |
| 50 | if (format === "svg") { |
| 51 | const svg = await getSvg({ cy: window.__cy }); |
| 52 | navigator.clipboard.writeText(svg); |
| 53 | setMessage(t`Copied SVG code to clipboard`); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | const { canvas } = await getCanvas({ |
| 58 | cy: window.__cy, |
| 59 | type: format as "png" | "jpg", |
| 60 | watermark, |
| 61 | scale, |
| 62 | }); |
| 63 | |
| 64 | canvas.toBlob(async (blob) => { |
| 65 | if (blob) { |
| 66 | try { |
| 67 | await navigator.clipboard.write([ |
| 68 | new ClipboardItem({ [blob.type]: blob }), |
nothing calls this directly
no test coverage detected