From 13f93e259d4e557272f10a549185a4757d6d2db6 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Thu, 22 Aug 2024 13:59:25 -0400 Subject: [PATCH] clean up HistoryDisplay a little bit --- .../src/components/history/HistoryDisplay.tsx | 41 ++++++------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/frontend/src/components/history/HistoryDisplay.tsx b/frontend/src/components/history/HistoryDisplay.tsx index 057c07d..5fb6bb2 100644 --- a/frontend/src/components/history/HistoryDisplay.tsx +++ b/frontend/src/components/history/HistoryDisplay.tsx @@ -19,29 +19,10 @@ interface Props { export default function HistoryDisplay({ shouldDisableReverts }: Props) { const { commits, head, setHead } = useProjectStore(); - // TODO: Clean this up + // TODO*: Clean this up more - 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( + // Put all commits into an array and sort by created date (oldest first) + const flatHistory = Object.values(commits).sort( (a, b) => new Date(a.date_created).getTime() - new Date(b.date_created).getTime() ); @@ -51,28 +32,32 @@ export default function HistoryDisplay({ shouldDisableReverts }: Props) { currentHash: string | null ) => { if (!parentHash) return null; - const parentIndex = newHistory.findIndex( + const parentIndex = flatHistory.findIndex( (item) => item.hash === parentHash ); - const currentIndex = newHistory.findIndex( + const currentIndex = flatHistory.findIndex( (item) => item.hash === currentHash ); + + // Only set parent version if the parent is not the previous commit + // and if it's not the first commit return parentIndex !== -1 && parentIndex != currentIndex - 1 ? parentIndex + 1 : null; }; - // Update newHistory to include the parent version - const updatedHistory = newHistory.map((item) => ({ + // Annotate history items with a summary and parent version + const annotatedHistory = flatHistory.map((item) => ({ ...item, + summary: summarizeHistoryItem(item), parentVersion: setParentVersion(item.parentHash, item.hash), })); - return updatedHistory.length === 0 ? null : ( + return annotatedHistory.length === 0 ? null : (

Versions