Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { SessionFileImagePreview } from '@/components/sessions/session-file-imag
import { SessionFileVideoPreview } from '@/components/sessions/session-file-video-preview';
import { MarkdownRenderer } from '@/components/ai-gui/markdown-renderer';
import { MarkdownFileResources } from '@/components/ai-gui/markdown-file-image';
import { SessionMonacoTextViewer } from '@/components/sessions/session-monaco-text-viewer';
import { LazySessionMonacoTextViewer } from '@/components/sessions/lazy-session-monaco-text-viewer';
import { SessionShikiTextViewer } from '@/components/sessions/session-shiki-text-viewer';
import { useFileWorkspaceTree } from '@/hooks/use-code-session';
import { isNativeAppShell } from '@/lib/native-platform';
Expand Down Expand Up @@ -878,7 +878,7 @@ function MobileTextPreview({
) : showHtml ? (
<ReadOnlyHtmlPreview text={text} />
) : (
<SessionMonacoTextViewer
<LazySessionMonacoTextViewer
key={path}
text={text}
language="html"
Expand Down Expand Up @@ -930,7 +930,7 @@ function MobileTextPreview({
<div
className={cn('min-h-0 flex-1 overflow-hidden overscroll-contain', bottomClearanceClassName)}
>
<SessionMonacoTextViewer
<LazySessionMonacoTextViewer
key={path}
text={text}
language={getSessionFileMonacoLanguageId(path, extendedCodeLanguagesEnabled)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as stylex from '@stylexjs/stylex';
import { lazy, Suspense, type ComponentProps } from 'react';
import { Spinner } from '@lody/ui/spinner';
import type { SessionMonacoTextViewer } from './session-monaco-text-viewer';

export type SessionMonacoTextViewerProps = ComponentProps<typeof SessionMonacoTextViewer>;

const styles = stylex.create({
loading: {
display: 'flex',
height: '100%',
minHeight: 0,
width: '100%',
alignItems: 'center',
justifyContent: 'center',
},
});

const LazyMonacoViewer = lazy(() =>
import('./session-monaco-text-viewer').then((module) => ({
default: module.SessionMonacoTextViewer,
}))
);

/**
* Code-splits the Monaco editor bundle: `session-monaco-text-viewer` and
* everything it imports (`monaco-editor` JS/CSS, workers, language
* registrations) load only when a text surface actually mounts the
* viewer. The fallback paints a centered spinner while the chunk loads.
*/
export function LazySessionMonacoTextViewer(props: SessionMonacoTextViewerProps) {
return (
<Suspense
fallback={
<div {...stylex.props(styles.loading)} role="status">
<Spinner label={null} />
</div>
}
>
<LazyMonacoViewer {...props} />
</Suspense>
);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import * as stylex from '@stylexjs/stylex';
import type { SessionFileErrorActions } from '@/lib/session-file-actions';
import { memo } from 'react';
import { lazy, memo, Suspense } from 'react';
import { useTranslation } from 'react-i18next';
import { Spinner } from '@lody/ui/spinner';
import { getImageMimeTypeForPath } from '@/lib/image-file-preview';
import { isPdfFilePath } from '@/lib/pdf-file-preview';
import { getOfficePreviewKind } from '@/lib/session-file-office-source';
import { getVideoMimeTypeForPath } from '@/lib/video-file-preview';
import { SessionFileVideoPreview } from './session-file-video-preview';
import { SessionFileImagePreview } from './session-file-image-preview';
import { SessionFileOfficePreview } from './session-file-office-preview';
import { SessionFilePdfPreview } from './session-file-pdf-preview';
import { SessionFileNoticeCard } from './session-file-error-state';
import { DocumentSelectionSurface } from './document-selection-surface';
import type { DocumentSelection } from '@/lib/document-selection';

// `pdfjs-dist` (JS + viewer CSS + worker URL) stays out of the eager
// file-viewer chunk; it loads only when a PDF preview actually mounts,
// matching how `SessionFileOfficePreview` lazy-loads its renderers.
const LazySessionFilePdfPreview = lazy(() =>
import('./session-file-pdf-preview').then((module) => ({
default: module.SessionFilePdfPreview,
}))
);

const styles = stylex.create({
loading: {
position: 'absolute',
inset: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
});

interface SessionFileBinaryPreviewProps {
readonly path: string;
readonly bytes?: Uint8Array;
Expand Down Expand Up @@ -73,12 +93,21 @@ function BinaryPreviewContent({

if (isPdfFilePath(path) && (url || bytes !== undefined)) {
return (
<SessionFilePdfPreview
key={url ?? `${path}:${bytes?.byteLength ?? 0}`}
bytes={bytes}
url={url}
fileActions={fileActions}
/>
<Suspense
fallback={
<div {...stylex.props(styles.loading)} role="status">
<Spinner label={null} />
<span>{t('sessions.fileViewer.pdf.loading', 'Loading PDF…')}</span>
</div>
}
>
<LazySessionFilePdfPreview
key={url ?? `${path}:${bytes?.byteLength ?? 0}`}
bytes={bytes}
url={url}
fileActions={fileActions}
/>
</Suspense>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,8 @@ import { usePostHog } from '@posthog/react';
import { capturePostHogEvent, getAnalyticsFileKind } from '@/lib/posthog-analytics';
import { useCodeCollabLiveText } from '@/hooks/use-code-collab-live-text';
import { useMachineFlockRows } from '@/hooks/use-machine-flock-rows';
import {
SessionMonacoTextViewer,
type SessionMonacoExternalTextUpdate,
} from './session-monaco-text-viewer';
import { LazySessionMonacoTextViewer } from './lazy-session-monaco-text-viewer';
import type { SessionMonacoExternalTextUpdate } from './session-monaco-text-viewer';
import {
useCodeCollabSaveText,
type SessionFileLiveSyncStatus,
Expand All @@ -114,7 +112,7 @@ import { useLatestRef } from '@/hooks/use-latest-ref';
import {
buildCodeCollabMonacoUri,
registerCodeCollabMonacoModelProvider,
} from '@/lib/session-monaco-language-providers';
} from '@/lib/code-collab-monaco-model-provider';
import {
normalizeSessionMonacoSelectedLines,
type SessionMonacoSelectedLines,
Expand Down Expand Up @@ -1499,6 +1497,8 @@ function SessionFileContentViewImpl({
// so Cmd-click / F12 reach the right provider when multiple files are
// open. Unregisters on unmount or when the active fileId / provider
// changes so a stale entry can't intercept a different mount.
// The URI is a serialized `code-collab://` string built without
// `monaco-editor`; the lazy viewer parses it once its chunk loads.
const lspModelUri = useMemo(
() =>
isLspEnabled && lspFileId ? buildCodeCollabMonacoUri(lspFileId, normalizedPath) : undefined,
Expand Down Expand Up @@ -1788,7 +1788,7 @@ function SessionFileContentViewImpl({
/>
) : (
<LazyProviderTextMonacoViewer
key={lspModelUri?.toString() ?? liveFileId ?? normalizedPath}
key={lspModelUri ?? liveFileId ?? normalizedPath}
text={resolveProviderEditorMountText(data.snapshot)}
language={getSessionFileMonacoLanguageId(
normalizedPath,
Expand Down Expand Up @@ -2776,11 +2776,11 @@ function SessionFileLspPanel({
}

function LazyProviderTextMonacoViewer(props: SessionTextMonacoViewerProps) {
return <LazyTextMonacoViewer {...props} />;
return <LazySessionMonacoTextViewer {...props} />;
}

function LazyTextMonacoViewer(props: SessionTextMonacoViewerProps) {
return <SessionMonacoTextViewer {...props} />;
return <LazySessionMonacoTextViewer {...props} />;
}

type SessionTextMonacoViewerProps = {
Expand All @@ -2807,5 +2807,7 @@ type SessionTextMonacoViewerProps = {
readonly externalTextUpdate?: SessionMonacoExternalTextUpdate;
readonly onExternalTextUpdateApplied?: (result: 'applied' | 'no-op') => void;
readonly findRequestSeq?: number;
readonly modelUri?: import('monaco-editor/esm/vs/editor/editor.api.js').Uri;
// Serialized `code-collab://` model URI; kept as a string so this module
// never needs the `monaco-editor` import.
readonly modelUri?: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ export function SessionMonacoTextViewer({
readonly character: number;
}) => void;
readonly onScrollChange?: (state: { readonly scrollTop: number }) => void;
// Optional Monaco model URI. When provided the viewer creates the
// Optional serialized Monaco model URI (e.g. from
// `buildCodeCollabMonacoUri`). When provided the viewer creates the
// model under this URI, which lets Monaco's globally-registered
// Code Collab definition/reference providers (see
// `lib/session-monaco-language-providers`) attach via the
// `code-collab://<fileId>` scheme. Defaults to Monaco's auto-generated
// URI so non-collab callers (story/diff viewer) keep working unchanged.
readonly modelUri?: monaco.Uri;
// Serialized so callers can build it without importing `monaco-editor`.
readonly modelUri?: string;
readonly className?: string;
}) {
const containerRef = useRef<HTMLDivElement>(null);
Expand Down
Loading
Loading