Skip to content

The open alternative to Salesforce, designed for AI.

1.7 KB
Raw Blame
import { EditLinkDropdownForm } from '@/advanced-text-editor/components/EditLinkDropdownForm';
import { type EditLinkEditor } from '@/advanced-text-editor/types/EditLinkEditor';
import { DropdownContent } from '@/ui/layout/dropdown/components/DropdownContent';
import { DropdownRoot } from '@/ui/layout/dropdown/components/DropdownRoot';
import { useLingui } from '@lingui/react/macro';
import { isNonEmptyString } from '@sniptt/guards';
import { LightIconButton } from 'twenty-ui/components/input';
import { Dropdown } from 'twenty-ui/components/navigation';
import { IconLink, IconPencil } from 'twenty-ui/icon';

type EditLinkDropdownProps = {
  dropdownId: string;
  defaultValue: string | undefined;
  editor: EditLinkEditor;
};

export const EditLinkDropdown = ({
  dropdownId,
  defaultValue = '',
  editor,
}: EditLinkDropdownProps) => {
  const isActive = isNonEmptyString(defaultValue);
  const { t } = useLingui();

  return (
    <DropdownRoot dropdownId={dropdownId} type="panel">
      <Dropdown.Trigger
        render={
          <LightIconButton
            aria-label={isActive ? t`Edit link` : t`Add link`}
            aria-pressed={isActive}
            variant={isActive ? 'soft' : 'ghost'}
            size="sm"
          >
            {isActive ? <IconPencil /> : <IconLink />}
          </LightIconButton>
        }
      />
      <DropdownContent
        sideOffset={0}
        align="end"
        finalFocus={(interaction) =>
          interaction === 'keyboard' ? editor.view.dom : false
        }
        aria-label={isActive ? t`Edit link` : t`Add link`}
      >
        <Dropdown.Section>
          <EditLinkDropdownForm defaultValue={defaultValue} editor={editor} />
        </Dropdown.Section>
      </DropdownContent>
    </DropdownRoot>
  );
};