import { useState } from "react"; import { Link, useParams, useSearchParams } from "react-router"; import { useQuery } from "@tanstack/react-query"; import { client } from "@/client"; import { GitCommitHorizontal, Search } from "lucide-react"; import { Input } from "@/components/ui/input"; import { cn } from "@/lib/utils"; import { workspaceColor, workspaceInitial } from "@/components/shell/shared"; import PaginationBar from "./pagination"; function formatTimeAgo(timeSecs: number) { const now = Math.floor(Date.now() / 1000); const diff = now - timeSecs; if (diff < 60) return "just now"; if (diff < 3600) return `${Math.floor(diff / 60)}m ago`; if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`; if (diff < 2592000) return `${Math.floor(diff / 86400)}d ago`; return new Date(timeSecs * 1000).toLocaleDateString("en-US", { month: "short", day: "numeric" }); } function AuthorAvatar({ author }: { author: { name: string } }) { return ( {workspaceInitial(author.name)} ); } const LIMIT = 30; export default function CommitsTab() { const { projectName = "", repoName = "" } = useParams(); const [commitsSearchParams, setCommitsSearchParams] = useSearchParams(); const offset = Number(commitsSearchParams.get("offset") ?? "0"); const setOffset = (n: number) => { const next = new URLSearchParams(commitsSearchParams); if (n <= 0) next.delete("offset"); else next.set("offset", String(n)); setCommitsSearchParams(next, { replace: true }); }; const { data: repoData } = useQuery({ queryKey: ["repo", projectName, repoName], queryFn: async () => { const res = await client.gitGetRepo(projectName, repoName); return res.data; }, enabled: Boolean(projectName) && Boolean(repoName), retry: false, }); const defaultBranch = repoData?.default_branch ?? "main"; const [search, setSearch] = useState(""); const [selectedBranch, setSelectedBranch] = useState(defaultBranch); const { data: commits = [], isLoading } = useQuery({ queryKey: ["repo", projectName, repoName, "commits", selectedBranch, offset], queryFn: async () => { const res = await client.gitCommitHistory(projectName, repoName, { branch: selectedBranch, skip: offset, limit: LIMIT }); return res.data.commits; }, enabled: Boolean(projectName) && Boolean(repoName), retry: false, }); const { data: branches = [] } = useQuery({ queryKey: ["repo", projectName, repoName, "branches"], queryFn: async () => { const res = await client.gitListBranches(projectName, repoName); return res.data.branches; }, enabled: Boolean(projectName) && Boolean(repoName), retry: false, }); const filtered = search ? commits.filter((c) => c.message.toLowerCase().includes(search.toLowerCase()) || c.oid.startsWith(search)) : commits; return (
{commit.oid.slice(0, 7)}
{search ? `No commits match "${search}"` : "Push your first commit to this branch to get started."}