summaryrefslogtreecommitdiff
path: root/apps/web/src/changelog/components/copy-markdown-button.tsx
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2026-04-20 11:31:17 +0200
committerMaze Winther <mazewinther@gmail.com>2026-04-20 11:31:17 +0200
commit3e89d2998588f8e35c524e523ba2ee853f0f6475 (patch)
tree57221f96c1259b3b07e3ddf8c1adefc502b9d8eb /apps/web/src/changelog/components/copy-markdown-button.tsx
parent729d10592f4e802f91c555c309ff3ba4b119f843 (diff)
downloadopencut-3e89d2998588f8e35c524e523ba2ee853f0f6475.tar.gz
refactor: restructure files to their domains, new preview overlay system, and dep graph
Diffstat (limited to 'apps/web/src/changelog/components/copy-markdown-button.tsx')
-rw-r--r--apps/web/src/changelog/components/copy-markdown-button.tsx98
1 files changed, 98 insertions, 0 deletions
diff --git a/apps/web/src/changelog/components/copy-markdown-button.tsx b/apps/web/src/changelog/components/copy-markdown-button.tsx
new file mode 100644
index 00000000..bc7e94bf
--- /dev/null
+++ b/apps/web/src/changelog/components/copy-markdown-button.tsx
@@ -0,0 +1,98 @@
+"use client";
+
+import { useState } from "react";
+import { CheckIcon, ClipboardIcon } from "lucide-react";
+import {
+ getSectionTitle,
+ groupAndOrderChanges,
+ isSectionCollapsible,
+} from "../utils";
+import type { Change } from "../utils";
+import { cn } from "@/utils/ui";
+import { Button } from "@/components/ui/button";
+
+function buildMarkdown({
+ description,
+ changes,
+}: {
+ description?: string;
+ changes: Change[];
+}): string {
+ const lines: string[] = [];
+
+ if (description) {
+ lines.push(description, "");
+ }
+
+ const { grouped, orderedTypes } = groupAndOrderChanges({ changes });
+
+ for (const type of orderedTypes) {
+ if (isSectionCollapsible({ type })) {
+ lines.push(
+ buildCollapsibleMarkdownSection({
+ title: getSectionTitle({ type }),
+ changes: grouped[type],
+ }),
+ "",
+ );
+ continue;
+ }
+
+ lines.push(`## ${getSectionTitle({ type })}`);
+ for (const change of grouped[type]) {
+ lines.push(`- ${change.text}`);
+ }
+ lines.push("");
+ }
+
+ return lines.join("\n").trimEnd();
+}
+
+function buildCollapsibleMarkdownSection({
+ title,
+ changes,
+}: {
+ title: string;
+ changes: Change[];
+}): string {
+ const bulletLines = changes.map((change) => `- ${change.text}`).join("\n");
+
+ return `<details>\n<summary>${title}</summary>\n\n${bulletLines}\n\n</details>`;
+}
+
+export function CopyMarkdownButton({
+ description,
+ changes,
+}: {
+ description?: string;
+ changes: Change[];
+}) {
+ const [copied, setCopied] = useState(false);
+
+ const handleCopy = async () => {
+ const markdown = buildMarkdown({ description, changes });
+ await navigator.clipboard.writeText(markdown);
+ setCopied(true);
+ setTimeout(() => setCopied(false), 2000);
+ };
+
+ return (
+ <Button
+ size="sm"
+ variant="text"
+ onClick={handleCopy}
+ className={cn(
+ "flex items-center gap-1.5",
+ copied && "pointer-events-none",
+ )}
+ title="Copy as markdown"
+ >
+ {copied ? (
+ <CheckIcon className="size-4" />
+ ) : (
+ <ClipboardIcon className="size-4" />
+ )}
+ {copied ? "Copied!" : "Copy markdown"}
+ </Button>
+ );
+}