Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.2 KB
Raw Blame
'use client';

import { ActionIcon, css, cx, Icon, Upload } from '@lobehub/ui';
import { FileUp, LibraryBig, PlusIcon, TypeIcon } from 'lucide-react';
import { memo, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import type { ExistingEditorAttachment } from '@/features/EditorCanvas/editorAttachments';
import { openLibraryFilePicker } from '@/features/LibraryModal';

import ActionDropdown, {
  type ActionDropdownMenuItems,
} from '../ChatInput/ActionBar/components/ActionDropdown';
import SubmenuChevron from '../ChatInput/ActionBar/components/SubmenuChevron';

const hotArea = css`
  &::before {
    content: '';
    position: absolute;
    inset: 0;
    background: transparent;
  }
`;

interface AttachmentMenuProps {
  disabled?: boolean;
  formatEnabled: boolean;
  onFiles: (files: File[]) => void | Promise<void>;
  onFormatEnabledChange: (enabled: boolean) => void;
  onLibraryFiles: (attachments: ExistingEditorAttachment[]) => void;
}

const AttachmentMenu = memo<AttachmentMenuProps>(
  ({ disabled, formatEnabled, onFiles, onFormatEnabledChange, onLibraryFiles }) => {
    const { t } = useTranslation(['chat', 'editor', 'file']);
    const [open, setOpen] = useState(false);

    const items = useMemo<ActionDropdownMenuItems>(
      () => [
        {
          children: [
            {
              closeOnClick: false,
              icon: <Icon icon={FileUp} size={18} />,
              key: 'upload',
              label: (
                <Upload
                  multiple
                  onFiles={(files) => {
                    setOpen(false);
                    void onFiles(files);
                  }}
                >
                  <div className={cx(hotArea)}>
                    {t('upload.action.fileOrImageUpload', { ns: 'chat' })}
                  </div>
                </Upload>
              ),
            },
            {
              icon: <Icon icon={LibraryBig} size={18} />,
              key: 'library',
              label: t('pageEditor.comments.attachments.chooseLibrary', { ns: 'file' }),
              onClick: () => {
                setOpen(false);
                openLibraryFilePicker(onLibraryFiles);
              },
            },
          ],
          extra: <SubmenuChevron />,
          icon: LibraryBig,
          key: 'attachments',
          label: t('plus.addAttachments', { ns: 'chat' }),
        },
        { type: 'divider' },
        {
          checked: formatEnabled,
          icon: TypeIcon,
          key: 'formatting',
          label: t('actions.typobar.title', { ns: 'editor' }),
          onCheckedChange: onFormatEnabledChange,
          type: 'switch',
        },
      ],
      [formatEnabled, onFiles, onFormatEnabledChange, onLibraryFiles, t],
    );

    const title = t('pageEditor.comments.attachments.add', { ns: 'file' });

    return (
      <ActionDropdown menu={{ items }} open={open} placement={'topLeft'} onOpenChange={setOpen}>
        <ActionIcon
          aria-label={title}
          disabled={disabled}
          icon={PlusIcon}
          size={{ blockSize: 32, size: 18 }}
          title={title}
          onClick={() => setOpen(true)}
        />
      </ActionDropdown>
    );
  },
);

AttachmentMenu.displayName = 'AttachmentMenu';

export default AttachmentMenu;