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

@@ -21,10 +21,12 @@ import {
} from "@/lib/models/Story";
import {useTranslations} from "next-intl";
import {LangContext} from "@/context/LangContext";
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
function GuideLineSetting(props: any, ref: any) {
const t = useTranslations();
const {lang} = useContext(LangContext);
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {book} = useContext(BookContext);
const {session} = useContext(SessionContext);
const userToken: string = session?.accessToken ? session?.accessToken : '';
@@ -75,7 +77,12 @@ function GuideLineSetting(props: any, ref: any) {
async function getAIGuideLine(): Promise<void> {
try {
const response: GuideLineAI = await System.authGetQueryToServer<GuideLineAI>(`book/ai/guideline`, userToken, lang, {id: bookId});
let response: GuideLineAI;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<GuideLineAI>('db:book:guideline:ai:get', {id: bookId});
} else {
response = await System.authGetQueryToServer<GuideLineAI>(`book/ai/guideline`, userToken, lang, {id: bookId});
}
if (response) {
setPlotSummary(response.globalResume);
setVerbTense(response.verbeTense?.toString() || '');
@@ -96,13 +103,17 @@ function GuideLineSetting(props: any, ref: any) {
async function getGuideLine(): Promise<void> {
try {
const response: GuideLine =
await System.authGetQueryToServer<GuideLine>(
let response: GuideLine;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<GuideLine>('db:book:guideline:get', {id: bookId});
} else {
response = await System.authGetQueryToServer<GuideLine>(
`book/guide-line`,
userToken,
lang,
{id: bookId},
);
}
if (response) {
setTone(response.tone);
setAtmosphere(response.atmosphere);
@@ -126,25 +137,30 @@ function GuideLineSetting(props: any, ref: any) {
async function savePersonal(): Promise<void> {
try {
const response: boolean =
await System.authPostToServer<boolean>(
let response: boolean;
const guidelineData = {
bookId: bookId,
tone: tone,
atmosphere: atmosphere,
writingStyle: writingStyle,
themes: themes,
symbolism: symbolism,
motifs: motifs,
narrativeVoice: narrativeVoice,
pacing: pacing,
intendedAudience: intendedAudience,
keyMessages: keyMessages,
};
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:book:guideline:update', guidelineData);
} else {
response = await System.authPostToServer<boolean>(
'book/guide-line',
{
bookId: bookId,
tone: tone,
atmosphere: atmosphere,
writingStyle: writingStyle,
themes: themes,
symbolism: symbolism,
motifs: motifs,
narrativeVoice: narrativeVoice,
pacing: pacing,
intendedAudience: intendedAudience,
keyMessages: keyMessages,
},
guidelineData,
userToken,
lang,
);
}
if (!response) {
errorMessage(t("guideLineSetting.saveError"));
return;