-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dashboard API key copy button (#3450)
* PBENCH-1166 Dashboard API key button
- Loading branch information
Showing
4 changed files
with
64 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SHOW_COPIED_TEXT_MS = 4000; |
52 changes: 52 additions & 0 deletions
52
dashboard/src/modules/components/ProfileComponent/ClipboardCopy.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Button, Tooltip } from "@patternfly/react-core"; | ||
import React, { useState } from "react"; | ||
|
||
import { CopyIcon } from "@patternfly/react-icons"; | ||
import { SHOW_COPIED_TEXT_MS } from "assets/constants/copyTextConstants"; | ||
|
||
const ClipboardCopy = ({ copyText }) => { | ||
const [isCopied, setIsCopied] = useState(false); | ||
|
||
/* Funcion has to be rewritten by removing document.execCommand() on upgrading to HTTPS */ | ||
const copyTextToClipboard = async (text) => | ||
"clipboard" in navigator | ||
? await navigator.clipboard.writeText(text) | ||
: document.execCommand("copy", true, text); | ||
|
||
// onClick handler function for the copy button | ||
const handleCopyClick = () => { | ||
// Asynchronously call copyTextToClipboard | ||
copyTextToClipboard(copyText) | ||
.then(() => { | ||
// If successful, update the isCopied state value | ||
setIsCopied(true); | ||
setTimeout(() => { | ||
setIsCopied(false); | ||
}, SHOW_COPIED_TEXT_MS); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className="key-cell-wrapper"> | ||
<div className="key-cell">{copyText}</div> | ||
<Tooltip | ||
aria="none" | ||
aria-live="polite" | ||
content={isCopied ? "Copied" : "Copy to clipboard"} | ||
> | ||
<Button | ||
variant="plain" | ||
className="copy-icon" | ||
onClick={() => handleCopyClick(copyText)} | ||
> | ||
<CopyIcon /> | ||
</Button> | ||
</Tooltip> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ClipboardCopy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters