Files
twenty/packages/twenty-front/src/hooks/useCopyToClipboard.tsx
T
Nabhag Motivaras 316f2ec38c refactor: to useCopyToClipboard to catch errors - when user has disable copy clipboard permission in browser (#13330)
as discussed here
https://github.com/twentyhq/twenty/issues/13292#issuecomment-3092215050
with @prastoin

- [x] introduce optional message param to `copyToClipboard` method in
`useCopyToClipboard` and refactor it across app, as in case if user has
disallowed clipboard permission in BROWSER unprotected clipboard access
breaks without catch.

- [x] Email copied to clipboard

- [x] run lingui extract
2025-07-30 11:13:55 +02:00

35 lines
1.0 KiB
TypeScript

import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { useTheme } from '@emotion/react';
import { useLingui } from '@lingui/react/macro';
import { IconCopy, IconExclamationCircle } from 'twenty-ui/display';
export const useCopyToClipboard = () => {
const theme = useTheme();
const { enqueueSuccessSnackBar, enqueueErrorSnackBar } = useSnackBar();
const { t } = useLingui();
const copyToClipboard = async (valueAsString: string, message?: string) => {
try {
await navigator.clipboard.writeText(valueAsString);
enqueueSuccessSnackBar({
message: message || t`Copied to clipboard`,
options: {
icon: <IconCopy size={theme.icon.size.md} />,
duration: 2000,
},
});
} catch {
enqueueErrorSnackBar({
message: t`Couldn't copy to clipboard`,
options: {
icon: <IconExclamationCircle size={16} color="red" />,
duration: 2000,
},
});
}
};
return { copyToClipboard };
};