Add offline mode logic for main chapters and refine IPC handlers
- Integrate `OfflineContext` into `MainChapter` and related components to handle offline scenarios for chapter operations. - Add conditional logic to toggle between server API requests and offline IPC handlers (`db:chapter:add`, `db:chapter:remove`). - Extend `GET /book/story` endpoint to include `mainChapter` data. - Refactor IPC handlers for chapters to accept structured data arguments with stricter typings. - Update frontend key assignments for `BookList` component for improved rendering stability.
This commit is contained in:
@@ -12,6 +12,7 @@ import AlertBox from "@/components/AlertBox";
|
||||
import CollapsableArea from "@/components/CollapsableArea";
|
||||
import {useTranslations} from "next-intl";
|
||||
import {LangContext} from "@/context/LangContext";
|
||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||
|
||||
interface MainChapterProps {
|
||||
chapters: ChapterListProps[];
|
||||
@@ -21,6 +22,7 @@ interface MainChapterProps {
|
||||
export default function MainChapter({chapters, setChapters}: MainChapterProps) {
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext(LangContext)
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const {book} = useContext(BookContext);
|
||||
const {session} = useContext(SessionContext);
|
||||
const {errorMessage, successMessage} = useContext(AlertContext);
|
||||
@@ -78,15 +80,16 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
|
||||
async function deleteChapter(): Promise<void> {
|
||||
try {
|
||||
setDeleteConfirmMessage(false);
|
||||
const response: boolean = await System.authDeleteToServer<boolean>(
|
||||
'chapter/remove',
|
||||
{
|
||||
bookId,
|
||||
chapterId: chapterIdToRemove,
|
||||
},
|
||||
token,
|
||||
lang,
|
||||
);
|
||||
let response: boolean;
|
||||
const deleteData = {
|
||||
bookId,
|
||||
chapterId: chapterIdToRemove,
|
||||
};
|
||||
if (isCurrentlyOffline()) {
|
||||
response = await window.electron.invoke<boolean>('db:chapter:remove', deleteData);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>('chapter/remove', deleteData, token, lang);
|
||||
}
|
||||
if (!response) {
|
||||
errorMessage(t("mainChapter.errorDelete"));
|
||||
}
|
||||
@@ -105,18 +108,20 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
|
||||
if (newChapterTitle.trim() === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const responseId: string = await System.authPostToServer<string>(
|
||||
'chapter/add',
|
||||
{
|
||||
bookId: bookId,
|
||||
wordsCount: 0,
|
||||
chapterOrder: newChapterOrder ? newChapterOrder : 0,
|
||||
title: newChapterTitle,
|
||||
},
|
||||
token,
|
||||
);
|
||||
let responseId: string;
|
||||
const chapterData = {
|
||||
bookId: bookId,
|
||||
wordsCount: 0,
|
||||
chapterOrder: newChapterOrder ? newChapterOrder : 0,
|
||||
title: newChapterTitle,
|
||||
};
|
||||
if (isCurrentlyOffline()) {
|
||||
responseId = await window.electron.invoke<string>('db:chapter:add', chapterData);
|
||||
} else {
|
||||
responseId = await System.authPostToServer<string>('chapter/add', chapterData, token);
|
||||
}
|
||||
if (!responseId) {
|
||||
errorMessage(t("mainChapter.errorAdd"));
|
||||
return;
|
||||
@@ -130,7 +135,7 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
|
||||
};
|
||||
setChapters([...chapters, newChapter]);
|
||||
setNewChapterTitle('');
|
||||
|
||||
|
||||
setNewChapterOrder(newChapterOrder + 1);
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
|
||||
Reference in New Issue
Block a user