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.

5.9 KB
Raw Blame
import {
  Button,
  Center,
  createStaticStyles,
  cssVar,
  FileTypeIcon,
  Flexbox,
  Icon,
  Text,
  Upload,
} from '@lobehub/ui';
import { ArrowUpIcon, PlusIcon } from 'lucide-react';
import { useTranslation } from 'react-i18next';

import { useCreateNewModal } from '@/features/LibraryModal';
import { useCurrentFolderId } from '@/features/ResourceManager/hooks/useCurrentFolderId';
import { useTopLevelFileUpload } from '@/features/ResourceManager/hooks/useTopLevelFileUpload';
import { useResourceManagerStore } from '@/features/ResourceManager/store';
import { getResourceSourceFilter } from '@/features/ResourceManager/store/selectors';
import { usePermission } from '@/hooks/usePermission';
import { useFileStore } from '@/store/file';
import { densityVars } from '@/styles/densityVars';
import { ResourceSourceFilter } from '@/types/files';

const ICON_SIZE = 80;

const styles = createStaticStyles(({ css, cssVar }) => ({
  actionTitle: css`
    margin-block-start: 12px;
    font-size: ${densityVars.pageTitleFontSize};
    color: ${cssVar.colorTextSecondary};
  `,
  card: css`
    cursor: pointer;

    position: relative;

    overflow: hidden;

    width: 200px;
    height: 140px;
    border-radius: ${cssVar.borderRadiusLG};

    font-weight: 500;
    text-align: center;

    background: ${cssVar.colorFillTertiary};
    box-shadow: 0 0 0 1px ${cssVar.colorFillTertiary} inset;

    transition: background 0.3s ease-in-out;

    &:hover {
      background: ${cssVar.colorFillSecondary};
    }
  `,
  glow: css`
    position: absolute;
    inset-block-end: -12px;
    inset-inline-end: 0;

    width: 48px;
    height: 48px;

    opacity: 0.5;
    filter: blur(24px);
  `,
  icon: css`
    position: absolute;
    z-index: 1;
    inset-block-end: -24px;
    inset-inline-end: 8px;

    flex: none;
  `,
}));

const EmptyPlaceholder = () => {
  const { t } = useTranslation('components');

  const pushDockFileList = useFileStore((s) => s.pushDockFileList);
  const uploadTopLevel = useTopLevelFileUpload();

  const libraryId = useResourceManagerStore((s) => s.libraryId);
  const [sourceFilter, setSourceFilter] = useResourceManagerStore((s) => [
    getResourceSourceFilter(s),
    s.setSourceFilter,
  ]);
  const currentFolderId = useCurrentFolderId();

  const { open } = useCreateNewModal();
  const { allowed: canCreate } = usePermission('create_content');

  // A narrowed source can empty a category that is not actually empty — the
  // images view opens on generated output, so a library of uploads would
  // otherwise read as "you have nothing yet". Name the filter and offer the
  // way back instead of the onboarding prompt.
  if (sourceFilter !== ResourceSourceFilter.All) {
    return (
      <Center gap={12} height={'100%'} style={{ paddingBottom: 100 }} width={'100%'}>
        <Text as={'h4'}>{t('FileManager.emptyStatus.filteredTitle')}</Text>
        <Button size={'small'} onClick={() => setSourceFilter(ResourceSourceFilter.All)}>
          {t('FileManager.emptyStatus.actions.showAllSources')}
        </Button>
      </Center>
    );
  }

  if (!canCreate) {
    return (
      <Center height={'100%'} style={{ paddingBottom: 100 }} width={'100%'}>
        <Text as={'h4'}>{t('FileManager.emptyStatus.title')}</Text>
      </Center>
    );
  }

  return (
    <Center gap={24} height={'100%'} style={{ paddingBottom: 100 }} width={'100%'}>
      <Flexbox justify={'center'} style={{ textAlign: 'center' }}>
        <Text as={'h4'}>{t('FileManager.emptyStatus.title')}</Text>
        <Text type={'secondary'}>{t('FileManager.emptyStatus.or')}</Text>
      </Flexbox>
      {/* Wraps so the three 200px cards stack on a phone instead of clipping. */}
      <Flexbox horizontal gap={12} justify={'center'} paddingInline={16} wrap={'wrap'}>
        {!libraryId && (
          <Flexbox
            className={styles.card}
            padding={16}
            onClick={() => {
              open();
            }}
          >
            <span className={styles.actionTitle}>
              {t('FileManager.emptyStatus.actions.knowledgeBase')}
            </span>
            <div className={styles.glow} style={{ background: cssVar.purple }} />
            <FileTypeIcon
              className={styles.icon}
              color={cssVar.purple}
              icon={<Icon color={'#fff'} icon={PlusIcon} />}
              size={ICON_SIZE}
              type={'folder'}
            />
          </Flexbox>
        )}
        <Upload multiple={true} onFiles={(files) => void uploadTopLevel(files)}>
          <Flexbox className={styles.card} padding={16}>
            <span className={styles.actionTitle}>{t('FileManager.emptyStatus.actions.file')}</span>
            <div className={styles.glow} style={{ background: cssVar.gold }} />
            <FileTypeIcon
              className={styles.icon}
              color={cssVar.gold}
              icon={<Icon color={'#fff'} icon={ArrowUpIcon} />}
              size={ICON_SIZE}
            />
          </Flexbox>
        </Upload>
        <Upload
          directory
          multiple={true}
          onFiles={(files) => {
            // Directory upload keeps its own path — the whole tree inherits
            // its root's visibility, so we skip the mode-driven default and
            // let the server infer from the parent chain.
            void pushDockFileList(files, libraryId, currentFolderId ?? undefined);
          }}
        >
          <Flexbox className={styles.card} padding={16}>
            <span className={styles.actionTitle}>
              {t('FileManager.emptyStatus.actions.folder')}
            </span>
            <div className={styles.glow} style={{ background: cssVar.geekblue }} />
            <FileTypeIcon
              className={styles.icon}
              color={cssVar.geekblue}
              icon={<Icon color={'#fff'} icon={ArrowUpIcon} />}
              size={ICON_SIZE}
              type={'folder'}
            />
          </Flexbox>
        </Upload>
      </Flexbox>
    </Center>
  );
};

export default EmptyPlaceholder;