Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { Check, Copy } from "lucide-react";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
/** A page address with a button that copies its full URL (this site's origin plus the path), or the path itself when `relative`. */
|
|
export function CopyAddress({ address, relative }: { address: string; relative?: boolean }) {
|
|
const t = useT();
|
|
const [state, setState] = useState<"idle" | "copied" | "failed">("idle");
|
|
|
|
async function copy() {
|
|
try {
|
|
await navigator.clipboard.writeText(relative ? address : `${window.location.origin}${address}`);
|
|
setState("copied");
|
|
setTimeout(() => setState("idle"), 2000);
|
|
} catch {
|
|
setState("failed");
|
|
}
|
|
}
|
|
|
|
return (
|
|
<span className="inline-flex items-center gap-1">
|
|
<span className="font-mono text-xs">{address}</span>
|
|
<Button type="button" variant="ghost" size="icon" className="size-7" aria-label={`${t("QrCopy")} ${address}`} title={t("QrCopy")} onClick={copy}>
|
|
{state === "copied" ? <Check className="size-3.5" aria-hidden /> : <Copy className="size-3.5" aria-hidden />}
|
|
</Button>
|
|
{state !== "idle" ? (
|
|
<span role="status" className={state === "failed" ? "text-xs text-destructive" : "text-xs text-muted-foreground"}>
|
|
{t(state === "copied" ? "QrCopied" : "CopyFailed")}
|
|
</span>
|
|
) : null}
|
|
</span>
|
|
);
|
|
}
|