ui / src / components / repo-ide / staged-changes.tsmain at d68d7e1
import { useSyncExternalStore } from "react";
import { z } from "zod";
import type { RepoFileChange } from "iterate/api";
const FileEntry = z.discriminatedUnion("type", [
z.object({ type: z.literal("write"), content: z.string() }),
z.object({ type: z.literal("delete") }),
]);
export type FileEntry = z.infer<typeof FileEntry>;
export type FileChange = { working?: FileEntry; staged?: FileEntry };
export type WorkingTreeChanges = ReadonlyMap<string, FileChange>;
/** What localStorage holds: entries a version of this store no longer writes fail the parse, and
* a store that fails it starts empty. */
const PersistedChanges = z.array(
z.tuple([z.string(), z.object({ working: FileEntry.optional(), staged: FileEntry.optional() })]),
);
/**
* The repo IDE's in-browser working tree, git-shaped: every path carries up
* to two snapshots — `working` (live uncommitted edits) and `staged` (what
* the next commit will take when anything is staged). Nothing here touches
* the backend; Commit flushes staged entries (or everything when nothing is
* staged) through one `repo.commitFiles` batch.
*
* Persistence: the whole map mirrors into localStorage keyed by repo AND
* HEAD commit oid, so a reload keeps your edits — but a HEAD that moved
* underneath you (someone else committed) orphans the old key instead of
* producing nonsense diffs against the new checkout.
*/
class WorkingTreeStore {
#changes: ReadonlyMap<string, FileChange>;
#listeners = new Set<() => void>();
readonly storageKey: string;
constructor(storageKey: string) {
this.storageKey = storageKey;
this.#changes = loadPersisted(storageKey);
}
/** Stable snapshot — replaced (never mutated) on every change. */
get changes(): WorkingTreeChanges {
return this.#changes;
}
subscribe = (listener: () => void): (() => void) => {
this.#listeners.add(listener);
return () => void this.#listeners.delete(listener);
};
/** Set (or with undefined, clear) the live uncommitted edit for a path.
* An edit identical to the staged snapshot is no edit at all — the file
* belongs only in Staged Changes then, like vscode. */
setWorking(path: string, entry: FileEntry | undefined): void {
this.#patch(path, (change) => ({
...change,
working: entry && change.staged && entriesEqual(entry, change.staged) ? undefined : entry,
}));
}
/** Set (or with undefined, clear) the staged snapshot for a path. A working
* edit the new snapshot just absorbed (block staging staged the last dirty
* chunk) is dropped for the same reason. */
setStaged(path: string, entry: FileEntry | undefined): void {
this.#patch(path, (change) => ({
working:
entry && change.working && entriesEqual(change.working, entry) ? undefined : change.working,
staged: entry,
}));
}
/** git add: the working entry becomes the staged snapshot. */
stage(path: string): void {
this.#patch(path, (change) => ({ staged: change.working || change.staged }));
}
/** git reset a path: drop the staged snapshot. Working edits survive; when
* there are none the staged snapshot becomes the working entry (so the
* change moves back to Changes instead of vanishing). */
unstage(path: string): void {
this.#patch(path, (change) => ({ working: change.working || change.staged }));
}
/** Revert a path's live edits to its baseline (staged snapshot, else HEAD). */
discardWorking(path: string): void {
this.#patch(path, (change) => ({ staged: change.staged }));
}
/** Post-commit cleanup that respects edits made while the commit RPC was in
* flight: a slot is cleared only while it still equals the entry that was
* committed; anything newer survives (and migrates to the new HEAD's store). */
clearCommitted(committed: ReadonlyMap<string, FileEntry>): void {
const next = new Map(this.#changes);
for (const [path, entry] of committed) {
const change = next.get(path);
if (!change) continue;
const workingCommitted = change.working && entriesEqual(change.working, entry);
const working = workingCommitted ? undefined : change.working;
// a commit of the live edit covers an older staged snapshot of the same path
const staged =
workingCommitted || (change.staged && entriesEqual(change.staged, entry))
? undefined
: change.staged;
if (working || staged) next.set(path, { working, staged });
else next.delete(path);
}
this.#commit(next);
}
discardAll(): void {
if (this.#changes.size === 0) return;
this.#commit(new Map());
}
/** Move every remaining change onto another store (post-commit: HEAD moved,
* surviving working edits belong to the new oid's key). */
migrateTo(other: WorkingTreeStore): void {
if (this === other) return;
const merged = new Map(other.changes);
for (const [path, change] of this.#changes) merged.set(path, change);
other.#commit(merged);
this.#commit(new Map());
}
#patch(path: string, update: (change: FileChange) => FileChange): void {
const next = new Map(this.#changes);
const updated = update(next.get(path) ?? {});
if (updated.working || updated.staged)
next.set(path, { working: updated.working, staged: updated.staged });
else next.delete(path);
this.#commit(next);
}
#commit(next: ReadonlyMap<string, FileChange>): void {
this.#changes = next;
persist(this.storageKey, next);
for (const listener of this.#listeners) listener();
}
}
function entriesEqual(left: FileEntry, right: FileEntry): boolean {
if (left.type !== right.type) return false;
if (left.type === "delete") return true;
return right.type === "write" && left.content === right.content;
}
/**
* Stores keyed per repo AND HEAD oid, module-level so staged work survives
* client-side navigation; localStorage carries it across reloads. Stale keys
* for the same repo at older heads are swept — their diffs would be against
* a checkout that no longer exists.
*/
const stores = new Map<string, WorkingTreeStore>();
export function workingTreeStore(input: { mindId: string; repoPath: string; commitOid: string }) {
const repoPrefix = `repo-ide-working-tree:${input.mindId}:${input.repoPath}:`;
const key = `${repoPrefix}${input.commitOid}`;
const existing = stores.get(key);
if (existing) return existing;
sweepStaleKeys(repoPrefix, key);
const created = new WorkingTreeStore(key);
stores.set(key, created);
return created;
}
export function useWorkingTree(store: WorkingTreeStore): WorkingTreeChanges {
return useSyncExternalStore(
store.subscribe,
() => store.changes,
() => store.changes,
);
}
/** The change a path currently represents, staged or not. */
export function effectiveEntry(change: FileChange): FileEntry | undefined {
return change.working || change.staged;
}
/** The pierre-tree git-status annotation for every changed path. */
export function workingTreeGitStatus(
changes: WorkingTreeChanges,
headPaths: ReadonlySet<string>,
): Array<{ path: string; status: "added" | "deleted" | "modified" }> {
return [...changes].flatMap(([path, change]) => {
const entry = effectiveEntry(change);
if (!entry) return [];
return [
{
path,
status:
entry.type === "delete"
? ("deleted" as const)
: headPaths.has(path)
? ("modified" as const)
: ("added" as const),
},
];
});
}
/** One working-tree entry as the wire shape `repo.commitFiles` takes. */
function fileChangeForEntry(path: string, entry: FileEntry): RepoFileChange {
if (entry.type === "delete") return { path, delete: true };
return { path, content: entry.content };
}
/**
* What Commit sends: the staged snapshots when anything is staged (vscode's
* "commit what's staged"), otherwise every change ("commit everything").
*/
export function commitPlan(changes: WorkingTreeChanges): {
mode: "staged" | "everything";
paths: string[];
fileChanges: RepoFileChange[];
/** What each path was sent as, for `clearCommitted` once the commit lands. */
entries: Map<string, FileEntry>;
} {
const staged = [...changes].filter(([, change]) => change.staged);
const pick = staged.length > 0 ? staged : [...changes];
const mode = staged.length > 0 ? ("staged" as const) : ("everything" as const);
const fileChanges: RepoFileChange[] = [];
const paths: string[] = [];
const entries = new Map<string, FileEntry>();
for (const [path, change] of pick) {
const entry = mode === "staged" ? change.staged : effectiveEntry(change);
if (!entry) continue;
paths.push(path);
entries.set(path, entry);
fileChanges.push(fileChangeForEntry(path, entry));
}
return { mode, paths, fileChanges, entries };
}
function persist(key: string, changes: WorkingTreeChanges): void {
try {
if (changes.size === 0) localStorage.removeItem(key);
else localStorage.setItem(key, JSON.stringify([...changes]));
} catch {
// Quota exceeded or storage unavailable: the in-memory store still works,
// the change just won't survive a reload.
}
}
function loadPersisted(key: string): ReadonlyMap<string, FileChange> {
try {
const raw = localStorage.getItem(key);
if (!raw) return new Map();
const parsed = PersistedChanges.safeParse(JSON.parse(raw));
return new Map(parsed.success ? parsed.data : []);
} catch {
return new Map();
}
}
function sweepStaleKeys(repoPrefix: string, currentKey: string): void {
try {
const stale: string[] = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key?.startsWith(repoPrefix) && key !== currentKey) stale.push(key);
}
for (const key of stale) localStorage.removeItem(key);
} catch {
// Storage unavailable — nothing to sweep.
}
}