Merge pull request #249 from Monadical-SAS/copy-final-summary

adds copy button for final summary
This commit is contained in:
Sara
2023-09-26 17:42:05 +02:00
committed by GitHub
2 changed files with 38 additions and 7 deletions

View File

@@ -1,12 +1,41 @@
import { useRef, useState } from "react";
type FinalSummaryProps = { type FinalSummaryProps = {
text: string; text: string;
}; };
export default function FinalSummary(props: FinalSummaryProps) { export default function FinalSummary(props: FinalSummaryProps) {
const finalSummaryRef = useRef<HTMLParagraphElement>(null);
const [isCopied, setIsCopied] = useState(false);
const handleCopyClick = () => {
let text_to_copy = finalSummaryRef.current?.innerText;
text_to_copy &&
navigator.clipboard.writeText(text_to_copy).then(() => {
setIsCopied(true);
// Reset the copied state after 2 seconds
setTimeout(() => setIsCopied(false), 2000);
});
};
return ( return (
<div className="overflow-y-auto h-auto max-h-full"> <div className="overflow-y-auto h-auto max-h-full">
<h2 className="text-xl md:text-2xl font-bold">Final Summary</h2> <div className="flex flex-row justify-between items-center">
<p>{props.text}</p> <h2 className="text-xl md:text-2xl font-bold">Final Summary</h2>
<button
onClick={handleCopyClick}
className={
(isCopied ? "bg-blue-500" : "bg-blue-400") +
" hover:bg-blue-500 focus-visible:bg-blue-500 text-white rounded p-2"
}
style={{ minHeight: "30px" }}
>
{isCopied ? "Copied!" : "Copy"}
</button>
</div>
<p ref={finalSummaryRef}>{props.text}</p>
</div> </div>
); );
} }

View File

@@ -11,12 +11,14 @@ const ShareLink = () => {
const handleCopyClick = () => { const handleCopyClick = () => {
if (inputRef.current) { if (inputRef.current) {
inputRef.current.select(); let text_to_copy = inputRef.current.value;
document.execCommand("copy");
setIsCopied(true);
// Reset the copied state after 2 seconds text_to_copy &&
setTimeout(() => setIsCopied(false), 2000); navigator.clipboard.writeText(text_to_copy).then(() => {
setIsCopied(true);
// Reset the copied state after 2 seconds
setTimeout(() => setIsCopied(false), 2000);
});
} }
}; };