Refactor <CopyIconButton>
to TypeScript (#34344)
This commit is contained in:
parent
6e607f97a3
commit
063030df82
1 changed files with 16 additions and 14 deletions
|
@ -1,29 +1,36 @@
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
import { useState, useCallback } from 'react';
|
import { useState, useCallback } from 'react';
|
||||||
|
|
||||||
import { defineMessages } from 'react-intl';
|
import { defineMessages } from 'react-intl';
|
||||||
|
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
|
||||||
import { useDispatch } from 'react-redux';
|
|
||||||
|
|
||||||
import ContentCopyIcon from '@/material-icons/400-24px/content_copy.svg?react';
|
import ContentCopyIcon from '@/material-icons/400-24px/content_copy.svg?react';
|
||||||
import { showAlert } from 'mastodon/actions/alerts';
|
import { showAlert } from 'mastodon/actions/alerts';
|
||||||
import { IconButton } from 'mastodon/components/icon_button';
|
import { IconButton } from 'mastodon/components/icon_button';
|
||||||
|
import { useAppDispatch } from 'mastodon/store';
|
||||||
|
|
||||||
const messages = defineMessages({
|
const messages = defineMessages({
|
||||||
copied: { id: 'copy_icon_button.copied', defaultMessage: 'Copied to clipboard' },
|
copied: {
|
||||||
|
id: 'copy_icon_button.copied',
|
||||||
|
defaultMessage: 'Copied to clipboard',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const CopyIconButton = ({ title, value, className }) => {
|
export const CopyIconButton: React.FC<{
|
||||||
|
title: string;
|
||||||
|
value: string;
|
||||||
|
className: string;
|
||||||
|
}> = ({ title, value, className }) => {
|
||||||
const [copied, setCopied] = useState(false);
|
const [copied, setCopied] = useState(false);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
const handleClick = useCallback(() => {
|
const handleClick = useCallback(() => {
|
||||||
navigator.clipboard.writeText(value);
|
void navigator.clipboard.writeText(value);
|
||||||
setCopied(true);
|
setCopied(true);
|
||||||
dispatch(showAlert({ message: messages.copied }));
|
dispatch(showAlert({ message: messages.copied }));
|
||||||
setTimeout(() => setCopied(false), 700);
|
setTimeout(() => {
|
||||||
|
setCopied(false);
|
||||||
|
}, 700);
|
||||||
}, [setCopied, value, dispatch]);
|
}, [setCopied, value, dispatch]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -31,13 +38,8 @@ export const CopyIconButton = ({ title, value, className }) => {
|
||||||
className={classNames(className, copied ? 'copied' : 'copyable')}
|
className={classNames(className, copied ? 'copied' : 'copyable')}
|
||||||
title={title}
|
title={title}
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
|
icon=''
|
||||||
iconComponent={ContentCopyIcon}
|
iconComponent={ContentCopyIcon}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
CopyIconButton.propTypes = {
|
|
||||||
title: PropTypes.string,
|
|
||||||
value: PropTypes.string,
|
|
||||||
className: PropTypes.string,
|
|
||||||
};
|
|
Loading…
Add table
Add a link
Reference in a new issue