import { useEffect, useState } from "react";
import { getLastCommits } from "~/fns/code";
import type { CommitBrief } from "~/lib/code-intel";
/** Lazily loads the latest commit per directory entry after the tree renders, since the walk can be slow on a cold cache. */
export function useLastCommits(owner: string, repo: string, sha: string, path: string) {
const [commits, setCommits] = useState<Record<string, CommitBrief | null> | null>(null);
useEffect(() => {
let cancelled = false;
setCommits(null);
getLastCommits({ data: { owner, repo, ref: sha, path } })
.then((result) => {
if (!cancelled) setCommits(result);
})
.catch(() => {
if (!cancelled) setCommits({});
});
return () => {
cancelled = true;
};
}, [owner, repo, sha, path]);
return commits;
}