8000 make history forward chronology and have non-changing indices to refe… · ag-python/screenshot-to-code@f5ddb77 · GitHub
[go: up one dir, main page]

Skip to content

Commit f5ddb77

Browse files
committed
make history forward chronology and have non-changing indices to refer to items
1 parent eff532b commit f5ddb77

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

frontend/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ function App() {
151151
} else {
152152
setAppHistory((prev) => {
153153
const newHistory: History = [
154+
...prev,
154155
{
155156
type: "ai_edit",
156157
code,
157158
inputs: {
158159
prompt: updateInstruction,
159160
},
160161
},
161-
...prev,
162162
];
163-
setCurrentVersion(0);
163+
setCurrentVersion(newHistory.length - 1);
164164
return newHistory;
165165
});
166166
}

frontend/src/components/history/HistoryDisplay.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function HistoryDisplay({
3232
<div className="flex flex-col h-screen">
3333
<h1 className="font-bold mb-2">History</h1>
3434
<ScrollArea className="flex-1 overflow-y-auto">
35-
<ul className="space-y-0">
35+
<ul className="space-y-0 flex flex-col-reverse">
3636
{history.map((item, index) => (
3737
<li
3838
key={index}
@@ -53,7 +53,7 @@ export default function HistoryDisplay({
5353
}
5454
>
5555
<h2 className="text-sm">{displayHistoryItemType(item.type)}</h2>
56-
<h2 className="text-sm">v{history.length - index}</h2>
56+
<h2 className="text-sm">v{index + 1}</h2>
5757
</li>
5858
))}
5959
</ul>

frontend/src/components/history/utils.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { extractHistoryTree } from "./utils";
33

44
const data = [
55
{
6-
type: "ai_edit" as const,
7-
code: "<html>3. edit with better icons and red text</html>",
6+
type: "ai_create" as const,
7+
code: "<html>1. create</html>",
88
inputs: {
9-
prompt: "make text red",
9+
image_url: "",
1010
},
1111
},
1212
{
@@ -17,22 +17,22 @@ const data = [
1717
},
1818
},
1919
{
20-
type: "ai_create" as const,
21-
code: "<html>1. create</html>",
20+
type: "ai_edit" as const,
21+
code: "<html>3. edit with better icons and red text</html>",
2222
inputs: {
23-
image_url: "",
23+
prompt: "make text red",
2424
},
2525
},
2626
];
2727

2828
test("should only include history from this point onward", () => {
29-
expect(extractHistoryTree(data, 0)).toEqual([
29+
expect(extractHistoryTree(data, 2)).toEqual([
3030
"<html>1. create</html>",
3131
"use better icons",
3232
"<html>2. edit with better icons</html>",
3333
"make text red",
3434
"<html>3. edit with better icons and red text</html>",
3535
]);
3636

37-
expect(extractHistoryTree(data, 2)).toEqual(["<html>1. create</html>"]);
37+
expect(extractHistoryTree(data, 0)).toEqual(["<html>1. create</html>"]);
3838
});

frontend/src/components/history/utils.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ export function extractHistoryTree(
44
history: History,
55
version: number
66
): string[] {
7-
// History is in reverse chronological order
8-
97
// Get all history items up to the current version
10-
const extractedHistory = history.slice(version);
11-
12-
const obj: string[] = [];
8+
const extractedHistory = history.slice(0, version + 1);
139

14-
// Reverse the history so that it is in chronological order for the server
15-
extractedHistory.reverse().forEach((item) => {
16-
// Don't include the image for ai_create since the server gets it passed and will include it directly
17-
if (item.type !== "ai_create") {
18-
obj.push(item.inputs.prompt);
10+
// Convert the history into a flat array of strings that the backend expects
11+
const flatHistory: string[] = [];
12+
extractedHistory.forEach((item) => {
13+
if (item.type === "ai_create") {
14+
// Don't include the image for ai_create since the server
15+
// gets it passed and will include it directly
16+
flatHistory.push(item.code);
17+
} else {
18+
flatHistory.push(item.inputs.prompt);
19+
flatHistory.push(item.code);
1920
}
20-
obj.push(item.code);
2121
});
2222

23-
return obj;
23+
return flatHistory;
2424
}

0 commit comments

Comments
 (0)
0