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
5 changes: 5 additions & 0 deletions .changeset/web-file-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hunkdiff": patch
---

The browser review page's file headers dim the directory so the file name reads first, say what kind of change a file is in a quiet chip ("new", "renamed, no content changes", "binary", "too large") instead of a body sentence, hide `+0 −0`, fold any file from a chevron (viewed files start folded, with no extra row), copy the file's path from a button shown on hover, and draw the Viewed checkbox as a bordered control that turns green when checked.
3 changes: 3 additions & 0 deletions packages/hunk/src/web/app/ReviewApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,9 @@ export function ReviewApp({ session }: { session: ReviewSession }) {
viewedStatuses={viewed.statuses}
collapsedFiles={viewed.collapsed}
viewed={viewedControls}
{...(pageHint("hunk.web.toggleViewed")
? { viewedKeyHint: pageHint("hunk.web.toggleViewed") }
: {})}
{...(streamSlots.length === 0 && visibleSlots.length > 0
? {
emptyMessage: (
Expand Down
77 changes: 77 additions & 0 deletions packages/hunk/src/web/format/fileHeader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, expect, test } from "bun:test";
import { webFileHeaderPath, webFileStateChip } from "./fileHeader";

describe("webFileHeaderPath", () => {
test("splits a path into its directory and basename", () => {
expect(
webFileHeaderPath({ path: "src/app/main.ts", changeKind: "change", untracked: false }),
).toEqual({ text: "src/app/main.ts", directory: "src/app/", basename: "main.ts" });
expect(
webFileHeaderPath({ path: "README.md", changeKind: "change", untracked: false }),
).toEqual({ text: "README.md", directory: "", basename: "README.md" });
});

test("keeps a rename's old name apart and its text as the shared label", () => {
const sameDirectory = webFileHeaderPath({
path: "src/app/renamed-new.ts",
previousPath: "src/app/renamed-old.ts",
changeKind: "rename-changed",
untracked: false,
});
expect(sameDirectory).toEqual({
text: "src/app/renamed-old.ts -> renamed-new.ts",
previous: "src/app/renamed-old.ts",
directory: "",
basename: "renamed-new.ts",
});

const moved = webFileHeaderPath({
path: "b/new.ts",
previousPath: "a/old.ts",
changeKind: "rename-pure",
untracked: false,
});
expect(moved).toEqual({
text: "a/old.ts -> b/new.ts",
previous: "a/old.ts",
directory: "b/",
basename: "new.ts",
});
expect(`${moved.previous} -> ${moved.directory}${moved.basename}`).toBe(moved.text);
});
});

describe("webFileStateChip", () => {
test("names the change kind and says nothing for a plain modification", () => {
expect(webFileStateChip({ changeKind: "change", untracked: false })).toBeNull();
expect(webFileStateChip({ changeKind: "new", untracked: false })).toBe("new");
expect(webFileStateChip({ changeKind: "new", untracked: true })).toBe("untracked");
expect(webFileStateChip({ changeKind: "deleted", untracked: false })).toBe("deleted");
});

test("says why a file renders no rows", () => {
expect(
webFileStateChip({ changeKind: "rename-pure", untracked: false, emptyReason: "rename-only" }),
).toBe("renamed, no content changes");
expect(
webFileStateChip({ changeKind: "change", untracked: false, emptyReason: "binary" }),
).toBe("binary");
expect(
webFileStateChip({ changeKind: "change", untracked: false, emptyReason: "too-large" }),
).toBe("too large");
expect(webFileStateChip({ changeKind: "new", untracked: false, emptyReason: "new-file" })).toBe(
"new · empty",
);
});

test("adds a binary file's sizes when given", () => {
expect(
webFileStateChip({
changeKind: "change",
untracked: false,
emptyReason: "binary",
sizes: "135 B → 127 B",
}),
).toBe("binary · 135 B → 127 B");
});
});
79 changes: 79 additions & 0 deletions packages/hunk/src/web/format/fileHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Words and path pieces the stream's file headers print.
*
* The header path is the shared label from `core/review/fileLabels.ts` (`old -> new` for a
* rename), split so the page can dim the directory and the old name while the text stays one
* copyable string. The state chip names what kind of change a file is from facts the review
* document already carries — its change kind, untracked flag, and the shared reason a file
* renders no rows — so the page never re-derives a file's kind on its own.
*/
import type { ReviewEmptyDiffReason } from "../../core/review/document";
import { reviewFileLabelParts } from "../../core/review/fileLabels";
import type { ReviewFileChangeKind } from "../../core/review/types";

/** The header path for one file, in the pieces the header styles differently. */
export interface WebFileHeaderPath {
/** The whole label, as the header's text content and tooltip read it. */
text: string;
/** The old path of a rename, drawn dimmed before the arrow. */
previous?: string;
/** The shown path's directory with its trailing slash, or empty. */
directory: string;
basename: string;
}

/** Split one file's header label into its old name, directory, and basename. */
export function webFileHeaderPath(file: {
path: string;
previousPath?: string;
changeKind: ReviewFileChangeKind;
untracked: boolean;
}): WebFileHeaderPath {
const { filename } = reviewFileLabelParts(file);
const renamed = file.previousPath !== undefined && file.previousPath !== file.path;
const previous = renamed ? file.previousPath : undefined;
const shown = previous !== undefined ? filename.slice(previous.length + " -> ".length) : filename;
const slash = shown.lastIndexOf("/");
return {
text: filename,
...(previous !== undefined ? { previous } : {}),
directory: shown.slice(0, slash + 1),
basename: shown.slice(slash + 1),
};
}

/** What a file's state chip says about a file with nothing to render, per shared reason. */
const EMPTY_REASON_WORDS: Record<ReviewEmptyDiffReason, string | null> = {
"rename-only": null,
binary: "binary",
"too-large": "too large",
"new-file": "empty",
"deleted-file": "empty",
"no-hunks": "no textual changes",
};

/**
* The quiet chip after a file's path: "new", "deleted", "untracked", "renamed, no content
* changes", and for a file that renders no rows the shared reason ("binary", "too large"),
* joined with ` · `. Returns null for an ordinary modified file.
*
* `sizes` is extra detail a binary file may carry when the page already holds it.
*/
export function webFileStateChip(file: {
changeKind: ReviewFileChangeKind;
untracked: boolean;
emptyReason?: ReviewEmptyDiffReason;
sizes?: string;
}): string | null {
const words: string[] = [];
if (file.untracked) words.push("untracked");
else if (file.changeKind === "new") words.push("new");
else if (file.changeKind === "deleted") words.push("deleted");
if (file.changeKind === "rename-pure" || file.emptyReason === "rename-only") {
words.push("renamed, no content changes");
}
const reason = file.emptyReason ? EMPTY_REASON_WORDS[file.emptyReason] : null;
if (reason) words.push(reason);
if (file.emptyReason === "binary" && file.sizes) words.push(file.sizes);
return words.length > 0 ? words.join(" · ") : null;
}
18 changes: 18 additions & 0 deletions packages/hunk/src/web/icons/iconPaths.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ export const ICON_PATHS = {
"chevron-right": <path d="m6 4 4 4-4 4" />,
/** A chevron pointing up. */
"chevron-up": <path d="m4 10 4-4 4 4" />,
/** An arrow pointing right. */
"arrow-right": <path d="M3 8h10M9 4l4 4-4 4" />,
/** Two overlapping sheets. */
copy: (
<>
<rect x="5.5" y="5.5" width="8" height="8" rx="1.5" />
<path d="M10.5 3.5v-.5A1.5 1.5 0 0 0 9 1.5H4A1.5 1.5 0 0 0 2.5 3v5A1.5 1.5 0 0 0 4 9.5h.5" />
</>
),
/** Angle brackets around code. */
code: <path d="M5.5 4.5 2 8l3.5 3.5M10.5 4.5 14 8l-3.5 3.5" />,
/** An open eye. */
eye: (
<>
<path d="M1.5 8S4 3.5 8 3.5 14.5 8 14.5 8 12 12.5 8 12.5 1.5 8 1.5 8z" />
<circle cx="8" cy="8" r="2" />
</>
),
/** A check mark. */
check: <path d="m3.5 8.5 3 3 6-7" />,
/** A cross. */
Expand Down
59 changes: 50 additions & 9 deletions packages/hunk/src/web/previews/FilePreviews.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/** @jsxImportSource react */
/**
* The stream's preview slots: a header toggle for Markdown files, and a body that shows an
* image preview or rendered Markdown in place of the default body when either applies.
* The stream's preview slots: a header toggle for Markdown files, a binary file's byte sizes
* for its header chip, and a body that shows an image preview or rendered Markdown in place of
* the default body when either applies.
*
* The stream renders these slots and knows nothing else about previews; everything they read
* comes from `PreviewContext`, which the page provides.
*/
import { useContext, useSyncExternalStore, type ReactNode } from "react";
import type { ReviewFile } from "../../core/review/types";
import { Icon, type IconName } from "../icons/Icon";
import { isPreviewImagePath } from "./imageSniff";
import { ImagePreview } from "./ImagePreview";
import { formatPreviewBytes, ImagePreview } from "./ImagePreview";
import { MarkdownPreview } from "./MarkdownPreview";
import { isMarkdownPath, markdownPreviewSide, PreviewContext } from "./previewContext";

Expand Down Expand Up @@ -53,13 +55,25 @@ export function FilePreviewHeaderAction({
}) {
const { context, available, rendered } = useMarkdownSource(fileKey, path, changeKind);
if (!available || !context) return null;
const modes = [
{ id: "diff", label: "Diff", title: "Show the diff", checked: !rendered },
{ id: "rendered", label: "Rendered", title: "Show the rendered document", checked: rendered },
] as const;
const modes: readonly {
id: "diff" | "rendered";
label: string;
icon: IconName;
title: string;
checked: boolean;
}[] = [
{ id: "diff", label: "Diff", icon: "code", title: "Show the diff", checked: !rendered },
{
id: "rendered",
label: "Rendered",
icon: "eye",
title: "Show the rendered document",
checked: rendered,
},
];
return (
<div
className="segmented small markdown-mode"
className="segmented icon-segmented markdown-mode"
role="radiogroup"
aria-label={`View ${path}`}
data-testid="markdown-mode"
Expand All @@ -71,18 +85,45 @@ export function FilePreviewHeaderAction({
role="radio"
aria-checked={mode.checked}
data-testid={`markdown-mode-${mode.id}`}
aria-label={mode.label}
title={mode.title}
onClick={() => {
if (!mode.checked) context.markdownModes.toggle(fileKey);
}}
>
{mode.label}
<Icon name={mode.icon} size={14} />
</button>
))}
</div>
);
}

/**
* A binary file's byte sizes as the page's resource catalog already states them
* (`135 B → 127 B`, or one size for an added or deleted file); nothing is read to learn them.
*/
export function useBinaryFileSizes(fileKey: string, binary: boolean): string | undefined {
const context = useContext(PreviewContext);
if (!binary || !context) return undefined;
const sizes = (["old", "new"] as const).flatMap((side) => {
const bytes = context
.resources(fileKey)
.find((resource) => resource.kind === "bytes" && resource.side === side);
return bytes?.byteLength !== undefined ? [formatPreviewBytes(bytes.byteLength)] : [];
});
return sizes.length > 0 ? sizes.join(" → ") : undefined;
}

/**
* Whether a file that renders no rows still has a body to show: an image preview may stand in
* for a binary image, and a too-large file says why it is not drawn. Every other such file is
* a header-only row whose state chip explains it.
*/
export function emptyFileHasBody(file: ReviewFile, emptyReason: string) {
if (emptyReason === "too-large") return true;
return emptyReason === "binary" && file.flags.binary && isPreviewImagePath(file.path);
}

/** The body: an image preview, rendered Markdown, or the stream's own body. */
export function FilePreviewBody({
fileKey,
Expand Down
Loading
Loading