Skip to content
Merged
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
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"dotenv": "^17.3.1",
"drizzle-orm": "0.45.1",
"electron-updater": "^6.7.3",
"exceljs": "^4.4.0",
"execa": "^9.6.0",
"express": "^5.1.0",
"fast-glob": "^3.3.3",
Expand Down
49 changes: 49 additions & 0 deletions apps/desktop/src/lib/trpc/routers/changes/file-contents.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import type { FileContents } from "shared/changes-types";
import { detectLanguage } from "shared/detect-language";
import type { SimpleGit } from "simple-git";
import { z } from "zod";
import { publicProcedure, router } from "../..";
import { toRegisteredWorktreeRelativePath } from "../workspace-fs-service";
import { getSimpleGitWithShellPath } from "../workspaces/utils/git-client";
import { getProcessEnvWithShellPath } from "../workspaces/utils/shell-env";

const MAX_FILE_SIZE = 2 * 1024 * 1024;
const MAX_BINARY_FILE_SIZE = 10 * 1024 * 1024;
const execFileAsync = promisify(execFile);

export const createFileContentsRouter = () => {
return router({
Expand Down Expand Up @@ -51,6 +56,50 @@ export const createFileContentsRouter = () => {
};
}),

readGitFileBinary: publicProcedure
.input(
z.object({
worktreePath: z.string(),
absolutePath: z.string(),
ref: z.string().default("HEAD"),
}),
)
.query(async ({ input }): Promise<{ content: string | null }> => {
const relativePath = toRegisteredWorktreeRelativePath(
input.worktreePath,
input.absolutePath,
);
const spec = `${input.ref}:${relativePath}`;
const git = await getSimpleGitWithShellPath(input.worktreePath);

try {
const sizeOutput = await git.raw(["cat-file", "-s", spec]);
const blobSize = Number.parseInt(sizeOutput.trim(), 10);
if (!Number.isNaN(blobSize) && blobSize > MAX_BINARY_FILE_SIZE) {
return { content: null };
}
} catch {
return { content: null };
}

try {
const env = await getProcessEnvWithShellPath();
const { stdout } = await execFileAsync(
"git",
["cat-file", "-p", spec],
{
cwd: input.worktreePath,
encoding: "buffer",
maxBuffer: MAX_BINARY_FILE_SIZE,
env,
},
);
return { content: (stdout as unknown as Buffer).toString("base64") };
} catch {
return { content: null };
}
}),

getGitOriginalContent: publicProcedure
.input(
z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useFileDocument } from "@superset/workspace-client";
import { SpreadsheetViewer } from "renderer/screens/main/components/WorkspaceView/ContentView/TabsContent/TabView/FileViewerPane/components/SpreadsheetViewer";
import { isSpreadsheetFile } from "shared/file-types";

interface WorkspaceFilePreviewContentProps {
selectedFilePath: string;
Expand Down Expand Up @@ -32,6 +34,14 @@ export function WorkspaceFilePreviewContent({
}

if (document.state.kind === "binary") {
if (isSpreadsheetFile(selectedFilePath)) {
return (
<SpreadsheetViewer
workspaceId={workspaceId}
filePath={selectedFilePath}
/>
);
}
return (
<div className="flex h-full items-center justify-center text-sm text-muted-foreground">
Binary files are not previewed yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ export function FileViewerPane({
)}
<div className="min-h-0 flex-1">
<FileViewerContent
workspaceId={normalizedWorkspaceId}
worktreePath={worktreePath}
diffCategory={diffCategory}
commitHash={commitHash}
viewMode={viewMode}
filePath={filePath}
isLoadingRaw={isLoadingRaw}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import { CodeEditor } from "renderer/screens/main/components/WorkspaceView/compo
import type { Tab } from "renderer/stores/tabs/types";
import type { DiffViewMode } from "shared/changes-types";
import { detectLanguage } from "shared/detect-language";
import { isImageFile } from "shared/file-types";
import { isImageFile, isSpreadsheetFile } from "shared/file-types";
import type { FileViewerMode } from "shared/tabs-types";
import { useScrollToFirstDiffChange } from "../../hooks/useScrollToFirstDiffChange";
import { DiffScrollbarDecorations } from "../DiffScrollbarDecorations";
import { DiffViewerContextMenu } from "../DiffViewerContextMenu";
import { FileEditorContextMenu } from "../FileEditorContextMenu";
import { MarkdownSearch } from "../MarkdownSearch";
import { SpreadsheetDiffViewer, SpreadsheetViewer } from "../SpreadsheetViewer";
import {
type DiffDomLocation,
getColumnFromDiffPoint,
Expand Down Expand Up @@ -101,6 +102,10 @@ interface TextSearchState {
}

interface FileViewerContentProps {
workspaceId?: string;
worktreePath?: string;
diffCategory?: import("shared/changes-types").ChangeCategory;
commitHash?: string;
viewMode: FileViewerMode;
filePath: string;
isLoadingRaw: boolean;
Expand Down Expand Up @@ -136,6 +141,10 @@ interface FileViewerContentProps {
}

export function FileViewerContent({
workspaceId,
worktreePath,
diffCategory,
commitHash,
viewMode,
filePath,
isLoadingRaw,
Expand Down Expand Up @@ -276,6 +285,23 @@ export function FileViewerContent({
rawFileData,
]);

if (
viewMode === "diff" &&
isSpreadsheetFile(filePath) &&
workspaceId &&
worktreePath
) {
return (
<SpreadsheetDiffViewer
workspaceId={workspaceId}
worktreePath={worktreePath}
filePath={filePath}
diffCategory={diffCategory}
commitHash={commitHash}
/>
);
}

if (viewMode === "diff") {
if (isLoadingDiff) {
return (
Expand Down Expand Up @@ -422,6 +448,15 @@ export function FileViewerContent({
);
}

if (
rawFileData?.ok === false &&
rawFileData.reason === "binary" &&
isSpreadsheetFile(filePath) &&
workspaceId
) {
return <SpreadsheetViewer workspaceId={workspaceId} filePath={filePath} />;
}

if (!rawFileData?.ok) {
const errorMessage =
rawFileData?.reason === "too-large"
Expand Down
Loading