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.

4.9 KB
Raw Blame
'use client';

import { ActionIcon, cssVar, DraggablePanel, Flexbox, useTheme } from '@lobehub/ui';
import { t as i18nT } from 'i18next';
import { DownloadIcon, InfoIcon, PanelRightCloseIcon } from 'lucide-react';
import { memo, useState } from 'react';

import FileDetail from '@/features/ResourceManager/FileDetail';
import { useResourceManagerStore } from '@/features/ResourceManager/store';
import { useDensityTokens, useHeaderActionSize } from '@/styles/density';
import { downloadFile } from '@/utils/client/downloadFile';

import FilePreview from './FilePreview';
import PagePreview from './PagePreview';
import { useDetailPanelFile } from './useDetailPanelFile';
import { DETAIL_PANEL_MIN_WIDTH, useDetailPanelWidth } from './useDetailPanelWidth';

/**
 * In-context right dock for the explorer list: single click on a file row
 * below shows a live preview here, split-screen style — the list stays
 * visible (no mask, no modal). The properties column is opt-in from the
 * header so the preview gets the whole dock by default. Double click still
 * commits to the fullscreen editor.
 */
const FileDetailPanel = memo(() => {
  const theme = useTheme();
  const { fontSize, headerHeight } = useDensityTokens();
  const headerActionSize = useHeaderActionSize(true);
  const detailPanelId = useResourceManagerStore((s) => s.detailPanelId);
  const detailPanelIsPage = useResourceManagerStore((s) => s.detailPanelIsPage);
  const closeDetailPanel = useResourceManagerStore((s) => s.closeDetailPanel);
  const [showProperties, setShowProperties] = useState(false);
  const { handleSizeChange, width } = useDetailPanelWidth();

  const fileDetail = useDetailPanelFile(detailPanelId);

  return (
    <DraggablePanel
      showHandleWideArea
      backgroundColor={cssVar.colorBgContainer}
      expand={!!detailPanelId}
      expandable={false}
      minWidth={DETAIL_PANEL_MIN_WIDTH}
      placement={'right'}
      size={{ height: '100%', width }}
      style={{
        borderInlineStart: `1px solid ${cssVar.colorBorderSecondary}`,
        boxShadow: theme.boxShadowTertiary,
      }}
      onSizeChange={handleSizeChange}
    >
      {fileDetail && (
        <Flexbox height={'100%'} style={{ minHeight: 0, overflow: 'hidden' }}>
          <Flexbox
            horizontal
            align={'center'}
            gap={8}
            justify={'space-between'}
            paddingInline={16}
            style={{
              flexShrink: 0,
              borderBottom: `1px solid ${cssVar.colorBorderSecondary}`,
              minHeight: headerHeight,
            }}
          >
            <span
              title={fileDetail.name}
              style={{
                color: theme.colorText,
                fontSize,
                fontWeight: 500,
                minWidth: 0,
                overflow: 'hidden',
                textOverflow: 'ellipsis',
                whiteSpace: 'nowrap',
              }}
            >
              {fileDetail.name}
            </span>
            <Flexbox horizontal align={'center'} gap={4} style={{ flexShrink: 0 }}>
              {fileDetail.url && (
                <ActionIcon
                  icon={DownloadIcon}
                  size={headerActionSize}
                  title={i18nT('download', { ns: 'common' })}
                  onClick={() => {
                    if (fileDetail.url) downloadFile(fileDetail.url, fileDetail.name);
                  }}
                />
              )}
              <ActionIcon
                active={showProperties}
                icon={InfoIcon}
                size={headerActionSize}
                title={i18nT('detail.basic.title', { ns: 'file' })}
                onClick={() => setShowProperties((value) => !value)}
              />
              <ActionIcon
                icon={PanelRightCloseIcon}
                size={headerActionSize}
                title={i18nT('close', { ns: 'common' })}
                onClick={closeDetailPanel}
              />
            </Flexbox>
          </Flexbox>
          <Flexbox horizontal flex={1} style={{ minHeight: 0, overflow: 'hidden' }}>
            <Flexbox flex={1} style={{ minHeight: 0, minWidth: 0, overflow: 'auto' }}>
              {detailPanelIsPage && detailPanelId ? (
                <PagePreview id={detailPanelId} />
              ) : (
                <FilePreview file={fileDetail} />
              )}
            </Flexbox>
            {showProperties && (
              <Flexbox
                style={{
                  borderInlineStart: `1px solid ${cssVar.colorSplit}`,
                  flexShrink: 0,
                  overflow: 'auto',
                  paddingBlock: 12,
                  paddingInline: 16,
                  width: 220,
                }}
              >
                <FileDetail {...fileDetail} showDownloadButton={false} showTitle={false} />
              </Flexbox>
            )}
          </Flexbox>
        </Flexbox>
      )}
    </DraggablePanel>
  );
});

FileDetailPanel.displayName = 'FileDetailPanel';

export default FileDetailPanel;