"use client"; import { useState } from "react"; type CopyState = "idle" | "copied" | "error"; type Props = { markdownUrl: string; mdxSource: string; }; export function DocsPageActions({ markdownUrl, mdxSource }: Props) { return (
View MD
); } function CopyMarkdownButton({ markdownUrl }: { markdownUrl: string }) { const [state, setState] = useState("idle"); const onClick = async () => { try { const response = await fetch(markdownUrl, { headers: { Accept: "text/markdown" }, }); if (!response.ok) { throw new Error(`Failed to fetch ${markdownUrl}`); } await navigator.clipboard.writeText(await response.text()); flash(setState, "copied"); } catch { flash(setState, "error"); } }; return ( ); } function CopyTextButton({ label, text }: { label: string; text: string }) { const [state, setState] = useState("idle"); const onClick = async () => { try { await navigator.clipboard.writeText(text); flash(setState, "copied"); } catch { flash(setState, "error"); } }; return ( ); } function ActionButton({ label, onClick, state, }: { label: string; onClick: () => void; state: CopyState; }) { return ( ); } function labelForState(state: CopyState, label: string) { if (state === "copied") return "Copied"; if (state === "error") return "Copy failed"; return label; } function flash( setState: (state: CopyState) => void, state: Exclude, ) { setState(state); window.setTimeout(() => setState("idle"), 1500); }