clean up history rendering
This commit is contained in:
parent
13f93e259d
commit
637f75b93e
@ -2,7 +2,7 @@ import toast from "react-hot-toast";
|
|||||||
import classNames from "classnames";
|
import classNames from "classnames";
|
||||||
|
|
||||||
import { Badge } from "../ui/badge";
|
import { Badge } from "../ui/badge";
|
||||||
import { summarizeHistoryItem } from "./utils";
|
import { renderHistory } from "./utils";
|
||||||
import {
|
import {
|
||||||
Collapsible,
|
Collapsible,
|
||||||
CollapsibleContent,
|
CollapsibleContent,
|
||||||
@ -19,45 +19,20 @@ interface Props {
|
|||||||
export default function HistoryDisplay({ shouldDisableReverts }: Props) {
|
export default function HistoryDisplay({ shouldDisableReverts }: Props) {
|
||||||
const { commits, head, setHead } = useProjectStore();
|
const { commits, head, setHead } = useProjectStore();
|
||||||
|
|
||||||
// TODO*: Clean this up more
|
|
||||||
|
|
||||||
// Put all commits into an array and sort by created date (oldest first)
|
// Put all commits into an array and sort by created date (oldest first)
|
||||||
const flatHistory = Object.values(commits).sort(
|
const flatHistory = Object.values(commits).sort(
|
||||||
(a, b) =>
|
(a, b) =>
|
||||||
new Date(a.date_created).getTime() - new Date(b.date_created).getTime()
|
new Date(a.date_created).getTime() - new Date(b.date_created).getTime()
|
||||||
);
|
);
|
||||||
|
|
||||||
const setParentVersion = (
|
// Annotate history items with a summary, parent version, etc.
|
||||||
parentHash: string | null,
|
const renderedHistory = renderHistory(flatHistory);
|
||||||
currentHash: string | null
|
|
||||||
) => {
|
|
||||||
if (!parentHash) return null;
|
|
||||||
const parentIndex = flatHistory.findIndex(
|
|
||||||
(item) => item.hash === parentHash
|
|
||||||
);
|
|
||||||
const currentIndex = flatHistory.findIndex(
|
|
||||||
(item) => item.hash === currentHash
|
|
||||||
);
|
|
||||||
|
|
||||||
// Only set parent version if the parent is not the previous commit
|
return renderedHistory.length === 0 ? null : (
|
||||||
// and if it's not the first commit
|
|
||||||
return parentIndex !== -1 && parentIndex != currentIndex - 1
|
|
||||||
? parentIndex + 1
|
|
||||||
: null;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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 annotatedHistory.length === 0 ? null : (
|
|
||||||
<div className="flex flex-col h-screen">
|
<div className="flex flex-col h-screen">
|
||||||
<h1 className="font-bold mb-2">Versions</h1>
|
<h1 className="font-bold mb-2">Versions</h1>
|
||||||
<ul className="space-y-0 flex flex-col-reverse">
|
<ul className="space-y-0 flex flex-col-reverse">
|
||||||
{annotatedHistory.map((item, index) => (
|
{renderedHistory.map((item, index) => (
|
||||||
<li key={index}>
|
<li key={index}>
|
||||||
<Collapsible>
|
<Collapsible>
|
||||||
<div
|
<div
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { Commit, CommitHash } from "./history_types";
|
import { Commit, CommitHash, CommitType } from "./history_types";
|
||||||
|
|
||||||
export function extractHistory(
|
export function extractHistory(
|
||||||
hash: CommitHash,
|
hash: CommitHash,
|
||||||
@ -29,6 +29,37 @@ export function extractHistory(
|
|||||||
return flatHistory;
|
return flatHistory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function displayHistoryItemType(itemType: CommitType) {
|
||||||
|
switch (itemType) {
|
||||||
|
case "ai_create":
|
||||||
|
return "Create";
|
||||||
|
case "ai_edit":
|
||||||
|
return "Edit";
|
||||||
|
case "code_create":
|
||||||
|
return "Imported from code";
|
||||||
|
default: {
|
||||||
|
const exhaustiveCheck: never = itemType;
|
||||||
|
throw new Error(`Unhandled case: ${exhaustiveCheck}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const setParentVersion = (commit: Commit, history: Commit[]) => {
|
||||||
|
// If the commit has no parent, return null
|
||||||
|
if (!commit.parentHash) return null;
|
||||||
|
|
||||||
|
const parentIndex = history.findIndex(
|
||||||
|
(item) => item.hash === commit.parentHash
|
||||||
|
);
|
||||||
|
const currentIndex = history.findIndex((item) => item.hash === commit.hash);
|
||||||
|
|
||||||
|
// Only set parent version if the parent is not the previous commit
|
||||||
|
// and parent exists
|
||||||
|
return parentIndex !== -1 && parentIndex != currentIndex - 1
|
||||||
|
? parentIndex + 1
|
||||||
|
: null;
|
||||||
|
};
|
||||||
|
|
||||||
export function summarizeHistoryItem(commit: Commit) {
|
export function summarizeHistoryItem(commit: Commit) {
|
||||||
const commitType = commit.type;
|
const commitType = commit.type;
|
||||||
switch (commitType) {
|
switch (commitType) {
|
||||||
@ -44,3 +75,19 @@ export function summarizeHistoryItem(commit: Commit) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const renderHistory = (history: Commit[]) => {
|
||||||
|
const renderedHistory = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < history.length; i++) {
|
||||||
|
const commit = history[i];
|
||||||
|
renderedHistory.push({
|
||||||
|
...commit,
|
||||||
|
type: displayHistoryItemType(commit.type),
|
||||||
|
summary: summarizeHistoryItem(commit),
|
||||||
|
parentVersion: setParentVersion(commit, history),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return renderedHistory;
|
||||||
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user