| 1 | "use client"; |
| 2 | |
| 3 | import { useCallback, useEffect, useRef, useState } from "react"; |
| 4 | import { usePathname } from "next/navigation"; |
| 5 | import { readBoundedBody } from "@/lib/bounded-body"; |
| 6 | import { Icon } from "./icon"; |
| 7 | |
| 8 | export function docsMarkdownSlug( |
| 9 | pathname: string, |
| 10 | locale: string, |
| 11 | availableSlugs: readonly string[], |
| 12 | ): string | null { |
| 13 | const normalized = pathname.replace(/\/+$/, ""); |
| 14 | const prefix = `/${locale}/docs/`; |
| 15 | if (!normalized.startsWith(prefix)) return null; |
| 16 | const slug = normalized.slice(prefix.length); |
| 17 | if (!slug || slug.includes("/")) return null; |
| 18 | return availableSlugs.includes(slug) ? slug : null; |
| 19 | } |
| 20 | |
| 21 | export function DocsCopyMarkdown({ |
| 22 | locale, |
| 23 | availableSlugs, |
| 24 | copiedLabel = "Copied", |
| 25 | }: { |
| 26 | locale: string; |
| 27 | availableSlugs: readonly string[]; |
| 28 | copiedLabel?: string; |
| 29 | }) { |
| 30 | const pathname = usePathname(); |
| 31 | const slug = docsMarkdownSlug(pathname, locale, availableSlugs); |
| 32 | const [copiedSlug, setCopiedSlug] = useState<string | null>(null); |
| 33 | const [busy, setBusy] = useState(false); |
| 34 | const cache = useRef<Map<string, string>>(new Map()); |
| 35 | const timer = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 36 | const copied = slug !== null && copiedSlug === slug; |
| 37 | |
| 38 | useEffect( |
| 39 | () => () => { |
| 40 | if (timer.current) clearTimeout(timer.current); |
| 41 | }, |
| 42 | [], |
| 43 | ); |
| 44 | |
| 45 | const copy = useCallback(async () => { |
| 46 | if (!slug || busy || typeof navigator === "undefined" || !navigator.clipboard) return; |
| 47 | setBusy(true); |
| 48 | try { |
| 49 | let markdown = cache.current.get(slug); |
| 50 | if (markdown === undefined) { |
| 51 | const response = await fetch(`/docs-markdown/${encodeURIComponent(slug)}.md`, { |
| 52 | credentials: "omit", |
| 53 | referrerPolicy: "no-referrer", |
| 54 | signal: AbortSignal.timeout(8000), |
| 55 | }); |
| 56 | if (!response.ok) return; |
| 57 | const bytes = await readBoundedBody(response, 2 * 1024 * 1024); |
| 58 | markdown = new TextDecoder("utf-8", { fatal: true }).decode(bytes); |
| 59 | cache.current.set(slug, markdown); |
| 60 | } |
| 61 | await navigator.clipboard.writeText(markdown); |
| 62 | setCopiedSlug(slug); |
| 63 | if (timer.current) clearTimeout(timer.current); |
| 64 | timer.current = setTimeout(() => setCopiedSlug(null), 1400); |
| 65 | } catch { |
| 66 | setCopiedSlug(null); |
| 67 | } finally { |
| 68 | setBusy(false); |
| 69 | } |
| 70 | }, [slug, busy]); |
| 71 | |
| 72 | if (!slug) return null; |
| 73 | |
| 74 | const copyLabel = locale === "zh" ? "复制 Markdown" : "Copy Markdown"; |
| 75 | const copiedWord = copiedLabel.replace(/\s*✓\s*$/u, ""); |
| 76 | |
| 77 | return ( |
| 78 | <div className="docs-copy-markdown"> |
| 79 | <button |
| 80 | type="button" |
| 81 | onClick={() => void copy()} |
| 82 | disabled={busy} |
| 83 | data-copied={copied} |
| 84 | className="btn btn-sm docs-copy-markdown-btn" |
| 85 | > |
| 86 | <Icon name={copied ? "check" : "copy"} className="icon" /> |
| 87 | <span>{copied ? copiedWord : copyLabel}</span> |
| 88 | </button> |
| 89 | <span className="sr-only" aria-live="polite"> |
| 90 | {copied ? copiedWord : ""} |
| 91 | </span> |
| 92 | </div> |
| 93 | ); |
| 94 | } |
| 95 |