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

@@ -70,9 +70,13 @@ export function CharacterComponent(props: any, ref: any) {
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 (book?.localBook) {
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);
@@ -122,10 +126,17 @@ export function CharacterComponent(props: any, ref: any) {
character: updatedCharacter,
});
} else {
characterId = await System.authPostToServer<string>(`character/add`, {
bookId: book?.bookId,
character: updatedCharacter,
}, session.accessToken, lang);
if (book?.localBook) {
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"));
@@ -151,9 +162,15 @@ export function CharacterComponent(props: any, ref: any) {
character: updatedCharacter,
});
} else {
response = await System.authPostToServer<boolean>(`character/update`, {
character: updatedCharacter,
}, session.accessToken, lang);
if (book?.localBook) {
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"));
@@ -205,11 +222,19 @@ export function CharacterComponent(props: any, ref: any) {
name: value.name,
});
} else {
attributeId = await System.authPostToServer<string>(`character/attribute/add`, {
characterId: selectedCharacter.id,
type: section,
name: value.name,
}, session.accessToken, lang);
if (book?.localBook) {
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"));
@@ -251,9 +276,15 @@ export function CharacterComponent(props: any, ref: any) {
attributeId: attrId,
});
} else {
response = await System.authDeleteToServer<boolean>(`character/attribute/delete`, {
attributeId: attrId,
}, session.accessToken, lang);
if (book?.localBook) {
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"));

View File

@@ -30,6 +30,7 @@ import CharacterSectionElement from "@/components/book/settings/characters/Chara
import {useTranslations} from "next-intl";
import {LangContext} from "@/context/LangContext";
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
import {BookContext} from "@/context/BookContext";
interface CharacterDetailProps {
selectedCharacter: CharacterProps | null;
@@ -57,6 +58,7 @@ export default function CharacterDetail(
const t = useTranslations();
const {lang} = useContext(LangContext);
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {book} = useContext(BookContext);
const {session} = useContext(SessionContext);
const {errorMessage} = useContext(AlertContext);
@@ -74,9 +76,15 @@ export default function CharacterDetail(
characterId: selectedCharacter?.id,
});
} else {
response = await System.authGetQueryToServer<CharacterAttribute>(`character/attribute`, session.accessToken, lang, {
characterId: selectedCharacter?.id,
});
if (book?.localBook) {
response = await window.electron.invoke<CharacterAttribute>('db:character:attributes', {
characterId: selectedCharacter?.id,
});
} else {
response = await System.authGetQueryToServer<CharacterAttribute>(`character/attribute`, session.accessToken, lang, {
characterId: selectedCharacter?.id,
});
}
}
if (response) {
setSelectedCharacter({