|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | + |
| 5 | +import { Button } from '../../../../components/ui/Button'; |
| 6 | + |
| 7 | +type SkillActionsProps = { |
| 8 | + /** Raw SKILL.md content, read at build time and passed in from the page. */ |
| 9 | + content: string; |
| 10 | + /** Suggested filename for the download. */ |
| 11 | + filename: string; |
| 12 | +}; |
| 13 | + |
| 14 | +// Copy / download the `build-with-viem-eip8130` skill straight from the UI, so |
| 15 | +// devs can drop it into their AI coding agent without cloning the repo. The |
| 16 | +// content is baked in at build time (see page.tsx), so these are pure |
| 17 | +// client-side clipboard/blob actions. |
| 18 | +export function SkillActions({ content, filename }: SkillActionsProps) { |
| 19 | + const [copied, setCopied] = useState(false); |
| 20 | + const disabled = !content; |
| 21 | + |
| 22 | + const copy = async () => { |
| 23 | + try { |
| 24 | + await navigator.clipboard.writeText(content); |
| 25 | + setCopied(true); |
| 26 | + setTimeout(() => setCopied(false), 1500); |
| 27 | + } catch { |
| 28 | + // clipboard unavailable (non-secure context) — use Download instead |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + const download = () => { |
| 33 | + const blob = new Blob([content], { type: 'text/markdown;charset=utf-8' }); |
| 34 | + const url = URL.createObjectURL(blob); |
| 35 | + const a = document.createElement('a'); |
| 36 | + a.href = url; |
| 37 | + a.download = filename; |
| 38 | + document.body.appendChild(a); |
| 39 | + a.click(); |
| 40 | + a.remove(); |
| 41 | + URL.revokeObjectURL(url); |
| 42 | + }; |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="flex flex-wrap items-center gap-3"> |
| 46 | + <Button variant="secondary" size="sm" onClick={download} disabled={disabled}> |
| 47 | + Grab the skill |
| 48 | + </Button> |
| 49 | + <Button variant="outline" size="sm" onClick={copy} disabled={disabled}> |
| 50 | + {copied ? 'Copied' : 'Copy'} |
| 51 | + </Button> |
| 52 | + </div> |
| 53 | + ); |
| 54 | +} |
0 commit comments