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

@@ -16,6 +16,7 @@ import WorldElementComponent from './WorldElement';
import SelectBox from "@/components/form/SelectBox";
import {useTranslations} from "next-intl";
import {LangContext, LangContextProps} from "@/context/LangContext";
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
export interface ElementSection {
title: string;
@@ -26,6 +27,7 @@ export interface ElementSection {
export function WorldSetting(props: any, ref: any) {
const t = useTranslations();
const {lang} = useContext<LangContextProps>(LangContext);
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {errorMessage, successMessage} = useContext(AlertContext);
const {session} = useContext(SessionContext);
const {book} = useContext(BookContext);
@@ -49,9 +51,14 @@ export function WorldSetting(props: any, ref: any) {
async function getWorlds() {
try {
const response: WorldProps[] = await System.authGetQueryToServer<WorldProps[]>(`book/worlds`, session.accessToken, lang, {
bookid: bookId,
});
let response: WorldProps[];
if (isCurrentlyOffline()) {
response = await window.electron.invoke<WorldProps[]>('db:book:worlds:get', {bookid: bookId});
} else {
response = await System.authGetQueryToServer<WorldProps[]>(`book/worlds`, session.accessToken, lang, {
bookid: bookId,
});
}
if (response) {
setWorlds(response);
const formattedWorlds: SelectBoxProps[] = response.map(
@@ -77,10 +84,18 @@ export function WorldSetting(props: any, ref: any) {
return;
}
try {
const worldId: string = await System.authPostToServer<string>('book/world/add', {
worldName: newWorldName,
bookId: bookId,
}, session.accessToken, lang);
let worldId: string;
if (isCurrentlyOffline()) {
worldId = await window.electron.invoke<string>('db:book:world:add', {
worldName: newWorldName,
bookId: bookId,
});
} else {
worldId = await System.authPostToServer<string>('book/world/add', {
worldName: newWorldName,
bookId: bookId,
}, session.accessToken, lang);
}
if (!worldId) {
errorMessage(t("worldSetting.addWorldError"));
return;
@@ -125,10 +140,18 @@ export function WorldSetting(props: any, ref: any) {
async function handleUpdateWorld(): Promise<void> {
try {
const response: boolean = await System.authPutToServer<boolean>('book/world/update', {
world: worlds[selectedWorldIndex],
bookId: bookId,
}, session.accessToken, lang);
let response: boolean;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:book:world:update', {
world: worlds[selectedWorldIndex],
bookId: bookId,
});
} else {
response = await System.authPutToServer<boolean>('book/world/update', {
world: worlds[selectedWorldIndex],
bookId: bookId,
}, session.accessToken, lang);
}
if (!response) {
errorMessage(t("worldSetting.updateWorldError"));
return;