- 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.
165 lines
5.3 KiB
TypeScript
165 lines
5.3 KiB
TypeScript
import { ipcMain } from 'electron';
|
|
import { createHandler } from '../database/LocalSystem.js';
|
|
import Chapter from '../database/models/Chapter.js';
|
|
import type { ChapterProps, CompanionContent, ActStory } from '../database/models/Chapter.js';
|
|
|
|
interface GetWholeChapterData {
|
|
id: string;
|
|
version: number;
|
|
bookid: string;
|
|
}
|
|
|
|
interface SaveChapterContentData {
|
|
chapterId: string;
|
|
version: number;
|
|
content: JSON;
|
|
totalWordCount: number;
|
|
currentTime: number;
|
|
}
|
|
|
|
interface AddChapterData {
|
|
bookId: string;
|
|
title: string;
|
|
chapterOrder: number;
|
|
}
|
|
|
|
interface UpdateChapterData {
|
|
chapterId: string;
|
|
title: string;
|
|
chapterOrder: number;
|
|
}
|
|
|
|
interface AddChapterInformationData {
|
|
chapterId: string;
|
|
actId: number;
|
|
bookId: string;
|
|
plotId: string | null;
|
|
incidentId: string | null;
|
|
}
|
|
|
|
interface GetChapterContentData {
|
|
chapterId: string;
|
|
version: number;
|
|
}
|
|
|
|
// GET /book/chapters - Get all chapters from a book
|
|
ipcMain.handle('db:book:chapters', createHandler<string, ChapterProps[]>(
|
|
function(userId: string, bookId: string, lang: 'fr' | 'en'): ChapterProps[] {
|
|
return Chapter.getAllChaptersFromABook(userId, bookId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// GET /chapter/whole - Get whole chapter
|
|
ipcMain.handle('db:chapter:whole', createHandler<GetWholeChapterData, ChapterProps>(
|
|
function(userId: string, data: GetWholeChapterData, lang: 'fr' | 'en'): ChapterProps {
|
|
return Chapter.getWholeChapter(userId, data.id, data.version, data.bookid, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// GET /chapter/:id/story - Get chapter story
|
|
ipcMain.handle('db:chapter:story', createHandler<string, ActStory[]>(
|
|
function(userId: string, chapterId: string, lang: 'fr' | 'en'): ActStory[] {
|
|
return Chapter.getChapterStory(userId, chapterId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// GET /chapter/content/companion - Get companion content
|
|
ipcMain.handle('db:chapter:content:companion', createHandler<GetChapterContentData, CompanionContent>(
|
|
function(userId: string, data: GetChapterContentData, lang: 'fr' | 'en'): CompanionContent {
|
|
return Chapter.getCompanionContent(userId, data.chapterId, data.version, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// GET /chapter/content - Get chapter content by version
|
|
ipcMain.handle('db:chapter:content:get', createHandler<GetChapterContentData, string>(
|
|
function(userId: string, data: GetChapterContentData, lang: 'fr' | 'en'): string {
|
|
return Chapter.getChapterContentByVersion(userId, data.chapterId, data.version, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// POST /chapter/content - Save chapter content
|
|
ipcMain.handle('db:chapter:content:save', createHandler<SaveChapterContentData, boolean>(
|
|
function(userId: string, data: SaveChapterContentData, lang: 'fr' | 'en'): boolean {
|
|
return Chapter.saveChapterContent(
|
|
userId,
|
|
data.chapterId,
|
|
data.version,
|
|
data.content,
|
|
data.totalWordCount,
|
|
data.currentTime,
|
|
lang
|
|
);
|
|
}
|
|
)
|
|
);
|
|
|
|
// GET /chapter/last-chapter - Get last chapter
|
|
ipcMain.handle('db:chapter:last', createHandler<string, ChapterProps | null>(
|
|
function(userId: string, bookId: string, lang: 'fr' | 'en'): ChapterProps | null {
|
|
return Chapter.getLastChapter(userId, bookId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// POST /chapter/add - Add new chapter
|
|
ipcMain.handle('db:chapter:add', createHandler<AddChapterData, string>(
|
|
function(userId: string, data: AddChapterData, lang: 'fr' | 'en'): string {
|
|
return Chapter.addChapter(userId, data.bookId, data.title, 0, data.chapterOrder, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// DELETE /chapter/remove - Remove chapter
|
|
interface RemoveChapterData {
|
|
chapterId: string;
|
|
bookId?: string;
|
|
}
|
|
ipcMain.handle('db:chapter:remove', createHandler<RemoveChapterData, boolean>(
|
|
function(userId: string, data: RemoveChapterData, lang: 'fr' | 'en'): boolean {
|
|
return Chapter.removeChapter(userId, data.chapterId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// POST /chapter/update - Update chapter
|
|
ipcMain.handle('db:chapter:update', createHandler<UpdateChapterData, boolean>(
|
|
function(userId: string, data: UpdateChapterData, lang: 'fr' | 'en'): boolean {
|
|
return Chapter.updateChapter(userId, data.chapterId, data.title, data.chapterOrder, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
// POST /chapter/resume/add - Add chapter information
|
|
ipcMain.handle('db:chapter:information:add', createHandler<AddChapterInformationData, string>(
|
|
function(userId: string, data: AddChapterInformationData, lang: 'fr' | 'en'): string {
|
|
return Chapter.addChapterInformation(
|
|
userId,
|
|
data.chapterId,
|
|
data.actId,
|
|
data.bookId,
|
|
data.plotId,
|
|
data.incidentId,
|
|
lang
|
|
);
|
|
}
|
|
)
|
|
);
|
|
|
|
// DELETE /chapter/resume/remove - Remove chapter information
|
|
interface RemoveChapterInfoData {
|
|
chapterInfoId: string;
|
|
}
|
|
ipcMain.handle('db:chapter:information:remove', createHandler<RemoveChapterInfoData, boolean>(
|
|
function(userId: string, data: RemoveChapterInfoData, lang: 'fr' | 'en'): boolean {
|
|
return Chapter.removeChapterInformation(userId, data.chapterInfoId, lang);
|
|
}
|
|
)
|
|
);
|
|
|
|
console.log('[IPC] Chapter handlers registered');
|