Skip to content

Commit

Permalink
Restore ClipboardButton to work in non-secure contexts. (#19654) (#…
Browse files Browse the repository at this point in the history
…19665)

* Restore `ClipboardButton` to work in non-secure contexts.

* Adding changelog snippet.

* Better changelog messages.

* Using `Tooltip` abstraction.
  • Loading branch information
dennisoelkers committed Jun 18, 2024
1 parent a20c863 commit 01ed0fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/issue-19643.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type="f"
message="Fixing copying to clipboard in non-secure contexts."

issues=["19643"]
pulls=["19654"]
25 changes: 23 additions & 2 deletions graylog2-web-interface/src/components/common/ClipboardButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import React from 'react';
import { CopyButton, Tooltip } from '@mantine/core';
import * as React from 'react';
import { useCallback, useState } from 'react';
import { useTimeout } from '@mantine/hooks';

import { Button } from 'components/bootstrap';
import type { BsSize } from 'components/bootstrap/types';
import type { StyleProps } from 'components/bootstrap/Button';
import copyToClipboard from 'util/copyToClipboard';
import Tooltip from 'components/common/Tooltip';

/**
* Component that renders a button to copy some text in the clipboard when pressed.
Expand All @@ -37,6 +40,24 @@ type Props = {
title: React.ReactNode,
}

type Args = {
copied: boolean,
copy: () => void,
}
type CopyButtonProps = {
value: string,
timeout: number,
children: (args: Args) => React.ReactElement,
};

const CopyButton = ({ children, value, timeout }: CopyButtonProps) => {
const [copied, setCopied] = useState(false);
const { start } = useTimeout(() => setCopied(false), timeout);
const copy = useCallback(() => copyToClipboard(value).then(() => { setCopied(true); start(); }), [start, value]);

return children({ copied, copy });
};

const ClipboardButton = ({ bsSize, bsStyle, buttonTitle, className, disabled, onSuccess, text, title }: Props) => {
const button = (copy: () => void) => (
<Button bsSize={bsSize}
Expand Down

0 comments on commit 01ed0fd

Please sign in to comment.