Files
pezkuwi-apps/packages/react-components/src/CopyButton.tsx
T
Satoshi Qazi Muhammed 45d479d2ec Move to @pezkuwi/dev 0.86.4 (eslint 10 toolchain)
- dev ^0.86.4 at the root and in packages/apps; directives renamed by
  pezkuwi-dev-migrate-eslint10.
- eslint --fix with the 0.86.4 config; one fix broke syntax
  (typescript-eslint prefer-optional-chain turned
  `(isStarted && onStart) && onStart()` into `(isStarted && onStart?.();`
  in TxButton) and is corrected by hand.
- The remaining findings fixed rather than disabled: optional chains
  (each suggestion reviewed), `cause` on rethrown errors, `if` instead of
  `a && promise` in effects, parallel instead of sequential awaits in
  Promise.all, Error rejections, no `delete` on arrays (the storage query
  cache is a Map; an upload's file list no longer keeps a hole), no
  `[object Object]` stringification (TFunction's second argument is text
  or options), dead code removed (referenda's never-shown expand button,
  react-query's unused CoreDescriptor).
2026-10-10 02:06:12 +04:00

77 lines
1.8 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { IconName } from '@fortawesome/fontawesome-svg-core';
import React, { useCallback } from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';
import { useQueue } from '@pezkuwi/react-hooks';
import { isString } from '@pezkuwi/util';
import Button from './Button/index.js';
import { styled } from './styled.js';
import { useTranslation } from './translate.js';
interface Props {
children?: React.ReactNode;
className?: string;
icon?: IconName;
label?: React.ReactNode;
type?: string;
isMnemonic?: boolean;
value?: React.ReactNode | null;
}
const NOOP = () => undefined;
function CopyButton ({ children, className = '', icon = 'copy', label, type, value }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { queueAction } = useQueue();
const _onCopy = useCallback(
(): void => {
queueAction?.({
action: t('clipboard'),
message: t('{{type}} copied', { replace: { type: type || t('value') } }),
status: 'queued'
});
},
[type, queueAction, t]
);
if (!isString(value)) {
return null;
}
return (
<StyledDiv className={`${className} ui--CopyButton`}>
<CopyToClipboard
onCopy={_onCopy}
text={value as string}
>
<div className='copyContainer'>
{children}
<span className='copySpan'>
<Button
className='icon-button show-on-hover'
icon={icon}
isDisabled={!value}
label={label}
onClick={NOOP}
/>
</span>
</div>
</CopyToClipboard>
</StyledDiv>
);
}
const StyledDiv = styled.div`
.copySpan {
white-space: nowrap;
}
`;
export default React.memo(CopyButton);