/** * The read-only paged preview for a file above the editable threshold. * * Two row shapes share one cursor ({@link useTextPager}): * * - `jsonl` — one row per record, collapsed to a one-line summary and * expandable into the host's `JsonTree`. A page is line-aligned, so a page * is exactly a run of whole records. * - everything else — one row per line, monospace, no wrapping (horizontal * scroll), which is the log-viewer shape and the reason a page's row count * stays bounded. * * The pager is EXPLICIT (previous/next page) rather than an infinite scroll. * Byte offsets only exist for pages already walked forward, so "scroll to the * end" cannot be offered honestly without indexing the whole file on open; * what the cursor CAN do is revisit any visited page in either direction, and * the pager says exactly that. See text-pages.ts for the full reasoning. * * Nothing here fetches the document: the file is above the editor's limit * precisely because holding it whole is what we are avoiding. */ import { useMemo, useState, type ReactNode } from 'react' import clsx from 'clsx' import { IconChevronLeftOutlineRegular, IconChevronRightOutlineRegular, IconRefreshOutlineRegular, JsonTree, type JsonTreeLabels } from '@deepseek-ai/dsh-client-ui-primitives' import type { SessionScope } from './api.ts' import { downloadUrl } from './api.ts' import { formatByteSize, MAX_EDITABLE_BYTES, MAX_EDITABLE_LABEL } from '../editor-limits.ts' import { isJsonlPath, isTreeRecord, parseRecord, summarizeRecord } from './jsonl.ts' import { splitLines, useTextPager } from './text-pages.ts' import { t } from './locales.ts' import css from './sidebar.module.css' /** Copy the host's `JsonTree` needs; this plugin owns the wording. */ function jsonLabels(): JsonTreeLabels { return { // The tree's primitive/compact copy actions are the same gesture as the // code block's copy, so they reuse that wording rather than inventing a // second vocabulary for the same button. copyValue: t('copy'), copyJson: t('copy'), copyPath: t('jsonCopyPath'), copyPrettyJson: t('jsonCopyPrettyJson'), copyCompactJson: t('jsonCopyCompactJson'), copied: t('copied'), copyFailed: t('jsonCopyFailed'), collapseNode: t('jsonCollapseNode'), expandNode: t('jsonExpandNode'), copyButtonTitle: (action) => t('jsonCopyButtonTitle', { action }), } } /** * The read-only preview of one over-limit file. * * @param props.scope - the session scope. * @param props.path - the file being previewed. * @param props.size - the file's size in bytes (from the read that refused it). * @param props.visible - whether the tab is on screen; a parked tab stops reading. * @param props.revision - bumped by the host's refresh to restart from page 1. * @returns the preview. */ export function LargeFileView(props: { scope: SessionScope path: string size: number visible: boolean revision: number }): ReactNode { const { scope, path, size, visible, revision } = props const pager = useTextPager(scope, path, visible, revision) const records = isJsonlPath(path) /** * Whether this file is over the EDITABLE threshold — which is a different * question from "is this view paged". * * A small `.jsonl` opens straight into this record list as its PREVIEW, and * announcing "the file is too large, paged preview only" over a 2 KB file * would be a lie. The threshold decides, not the component. */ const tooLarge = size > MAX_EDITABLE_BYTES const rows = useMemo(() => splitLines(pager.text), [pager.text]) /** Which record rows are expanded (reset with every page). */ const [expanded, setExpanded] = useState>(new Set()) const pageKey = `${pager.page}` const labels = useMemo(() => jsonLabels(), []) const parsed = useMemo( () => (records ? rows.map(parseRecord) : []), [records, rows], ) /** Absolute line/record number of row `index` on the current page. */ const numberAt = (index: number): number => pager.firstLine + index const selectPage = (next: () => void): void => { setExpanded(new Set()) next() } return (
{tooLarge && (
{t('previewOnlyNotice', { size: formatByteSize(size), limit: MAX_EDITABLE_LABEL })} {t('downloadToView')}
)}
{pager.error !== null &&
{pager.error}
} {pager.error === null && pager.loading && pager.text === '' && (
{t('loading')}
)} {pager.error === null && !(pager.loading && pager.text === '') && records && (
    {parsed.map((record, index) => ( { setExpanded((previous) => { const next = new Set(previous) if (next.has(index)) next.delete(index) else next.add(index) return next }) }} /> ))}
)} {pager.error === null && !(pager.loading && pager.text === '') && !records && (
{rows.map((line, index) => (
{line === '' ? '\u00a0' : line}
))} {rows.length === 0 && !pager.loading && (
{t('largeFileEmpty')}
)}
)}
{records ? t('jsonlRecordRange', { from: pager.firstLine, to: numberAt(Math.max(0, rows.length - 1)) }) : t('pagedLineRange', { from: pager.firstLine, to: numberAt(Math.max(0, rows.length - 1)) })} {' · '} {pager.atEnd ? t('pagedPageOfTotal', { page: pager.page, total: pager.page }) : t('pagedPage', { page: pager.page })}
) } /** One JSONL record row: a summary line, expanding into the host's JSON tree. */ function RecordRow(props: { number: number /** The raw line, shown verbatim when the record does not parse. */ line: string record: ReturnType expanded: boolean labels: JsonTreeLabels onToggle: () => void }): ReactNode { const { number, line, record, expanded, labels, onToggle } = props if (!record.ok) { // A blank line is not a broken record — JSONL files normally end with one, // and the host's pages are line-aligned, so it lands as its own row. if (record.error === '') return null return (
  • {t('jsonlInvalidRecord')}
    {line}
  • ) } const value = record.value const tree = isTreeRecord(value) return (
  • {tree && expanded && (
    )}
  • ) }