Fix undo and redo by using initial commit hash for chat (#94)
This commit is contained in:
@@ -189,12 +189,20 @@ export function registerAppHandlers() {
|
||||
});
|
||||
|
||||
// Create initial commit
|
||||
await git.commit({
|
||||
const commitHash = await git.commit({
|
||||
fs: fs,
|
||||
dir: fullAppPath,
|
||||
message: "Init from react vite template",
|
||||
author: await getGitAuthor(),
|
||||
});
|
||||
|
||||
// Update chat with initial commit hash
|
||||
await db
|
||||
.update(chats)
|
||||
.set({
|
||||
initialCommitHash: commitHash,
|
||||
})
|
||||
.where(eq(chats.id, chat.id));
|
||||
} catch (error) {
|
||||
logger.error("Error in background app initialization:", error);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,59 @@
|
||||
import { ipcMain } from "electron";
|
||||
import { db } from "../../db";
|
||||
import { chats } from "../../db/schema";
|
||||
import { apps, chats, messages } from "../../db/schema";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import type { ChatSummary } from "../../lib/schemas";
|
||||
import * as git from "isomorphic-git";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import log from "electron-log";
|
||||
import { getDyadAppPath } from "../../paths/paths";
|
||||
|
||||
const logger = log.scope("chat_handlers");
|
||||
|
||||
export function registerChatHandlers() {
|
||||
ipcMain.handle("create-chat", async (_, appId: number) => {
|
||||
// Get the app's path first
|
||||
const app = await db.query.apps.findFirst({
|
||||
where: eq(apps.id, appId),
|
||||
columns: {
|
||||
path: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!app) {
|
||||
throw new Error("App not found");
|
||||
}
|
||||
|
||||
let initialCommitHash = null;
|
||||
try {
|
||||
// Get the current git revision of main branch
|
||||
initialCommitHash = await git.resolveRef({
|
||||
fs,
|
||||
dir: getDyadAppPath(app.path),
|
||||
ref: "main",
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("Error getting git revision:", error);
|
||||
// Continue without the git revision
|
||||
}
|
||||
|
||||
// Create a new chat
|
||||
const [chat] = await db
|
||||
.insert(chats)
|
||||
.values({
|
||||
appId,
|
||||
initialCommitHash,
|
||||
})
|
||||
.returning();
|
||||
|
||||
logger.info(
|
||||
"Created chat:",
|
||||
chat.id,
|
||||
"for app:",
|
||||
appId,
|
||||
"with initial commit hash:",
|
||||
initialCommitHash
|
||||
);
|
||||
return chat.id;
|
||||
});
|
||||
|
||||
@@ -70,7 +110,17 @@ export function registerChatHandlers() {
|
||||
await db.delete(chats).where(eq(chats.id, chatId));
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error("Error deleting chat:", error);
|
||||
logger.error("Error deleting chat:", error);
|
||||
return { success: false, error: (error as Error).message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle("delete-messages", async (_, chatId: number) => {
|
||||
try {
|
||||
await db.delete(messages).where(eq(messages.chatId, chatId));
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
logger.error("Error deleting messages:", error);
|
||||
return { success: false, error: (error as Error).message };
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user