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

@@ -9,6 +9,7 @@ import {BookContext} from "@/context/BookContext";
import CharacterDetail from "@/components/book/settings/characters/CharacterDetail";
import {useTranslations} from "next-intl";
import {LangContext, LangContextProps} from "@/context/LangContext";
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
interface CharacterDetailProps {
selectedCharacter: CharacterProps | null;
@@ -46,6 +47,7 @@ const initialCharacterState: CharacterProps = {
export function CharacterComponent(props: any, ref: any) {
const t = useTranslations();
const {lang} = useContext<LangContextProps>(LangContext)
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {session} = useContext(SessionContext);
const {book} = useContext(BookContext);
const {errorMessage, successMessage} = useContext(AlertContext);
@@ -64,9 +66,14 @@ export function CharacterComponent(props: any, ref: any) {
async function getCharacters(): Promise<void> {
try {
const response: CharacterProps[] = await System.authGetQueryToServer<CharacterProps[]>(`character/list`, session.accessToken, lang, {
bookid: book?.bookId,
});
let response: CharacterProps[];
if (isCurrentlyOffline()) {
response = await window.electron.invoke<CharacterProps[]>('db:character:list', {bookid: book?.bookId});
} else {
response = await System.authGetQueryToServer<CharacterProps[]>(`character/list`, session.accessToken, lang, {
bookid: book?.bookId,
});
}
if (response) {
setCharacters(response);
}
@@ -108,10 +115,18 @@ export function CharacterComponent(props: any, ref: any) {
return;
}
try {
const characterId: string = await System.authPostToServer<string>(`character/add`, {
bookId: book?.bookId,
character: updatedCharacter,
}, session.accessToken, lang);
let characterId: string;
if (isCurrentlyOffline()) {
characterId = await window.electron.invoke<string>('db:character:create', {
bookId: book?.bookId,
character: updatedCharacter,
});
} else {
characterId = await System.authPostToServer<string>(`character/add`, {
bookId: book?.bookId,
character: updatedCharacter,
}, session.accessToken, lang);
}
if (!characterId) {
errorMessage(t("characterComponent.errorAddCharacter"));
return;
@@ -130,9 +145,16 @@ export function CharacterComponent(props: any, ref: any) {
async function updateCharacter(updatedCharacter: CharacterProps,): Promise<void> {
try {
const response: boolean = await System.authPostToServer<boolean>(`character/update`, {
character: updatedCharacter,
}, session.accessToken, lang);
let response: boolean;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:character:update', {
character: updatedCharacter,
});
} else {
response = await System.authPostToServer<boolean>(`character/update`, {
character: updatedCharacter,
}, session.accessToken, lang);
}
if (!response) {
errorMessage(t("characterComponent.errorUpdateCharacter"));
return;
@@ -175,11 +197,20 @@ export function CharacterComponent(props: any, ref: any) {
setSelectedCharacter({...selectedCharacter, [section]: updatedSection});
} else {
try {
const attributeId: string = await System.authPostToServer<string>(`character/attribute/add`, {
characterId: selectedCharacter.id,
type: section,
name: value.name,
}, session.accessToken, lang);
let attributeId: string;
if (isCurrentlyOffline()) {
attributeId = await window.electron.invoke<string>('db:character:attribute:add', {
characterId: selectedCharacter.id,
type: section,
name: value.name,
});
} else {
attributeId = await System.authPostToServer<string>(`character/attribute/add`, {
characterId: selectedCharacter.id,
type: section,
name: value.name,
}, session.accessToken, lang);
}
if (!attributeId) {
errorMessage(t("characterComponent.errorAddAttribute"));
return;
@@ -214,9 +245,16 @@ export function CharacterComponent(props: any, ref: any) {
setSelectedCharacter({...selectedCharacter, [section]: updatedSection});
} else {
try {
const response: boolean = await System.authDeleteToServer<boolean>(`character/attribute/delete`, {
attributeId: attrId,
}, session.accessToken, lang);
let response: boolean;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:character:attribute:delete', {
attributeId: attrId,
});
} else {
response = await System.authDeleteToServer<boolean>(`character/attribute/delete`, {
attributeId: attrId,
}, session.accessToken, lang);
}
if (!response) {
errorMessage(t("characterComponent.errorRemoveAttribute"));
return;