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
20 changes: 18 additions & 2 deletions apps/desktop/src/lib/trpc/routers/changes/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import {
parseNameStatus,
} from "./utils/parse-status";

/** Server-side cache to avoid re-running git commands when polled frequently */
const STATUS_CACHE_TTL_MS = 2_000;
const statusCache = new Map<
string,
{ result: GitChangesStatus; timestamp: number }
>();

export const createStatusRouter = () => {
return router({
getStatus: publicProcedure
Expand All @@ -23,8 +30,14 @@ export const createStatusRouter = () => {
.query(async ({ input }): Promise<GitChangesStatus> => {
assertRegisteredWorktree(input.worktreePath);

const git = simpleGit(input.worktreePath);
const defaultBranch = input.defaultBranch || "main";
const cacheKey = `${input.worktreePath}:${defaultBranch}`;
const cached = statusCache.get(cacheKey);
if (cached && Date.now() - cached.timestamp < STATUS_CACHE_TTL_MS) {
return cached.result;
}

const git = simpleGit(input.worktreePath);

const status = await getStatusNoLock(input.worktreePath);
const parsed = parseGitStatus(status);
Expand All @@ -41,7 +54,7 @@ export const createStatusRouter = () => {
applyUntrackedLineCount(input.worktreePath, parsed.untracked),
]);

return {
const result: GitChangesStatus = {
branch: parsed.branch,
defaultBranch,
againstBase: branchComparison.againstBase,
Expand All @@ -55,6 +68,9 @@ export const createStatusRouter = () => {
pullCount: trackingStatus.pullCount,
hasUpstream: trackingStatus.hasUpstream,
};

statusCache.set(cacheKey, { result, timestamp: Date.now() });
return result;
}),

getCommitFiles: publicProcedure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function CommitSection({
onToggleFile,
scrollElementRef,
}: CommitSectionProps) {
const [isCommitExpanded, setIsCommitExpanded] = useState(true);
const [isCommitExpanded, setIsCommitExpanded] = useState(false);

const { data: commitFiles } = electronTrpc.changes.getCommitFiles.useQuery(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useVirtualizer } from "@tanstack/react-virtual";
import { type RefObject, useCallback, useRef } from "react";
import { defaultRangeExtractor, useVirtualizer } from "@tanstack/react-virtual";
import { type RefObject, useRef } from "react";
import type { ChangeCategory, ChangedFile } from "shared/changes-types";
import { FileDiffSection } from "../FileDiffSection";

Expand Down Expand Up @@ -36,35 +36,13 @@ export function VirtualizedFileList({
isActioning = false,
}: VirtualizedFileListProps) {
const listRef = useRef<HTMLDivElement>(null);
const renderedIndicesRef = useRef<Set<number>>(new Set());

const rangeExtractor = useCallback(
(range: { startIndex: number; endIndex: number }) => {
const indices = new Set<number>(renderedIndicesRef.current);

const start = Math.max(0, range.startIndex - OVERSCAN);
const end = Math.min(files.length - 1, range.endIndex + OVERSCAN);
for (let i = start; i <= end; i++) {
indices.add(i);
}

for (const idx of indices) {
if (idx >= files.length) {
indices.delete(idx);
}
}

renderedIndicesRef.current = indices;
return Array.from(indices).sort((a, b) => a - b);
},
[files.length],
);

const virtualizer = useVirtualizer({
count: files.length,
getScrollElement: () => scrollElementRef.current,
estimateSize: () => ESTIMATED_COLLAPSED_HEIGHT,
rangeExtractor,
rangeExtractor: defaultRangeExtractor,
overscan: OVERSCAN,
scrollMargin: listRef.current?.offsetTop ?? 0,
});

Expand Down
Loading