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.0 KB
Raw Blame
'use client';

import { type BuiltinRenderProps } from '@lobechat/types';
import { Block, Flexbox, Highlighter, Markdown, Text } from '@lobehub/ui';
import * as stylex from '@stylexjs/stylex';
import { memo } from 'react';

import type { ReadReferenceParams, ReadReferenceState } from '../../../types';

const styles = stylex.create({
  container: { overflow: 'hidden', paddingInlineStart: '8px', paddingInlineEnd: '0' },
});

const getFileExtension = (path: string): string => {
  const parts = path.split('.');
  return parts.length > 1 ? parts.pop()?.toLowerCase() || 'text' : 'text';
};

const languageMap: Record<string, string> = {
  css: 'css',
  go: 'go',
  html: 'html',
  java: 'java',
  js: 'javascript',
  json: 'json',
  jsx: 'jsx',
  md: 'markdown',
  py: 'python',
  rs: 'rust',
  scss: 'scss',
  sh: 'bash',
  sql: 'sql',
  ts: 'typescript',
  tsx: 'tsx',
  xml: 'xml',
  xsd: 'xml',
  yaml: 'yaml',
  yml: 'yaml',
};

const getLanguage = (ext: string): string => languageMap[ext] || 'text';

const formatSize = (bytes: number): string => {
  if (bytes === 0) return '0 B';
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};

const ReadReference = memo<BuiltinRenderProps<ReadReferenceParams, ReadReferenceState>>(
  ({ content, pluginState }) => {
    const { encoding, fullPath, path, size } = pluginState || {};

    if (!path || !content) return null;

    const displayPath = fullPath || path;

    const ext = getFileExtension(path);
    const isMarkdown = ext === 'md' || ext === 'markdown';
    const isBinary = encoding === 'base64';

    const sizeText = size ? formatSize(size) : '';

    return (
      <Flexbox className={stylex.props(styles.container).className} gap={8}>
        <Flexbox horizontal align={'center'} justify={'space-between'}>
          <Text code ellipsis as={'span'} fontSize={12}>
            {displayPath}
          </Text>
          {sizeText && (
            <Text code noWrap as={'span'} fontSize={12} type={'secondary'}>
              {sizeText}
            </Text>
          )}
        </Flexbox>

        {isBinary ? (
          <Block padding={12} variant={'outlined'}>
            <Text fontSize={12} type={'secondary'}>
              Binary file ({sizeText})
            </Text>
          </Block>
        ) : isMarkdown ? (
          <Block padding={12} variant={'outlined'}>
            <Markdown style={{ overflow: 'unset' }} variant={'chat'}>
              {content}
            </Markdown>
          </Block>
        ) : (
          <Block padding={8} variant={'outlined'}>
            <Highlighter
              showLanguage
              wrap
              language={getLanguage(ext)}
              style={{ maxHeight: 400, overflow: 'auto' }}
              variant={'borderless'}
            >
              {content}
            </Highlighter>
          </Block>
        )}
      </Flexbox>
    );
  },
);

ReadReference.displayName = 'ReadReference';

export default ReadReference;