import { useState, useEffect } from 'react' import { Link } from 'react-router-dom' import { formatDistanceToNow } from 'date-fns' import { GitCommit } from 'lucide-react' import { Button } from '@/components/ui/button' import { Skeleton } from '@/components/ui/skeleton' import { getCommits } from '@/lib/gitApi' import type { GitCommit as GitCommitType } from '@/lib/gitApi' import { useRepo } from '@/lib/repo' interface CommitListProps { ref_: string path?: string } const PAGE_SIZE = 30 // Paginated commit history grouped by calendar date. Each row links to the // commit detail page. Used in CodePage's "History" view. export function CommitList({ ref_, path }: CommitListProps) { const repo = useRepo() const [commits, setCommits] = useState([]) const [loading, setLoading] = useState(true) const [loadingMore, setLoadingMore] = useState(false) const [error, setError] = useState(null) const [hasMore, setHasMore] = useState(true) useEffect(() => { setCommits([]) setHasMore(true) setError(null) setLoading(true) getCommits(ref_, { path, limit: PAGE_SIZE }) .then((data) => { setCommits(data) setHasMore(data.length === PAGE_SIZE) }) .catch((e: Error) => setError(e.message)) .finally(() => setLoading(false)) }, [ref_, path]) function loadMore() { const last = commits[commits.length - 1] if (!last) return setLoadingMore(true) getCommits(ref_, { path, limit: PAGE_SIZE, after: last.hash }) .then((data) => { setCommits((prev) => [...prev, ...data]) setHasMore(data.length === PAGE_SIZE) }) .catch((e: Error) => setError(e.message)) .finally(() => setLoadingMore(false)) } if (loading) return if (error) { return (
{error}
) } // Group commits by date (YYYY-MM-DD) const groups = groupByDate(commits) return (
{groups.map(([date, group]) => (

Commits on {date}

{group.map((commit) => ( ))}
))} {hasMore && (
)}
) } function CommitRow({ commit, repo }: { commit: GitCommitType; repo: string | null }) { const commitPath = repo ? `/${repo}/commit/${commit.hash}` : `/commit/${commit.hash}` return (
{commit.message}

{commit.authorName} ·{' '} {formatDistanceToNow(new Date(commit.date), { addSuffix: true })}

{commit.shortHash}
) } function groupByDate(commits: GitCommitType[]): [string, GitCommitType[]][] { const map = new Map() for (const c of commits) { const date = new Date(c.date).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric', }) const group = map.get(date) ?? [] group.push(c) map.set(date, group) } return Array.from(map.entries()) } function CommitListSkeleton() { return (
{Array.from({ length: 2 }).map((_, g) => (
{Array.from({ length: 4 }).map((_, i) => (
))}
))}
) }