Add offline mode logic for book, story, and world operations

- Integrate `OfflineContext` into book, story settings, and related components.
- Add conditional logic to toggle between server API requests and offline IPC handlers (`db:book:delete`, `db:book:story:get`, `db:location:all`, etc.).
- Refactor and update IPC handlers to accept structured data arguments for improved consistency (`data: object`).
- Ensure stricter typings in IPC handlers and frontend functions.
- Improve error handling and user feedback in both online and offline modes.
This commit is contained in:
natreex
2025-11-26 22:52:34 -05:00
parent e1abcd9490
commit 23f1592c22
15 changed files with 518 additions and 201 deletions

View File

@@ -12,6 +12,7 @@ import Issues from "@/components/book/settings/story/Issue";
import Act from "@/components/book/settings/story/Act";
import {useTranslations} from "next-intl";
import {LangContext, LangContextProps} from "@/context/LangContext";
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
export const StoryContext = createContext<{
acts: ActType[];
@@ -41,6 +42,7 @@ interface StoryFetchData {
export function Story(props: any, ref: any) {
const t = useTranslations();
const {lang} = useContext<LangContextProps>(LangContext);
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {book} = useContext(BookContext);
const bookId: string = book?.bookId ? book.bookId.toString() : '';
const {session} = useContext(SessionContext);
@@ -68,9 +70,14 @@ export function Story(props: any, ref: any) {
async function getStoryData(): Promise<void> {
try {
const response: StoryFetchData = await System.authGetQueryToServer<StoryFetchData>(`book/story`, userToken, lang, {
bookid: bookId,
});
let response: StoryFetchData;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<StoryFetchData>('db:book:story:get', {bookid: bookId});
} else {
response = await System.authGetQueryToServer<StoryFetchData>(`book/story`, userToken, lang, {
bookid: bookId,
});
}
if (response) {
setActs(response.acts);
setMainChapters(response.mainChapter);
@@ -119,13 +126,18 @@ export function Story(props: any, ref: any) {
async function handleSave(): Promise<void> {
try {
const response: boolean =
await System.authPostToServer<boolean>('book/story', {
bookId,
acts,
mainChapters,
issues,
}, userToken, lang);
let response: boolean;
const storyData = {
bookId,
acts,
mainChapters,
issues,
};
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:book:story:update', storyData);
} else {
response = await System.authPostToServer<boolean>('book/story', storyData, userToken, lang);
}
if (!response) {
errorMessage(t("story.errorSave"))
}