Refactor character, chapter, and story components to support offline mode

- Add `OfflineContext` and `BookContext` to components for offline state management.
- Introduce conditional logic to toggle between server API requests and offline IPC handlers for CRUD operations.
- Refine `TextEditor`, `DraftCompanion`, and other components to disable actions or features unavailable in offline mode.
- Improve error handling and user feedback in both online and offline scenarios.
This commit is contained in:
natreex
2025-12-19 15:42:35 -05:00
parent 43c7ef375c
commit ff530f3442
16 changed files with 454 additions and 157 deletions

View File

@@ -64,9 +64,13 @@ export function LocationComponent(props: any, ref: any) {
if (isCurrentlyOffline()) {
response = await window.electron.invoke<LocationProps[]>('db:location:all', {bookid: bookId});
} else {
response = await System.authGetQueryToServer<LocationProps[]>(`location/all`, token, lang, {
bookid: bookId,
});
if (book?.localBook) {
response = await window.electron.invoke<LocationProps[]>('db:location:all', {bookid: bookId});
} else {
response = await System.authGetQueryToServer<LocationProps[]>(`location/all`, token, lang, {
bookid: bookId,
});
}
}
if (response && response.length > 0) {
setSections(response);
@@ -93,10 +97,17 @@ export function LocationComponent(props: any, ref: any) {
locationName: newSectionName,
});
} else {
sectionId = await System.authPostToServer<string>(`location/section/add`, {
bookId: bookId,
locationName: newSectionName,
}, token, lang);
if (book?.localBook) {
sectionId = await window.electron.invoke<string>('db:location:section:add', {
bookId: bookId,
locationName: newSectionName,
});
} else {
sectionId = await System.authPostToServer<string>(`location/section/add`, {
bookId: bookId,
locationName: newSectionName,
}, token, lang);
}
}
if (!sectionId) {
errorMessage(t('locationComponent.errorUnknownAddSection'));
@@ -132,12 +143,20 @@ export function LocationComponent(props: any, ref: any) {
elementName: newElementNames[sectionId],
});
} else {
elementId = await System.authPostToServer<string>(`location/element/add`, {
if (book?.localBook) {
elementId = await window.electron.invoke<string>('db:location:element:add', {
bookId: bookId,
locationId: sectionId,
elementName: newElementNames[sectionId],
},
token, lang);
});
} else {
elementId = await System.authPostToServer<string>(`location/element/add`, {
bookId: bookId,
locationId: sectionId,
elementName: newElementNames[sectionId],
},
token, lang);
}
}
if (!elementId) {
errorMessage(t('locationComponent.errorUnknownAddElement'));
@@ -198,10 +217,17 @@ export function LocationComponent(props: any, ref: any) {
subElementName: newSubElementNames[elementIndex],
});
} else {
subElementId = await System.authPostToServer<string>(`location/sub-element/add`, {
elementId: sections[sectionIndex].elements[elementIndex].id,
subElementName: newSubElementNames[elementIndex],
}, token, lang);
if (book?.localBook) {
subElementId = await window.electron.invoke<string>('db:location:subelement:add', {
elementId: sections[sectionIndex].elements[elementIndex].id,
subElementName: newSubElementNames[elementIndex],
});
} else {
subElementId = await System.authPostToServer<string>(`location/sub-element/add`, {
elementId: sections[sectionIndex].elements[elementIndex].id,
subElementName: newSubElementNames[elementIndex],
}, token, lang);
}
}
if (!subElementId) {
errorMessage(t('locationComponent.errorUnknownAddSubElement'));
@@ -254,9 +280,15 @@ export function LocationComponent(props: any, ref: any) {
elementId: elementId,
});
} else {
response = await System.authDeleteToServer<boolean>(`location/element/delete`, {
elementId: elementId,
}, token, lang);
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:location:element:delete', {
elementId: elementId,
});
} else {
response = await System.authDeleteToServer<boolean>(`location/element/delete`, {
elementId: elementId,
}, token, lang);
}
}
if (!response) {
errorMessage(t('locationComponent.errorUnknownDeleteElement'));
@@ -288,9 +320,15 @@ export function LocationComponent(props: any, ref: any) {
subElementId: subElementId,
});
} else {
response = await System.authDeleteToServer<boolean>(`location/sub-element/delete`, {
subElementId: subElementId,
}, token, lang);
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:location:subelement:delete', {
subElementId: subElementId,
});
} else {
response = await System.authDeleteToServer<boolean>(`location/sub-element/delete`, {
subElementId: subElementId,
}, token, lang);
}
}
if (!response) {
errorMessage(t('locationComponent.errorUnknownDeleteSubElement'));
@@ -317,9 +355,15 @@ export function LocationComponent(props: any, ref: any) {
locationId: sectionId,
});
} else {
response = await System.authDeleteToServer<boolean>(`location/delete`, {
locationId: sectionId,
}, token, lang);
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:location:delete', {
locationId: sectionId,
});
} else {
response = await System.authDeleteToServer<boolean>(`location/delete`, {
locationId: sectionId,
}, token, lang);
}
}
if (!response) {
errorMessage(t('locationComponent.errorUnknownDeleteSection'));
@@ -344,9 +388,15 @@ export function LocationComponent(props: any, ref: any) {
locations: sections,
});
} else {
response = await System.authPostToServer<boolean>(`location/update`, {
locations: sections,
}, token, lang);
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:location:update', {
locations: sections,
});
} else {
response = await System.authPostToServer<boolean>(`location/update`, {
locations: sections,
}, token, lang);
}
}
if (!response) {
errorMessage(t('locationComponent.errorUnknownSave'));