Skip to content

The open alternative to Salesforce, designed for AI.

2.3 KB
Raw Blame
import { BubbleMenuIconButton } from '@/advanced-text-editor/components/BubbleMenuIconButton';
import { EditLinkDropdown } from '@/advanced-text-editor/components/EditLinkDropdown';
import { StyledBubbleMenuContainer } from '@/advanced-text-editor/components/StyledBubbleMenuContainer';
import { useLiveEditorState } from '@/advanced-text-editor/hooks/useLiveEditorState';
import { getEditLinkDropdownId } from '@/advanced-text-editor/utils/getEditLinkDropdownId';
import { type Editor } from '@tiptap/core';
import { BubbleMenu } from '@tiptap/react/menus';
import { IconExternalLink, IconLinkOff } from 'twenty-ui/icon';
import { getSafeUrl } from 'twenty-shared/utils';
import { openUrlInNewTab } from '~/utils/openUrlInNewTab';
import { useLingui } from '@lingui/react/macro';

type LinkBubbleMenuProps = {
  editor: Editor;
};

export const LinkBubbleMenu = ({ editor }: LinkBubbleMenuProps) => {
  const { t } = useLingui();
  const state = useLiveEditorState(editor, (currentEditor) => {
    return {
      linkHref: currentEditor.getAttributes('link').href || '',
    };
  });

  const handleShouldShow = () => {
    return editor.isActive('link');
  };

  const menuActions = [
    {
      Icon: IconExternalLink,
      label: t`Open link`,
      onClick: () => {
        const safeHref = getSafeUrl(state.linkHref);

        if (safeHref) {
          openUrlInNewTab(safeHref);
        }
      },
    },
    {
      Icon: IconLinkOff,
      label: t`Remove link`,
      onClick: () =>
        editor.chain().focus().extendMarkRange('link').unsetLink().run(),
    },
  ];

  return (
    <BubbleMenu
      pluginKey="link-bubble-menu"
      editor={editor}
      shouldShow={handleShouldShow}
      updateDelay={0}
    >
      <StyledBubbleMenuContainer>
        <EditLinkDropdown
          dropdownId={getEditLinkDropdownId({
            editorInstanceId: editor.instanceId,
            bubbleMenuType: 'link',
          })}
          defaultValue={state.linkHref}
          editor={editor}
        />
        {menuActions.map(({ label, Icon, onClick }) => {
          return (
            <BubbleMenuIconButton
              key={Icon.name || Icon.displayName || 'unknown'}
              label={label}
              Icon={Icon}
              onClick={onClick}
            />
          );
        })}
      </StyledBubbleMenuContainer>
    </BubbleMenu>
  );
};