Skip to content

The open alternative to Salesforce, designed for AI.

1.7 KB
Raw Blame
import { useLingui } from '@lingui/react/macro';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
import { IconCopy, IconExclamationCircle } from 'twenty-ui/icon';
import { useTheme } from 'twenty-ui/theme';
export const useCopyToClipboard = () => {
  const theme = useTheme();
  const { enqueueToast } = useToast();
  const { t } = useLingui();

  const writeToClipboard = async (
    valueAsString: string,
    successMessage: string | null,
  ) => {
    if (!window.isSecureContext) {
      enqueueToast({
        status: 'error',
        children: t`Clipboard requires a secure connection (HTTPS). Please access this app over HTTPS to enable copying.`,
        icon: <IconExclamationCircle size={16} color="red" />,
        duration: 6000,
      });

      return;
    }

    try {
      await navigator.clipboard.writeText(valueAsString);

      if (!isDefined(successMessage)) {
        return;
      }

      enqueueToast({
        status: 'success',
        children: successMessage,
        icon: <IconCopy size={theme.icon.size.md} />,
        duration: 2000,
      });
    } catch {
      enqueueToast({
        status: 'error',
        children: t`Couldn't copy to clipboard`,
        icon: <IconExclamationCircle size={16} color="red" />,
        duration: 2000,
      });
    }
  };

  const copyToClipboard = async (valueAsString: string, message?: string) =>
    await writeToClipboard(valueAsString, message || t`Copied to clipboard`);

  const copyToClipboardWithoutSuccessToast = async (valueAsString: string) =>
    await writeToClipboard(valueAsString, null);

  return { copyToClipboard, copyToClipboardWithoutSuccessToast };
};