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.

1.6 KB
Raw Blame
import { type CSSProperties } from 'react';
import { memo } from 'react';

import { useChatInputUploadKey } from '@/features/ChatInput/hooks/useChatInputUpload';
import { useFileStore } from '@/store/file';
import { type UploadFileItem } from '@/types/files';

import File from './File';
import Image from './Image';

interface FileItemProps extends UploadFileItem {
  alt?: string;
  className?: string;
  loading?: boolean;
  onClick?: () => void;
  onRemove?: () => void;
  style?: CSSProperties;
  url?: string;
}

const FileItem = memo<FileItemProps>((props) => {
  const { errorCode, file, id, previewUrl, status } = props;
  const contextKey = useChatInputUploadKey();
  const [removeChatUploadFile, retryChatUploadFile] = useFileStore((s) => [
    s.removeChatUploadFile,
    s.retryChatUploadFile,
  ]);
  const removeFile = (fileId: string) => {
    if (contextKey) void removeChatUploadFile(contextKey, fileId);
  };
  const retryFile = async (fileId: string) => {
    if (contextKey) await retryChatUploadFile(contextKey, fileId);
  };

  if (file.type.startsWith('image')) {
    return (
      <Image
        alt={file.name}
        error={status === 'error'}
        errorCode={errorCode}
        loading={status === 'pending'}
        src={previewUrl}
        onRemove={() => {
          removeFile(id);
        }}
        onRetry={() => {
          void retryFile(id);
        }}
      />
    );
  }

  return (
    <File
      {...props}
      onRemove={() => removeFile(id)}
      onRetry={() => {
        void retryFile(id);
      }}
    />
  );
});

export default FileItem;