feat(pages): copy button for page addresses #54

Merged
ngqile merged 1 commits from pages-copy-address into main 2026-12-02 09:30:03 +00:00
5 changed files with 67 additions and 2 deletions

No files matched your search

@@ -0,0 +1,36 @@
"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). */
export function CopyAddress({ address }: { address: string }) {
const t = useT();
const [state, setState] = useState<"idle" | "copied" | "failed">("idle");
async function copy() {
try {
await navigator.clipboard.writeText(`${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>
);
}
@@ -35,6 +35,15 @@ beforeEach(() => {
});
describe("PageEditor", () => {
it("copies the saved page's full address", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
await screen.findByLabelText("Event");
const write = vi.spyOn(navigator.clipboard, "writeText");
await user.click(screen.getByRole("button", { name: "Copy link /p/INS/hotel" }));
expect(write).toHaveBeenCalledWith(`${window.location.origin}/p/INS/hotel`);
});
it("names the rich text box after its field label", async () => {
const user = userEvent.setup();
render(<PageEditor pageId={5} />);
@@ -14,6 +14,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import { ApiError } from "@/app/lib/clientApi";
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
import { useCompanyScope } from "@/app/lib/CompanyScope";
import { CopyAddress } from "./CopyAddress";
import { BLOCK_LABELS, BLOCK_TYPES, emptyBlock, emptyPage, pageAddress, pageBody, withTitle, type CmsBlock, type CmsPageDraft, type PageStatus } from "@/app/lib/cmsPages";
import { deletePage, getPage, savePage, uploadPageImage } from "@/app/lib/services/adminPages";
import { listAdminEvents } from "@/app/lib/services/adminEvents";
@@ -198,7 +199,8 @@ export function PageEditor({ pageId }: { pageId?: number }) {
return (
<div className="space-y-4">
<div className="flex flex-wrap justify-end gap-2">
<div className="flex flex-wrap items-center justify-end gap-2">
{saved && code ? <span className="mr-auto"><CopyAddress address={pageAddress(code, saved.slug)} /></span> : null}
{draft.id != null ? (
<Button variant="outline" asChild>
<Link href={`/pages/${draft.id}/preview`}>{t("PagesPreview")}</Link>
@@ -17,6 +17,23 @@ beforeEach(() => {
});
describe("PagesPage", () => {
it("copies a page's full address and reports a clipboard failure", async () => {
const user = userEvent.setup();
mocks.listPages.mockResolvedValue({
items: [{ id: 5, companyId: 2, slug: "hotel-tech", title: "Hotel tech", status: "PUBLISHED", metaDescription: null, blocks: [], updatedAt: "2026-12-02T10:15:00" }],
total: 1,
});
render(<PagesPage />);
const write = vi.spyOn(navigator.clipboard, "writeText");
await user.click(await screen.findByRole("button", { name: "Copy link /p/INS/hotel-tech" }));
expect(write).toHaveBeenCalledWith(`${window.location.origin}/p/INS/hotel-tech`);
expect(await screen.findByText("Link copied")).toBeInTheDocument();
write.mockRejectedValueOnce(new Error("denied"));
await user.click(screen.getByRole("button", { name: "Copy link /p/INS/hotel-tech" }));
expect(await screen.findByText("Could not copy.")).toBeInTheDocument();
});
it("lists title, address, status and update time", async () => {
mocks.listPages.mockResolvedValue({
items: [{ id: 5, companyId: 2, slug: "hotel-tech", title: "Hotel tech", status: "PUBLISHED", metaDescription: null, blocks: [], updatedAt: "2026-12-02T10:15:00" }],
+2 -1
View File
@@ -18,6 +18,7 @@ import { useFitPageSize, MIN_PAGE_SIZE } from "@/app/lib/hooks/useFitPageSize";
import { useCompanyScope } from "@/app/lib/CompanyScope";
import { listPages } from "@/app/lib/services/adminPages";
import { formatEmailDate } from "@/app/lib/services/adminEmails";
import { CopyAddress } from "@/app/components/pages/CopyAddress";
import { pageAddress, type CmsPage } from "@/app/lib/cmsPages";
import { useT } from "@/app/lib/i18n/MessagesProvider";
@@ -63,7 +64,7 @@ function PagesPageContent() {
{
id: "address",
header: t("PagesAddress"),
cell: ({ row }) => <span className="font-mono text-xs">{pageAddress(codeOf(row.original.companyId) || companyCode || "", row.original.slug)}</span>,
cell: ({ row }) => <CopyAddress address={pageAddress(codeOf(row.original.companyId) || companyCode || "", row.original.slug)} />,
},
{
accessorKey: "status",