Add error handling, enhance syncing, and refactor deletion logic
- Introduce new error messages for syncing and book deletion in `en.json`. - Update `DeleteBook` to support local-only deletion and synced book management. - Refine offline/online behavior with `deleteLocalToo` checkbox and update related state handling. - Extend repository and IPC methods to handle optional IDs for updates. - Add `SyncQueueContext` for queueing offline changes and improving synchronization workflows. - Enhance refined text generation logic in `DraftCompanion` and `GhostWriter` components. - Replace PUT with PATCH for world updates to align with API expectations. - Streamline `AlertBox` by integrating dynamic translation keys for deletion prompts.
This commit is contained in:
@@ -15,6 +15,7 @@ interface AlertBoxProps {
|
||||
cancelText?: string;
|
||||
onConfirm: () => Promise<void>;
|
||||
onCancel: () => void;
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
export default function AlertBox(
|
||||
@@ -25,7 +26,8 @@ export default function AlertBox(
|
||||
confirmText = 'Confirmer',
|
||||
cancelText = 'Annuler',
|
||||
onConfirm,
|
||||
onCancel
|
||||
onCancel,
|
||||
children
|
||||
}: AlertBoxProps) {
|
||||
const [mounted, setMounted] = useState(false);
|
||||
|
||||
@@ -86,9 +88,9 @@ export default function AlertBox(
|
||||
</div>
|
||||
|
||||
<div className="p-6 bg-dark-background/30">
|
||||
<p className="mb-6 text-text-primary whitespace-pre-line leading-relaxed">{message}</p>
|
||||
|
||||
<div className="flex justify-end gap-3">
|
||||
<p className="text-text-primary whitespace-pre-line leading-relaxed">{message}</p>
|
||||
{children}
|
||||
<div className="flex justify-end gap-3 mt-6">
|
||||
<CancelButton callBackFunction={onCancel} text={cancelText}/>
|
||||
<ConfirmButton text={confirmText} buttonType={type} callBackFunction={onConfirm}/>
|
||||
</div>
|
||||
|
||||
@@ -147,7 +147,7 @@ export default function ShortStoryGenerator({onClose}: ShortStoryGeneratorProps)
|
||||
infoMessage(t("shortStoryGenerator.result.abortSuccess"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleGeneration(): Promise<void> {
|
||||
setIsGenerating(true);
|
||||
setGeneratedText('');
|
||||
@@ -213,17 +213,14 @@ export default function ShortStoryGenerator({onClose}: ShortStoryGeneratorProps)
|
||||
totalPrice?: number;
|
||||
totalCost?: number;
|
||||
} = JSON.parse(line.slice(6));
|
||||
|
||||
|
||||
if (data.content && data.content !== 'starting') {
|
||||
accumulatedText += data.content;
|
||||
setGeneratedText(accumulatedText);
|
||||
}
|
||||
|
||||
if (data.title) {
|
||||
setGeneratedStoryTitle(data.title);
|
||||
}
|
||||
|
||||
if (data.useYourKey !== undefined && data.totalPrice !== undefined) {
|
||||
if (data.title && data.useYourKey !== undefined && data.totalPrice !== undefined) {
|
||||
setGeneratedStoryTitle(data.title);
|
||||
if (data.useYourKey) {
|
||||
setTotalPrice((prev: number): number => prev + data.totalPrice!);
|
||||
} else {
|
||||
@@ -231,16 +228,15 @@ export default function ShortStoryGenerator({onClose}: ShortStoryGeneratorProps)
|
||||
}
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
console.error('Error parsing SSE data:', e);
|
||||
errorMessage(t("shortStoryGenerator.result.parsingError"));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
// Si le reader est annulé ou une erreur survient, sortir
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setIsGenerating(false);
|
||||
setHasGenerated(true);
|
||||
setAbortController(null);
|
||||
|
||||
@@ -23,7 +23,7 @@ export default function SyncBook({bookId, status}: SyncBookProps) {
|
||||
async function upload(): Promise<void> {
|
||||
if (isOffline) return;
|
||||
setIsLoading(true);
|
||||
const success = await hookUpload(bookId);
|
||||
const success:boolean = await hookUpload(bookId);
|
||||
if (success) setCurrentStatus('synced');
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ export default function BookList() {
|
||||
if (!isCurrentlyOffline()) {
|
||||
const response: boolean = await System.authPostToServer<boolean>(
|
||||
'logs/tour',
|
||||
{plateforme: 'web', tour: 'new-first-book'},
|
||||
{plateforme: 'desktop', tour: 'new-first-book'},
|
||||
session.accessToken, lang
|
||||
);
|
||||
if (response) {
|
||||
|
||||
@@ -68,7 +68,7 @@ function BasicInformationSetting(props: any, ref: any) {
|
||||
},
|
||||
params: {
|
||||
lang: lang,
|
||||
plateforme: 'web',
|
||||
plateforme: 'desktop',
|
||||
},
|
||||
data: formData,
|
||||
responseType: 'arraybuffer'
|
||||
|
||||
@@ -9,6 +9,7 @@ import AlertBox from "@/components/AlertBox";
|
||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
|
||||
import {SyncedBook} from "@/lib/models/SyncedBook";
|
||||
import {useTranslations} from "next-intl";
|
||||
|
||||
interface DeleteBookProps {
|
||||
bookId: string;
|
||||
@@ -19,10 +20,16 @@ export default function DeleteBook({bookId}: DeleteBookProps) {
|
||||
const {lang} = useContext<LangContextProps>(LangContext)
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const [showConfirmBox, setShowConfirmBox] = useState<boolean>(false);
|
||||
const [deleteLocalToo, setDeleteLocalToo] = useState<boolean>(false);
|
||||
const {errorMessage} = useContext<AlertContextProps>(AlertContext)
|
||||
const {serverOnlyBooks,setServerOnlyBooks,localOnlyBooks,setLocalOnlyBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
|
||||
const {serverOnlyBooks,setServerOnlyBooks,localOnlyBooks,setLocalOnlyBooks,localSyncedBooks,setLocalSyncedBooks,setServerSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const t = useTranslations('deleteBook');
|
||||
|
||||
const ifLocalOnlyBook: SyncedBook | undefined = localOnlyBooks.find((book: SyncedBook): boolean => book.id === bookId);
|
||||
const ifSyncedBook: SyncedBook | undefined = localSyncedBooks.find((book: SyncedBook): boolean => book.id === bookId);
|
||||
|
||||
function handleConfirmation(): void {
|
||||
setDeleteLocalToo(false);
|
||||
setShowConfirmBox(true);
|
||||
}
|
||||
|
||||
@@ -30,9 +37,8 @@ export default function DeleteBook({bookId}: DeleteBookProps) {
|
||||
try {
|
||||
let response: boolean;
|
||||
const deleteData = { id: bookId };
|
||||
const ifLocalBook: SyncedBook | undefined = localOnlyBooks.find((book: SyncedBook): boolean => book.id === bookId);
|
||||
|
||||
if (isCurrentlyOffline() || ifLocalBook) {
|
||||
if (isCurrentlyOffline() || ifLocalOnlyBook) {
|
||||
response = await window.electron.invoke<boolean>('db:book:delete', deleteData);
|
||||
} else {
|
||||
response = await System.authDeleteToServer<boolean>(
|
||||
@@ -41,11 +47,23 @@ export default function DeleteBook({bookId}: DeleteBookProps) {
|
||||
session.accessToken,
|
||||
lang
|
||||
);
|
||||
// If synced book and user wants to delete local too
|
||||
if (response && ifSyncedBook && deleteLocalToo) {
|
||||
await window.electron.invoke<boolean>('db:book:delete', deleteData);
|
||||
}
|
||||
}
|
||||
if (response) {
|
||||
setShowConfirmBox(false);
|
||||
if (ifLocalBook) {
|
||||
if (ifLocalOnlyBook) {
|
||||
setLocalOnlyBooks(localOnlyBooks.filter((b: SyncedBook): boolean => b.id !== bookId));
|
||||
} else if (ifSyncedBook) {
|
||||
// Remove from synced lists
|
||||
setLocalSyncedBooks(localSyncedBooks.filter((b: SyncedBook): boolean => b.id !== bookId));
|
||||
setServerSyncedBooks((prev: SyncedBook[]): SyncedBook[] => prev.filter((b: SyncedBook): boolean => b.id !== bookId));
|
||||
// If not deleting local, move to localOnlyBooks
|
||||
if (!deleteLocalToo) {
|
||||
setLocalOnlyBooks([...localOnlyBooks, ifSyncedBook]);
|
||||
}
|
||||
} else {
|
||||
setServerOnlyBooks(serverOnlyBooks.filter((b: SyncedBook): boolean => b.id !== bookId));
|
||||
}
|
||||
@@ -54,7 +72,7 @@ export default function DeleteBook({bookId}: DeleteBookProps) {
|
||||
if (e instanceof Error) {
|
||||
errorMessage(e.message)
|
||||
} else {
|
||||
errorMessage("Une erreur inconnue est survenue lors de la suppression du livre.");
|
||||
errorMessage(t('errorUnknown'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,10 +85,32 @@ export default function DeleteBook({bookId}: DeleteBookProps) {
|
||||
</button>
|
||||
{
|
||||
showConfirmBox && (
|
||||
<AlertBox title={'Suppression du livre'}
|
||||
message={'Vous être sur le point de supprimer votre livre définitivement.'} type={"danger"}
|
||||
onConfirm={handleDeleteBook} onCancel={() => setShowConfirmBox(false)}
|
||||
confirmText={'Supprimer'} cancelText={'Annuler'}/>
|
||||
<AlertBox title={t('title')}
|
||||
message={t('message')}
|
||||
type={"danger"}
|
||||
onConfirm={handleDeleteBook}
|
||||
onCancel={() => setShowConfirmBox(false)}
|
||||
confirmText={t('confirm')}
|
||||
cancelText={t('cancel')}>
|
||||
{ifSyncedBook && !isCurrentlyOffline() && (
|
||||
<div className="mt-4 p-3 bg-error/10 border border-error/30 rounded-lg">
|
||||
<label className="flex items-center gap-3 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={deleteLocalToo}
|
||||
onChange={(e) => setDeleteLocalToo(e.target.checked)}
|
||||
className="w-5 h-5 accent-error cursor-pointer"
|
||||
/>
|
||||
<span className="text-sm text-text-primary">
|
||||
{t('deleteLocalToo')}
|
||||
</span>
|
||||
</label>
|
||||
<p className="text-xs text-error mt-2">
|
||||
{t('deleteLocalWarning')}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</AlertBox>
|
||||
)
|
||||
}
|
||||
</>
|
||||
|
||||
@@ -165,7 +165,7 @@ export function WorldSetting(props: any, ref: any) {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await window.electron.invoke<boolean>('db:book:world:update', worldData);
|
||||
} else {
|
||||
response = await System.authPutToServer<boolean>('book/world/update', worldData, session.accessToken, lang);
|
||||
response = await System.authPatchToServer<boolean>('book/world/update', worldData, session.accessToken, lang);
|
||||
|
||||
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
|
||||
addToQueue('db:book:world:update', worldData);
|
||||
|
||||
@@ -198,13 +198,13 @@ export default function DraftCompanion() {
|
||||
infoMessage(t("draftCompanion.abortSuccess"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleQuillSenseRefined(): Promise<void> {
|
||||
if (chapter && session?.accessToken) {
|
||||
setIsRefining(true);
|
||||
setShowRefinedText(false);
|
||||
setRefinedText('');
|
||||
|
||||
|
||||
try {
|
||||
const response: Response = await fetch(`${configs.apiUrl}quillsense/refine`, {
|
||||
method: 'POST',
|
||||
@@ -260,21 +260,21 @@ export default function DraftCompanion() {
|
||||
const dataStr: string = line.slice(6);
|
||||
const data: {
|
||||
content?: string;
|
||||
totalCost?: number;
|
||||
totalPrice?: number;
|
||||
useYourKey?: boolean;
|
||||
aborted?: boolean;
|
||||
} = JSON.parse(dataStr);
|
||||
|
||||
if ('totalCost' in data && 'useYourKey' in data && 'totalPrice' in data) {
|
||||
if ('content' in data && data.content) {
|
||||
accumulatedText += data.content;
|
||||
setRefinedText(accumulatedText);
|
||||
}
|
||||
|
||||
else if ('useYourKey' in data && 'totalPrice' in data) {
|
||||
if (data.useYourKey) {
|
||||
setTotalPrice((prev: number): number => prev + data.totalPrice!);
|
||||
} else {
|
||||
setTotalCredits(data.totalPrice!);
|
||||
}
|
||||
} else if ('content' in data && data.content && data.content !== 'starting') {
|
||||
accumulatedText += data.content;
|
||||
setRefinedText(accumulatedText);
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
console.error('Error parsing SSE data:', e);
|
||||
|
||||
@@ -107,7 +107,7 @@ export default function GhostWriter() {
|
||||
infoMessage(t("ghostWriter.abortSuccess"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function handleGenerateGhostWriter(): Promise<void> {
|
||||
setIsGenerating(true);
|
||||
setIsTextGenerated(false);
|
||||
@@ -187,21 +187,20 @@ export default function GhostWriter() {
|
||||
totalCost?: number;
|
||||
totalPrice?: number;
|
||||
useYourKey?: boolean;
|
||||
aborted?: boolean;
|
||||
} = JSON.parse(dataStr);
|
||||
|
||||
if ('totalCost' in data && 'useYourKey' in data && 'totalPrice' in data) {
|
||||
if ('content' in data && data.content) {
|
||||
accumulatedText += data.content;
|
||||
setTextGenerated(accumulatedText);
|
||||
}
|
||||
else if ('useYourKey' in data && 'totalPrice' in data) {
|
||||
if (data.useYourKey) {
|
||||
setTotalPrice((prev: number): number => prev + data.totalPrice!);
|
||||
} else {
|
||||
setTotalCredits(data.totalPrice!);
|
||||
}
|
||||
} else if ('content' in data && data.content && data.content !== 'starting') {
|
||||
accumulatedText += data.content;
|
||||
setTextGenerated(accumulatedText);
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
console.error('Error parsing SSE data:', e);
|
||||
errorMessage(t('ghostWriter.errorProcessingData'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,7 +208,7 @@ export default function GhostWriter() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setIsGenerating(false);
|
||||
setIsTextGenerated(true);
|
||||
setAbortController(null);
|
||||
|
||||
@@ -8,7 +8,7 @@ import { faWifi, faCircle } from '@fortawesome/free-solid-svg-icons';
|
||||
export default function OfflineToggle() {
|
||||
const { offlineMode, toggleOfflineMode } = useContext(OfflineContext);
|
||||
|
||||
if (!window.electron) {
|
||||
if (!window.electron || !offlineMode.isDatabaseInitialized) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user