-
Notifications
You must be signed in to change notification settings - Fork 620
[Portal] Feature: Add Copy Page button to documentation pages #8421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||||||
| "use client"; | ||||||||||||||||||
|
|
||||||||||||||||||
| import { CheckIcon, CopyIcon } from "lucide-react"; | ||||||||||||||||||
| import { useState } from "react"; | ||||||||||||||||||
| import { Button } from "../ui/button"; | ||||||||||||||||||
|
|
||||||||||||||||||
| export function CopyPageButton() { | ||||||||||||||||||
| const [copied, setCopied] = useState(false); | ||||||||||||||||||
|
|
||||||||||||||||||
| const handleCopy = async () => { | ||||||||||||||||||
| try { | ||||||||||||||||||
| // Get the main content element | ||||||||||||||||||
| const mainElement = document.querySelector("main"); | ||||||||||||||||||
| if (!mainElement) { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Get text content, preserving some structure | ||||||||||||||||||
| const textContent = mainElement.innerText || mainElement.textContent || ""; | ||||||||||||||||||
|
|
||||||||||||||||||
| await navigator.clipboard.writeText(textContent); | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major: Check clipboard API availability before using. The Apply this diff to add a feature check: + // Check if clipboard API is available
+ if (!navigator.clipboard) {
+ console.error("Clipboard API not available");
+ return;
+ }
+
await navigator.clipboard.writeText(textContent);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| setCopied(true); | ||||||||||||||||||
|
|
||||||||||||||||||
| // Reset after 2 seconds | ||||||||||||||||||
| setTimeout(() => { | ||||||||||||||||||
| setCopied(false); | ||||||||||||||||||
| }, 2000); | ||||||||||||||||||
| } catch (error) { | ||||||||||||||||||
| console.error("Failed to copy page content:", error); | ||||||||||||||||||
| } | ||||||||||||||||||
| }; | ||||||||||||||||||
|
Comment on lines
+10
to
+31
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Clean up setTimeout to prevent memory leaks. The setTimeout on lines 25-27 is not cleaned up if the component unmounts before the 2-second delay completes. This will cause React warnings about state updates on unmounted components and potential memory leaks. Apply this diff to fix the cleanup issue: export function CopyPageButton() {
const [copied, setCopied] = useState(false);
+ const timeoutRef = useRef<NodeJS.Timeout>();
const handleCopy = async () => {
try {
+ // Clear any existing timeout
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+
// Get the main content element
const mainElement = document.querySelector("main");
if (!mainElement) {
return;
}
// Get text content, preserving some structure
const textContent = mainElement.innerText || mainElement.textContent || "";
await navigator.clipboard.writeText(textContent);
setCopied(true);
// Reset after 2 seconds
- setTimeout(() => {
+ timeoutRef.current = setTimeout(() => {
setCopied(false);
}, 2000);
} catch (error) {
console.error("Failed to copy page content:", error);
}
};
+ useEffect(() => {
+ return () => {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current);
+ }
+ };
+ }, []);Don't forget to import -import { useState } from "react";
+import { useState, useRef, useEffect } from "react";
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| return ( | ||||||||||||||||||
| <Button | ||||||||||||||||||
| variant="outline" | ||||||||||||||||||
| size="sm" | ||||||||||||||||||
| onClick={handleCopy} | ||||||||||||||||||
| className="gap-2" | ||||||||||||||||||
| > | ||||||||||||||||||
| {copied ? ( | ||||||||||||||||||
| <> | ||||||||||||||||||
| <CheckIcon className="size-4" /> | ||||||||||||||||||
| Copied! | ||||||||||||||||||
| </> | ||||||||||||||||||
| ) : ( | ||||||||||||||||||
| <> | ||||||||||||||||||
| <CopyIcon className="size-4" /> | ||||||||||||||||||
| Copy Page | ||||||||||||||||||
| </> | ||||||||||||||||||
| )} | ||||||||||||||||||
| </Button> | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: Provide user feedback when main element is not found.
If the main element is not found, the function returns silently. Users won't know why the copy operation didn't work, which creates a poor user experience.
Consider showing a toast notification or brief error state:
const mainElement = document.querySelector("main"); if (!mainElement) { + console.error("Main content element not found"); + // Consider showing user feedback here (e.g., toast notification) return; }📝 Committable suggestion
🤖 Prompt for AI Agents