Skip to content
Merged
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
11 changes: 8 additions & 3 deletions hooks/useRepositoryManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { useState, useCallback, useRef } from 'react';
import { useState, useCallback, useRef, useEffect } from 'react';
import type { Repository, LogEntry, Task, GlobalSettings, TaskStep, GitRepository } from '../types';
import { RepoStatus, BuildHealth, LogLevel, TaskStepType, VcsType } from '../types';

Expand Down Expand Up @@ -37,6 +37,11 @@ export const useRepositoryManager = ({ repositories, updateRepository }: { repos
const [logs, setLogs] = useState<Record<string, LogEntry[]>>({});
const [isProcessing, setIsProcessing] = useState<Set<string>>(new Set());
const activeExecutionsRef = useRef<Map<string, { stepExecutionId: string | null }>>(new Map());
const repositoriesRef = useRef<Repository[]>(repositories);

useEffect(() => {
repositoriesRef.current = repositories;
}, [repositories]);
Comment on lines +40 to +44

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Update ref synchronously to avoid lost edits

The new repositoriesRef is populated inside a useEffect, which only runs after the component renders. If a repository is saved (causing a re‑render) and a task finishes before this effect fires, updateRepoStatus will still read the stale array from before the edit and call updateRepository with that snapshot, overwriting the just-saved changes. To guarantee status updates always merge with the latest repository data, assign repositoriesRef.current = repositories during render (or use useLayoutEffect) so the ref is current before any async callbacks fire.

Useful? React with 👍 / 👎.


const addLogEntry = useCallback((repoId: string, message: string, level: LogLevel) => {
const newEntry: LogEntry = {
Expand All @@ -51,7 +56,7 @@ export const useRepositoryManager = ({ repositories, updateRepository }: { repos
}, []);

const updateRepoStatus = useCallback((repoId: string, status: RepoStatus, buildHealth?: BuildHealth) => {
const repoToUpdate = repositories.find(r => r.id === repoId);
const repoToUpdate = repositoriesRef.current.find(r => r.id === repoId);
if (repoToUpdate) {
updateRepository({
...repoToUpdate,
Expand All @@ -60,7 +65,7 @@ export const useRepositoryManager = ({ repositories, updateRepository }: { repos
...(buildHealth && { buildHealth }),
});
}
}, [repositories, updateRepository]);
}, [updateRepository]);

const runTask = useCallback(async (
repo: Repository,
Expand Down