Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.1 KB
Raw Blame
import { t } from 'i18next';
import { Table2 } from 'lucide-react';

import { CopyButton } from '@/components/custom/clipboard/copy-button';
import { DownloadButton } from '@/components/custom/download-button';
import { cn } from '@/lib/utils';

import { PreviewCard } from './preview-card';
import { previewUtils, TableData } from './preview-utils';

const INLINE_MAX_ROWS = 50;

function SpreadsheetTable({
  table,
  maxRows,
  className,
}: {
  table: TableData;
  maxRows: number;
  className?: string;
}) {
  const totalRows = table.rows.length;
  const rows = table.rows.slice(0, maxRows);
  const truncated = totalRows > maxRows;

  return (
    <div className={cn('overflow-auto', className)}>
      <table className="w-full table-fixed text-sm">
        <thead className="sticky top-0 z-10">
          <tr className="border-b border-gray-6 bg-gray-3">
            {table.headers.map((header, i) => (
              <th
                key={i}
                className="break-words bg-gray-3 px-3 py-2 text-left align-top font-medium text-gray-11"
              >
                {header}
              </th>
            ))}
          </tr>
        </thead>
        <tbody>
          {rows.map((row, rowIdx) => (
            <tr
              key={rowIdx}
              className="border-b border-gray-6 transition-colors last:border-b-0 hover:bg-gray-4/30"
            >
              {row.map((cell, colIdx) => (
                <td key={colIdx} className="px-3 py-2 align-top">
                  {cell === '' ? (
                    <span className="italic text-gray-11">{t('empty')}</span>
                  ) : (
                    <span className="whitespace-pre-wrap break-words">
                      {cell}
                    </span>
                  )}
                </td>
              ))}
            </tr>
          ))}
        </tbody>
      </table>
      {truncated && (
        <div className="border-t border-gray-6 px-3 py-2 text-xs text-gray-11">
          {t('Showing {shown} of {total} rows — expand to see all', {
            shown: rows.length,
            total: totalRows,
          })}
        </div>
      )}
    </div>
  );
}

export function SpreadsheetPreview({
  table,
  label,
  fileName = 'data',
}: {
  table: TableData;
  label?: string;
  fileName?: string;
}) {
  const headerLabel =
    label ??
    t('{rows} rows × {cols} columns', {
      rows: table.rows.length,
      cols: table.headers.length,
    });

  const actions = (
    <>
      <CopyButton
        textToCopy={previewUtils.buildTsv(table)}
        variant="ghost"
        size="icon-sm"
      />
      <DownloadButton
        fileName={fileName}
        textToDownload={previewUtils.buildCsv(table)}
        mimeType="text/csv"
        extension="csv"
        variant="ghost"
        size="icon-sm"
      />
    </>
  );

  return (
    <PreviewCard
      icon={Table2}
      label={headerLabel}
      actions={actions}
      renderExpanded={() => (
        <SpreadsheetTable table={table} maxRows={5000} className="h-full" />
      )}
    >
      <SpreadsheetTable
        table={table}
        maxRows={INLINE_MAX_ROWS}
        className="max-h-[360px]"
      />
    </PreviewCard>
  );
}