Skip to content
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

Dashboard API key copy button #3450

Merged
merged 3 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dashboard/src/assets/constants/copyTextConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SHOW_COPIED_TEXT_MS = 4000;
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;
14 changes: 4 additions & 10 deletions dashboard/src/modules/components/ProfileComponent/KeyListTable.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Button, ClipboardCopy } from "@patternfly/react-core";
import {
TableComposable,
Tbody,
Expand All @@ -9,6 +8,8 @@ import {
} from "@patternfly/react-table";
import { useDispatch, useSelector } from "react-redux";

import { Button } from "@patternfly/react-core";
import ClipboardCopy from "./ClipboardCopy";
import React from "react";
import { TrashIcon } from "@patternfly/react-icons";
import { deleteAPIKey } from "actions/keyManagementActions";
Expand All @@ -22,7 +23,6 @@ const KeyListTable = () => {
created: "Created Date & Time",
key: "API key",
};

return (
<TableComposable aria-label="key list table" isStriped>
<Thead>
Expand All @@ -40,14 +40,8 @@ const KeyListTable = () => {
<Td dataLabel={columnNames.created}>
{formatDateTime(item.created)}
</Td>
<Td dataLabel={columnNames.key} className="key-cell">
<ClipboardCopy
hoverTip="Copy API key"
clickTip="Copied"
variant="plain"
>
{item.key}
</ClipboardCopy>
<Td dataLabel={columnNames.key}>
<ClipboardCopy copyText={item.key} />
</Td>

<Td className="delete-icon-cell">
Expand Down
7 changes: 7 additions & 0 deletions dashboard/src/modules/components/ProfileComponent/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
display: block;
text-overflow: ellipsis;
}
.key-cell-wrapper {
display: flex;
.copy-icon {
padding-top: 0;
}
}

.pf-c-clipboard-copy__group {
input {
background-color: transparent;
Expand Down