Skip to content

The open alternative to Salesforce, designed for AI.

3.0 KB
Raw Blame
import { SkeletonLoader } from '@/activities/components/SkeletonLoader';
import { AttachmentList } from '@/activities/files/components/AttachmentList';
import { AttachmentUploadTrigger } from '@/activities/files/components/AttachmentUploadTrigger';
import { DropZone } from '@/activities/files/components/DropZone';
import { type Attachment } from '@/activities/files/types/Attachment';
import { type ActivityTargetableObject } from '@/activities/types/ActivityTargetableEntity';
import { AnimatedPlaceholder } from '@/ui/feedback/empty-state/components/AnimatedPlaceholder/AnimatedPlaceholder';
import { EmptyState } from '@/ui/feedback/empty-state/components/EmptyState';
import { styled } from '@linaria/react';
import { Trans, useLingui } from '@lingui/react/macro';
import { useState } from 'react';

import { IconPlus } from 'twenty-ui/icon';
import { Button } from 'twenty-ui/primitives/input';

const StyledAttachmentsContainer = styled.div`
  display: flex;
  flex: 1;
  flex-direction: column;
  height: 100%;
  overflow: auto;
`;

const StyledDropZoneContainer = styled.div`
  height: 100%;
`;

type FilesCardContentProps = {
  attachments: Attachment[];
  canUploadFiles: boolean;
  loading: boolean;
  onUploadFiles: (files: File[]) => Promise<void>;
  targetRecord: ActivityTargetableObject;
};

export const FilesCardContent = ({
  attachments,
  canUploadFiles,
  loading,
  onUploadFiles,
  targetRecord,
}: FilesCardContentProps) => {
  const [isDraggingFile, setIsDraggingFile] = useState(false);
  const { t } = useLingui();

  const isAttachmentsEmpty = attachments.length === 0;

  if (loading && isAttachmentsEmpty) {
    return <SkeletonLoader />;
  }

  if (isAttachmentsEmpty) {
    return (
      <StyledDropZoneContainer
        onDragEnter={() => canUploadFiles && setIsDraggingFile(true)}
      >
        {isDraggingFile && canUploadFiles ? (
          <DropZone
            setIsDraggingFile={setIsDraggingFile}
            onUploadFiles={onUploadFiles}
          />
        ) : (
          <EmptyState.Root>
            <AnimatedPlaceholder type="noFile" />
            <EmptyState.Content>
              <EmptyState.Title>
                <Trans>No Files</Trans>
              </EmptyState.Title>
              <EmptyState.Description>
                <Trans>There are no associated files with this record.</Trans>
              </EmptyState.Description>
            </EmptyState.Content>
            {canUploadFiles && (
              <AttachmentUploadTrigger targetableObject={targetRecord}>
                {({ openFilePicker }) => (
                  <Button
                    startIcon={<IconPlus />}
                    onClick={openFilePicker}
                    variant="outline"
                  >{t`Add file`}</Button>
                )}
              </AttachmentUploadTrigger>
            )}
          </EmptyState.Root>
        )}
      </StyledDropZoneContainer>
    );
  }

  return (
    <StyledAttachmentsContainer>
      <AttachmentList
        targetableObject={targetRecord}
        attachments={attachments}
      />
    </StyledAttachmentsContainer>
  );
};