| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useRef, useState } from "react"; |
| 4 | import { Icon } from "./icon"; |
| 5 | import { recordFunnel, recordUsage } from "./usage-counting"; |
| 6 | |
| 7 | interface Props { |
| 8 | cmd: string; |
| 9 | copyLabel?: string; |
| 10 | copiedLabel?: string; |
| 11 | copyLocale?: string; |
| 12 | installIntent?: boolean; |
| 13 | html?: string; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * A command in a code block with one button that copies it: the copy icon |
| 18 | * and the word, so the action reads without guessing at the glyph. The check |
| 19 | * mark, "Copied" and its announcement appear only after the clipboard write |
| 20 | * succeeds; a failed write never shows a false confirmation. |
| 21 | */ |
| 22 | export function InstallCodeBlock({ cmd, copyLabel = "Copy", copiedLabel = "Copied", copyLocale, installIntent = false, html }: Props) { |
| 23 | const [copiedCmd, setCopiedCmd] = useState<string | null>(null); |
| 24 | const timer = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 25 | const copied = copiedCmd === cmd; |
| 26 | |
| 27 | useEffect( |
| 28 | () => () => { |
| 29 | if (timer.current) clearTimeout(timer.current); |
| 30 | }, |
| 31 | [], |
| 32 | ); |
| 33 | |
| 34 | const copy = async () => { |
| 35 | if (typeof navigator === "undefined" || !navigator.clipboard) return; |
| 36 | try { |
| 37 | await navigator.clipboard.writeText(cmd); |
| 38 | setCopiedCmd(cmd); |
| 39 | if (installIntent) { |
| 40 | recordUsage("install_copy"); |
| 41 | recordFunnel("install_intent"); |
| 42 | } |
| 43 | if (timer.current) clearTimeout(timer.current); |
| 44 | timer.current = setTimeout(() => { |
| 45 | setCopiedCmd((current) => (current === cmd ? null : current)); |
| 46 | }, 1400); |
| 47 | } catch { |
| 48 | // Clipboard write failed (permissions, non-secure context, or a focused |
| 49 | // write race) — never show a false "Copied" confirmation. |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | const multiline = cmd.includes("\n"); |
| 54 | const copiedWord = copiedLabel.replace(/\s*✓\s*$/u, ""); |
| 55 | |
| 56 | return ( |
| 57 | <div className="code"> |
| 58 | {html ? ( |
| 59 | <pre tabIndex={0} className={multiline ? "code-block" : "code-block code-block-single"} dangerouslySetInnerHTML={{ __html: html }} /> |
| 60 | ) : ( |
| 61 | <pre tabIndex={0} className={multiline ? "code-block" : "code-block code-block-single"}>{cmd}</pre> |
| 62 | )} |
| 63 | <button |
| 64 | type="button" |
| 65 | lang={copyLocale} |
| 66 | onClick={copy} |
| 67 | data-copied={copied} |
| 68 | className="btn btn-sm code-copy" |
| 69 | > |
| 70 | <Icon name={copied ? "check" : "copy"} className="icon" /> |
| 71 | <span className="code-copy-label">{copied ? copiedWord : copyLabel}</span> |
| 72 | </button> |
| 73 | <span className="sr-only" aria-live="polite" lang={copyLocale}> |
| 74 | {/* The check mark is drawn by the icon; announce only the word. */} |
| 75 | {copied ? copiedWord : ""} |
| 76 | </span> |
| 77 | </div> |
| 78 | ); |
| 79 | } |
| 80 |