import toast from "react-hot-toast"; import classNames from "classnames"; import { Badge } from "../ui/badge"; import { summarizeHistoryItem } from "./utils"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "../ui/collapsible"; import { Button } from "../ui/button"; import { CaretSortIcon } from "@radix-ui/react-icons"; import { useProjectStore } from "../../store/project-store"; interface Props { shouldDisableReverts: boolean; } export default function HistoryDisplay({ shouldDisableReverts }: Props) { const { commits, head, setHead } = useProjectStore(); // TODO: Clean this up const newHistory = Object.values(commits).flatMap((commit) => { if ( commit.type === "ai_create" || commit.type === "ai_edit" || commit.type === "code_create" ) { return { type: commit.type, hash: commit.hash, summary: summarizeHistoryItem(commit), parentHash: commit.parentHash, code: commit.variants[commit.selectedVariantIndex].code, inputs: commit.inputs, date_created: commit.date_created, }; } return []; }); // Sort by date created newHistory.sort( (a, b) => new Date(a.date_created).getTime() - new Date(b.date_created).getTime() ); const setParentVersion = ( parentHash: string | null, currentHash: string | null ) => { if (!parentHash) return null; const parentIndex = newHistory.findIndex( (item) => item.hash === parentHash ); const currentIndex = newHistory.findIndex( (item) => item.hash === currentHash ); return parentIndex !== -1 && parentIndex != currentIndex - 1 ? parentIndex + 1 : null; }; // Update newHistory to include the parent version const updatedHistory = newHistory.map((item) => ({ ...item, parentVersion: setParentVersion(item.parentHash, item.hash), })); return updatedHistory.length === 0 ? null : (

Versions

); }