import { useState, useCallback } from "preact/hooks";
import { clipboardCopy } from "../../../shared/utils/clipboard";
import { useT } from "../../../shared/i18n/context";
import type { TranslationKey } from "../../../shared/i18n/translations";
interface CopyButtonProps {
getText: () => string;
class?: string;
titleKey?: string;
variant?: "icon" | "label";
}
const SVG_COPY = (
);
const SVG_CHECK = (
);
const SVG_FAIL = (
);
export function CopyButton({ getText, class: className, titleKey, variant = "icon" }: CopyButtonProps) {
const t = useT();
const [state, setState] = useState<"idle" | "ok" | "fail">("idle");
const handleCopy = useCallback(async () => {
const ok = await clipboardCopy(getText());
setState(ok ? "ok" : "fail");
setTimeout(() => setState("idle"), 2000);
}, [getText]);
if (variant === "label") {
const bgClass =
state === "ok"
? "bg-primary hover:bg-primary-hover"
: state === "fail"
? "bg-red-600 hover:bg-red-700"
: "bg-slate-700 hover:bg-slate-600";
return (
);
}
return (
);
}