Add error handling, enhance syncing, and refactor deletion logic

- Introduce new error messages for syncing and book deletion in `en.json`.
- Update `DeleteBook` to support local-only deletion and synced book management.
- Refine offline/online behavior with `deleteLocalToo` checkbox and update related state handling.
- Extend repository and IPC methods to handle optional IDs for updates.
- Add `SyncQueueContext` for queueing offline changes and improving synchronization workflows.
- Enhance refined text generation logic in `DraftCompanion` and `GhostWriter` components.
- Replace PUT with PATCH for world updates to align with API expectations.
- Streamline `AlertBox` by integrating dynamic translation keys for deletion prompts.
This commit is contained in:
natreex
2026-01-10 15:50:03 -05:00
parent 060693f152
commit 7f34421212
26 changed files with 506 additions and 100 deletions

View File

@@ -487,11 +487,11 @@ export default class Book {
return BookRepo.updateGuideLine(userId, bookId, encryptedTone, encryptedAtmosphere, encryptedWritingStyle, encryptedThemes, encryptedSymbolism, encryptedMotifs, encryptedNarrativeVoice, encryptedPacing, encryptedKeyMessages, encryptedIntendedAudience, lang);
}
public static addNewIncident(userId: string, bookId: string, name: string, lang: 'fr' | 'en' = 'fr'): string {
public static addNewIncident(userId: string, bookId: string, name: string, lang: 'fr' | 'en' = 'fr', existingIncidentId?: string): string {
const userKey: string = getUserEncryptionKey(userId);
const encryptedName:string = System.encryptDataWithUserKey(name,userKey);
const hashedName:string = System.hashElement(name);
const incidentId: string = System.createUniqueId();
const incidentId: string = existingIncidentId || System.createUniqueId();
return BookRepo.insertNewIncident(incidentId, userId, bookId, encryptedName, hashedName, lang);
}
public static async getPlotPoints(userId:string, bookId: string,actChapters:ActChapter[], lang: 'fr' | 'en' = 'fr'):Promise<PlotPoint[]>{
@@ -608,11 +608,11 @@ export default class Book {
return BookRepo.updateBookBasicInformation(userId, encryptedTitle, hashedTitle, encryptedSubTitle, hashedSubTitle, encryptedSummary, publicationDate, wordCount, bookId, lang);
}
static addNewPlotPoint(userId: string, bookId: string, incidentId: string, name: string, lang: 'fr' | 'en' = 'fr'): string {
static addNewPlotPoint(userId: string, bookId: string, incidentId: string, name: string, lang: 'fr' | 'en' = 'fr', existingPlotPointId?: string): string {
const userKey:string = getUserEncryptionKey(userId);
const encryptedName:string = System.encryptDataWithUserKey(name, userKey);
const hashedName:string = System.hashElement(name);
const plotPointId: string = System.createUniqueId();
const plotPointId: string = existingPlotPointId || System.createUniqueId();
return BookRepo.insertNewPlotPoint(plotPointId, userId, bookId, encryptedName, hashedName, incidentId, lang);
}
@@ -620,11 +620,11 @@ export default class Book {
return BookRepo.deletePlotPoint(userId, plotId, lang);
}
public static addNewIssue(userId: string, bookId: string, name: string, lang: 'fr' | 'en' = 'fr'): string {
public static addNewIssue(userId: string, bookId: string, name: string, lang: 'fr' | 'en' = 'fr', existingIssueId?: string): string {
const userKey:string = getUserEncryptionKey(userId);
const encryptedName:string = System.encryptDataWithUserKey(name,userKey);
const hashedName:string = System.hashElement(name);
const issueId: string = System.createUniqueId();
const issueId: string = existingIssueId || System.createUniqueId();
return BookRepo.insertNewIssue(issueId, userId, bookId, encryptedName, hashedName,lang);
}
@@ -691,14 +691,14 @@ export default class Book {
return true;
}
public static addNewWorld(userId: string, bookId: string, worldName: string, lang: 'fr' | 'en' = 'fr'): string {
public static addNewWorld(userId: string, bookId: string, worldName: string, lang: 'fr' | 'en' = 'fr', existingWorldId?: string): string {
const userKey: string = getUserEncryptionKey(userId);
const hashedName: string = System.hashElement(worldName);
if (BookRepo.checkWorldExist(userId, bookId, hashedName, lang)) {
if (!existingWorldId && BookRepo.checkWorldExist(userId, bookId, hashedName, lang)) {
throw new Error(lang === "fr" ? `Tu as déjà un monde ${worldName}.` : `You already have a world named ${worldName}.`);
}
const encryptedName: string = System.encryptDataWithUserKey(worldName, userKey);
const worldId: string = System.createUniqueId();
const worldId: string = existingWorldId || System.createUniqueId();
return BookRepo.insertNewWorld(worldId, userId, bookId, encryptedName, hashedName, lang);
}
@@ -875,15 +875,15 @@ export default class Book {
return BookRepo.updateWorldElements(userId, elements, lang);
}
public static addNewElementToWorld(userId: string, worldId: string, elementName: string, elementType: string, lang: 'fr' | 'en' = 'fr'): string {
public static addNewElementToWorld(userId: string, worldId: string, elementName: string, elementType: string, lang: 'fr' | 'en' = 'fr', existingElementId?: string): string {
const userKey: string = getUserEncryptionKey(userId);
const hashedName: string = System.hashElement(elementName);
if (BookRepo.checkElementExist(worldId, hashedName, lang)) {
if (!existingElementId && BookRepo.checkElementExist(worldId, hashedName, lang)) {
throw new Error(lang === "fr" ? `Vous avez déjà un élément avec ce nom ${elementName}.` : `You already have an element named ${elementName}.`);
}
const elementTypeId: number = Book.getElementTypes(elementType);
const encryptedName: string = System.encryptDataWithUserKey(elementName, userKey);
const elementId: string = System.createUniqueId();
const elementId: string = existingElementId || System.createUniqueId();
return BookRepo.insertNewElement(userId, elementId, elementTypeId, worldId, encryptedName, hashedName, lang);
}
public static getElementTypes(elementType:string):number{

View File

@@ -172,15 +172,15 @@ export default class Chapter {
};
}
public static addChapter(userId: string, bookId: string, title: string, wordsCount: number, chapterOrder: number, lang: 'fr' | 'en' = 'fr'): string {
public static addChapter(userId: string, bookId: string, title: string, wordsCount: number, chapterOrder: number, lang: 'fr' | 'en' = 'fr', existingChapterId?: string): string {
const hashedTitle: string = System.hashElement(title);
const userKey: string = getUserEncryptionKey(userId);
const encryptedTitle: string = System.encryptDataWithUserKey(title, userKey);
if (ChapterRepo.checkNameDuplication(userId, bookId, hashedTitle, lang)) {
if (!existingChapterId && ChapterRepo.checkNameDuplication(userId, bookId, hashedTitle, lang)) {
throw new Error(lang === 'fr' ? `Ce nom de chapitre existe déjà.` : `This chapter name already exists.`);
}
const chapterId: string = System.createUniqueId();
const chapterId: string = existingChapterId || System.createUniqueId();
return ChapterRepo.insertChapter(chapterId, userId, bookId, encryptedTitle, hashedTitle, wordsCount, chapterOrder, lang);
}
@@ -188,8 +188,8 @@ export default class Chapter {
return ChapterRepo.deleteChapter(userId, chapterId, lang);
}
public static addChapterInformation(userId: string, chapterId: string, actId: number, bookId: string, plotId: string | null, incidentId: string | null, lang: 'fr' | 'en' = 'fr'): string {
const chapterInfoId: string = System.createUniqueId();
public static addChapterInformation(userId: string, chapterId: string, actId: number, bookId: string, plotId: string | null, incidentId: string | null, lang: 'fr' | 'en' = 'fr', existingChapterInfoId?: string): string {
const chapterInfoId: string = existingChapterInfoId || System.createUniqueId();
return ChapterRepo.insertChapterInformation(chapterInfoId, userId, chapterId, actId, bookId, plotId, incidentId, lang);
}

View File

@@ -88,9 +88,9 @@ export default class Character {
return characterList;
}
public static addNewCharacter(userId: string, character: CharacterPropsPost, bookId: string, lang: 'fr' | 'en' = 'fr'): string {
public static addNewCharacter(userId: string, character: CharacterPropsPost, bookId: string, lang: 'fr' | 'en' = 'fr', existingCharacterId?: string): string {
const userKey: string = getUserEncryptionKey(userId);
const characterId: string = System.createUniqueId();
const characterId: string = existingCharacterId || System.createUniqueId();
const encryptedName: string = System.encryptDataWithUserKey(character.name, userKey);
const encryptedLastName: string = System.encryptDataWithUserKey(character.lastName, userKey);
const encryptedTitle: string = System.encryptDataWithUserKey(character.title, userKey);
@@ -132,9 +132,9 @@ export default class Character {
return CharacterRepo.updateCharacter(userId, character.id, encryptedName, encryptedLastName, encryptedTitle, encryptedCategory, encryptedImage, encryptedRole, encryptedBiography, encryptedHistory, System.timeStampInSeconds(), lang);
}
static addNewAttribute(characterId: string, userId: string, type: string, name: string, lang: 'fr' | 'en' = 'fr'): string {
static addNewAttribute(characterId: string, userId: string, type: string, name: string, lang: 'fr' | 'en' = 'fr', existingAttributeId?: string): string {
const userKey: string = getUserEncryptionKey(userId);
const attributeId: string = System.createUniqueId();
const attributeId: string = existingAttributeId || System.createUniqueId();
const encryptedType: string = System.encryptDataWithUserKey(type, userKey);
const encryptedName: string = System.encryptDataWithUserKey(name, userKey);
return CharacterRepo.insertAttribute(attributeId, characterId, userId, encryptedType, encryptedName, lang);

View File

@@ -88,27 +88,27 @@ export default class Location {
return locationArray;
}
static addLocationSection(userId: string, locationName: string, bookId: string, lang: 'fr' | 'en' = 'fr'): string {
static addLocationSection(userId: string, locationName: string, bookId: string, lang: 'fr' | 'en' = 'fr', existingLocationId?: string): string {
const userKey: string = getUserEncryptionKey(userId);
const originalName: string = System.hashElement(locationName);
const encryptedName: string = System.encryptDataWithUserKey(locationName, userKey);
const locationId: string = System.createUniqueId();
const locationId: string = existingLocationId || System.createUniqueId();
return LocationRepo.insertLocation(userId, locationId, bookId, encryptedName, originalName, lang);
}
static addLocationElement(userId: string, locationId: string, elementName: string, lang: 'fr' | 'en' = 'fr') {
static addLocationElement(userId: string, locationId: string, elementName: string, lang: 'fr' | 'en' = 'fr', existingElementId?: string) {
const userKey: string = getUserEncryptionKey(userId);
const originalName: string = System.hashElement(elementName);
const encryptedName: string = System.encryptDataWithUserKey(elementName, userKey);
const elementId: string = System.createUniqueId();
const elementId: string = existingElementId || System.createUniqueId();
return LocationRepo.insertLocationElement(userId, elementId, locationId, encryptedName, originalName, lang)
}
static addLocationSubElement(userId: string, elementId: string, subElementName: string, lang: 'fr' | 'en' = 'fr') {
static addLocationSubElement(userId: string, elementId: string, subElementName: string, lang: 'fr' | 'en' = 'fr', existingSubElementId?: string) {
const userKey: string = getUserEncryptionKey(userId);
const originalName: string = System.hashElement(subElementName);
const encryptedName: string = System.encryptDataWithUserKey(subElementName, userKey);
const subElementId: string = System.createUniqueId();
const subElementId: string = existingSubElementId || System.createUniqueId();
return LocationRepo.insertLocationSubElement(userId, subElementId, elementId, encryptedName, originalName, lang)
}

View File

@@ -53,28 +53,33 @@ interface CreateBookData {
interface AddIncidentData {
bookId: string;
name: string;
incidentId?: string;
}
interface AddPlotPointData {
bookId: string;
name: string;
incidentId: string;
plotPointId?: string;
}
interface AddIssueData {
bookId: string;
name: string;
issueId?: string;
}
interface AddWorldData {
bookId: string;
worldName: string;
id?: string;
}
interface AddWorldElementData {
worldId: string;
elementName: string;
elementType: number;
id?: string;
}
interface SetAIGuideLineData {
@@ -235,7 +240,7 @@ ipcMain.handle(
'db:book:incident:add',
createHandler<AddIncidentData, string>(
function(userId: string, data: AddIncidentData, lang: 'fr' | 'en') {
return Book.addNewIncident(userId, data.bookId, data.name, lang);
return Book.addNewIncident(userId, data.bookId, data.name, lang, data.incidentId);
}
)
);
@@ -261,7 +266,8 @@ ipcMain.handle('db:book:plot:add', createHandler<AddPlotPointData, string>(
data.bookId,
data.incidentId,
data.name,
lang
lang,
data.plotPointId
);
}
)
@@ -283,7 +289,7 @@ ipcMain.handle(
// POST /book/issue/add - Add issue
ipcMain.handle('db:book:issue:add', createHandler<AddIssueData, string>(
function(userId: string, data: AddIssueData, lang: 'fr' | 'en') {
return Book.addNewIssue(userId, data.bookId, data.name, lang);
return Book.addNewIssue(userId, data.bookId, data.name, lang, data.issueId);
}
)
);
@@ -314,7 +320,7 @@ ipcMain.handle('db:book:worlds:get', createHandler<GetWorldsData, WorldProps[]>(
// POST /book/world/add - Add world
ipcMain.handle('db:book:world:add', createHandler<AddWorldData, string>(
function(userId: string, data: AddWorldData, lang: 'fr' | 'en') {
return Book.addNewWorld(userId, data.bookId, data.worldName, lang);
return Book.addNewWorld(userId, data.bookId, data.worldName, lang, data.id);
}
)
);
@@ -327,7 +333,8 @@ ipcMain.handle('db:book:world:element:add', createHandler<AddWorldElementData, s
data.worldId,
data.elementName,
data.elementType.toString(),
lang
lang,
data.id
);
}
)

View File

@@ -21,6 +21,7 @@ interface AddChapterData {
bookId: string;
title: string;
chapterOrder: number;
chapterId?: string;
}
interface UpdateChapterData {
@@ -35,6 +36,7 @@ interface AddChapterInformationData {
bookId: string;
plotId: string | null;
incidentId: string | null;
chapterInfoId?: string;
}
interface GetChapterContentData {
@@ -109,7 +111,7 @@ ipcMain.handle('db:chapter:last', createHandler<string, ChapterProps | null>(
// POST /chapter/add - Add new chapter
ipcMain.handle('db:chapter:add', createHandler<AddChapterData, string>(
function(userId: string, data: AddChapterData, lang: 'fr' | 'en'): string {
return Chapter.addChapter(userId, data.bookId, data.title, 0, data.chapterOrder, lang);
return Chapter.addChapter(userId, data.bookId, data.title, 0, data.chapterOrder, lang, data.chapterId);
}
)
);
@@ -144,7 +146,8 @@ ipcMain.handle('db:chapter:information:add', createHandler<AddChapterInformation
data.bookId,
data.plotId,
data.incidentId,
lang
lang,
data.chapterInfoId
);
}
)

View File

@@ -6,12 +6,14 @@ import type { CharacterProps, CharacterPropsPost, CharacterAttribute } from '../
interface AddCharacterData {
character: CharacterPropsPost;
bookId: string;
id?: string;
}
interface AddAttributeData {
characterId: string;
type: string;
name: string;
id?: string;
}
// GET /character/list - Get character list
@@ -39,7 +41,7 @@ ipcMain.handle('db:character:attributes', createHandler<GetCharacterAttributesDa
// POST /character/add - Add new character
ipcMain.handle('db:character:create', createHandler<AddCharacterData, string>(
function(userId: string, data: AddCharacterData, lang: 'fr' | 'en'): string {
return Character.addNewCharacter(userId, data.character, data.bookId, lang);
return Character.addNewCharacter(userId, data.character, data.bookId, lang, data.id);
}
)
);
@@ -47,7 +49,7 @@ ipcMain.handle('db:character:create', createHandler<AddCharacterData, string>(
// POST /character/attribute/add - Add attribute to character
ipcMain.handle('db:character:attribute:add', createHandler<AddAttributeData, string>(
function(userId: string, data: AddAttributeData, lang: 'fr' | 'en'): string {
return Character.addNewAttribute(data.characterId, userId, data.type, data.name, lang);
return Character.addNewAttribute(data.characterId, userId, data.type, data.name, lang, data.id);
}
)
);

View File

@@ -11,16 +11,19 @@ interface UpdateLocationResponse {
interface AddLocationSectionData {
locationName: string;
bookId: string;
id?: string;
}
interface AddLocationElementData {
locationId: string;
elementName: string;
id?: string;
}
interface AddLocationSubElementData {
elementId: string;
subElementName: string;
id?: string;
}
interface UpdateLocationData {
@@ -41,7 +44,7 @@ ipcMain.handle('db:location:all', createHandler<GetAllLocationsData, LocationPro
// POST /location/section/add - Add location section
ipcMain.handle('db:location:section:add', createHandler<AddLocationSectionData, string>(
function(userId: string, data: AddLocationSectionData, lang: 'fr' | 'en'): string {
return Location.addLocationSection(userId, data.locationName, data.bookId, lang);
return Location.addLocationSection(userId, data.locationName, data.bookId, lang, data.id);
}
)
);
@@ -49,7 +52,7 @@ ipcMain.handle('db:location:section:add', createHandler<AddLocationSectionData,
// POST /location/element/add - Add location element
ipcMain.handle('db:location:element:add', createHandler<AddLocationElementData, string>(
function(userId: string, data: AddLocationElementData, lang: 'fr' | 'en'): string {
return Location.addLocationElement(userId, data.locationId, data.elementName, lang);
return Location.addLocationElement(userId, data.locationId, data.elementName, lang, data.id);
}
)
);
@@ -57,7 +60,7 @@ ipcMain.handle('db:location:element:add', createHandler<AddLocationElementData,
// POST /location/sub-element/add - Add location sub-element
ipcMain.handle('db:location:subelement:add', createHandler<AddLocationSubElementData, string>(
function(userId: string, data: AddLocationSubElementData, lang: 'fr' | 'en'): string {
return Location.addLocationSubElement(userId, data.elementId, data.subElementName, lang);
return Location.addLocationSubElement(userId, data.elementId, data.subElementName, lang, data.id);
}
)
);

View File

@@ -1,4 +1,4 @@
import {app, BrowserWindow, ipcMain, IpcMainInvokeEvent, Menu, nativeImage, protocol, safeStorage, shell} from 'electron';
import {app, BrowserWindow, dialog, ipcMain, IpcMainInvokeEvent, Menu, nativeImage, protocol, safeStorage, shell} from 'electron';
import * as path from 'path';
import {fileURLToPath} from 'url';
import * as fs from 'fs';
@@ -519,6 +519,73 @@ ipcMain.handle('db-initialize', (_event, userId: string, encryptionKey: string)
}
});
/**
* Emergency restore - Clean up ALL local data
*/
function performEmergencyRestore(): void {
try {
// Close database connection
const db: DatabaseService = getDatabaseService();
db.close();
// Get storage and userId before clearing
const storage: SecureStorage = getSecureStorage();
const userId = storage.get<string>('userId');
const lastUserId = storage.get<string>('lastUserId');
// Delete user-specific data
if (userId) {
storage.delete(`pin-${userId}`);
storage.delete(`encryptionKey-${userId}`);
}
if (lastUserId && lastUserId !== userId) {
storage.delete(`pin-${lastUserId}`);
storage.delete(`encryptionKey-${lastUserId}`);
}
// Delete all general data
storage.delete('authToken');
storage.delete('userId');
storage.delete('lastUserId');
storage.delete('userLang');
storage.delete('offlineMode');
storage.delete('syncInterval');
// Save cleared storage
storage.save();
// Delete database file
const userDataPath: string = app.getPath('userData');
const dbPath: string = path.join(userDataPath, 'eritors-local.db');
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
// Delete secure config file to ensure complete reset
const secureConfigPath: string = path.join(userDataPath, 'secure-config.json');
if (fs.existsSync(secureConfigPath)) {
fs.unlinkSync(secureConfigPath);
}
console.log('[Emergency Restore] All local data cleared successfully');
} catch (error) {
console.error('[Emergency Restore] Error:', error);
}
// Restart app
if (mainWindow) {
mainWindow.close();
mainWindow = null;
}
if (loginWindow) {
loginWindow.close();
loginWindow = null;
}
app.relaunch();
app.exit(0);
}
app.whenReady().then(():void => {
// Security: Disable web cache in production
if (!isDev) {
@@ -558,6 +625,29 @@ app.whenReady().then(():void => {
submenu: [
{ role: 'toggleDevTools' }
]
},
{
label: 'Help',
submenu: [
{
label: 'Restore App',
click: () => {
dialog.showMessageBox({
type: 'warning',
buttons: ['Cancel', 'Restore'],
defaultId: 0,
cancelId: 0,
title: 'Restore App',
message: 'Are you sure you want to restore the app?',
detail: 'This will delete all local data including: PIN codes, encryption keys, local database, and authentication tokens. The app will restart after restoration.'
}).then((result) => {
if (result.response === 1) {
performEmergencyRestore();
}
});
}
}
]
}
];