From 3d4feaa680707f468d907f91b786ac4daedf3ac2 Mon Sep 17 00:00:00 2001 From: natreex Date: Thu, 15 Jan 2026 16:12:20 -0500 Subject: [PATCH] Refactor IPC handlers, types, and models for streamlined data handling - Unified return types across IPC handlers (`Character`, `Location`, `World`, and `Book`) for consistency. - Replaced `BookListProps` with `BookProps` for simplified type usage in components and handlers. - Cleaned up obsolete `BookListProps` interface and applied consistent typings throughout. - Updated imports to include revised response types (`CharacterListResponse`, `LocationListResponse`, `WorldListResponse`). - Adjusted data transformation logic in `BookList` and related components to align with new type definitions. --- components/ScribeControllerBar.tsx | 8 +++---- components/book/BookList.tsx | 38 +++++++++++++++--------------- electron/ipc/book.ipc.ts | 6 ++--- electron/ipc/character.ipc.ts | 8 +++---- electron/ipc/location.ipc.ts | 6 ++--- lib/models/Book.ts | 17 ------------- 6 files changed, 33 insertions(+), 50 deletions(-) diff --git a/components/ScribeControllerBar.tsx b/components/ScribeControllerBar.tsx index 355963b..a364a2e 100644 --- a/components/ScribeControllerBar.tsx +++ b/components/ScribeControllerBar.tsx @@ -9,7 +9,7 @@ import {faGear, faGlobe, faHome} from "@fortawesome/free-solid-svg-icons"; import {SelectBoxProps} from "@/shared/interface"; import {AlertContext} from "@/context/AlertContext"; import {SessionContext} from "@/context/SessionContext"; -import Book, {BookListProps} from "@/lib/models/Book"; +import Book, {BookProps} from "@/lib/models/Book"; import Modal from "@/components/Modal"; import BookSetting from "@/components/book/settings/BookSetting"; import SelectBox from "@/components/form/SelectBox"; @@ -61,7 +61,7 @@ export default function ScribeControllerBar() { async function getBook(bookId: string): Promise { try { - const response: BookListProps = await System.authGetQueryToServer(`book/basic-information`, session.accessToken, lang, { + const response: BookProps = await System.authGetQueryToServer(`book/basic-information`, session.accessToken, lang, { id: bookId, }); if (!response) { @@ -69,12 +69,12 @@ export default function ScribeControllerBar() { return; } setBook!!({ - bookId: response.id, + bookId: response.bookId, type: response.type, title: response.title, subTitle: response.subTitle, summary: response.summary, - publicationDate: response.desiredReleaseDate, + publicationDate: response.publicationDate, desiredWordCount: response.desiredWordCount, totalWordCount: response.desiredWordCount, quillsenseEnabled: response.quillsenseEnabled, diff --git a/components/book/BookList.tsx b/components/book/BookList.tsx index 6758730..7fe51b4 100644 --- a/components/book/BookList.tsx +++ b/components/book/BookList.tsx @@ -6,7 +6,7 @@ import SearchBook from "./SearchBook"; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; import {faBook, faDownload, faGear, faTrash} from "@fortawesome/free-solid-svg-icons"; import {SessionContext} from "@/context/SessionContext"; -import Book, {BookListProps, BookProps} from "@/lib/models/Book"; +import Book, {BookProps} from "@/lib/models/Book"; import BookCard from "@/components/book/BookCard"; import BookCardSkeleton from "@/components/book/BookCardSkeleton"; import GuideTour, {GuideStep} from "@/components/GuideTour"; @@ -139,40 +139,40 @@ export default function BookList() { async function getBooks(): Promise { setIsLoadingBooks(true); try { - let bookResponse: (BookListProps & { itIsLocal: boolean })[] = []; + let bookResponse: (BookProps & { itIsLocal: boolean })[] = []; if (!isCurrentlyOffline()) { - const [onlineBooks, localBooks]: [BookListProps[], BookListProps[]] = await Promise.all([ - System.authGetQueryToServer('books', accessToken, lang), + const [onlineBooks, localBooks]: [BookProps[], BookProps[]] = await Promise.all([ + System.authGetQueryToServer('books', accessToken, lang), offlineMode.isDatabaseInitialized - ? window.electron.invoke('db:book:books') + ? window.electron.invoke('db:book:books') : Promise.resolve([]) ]); - const onlineBookIds: Set = new Set(onlineBooks.map((book: BookListProps): string => book.id)); - const uniqueLocalBooks: BookListProps[] = localBooks.filter((book: BookListProps): boolean => !onlineBookIds.has(book.id)); + const onlineBookIds: Set = new Set(onlineBooks.map((book: BookProps): string => book.bookId)); + const uniqueLocalBooks: BookProps[] = localBooks.filter((book: BookProps): boolean => !onlineBookIds.has(book.bookId)); bookResponse = [ - ...onlineBooks.map((book: BookListProps): BookListProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: false })), - ...uniqueLocalBooks.map((book: BookListProps): BookListProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: true })) + ...onlineBooks.map((book: BookProps): BookProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: false })), + ...uniqueLocalBooks.map((book: BookProps): BookProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: true })) ]; } else { if (!offlineMode.isDatabaseInitialized) { setIsLoadingBooks(false); return; } - const localBooks: BookListProps[] = await window.electron.invoke('db:book:books'); - bookResponse = localBooks.map((book: BookListProps): BookListProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: true })); + const localBooks: BookProps[] = await window.electron.invoke('db:book:books'); + bookResponse = localBooks.map((book: BookProps): BookProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: true })); } if (bookResponse) { - const booksByType: Record = bookResponse.reduce((groups: Record, book: BookListProps): Record => { + const booksByType: Record = bookResponse.reduce((groups: Record, book: BookProps): Record => { const imageDataUrl: string = book.coverImage ? 'data:image/jpeg;base64,' + book.coverImage : ''; const categoryLabel: string = Book.getBookTypeLabel(book.type); const transformedBook: BookProps = { - bookId: book.id, + bookId: book.bookId, type: categoryLabel, title: book.title, subTitle: book.subTitle, summary: book.summary, - serie: book.serieId, - publicationDate: book.desiredReleaseDate, + serie: book.serie, + publicationDate: book.publicationDate, desiredWordCount: book.desiredWordCount, totalWordCount: 0, coverImage: imageDataUrl, @@ -229,7 +229,7 @@ export default function BookList() { async function getBook(bookId: string): Promise { try { let localBookOnly: boolean = false; - let bookResponse: BookListProps|null = null; + let bookResponse: BookProps|null = null; if (isCurrentlyOffline()){ if (!offlineMode.isDatabaseInitialized) { errorMessage(t("bookList.errorBookDetails")); @@ -246,7 +246,7 @@ export default function BookList() { localBookOnly = true; } if (!bookResponse) { - bookResponse = await System.authGetQueryToServer(`book/basic-information`, accessToken, lang, {id: bookId}); + bookResponse = await System.authGetQueryToServer(`book/basic-information`, accessToken, lang, {id: bookId}); } } if (!bookResponse) { @@ -260,8 +260,8 @@ export default function BookList() { subTitle: bookResponse?.subTitle || '', summary: bookResponse?.summary || '', type: bookResponse?.type || '', - serie: bookResponse?.serieId, - publicationDate: bookResponse?.desiredReleaseDate || '', + serie: bookResponse?.serie, + publicationDate: bookResponse?.publicationDate || '', desiredWordCount: bookResponse?.desiredWordCount || 0, totalWordCount: 0, localBook: localBookOnly, diff --git a/electron/ipc/book.ipc.ts b/electron/ipc/book.ipc.ts index bafc4db..80f1293 100644 --- a/electron/ipc/book.ipc.ts +++ b/electron/ipc/book.ipc.ts @@ -12,7 +12,7 @@ import Upload from "../database/models/Upload.js"; import GuideLine, {GuideLineAI} from "../database/models/GuideLine.js"; import Incident from "../database/models/Incident.js"; import PlotPoint from "../database/models/PlotPoint.js"; -import World, {WorldProps} from "../database/models/World.js"; +import World, {WorldListResponse, WorldProps} from "../database/models/World.js"; interface UpdateBookBasicData { title: string; @@ -330,8 +330,8 @@ ipcMain.handle('db:book:issue:remove', createHandler( interface GetWorldsData { bookid: string; } -ipcMain.handle('db:book:worlds:get', createHandler( - function(userId: string, data: GetWorldsData, lang: 'fr' | 'en') { +ipcMain.handle('db:book:worlds:get', createHandler( + function(userId: string, data: GetWorldsData, lang: 'fr' | 'en'): WorldListResponse { return World.getWorlds(userId, data.bookid, lang); } ) diff --git a/electron/ipc/character.ipc.ts b/electron/ipc/character.ipc.ts index d4df615..865c142 100644 --- a/electron/ipc/character.ipc.ts +++ b/electron/ipc/character.ipc.ts @@ -1,7 +1,7 @@ import { ipcMain } from 'electron'; import { createHandler } from '../database/LocalSystem.js'; -import Character from '../database/models/Character.js'; -import type { CharacterProps, CharacterPropsPost, CharacterAttribute } from '../database/models/Character.js'; +import Character, {CharacterListResponse} from '../database/models/Character.js'; +import type { CharacterPropsPost, CharacterAttribute } from '../database/models/Character.js'; interface AddCharacterData { character: CharacterPropsPost; @@ -20,8 +20,8 @@ interface AddAttributeData { interface GetCharacterListData { bookid: string; } -ipcMain.handle('db:character:list', createHandler( - function(userId: string, data: GetCharacterListData, lang: 'fr' | 'en'): CharacterProps[] { +ipcMain.handle('db:character:list', createHandler( + function(userId: string, data: GetCharacterListData, lang: 'fr' | 'en'): CharacterListResponse { return Character.getCharacterList(userId, data.bookid, lang); } ) diff --git a/electron/ipc/location.ipc.ts b/electron/ipc/location.ipc.ts index 682b8f5..3adc741 100644 --- a/electron/ipc/location.ipc.ts +++ b/electron/ipc/location.ipc.ts @@ -1,6 +1,6 @@ import { ipcMain } from 'electron'; import { createHandler } from '../database/LocalSystem.js'; -import Location from '../database/models/Location.js'; +import Location, {LocationListResponse} from '../database/models/Location.js'; import type { LocationProps } from '../database/models/Location.js'; interface UpdateLocationResponse { @@ -34,8 +34,8 @@ interface UpdateLocationData { interface GetAllLocationsData { bookid: string; } -ipcMain.handle('db:location:all', createHandler( - function(userId: string, data: GetAllLocationsData, lang: 'fr' | 'en'): LocationProps[] { +ipcMain.handle('db:location:all', createHandler( + function(userId: string, data: GetAllLocationsData, lang: 'fr' | 'en'): LocationListResponse { return Location.getAllLocations(userId, data.bookid, lang); } ) diff --git a/lib/models/Book.ts b/lib/models/Book.ts index 6e7bdfc..f40d1a4 100644 --- a/lib/models/Book.ts +++ b/lib/models/Book.ts @@ -81,23 +81,6 @@ export interface BookProps { tools?: BookToolsSettings; } -export interface BookListProps { - id: string; - type: string; - authorId: string; - title: string; - subTitle?: string; - summary?: string; - serieId?: number; - desiredReleaseDate?: string; - desiredWordCount?: number; - wordCount?: number; - coverImage?: string; - bookMeta?: string; - itIsLocal?: boolean; - quillsenseEnabled?: boolean; -} - export interface GuideLine { tone: string; atmosphere: string;