Refactor imports, streamline database IPC handlers, and improve offline support

- Replace absolute import paths with relative paths for consistency across files.
- Transition database operations in Electron main process to modular handlers in `ipc/book.ipc.ts` using the `createHandler` pattern.
- Update database sync service to use `window.electron.invoke()` for improved reliability and structure.
- Refactor `AddNewBookForm` to handle both online and offline book creation seamlessly.
This commit is contained in:
natreex
2025-11-18 21:28:41 -05:00
parent d018e75be4
commit 004008cc13
13 changed files with 45 additions and 237 deletions

View File

@@ -222,192 +222,8 @@ ipcMain.handle('db-initialize', (_event, userId: string, encryptionKey: string)
}
});
/**
* Get all books
*/
ipcMain.handle('db-get-books', () => {
try {
const db = getDatabaseService();
const books = db.getBooks();
return { success: true, data: books };
} catch (error) {
console.error('Failed to get books:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Get single book with all data
*/
ipcMain.handle('db-get-book', (_event, bookId: string) => {
try {
const db = getDatabaseService();
const book = db.getBook(bookId);
return { success: true, data: book };
} catch (error) {
console.error('Failed to get book:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Save book
*/
ipcMain.handle('db-save-book', (_event, book: any, authorId?: string) => {
try {
const db = getDatabaseService();
db.saveBook(book, authorId);
return { success: true };
} catch (error) {
console.error('Failed to save book:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Delete book
*/
ipcMain.handle('db-delete-book', (_event, bookId: string) => {
try {
const db = getDatabaseService();
db.deleteBook(bookId);
return { success: true };
} catch (error) {
console.error('Failed to delete book:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Save chapter
*/
ipcMain.handle('db-save-chapter', (_event, chapter: any, bookId: string, contentId?: string) => {
try {
const db = getDatabaseService();
db.saveChapter(chapter, bookId, contentId);
return { success: true };
} catch (error) {
console.error('Failed to save chapter:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Get characters for a book
*/
ipcMain.handle('db-get-characters', (_event, bookId: string) => {
try {
const db = getDatabaseService();
const characters = db.getCharacters(bookId);
return { success: true, data: characters };
} catch (error) {
console.error('Failed to get characters:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Save character
*/
ipcMain.handle('db-save-character', (_event, character: any, bookId: string) => {
try {
const db = getDatabaseService();
db.saveCharacter(character, bookId);
return { success: true };
} catch (error) {
console.error('Failed to save character:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Get AI conversations for a book
*/
ipcMain.handle('db-get-conversations', (_event, bookId: string) => {
try {
const db = getDatabaseService();
const conversations = db.getConversations(bookId);
return { success: true, data: conversations };
} catch (error) {
console.error('Failed to get conversations:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Save AI conversation
*/
ipcMain.handle('db-save-conversation', (_event, conversation: any, bookId: string) => {
try {
const db = getDatabaseService();
db.saveConversation(conversation, bookId);
return { success: true };
} catch (error) {
console.error('Failed to save conversation:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Get sync status
*/
ipcMain.handle('db-get-sync-status', () => {
try {
const db = getDatabaseService();
const status = db.getSyncStatus();
return { success: true, data: status };
} catch (error) {
console.error('Failed to get sync status:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
/**
* Get pending changes
*/
ipcMain.handle('db-get-pending-changes', (_event, limit: number = 100) => {
try {
const db = getDatabaseService();
const changes = db.getPendingChanges(limit);
return { success: true, data: changes };
} catch (error) {
console.error('Failed to get pending changes:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
};
}
});
// NOTE: All database IPC handlers have been moved to ./ipc/book.ipc.ts
// and use the new createHandler() pattern with auto userId/lang injection
app.whenReady().then(() => {
console.log('App ready, isDev:', isDev);