Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"imports": {
"$fresh/": "https://deno.land/x/fresh@1.7.3/",
"@swanfactory/hclang": "jsr:@swanfactory/hclang@^0.6.9",
"@swanfactory/hclang": "jsr:@swanfactory/hclang@^0.7.1",
"preact": "https://esm.sh/preact@10.22.0",
"preact/": "https://esm.sh/preact@10.22.0/",
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
Expand Down
86 changes: 81 additions & 5 deletions islands/Interpreter.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { useState } from "preact/hooks";
// import { execute } from "@swanfactory/hclang";
import { useState, useEffect } from "preact/hooks";
import { execute } from "@swanfactory/hclang";

interface HistoryItem {
code: string;
result: string;
timestamp: number;
}

function evaluateCode(code: string): string {
console.log(`Evaluating code: ${code}`);
// console.log(execute);
console.log(execute);
try {
const result = code.toUpperCase();
const result = execute(code);
console.log(`Result: ${result}`);
return result.toString();
} catch (error: unknown) {
Expand All @@ -19,20 +25,59 @@ export default function Interpreter() {
const [error, setError] = useState<string>("");
const [isLoading, setIsLoading] = useState<boolean>(false);
const [result, setResult] = useState<string>("");
const [history, setHistory] = useState<HistoryItem[]>([]);

useEffect(() => {
const savedHistory = localStorage.getItem('hc-history');
if (savedHistory) {
setHistory(JSON.parse(savedHistory));
}
}, []);

const handleEvaluation = (code: string): void => {
setError("");
setIsLoading(true);
try {
const evalResult = evaluateCode(code);
// Build context from history
const context = history
.map(item => item.code)
.reverse()
.join("\n");

// Combine history with new code
const fullCode = context ? `${context}\n${code}` : code;

const evalResult = evaluateCode(fullCode);
setResult(evalResult);

const newHistoryItem = {
code,
result: evalResult,
timestamp: Date.now()
};

const updatedHistory = [newHistoryItem, ...history].slice(0, 50); // Keep last 50 items
setHistory(updatedHistory);
localStorage.setItem('hc-history', JSON.stringify(updatedHistory));
} catch (e: unknown) {
setError(e instanceof Error ? e.message : String(e));
} finally {
setIsLoading(false);
}
};

const handleHistoryClick = (historyItem: HistoryItem) => {
setText((prev) => prev + "\n" + historyItem.code);
};

const clearHistory = () => {
setHistory([]);
setText("");
setResult("");
setError("");
localStorage.removeItem('hc-history');
};

return (
<div>
<textarea
Expand All @@ -55,6 +100,37 @@ export default function Interpreter() {
</div>
)}
</div>
<div style={{ marginTop: "20px" }}>
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
<h3>History</h3>
<button
onClick={clearHistory}
style={{ padding: "4px 8px" }}
>
Clear History
</button>
</div>
<div style={{ maxHeight: "200px", overflowY: "auto" }}>
{history.map((item, index) => (
<div
key={item.timestamp}
onClick={() => handleHistoryClick(item)}
style={{
cursor: "pointer",
padding: "8px",
margin: "4px",
border: "1px solid #ccc",
borderRadius: "4px"
}}
>
<pre style={{ margin: 0 }}>{item.code}</pre>
<small style={{ color: "#666" }}>
{new Date(item.timestamp).toLocaleString()}
</small>
</div>
))}
</div>
</div>
</div>
);
}