Remove unused components and models for improved maintainability
- Deleted redundant components (`AddActionButton`, `AlertBox`, `AlertStack`, `BackButton`, `CancelButton`, and `CollapsableArea`) and related files. - Removed unused models (`Book`, `BookSerie`, `BookTables`, `Character`, and `Chapter`) to reduce codebase clutter. - Updated project structure and references to reflect these removals.
This commit is contained in:
@@ -1,29 +1,18 @@
|
||||
'use client'
|
||||
import {useCallback, useContext, useEffect, useState} from 'react';
|
||||
import {
|
||||
initialSpellState,
|
||||
SpellEditState,
|
||||
SpellListItem,
|
||||
SpellListResponse,
|
||||
SpellProps,
|
||||
SpellTagProps
|
||||
} from '@/lib/models/Spell';
|
||||
import {SeriesSpellDetailResponse, SeriesSpellListItem, SeriesSpellListResponse} from '@/lib/models/Series';
|
||||
import {SessionContext} from '@/context/SessionContext';
|
||||
import {BookContext} from '@/context/BookContext';
|
||||
import {AlertContext} from '@/context/AlertContext';
|
||||
import {SpellEditState, SpellListItem, SpellListResponse, SpellProps, SpellTagProps} from '@/lib/types/spell';
|
||||
import {initialSpellState} from '@/lib/constants/spell';
|
||||
import {SeriesSpellDetailResponse, SeriesSpellListItem, SeriesSpellListResponse} from '@/lib/types/series';
|
||||
import {SessionContext, SessionContextProps} from '@/context/SessionContext';
|
||||
import {BookContext, BookContextProps} from '@/context/BookContext';
|
||||
import {AlertContext, AlertContextProps} from '@/context/AlertContext';
|
||||
import {LangContext, LangContextProps} from '@/context/LangContext';
|
||||
import System from '@/lib/models/System';
|
||||
import {useTranslations} from 'next-intl';
|
||||
import {ViewMode} from '@/shared/interface';
|
||||
import OfflineContext, {OfflineContextType} from '@/context/OfflineContext';
|
||||
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from '@/context/SyncQueueContext';
|
||||
import {BooksSyncContext, BooksSyncContextProps} from '@/context/BooksSyncContext';
|
||||
import {SyncedBook} from '@/lib/models/SyncedBook';
|
||||
import {SeriesContext, SeriesContextProps} from '@/context/SeriesContext';
|
||||
import {SeriesSyncContext, SeriesSyncContextProps} from '@/context/SeriesSyncContext';
|
||||
import {SyncedSeries} from '@/lib/models/SyncedSeries';
|
||||
import {apiDelete, apiGet, apiPatch, apiPost, apiPut} from '@/lib/api/client';
|
||||
import {useTranslations} from '@/lib/i18n';
|
||||
import {ViewMode} from '@/lib/types/settings';
|
||||
import {isDesktop} from '@/lib/configs';
|
||||
import * as tauri from '@/lib/tauri';
|
||||
import OfflineContext, {OfflineContextType} from '@/context/OfflineContext';
|
||||
|
||||
export interface UseSpellsConfig {
|
||||
entityType: 'book' | 'series';
|
||||
@@ -70,15 +59,11 @@ export interface UseSpellsReturn {
|
||||
export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
const {entityType, entityId} = config;
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext<LangContextProps>(LangContext);
|
||||
const {session} = useContext(SessionContext);
|
||||
const {book, setBook} = useContext(BookContext);
|
||||
const {errorMessage, successMessage} = useContext(AlertContext);
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const {addToQueue} = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const {localSeries} = useContext<SeriesContextProps>(SeriesContext);
|
||||
const {localSyncedSeries} = useContext<SeriesSyncContextProps>(SeriesSyncContext);
|
||||
const {lang}: LangContextProps = useContext<LangContextProps>(LangContext);
|
||||
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
|
||||
const {book, setBook}: BookContextProps = useContext<BookContextProps>(BookContext);
|
||||
const {errorMessage, successMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
|
||||
const {isCurrentlyOffline}: OfflineContextType = useContext<OfflineContextType>(OfflineContext);
|
||||
|
||||
const [spells, setSpells] = useState<SpellListItem[]>([]);
|
||||
const [seriesSpells, setSeriesSpells] = useState<SeriesSpellListItem[]>([]);
|
||||
@@ -91,7 +76,7 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
|
||||
const [viewMode, setViewMode] = useState<ViewMode>('list');
|
||||
const [spellBackup, setSpellBackup] = useState<SpellEditState | null>(null);
|
||||
|
||||
|
||||
const isSeriesMode: boolean = entityType === 'series';
|
||||
const bookSeriesId: string | null = book?.seriesId || null;
|
||||
const userToken: string = session?.accessToken || '';
|
||||
@@ -107,48 +92,36 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
refreshSeriesSpells().then();
|
||||
}
|
||||
}, [bookSeriesId, isSeriesMode]);
|
||||
|
||||
|
||||
const refreshSeriesSpells = useCallback(async function (): Promise<void> {
|
||||
if (!bookSeriesId) return;
|
||||
try {
|
||||
let response: SeriesSpellListResponse;
|
||||
// Dual logic: offline ou livre local → IPC, sinon serveur
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.getSeriesSpellList(bookSeriesId) as SeriesSpellListResponse;
|
||||
} else {
|
||||
response = await System.authGetQueryToServer<SeriesSpellListResponse>(
|
||||
'series/spell/list',
|
||||
userToken,
|
||||
lang,
|
||||
{seriesid: bookSeriesId}
|
||||
);
|
||||
}
|
||||
const response: SeriesSpellListResponse = await apiGet<SeriesSpellListResponse>(
|
||||
'series/spell/list',
|
||||
userToken,
|
||||
lang,
|
||||
{seriesid: bookSeriesId}
|
||||
);
|
||||
if (response) {
|
||||
setSeriesSpells(response.spells);
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
console.error('Error loading series spells:', e.message);
|
||||
errorMessage(e.message);
|
||||
}
|
||||
}
|
||||
}, [bookSeriesId, userToken, lang, isCurrentlyOffline, book?.localBook]);
|
||||
|
||||
}, [bookSeriesId, userToken, lang]);
|
||||
|
||||
const refreshSpells = useCallback(async function (): Promise<void> {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
let response: SeriesSpellListResponse;
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
response = await tauri.getSeriesSpellList(entityId) as SeriesSpellListResponse;
|
||||
} else {
|
||||
response = await System.authGetQueryToServer<SeriesSpellListResponse>(
|
||||
'series/spell/list',
|
||||
userToken,
|
||||
lang,
|
||||
{seriesid: entityId}
|
||||
);
|
||||
}
|
||||
const response: SeriesSpellListResponse = await apiGet<SeriesSpellListResponse>(
|
||||
'series/spell/list',
|
||||
userToken,
|
||||
lang,
|
||||
{seriesid: entityId}
|
||||
);
|
||||
if (response) {
|
||||
const mappedSpells: SpellListItem[] = response.spells.map(function (spell: SeriesSpellListItem): SpellListItem {
|
||||
return {
|
||||
@@ -168,17 +141,12 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
}
|
||||
} else {
|
||||
let response: SpellListResponse;
|
||||
if (isCurrentlyOffline()) {
|
||||
response = await tauri.getSpellList(entityId, true) as SpellListResponse;
|
||||
} else if (book?.localBook) {
|
||||
response = await tauri.getSpellList(entityId, true) as SpellListResponse;
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
response = await tauri.getSpellList(entityId, book?.tools?.spells ?? false) as SpellListResponse;
|
||||
} else {
|
||||
response = await System.authGetQueryToServer<SpellListResponse>(
|
||||
'spell/list',
|
||||
userToken,
|
||||
lang,
|
||||
{bookid: entityId}
|
||||
);
|
||||
response = await apiGet<SpellListResponse>('spell/list', userToken, lang, {
|
||||
bookid: entityId
|
||||
});
|
||||
}
|
||||
if (response) {
|
||||
setSpells(response.spells.map(function (spell: SpellListItem): SpellListItem {
|
||||
@@ -211,13 +179,13 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [entityId, isSeriesMode, userToken, lang, book, setBook, errorMessage, t, isCurrentlyOffline]);
|
||||
|
||||
}, [entityId, isSeriesMode, userToken, lang, book, setBook, errorMessage, t]);
|
||||
|
||||
const selectSpell = useCallback(async function (spell: SpellListItem): Promise<void> {
|
||||
const tagIds: string[] = spell.tags ? spell.tags.map(function (tag: SpellTagProps): string {
|
||||
return tag.id;
|
||||
}) : [];
|
||||
|
||||
|
||||
setSelectedSpell({
|
||||
id: spell.id,
|
||||
name: spell.name,
|
||||
@@ -231,21 +199,15 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
seriesSpellId: spell.seriesSpellId || null,
|
||||
});
|
||||
setSelectedSeriesSpell(null);
|
||||
|
||||
|
||||
try {
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
let response: SeriesSpellDetailResponse;
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
response = await tauri.getSeriesSpellDetail(spell.id) as SeriesSpellDetailResponse;
|
||||
} else {
|
||||
response = await System.authGetQueryToServer<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: spell.id}
|
||||
);
|
||||
}
|
||||
const response: SeriesSpellDetailResponse = await apiGet<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: spell.id}
|
||||
);
|
||||
if (response) {
|
||||
setSelectedSpell(function (prev: SpellEditState | null): SpellEditState | null {
|
||||
if (!prev) return null;
|
||||
@@ -261,17 +223,12 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
}
|
||||
} else {
|
||||
let response: SpellProps;
|
||||
if (isCurrentlyOffline()) {
|
||||
response = await tauri.getSpellDetail(spell.id) as SpellProps;
|
||||
} else if (book?.localBook) {
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
response = await tauri.getSpellDetail(spell.id) as SpellProps;
|
||||
} else {
|
||||
response = await System.authGetQueryToServer<SpellProps>(
|
||||
'spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: spell.id}
|
||||
);
|
||||
response = await apiGet<SpellProps>('spell/detail', userToken, lang, {
|
||||
spellid: spell.id
|
||||
});
|
||||
}
|
||||
if (response) {
|
||||
setSelectedSpell(function (prev: SpellEditState | null): SpellEditState | null {
|
||||
@@ -286,19 +243,14 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
seriesSpellId: response.seriesSpellId || null,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
if (response.seriesSpellId) {
|
||||
let seriesSpellResponse: SeriesSpellDetailResponse;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
seriesSpellResponse = await tauri.getSeriesSpellDetail(response.seriesSpellId) as SeriesSpellDetailResponse;
|
||||
} else {
|
||||
seriesSpellResponse = await System.authGetQueryToServer<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: response.seriesSpellId}
|
||||
);
|
||||
}
|
||||
const seriesSpellResponse: SeriesSpellDetailResponse = await apiGet<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: response.seriesSpellId}
|
||||
);
|
||||
if (seriesSpellResponse) {
|
||||
setSelectedSeriesSpell(seriesSpellResponse);
|
||||
}
|
||||
@@ -310,44 +262,40 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
errorMessage(e.message);
|
||||
}
|
||||
}
|
||||
}, [isSeriesMode, userToken, lang, errorMessage, isCurrentlyOffline, book?.localBook]);
|
||||
|
||||
}, [isSeriesMode, userToken, lang, errorMessage]);
|
||||
|
||||
const addNewSpell = useCallback(function (): void {
|
||||
setSelectedSpell({...initialSpellState});
|
||||
setSelectedSeriesSpell(null);
|
||||
setViewMode('edit');
|
||||
setSpellBackup(null);
|
||||
}, []);
|
||||
|
||||
|
||||
const clearSelection = useCallback(function (): void {
|
||||
setSelectedSpell(null);
|
||||
setSelectedSeriesSpell(null);
|
||||
setViewMode('list');
|
||||
setSpellBackup(null);
|
||||
}, []);
|
||||
|
||||
|
||||
const updateSpellField = useCallback(function (key: keyof SpellEditState, value: string | string[] | null): void {
|
||||
if (selectedSpell) {
|
||||
setSelectedSpell({...selectedSpell, [key]: value});
|
||||
}
|
||||
}, [selectedSpell]);
|
||||
|
||||
|
||||
const toggleTool = useCallback(async function (enabled: boolean): Promise<void> {
|
||||
if (isSeriesMode) return;
|
||||
try {
|
||||
const requestData = {
|
||||
bookId: book?.bookId,
|
||||
toolName: 'spells',
|
||||
enabled: enabled
|
||||
};
|
||||
let response: boolean;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
response = await tauri.updateBookToolSetting(requestData.bookId, requestData.toolName, requestData.enabled);
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
response = await tauri.updateBookToolSetting(book?.bookId ?? '', 'spells', enabled);
|
||||
} else {
|
||||
response = await System.authPatchToServer<boolean>('book/tool-setting', requestData, userToken, lang);
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === book?.bookId)) {
|
||||
addToQueue('update_book_tool_setting', {data: requestData});
|
||||
}
|
||||
response = await apiPatch<boolean>('book/tool-setting', {
|
||||
bookId: book?.bookId,
|
||||
toolName: 'spells',
|
||||
enabled: enabled
|
||||
}, userToken, lang);
|
||||
}
|
||||
if (response && setBook && book) {
|
||||
setToolEnabled(enabled);
|
||||
@@ -366,18 +314,18 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
errorMessage(e.message);
|
||||
}
|
||||
}
|
||||
}, [isSeriesMode, book, setBook, userToken, lang, errorMessage, isCurrentlyOffline, localSyncedBooks, addToQueue]);
|
||||
|
||||
}, [isSeriesMode, book, setBook, userToken, lang, errorMessage]);
|
||||
|
||||
const saveSpell = useCallback(async function (): Promise<boolean> {
|
||||
if (!selectedSpell) return false;
|
||||
|
||||
|
||||
if (selectedSpell.id === null) {
|
||||
return await addSpellInternal(selectedSpell);
|
||||
} else {
|
||||
return await updateSpellInternal(selectedSpell);
|
||||
}
|
||||
}, [selectedSpell]);
|
||||
|
||||
|
||||
async function addSpellInternal(spell: SpellEditState): Promise<boolean> {
|
||||
if (!spell.name) {
|
||||
errorMessage(t("spellComponent.errorNameRequired"));
|
||||
@@ -385,49 +333,23 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
}
|
||||
try {
|
||||
let newSpellId: string;
|
||||
const spellData: Record<string, string | string[] | null> = {
|
||||
...(isSeriesMode ? {seriesId: entityId} : {bookId: entityId}),
|
||||
name: spell.name,
|
||||
description: spell.description,
|
||||
appearance: spell.appearance,
|
||||
tags: spell.tags,
|
||||
powerLevel: spell.powerLevel,
|
||||
components: spell.components,
|
||||
limitations: spell.limitations,
|
||||
notes: spell.notes,
|
||||
};
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
const data = {
|
||||
seriesId: entityId,
|
||||
name: spell.name,
|
||||
description: spell.description,
|
||||
appearance: spell.appearance,
|
||||
tags: spell.tags,
|
||||
powerLevel: spell.powerLevel,
|
||||
components: spell.components,
|
||||
limitations: spell.limitations,
|
||||
notes: spell.notes,
|
||||
};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
newSpellId = await tauri.addSeriesSpell(data);
|
||||
} else {
|
||||
newSpellId = await System.authPostToServer<string>('series/spell/add', data, userToken, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === entityId)) {
|
||||
addToQueue('add_series_spell', {data: {...data, id: newSpellId}});
|
||||
}
|
||||
}
|
||||
newSpellId = await apiPost<string>('series/spell/add', spellData, userToken, lang);
|
||||
} else if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
newSpellId = await tauri.createSpell(entityId, spellData) as string;
|
||||
} else {
|
||||
const data = {
|
||||
bookId: entityId,
|
||||
spell: {
|
||||
name: spell.name,
|
||||
description: spell.description,
|
||||
appearance: spell.appearance,
|
||||
tags: spell.tags,
|
||||
powerLevel: spell.powerLevel,
|
||||
components: spell.components,
|
||||
limitations: spell.limitations,
|
||||
notes: spell.notes,
|
||||
}
|
||||
};
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
newSpellId = await tauri.createSpell(data.bookId, data.spell);
|
||||
} else {
|
||||
newSpellId = await System.authPostToServer<string>('spell/add', data, userToken, lang);
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('create_spell', {data: {...data, id: newSpellId}});
|
||||
}
|
||||
}
|
||||
newSpellId = await apiPost<string>('spell/add', spellData, userToken, lang);
|
||||
}
|
||||
if (!newSpellId) {
|
||||
errorMessage(t("spellComponent.errorAddSpell"));
|
||||
@@ -458,7 +380,7 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function updateSpellInternal(spellToUpdate: SpellEditState): Promise<boolean> {
|
||||
if (!spellToUpdate.id) return false;
|
||||
if (!spellToUpdate.name) {
|
||||
@@ -466,8 +388,7 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
let success: boolean;
|
||||
const data = {
|
||||
const spellData: Record<string, string | string[] | null> = {
|
||||
id: spellToUpdate.id,
|
||||
name: spellToUpdate.name,
|
||||
description: spellToUpdate.description,
|
||||
@@ -478,25 +399,13 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
limitations: spellToUpdate.limitations,
|
||||
notes: spellToUpdate.notes,
|
||||
};
|
||||
let success: boolean;
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
success = await tauri.updateSeriesSpell(data);
|
||||
} else {
|
||||
success = await System.authPutToServer<boolean>('series/spell/update', data, userToken, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === entityId)) {
|
||||
addToQueue('update_series_spell', {data});
|
||||
}
|
||||
}
|
||||
success = await apiPut<boolean>('series/spell/update', spellData, userToken, lang);
|
||||
} else if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
success = await tauri.updateSpell(spellToUpdate.id!, spellData);
|
||||
} else {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
success = await tauri.updateSpell(data.id, data);
|
||||
} else {
|
||||
success = await System.authPutToServer<boolean>('spell/update', data, userToken, lang);
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('update_spell', {data});
|
||||
}
|
||||
}
|
||||
success = await apiPut<boolean>('spell/update', spellData, userToken, lang);
|
||||
}
|
||||
if (!success) {
|
||||
errorMessage(t("spellComponent.errorUpdateSpell"));
|
||||
@@ -529,32 +438,16 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const deleteSpell = useCallback(async function (spellId: string): Promise<void> {
|
||||
try {
|
||||
let success: boolean;
|
||||
const deletedAt: number = System.timeStampInSeconds();
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
const requestData = {spellId, deletedAt};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
success = await tauri.deleteSeriesSpell(requestData.spellId, requestData.deletedAt);
|
||||
} else {
|
||||
success = await System.authDeleteToServer<boolean>('series/spell/delete', requestData, userToken, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === entityId)) {
|
||||
addToQueue('delete_series_spell', {data: requestData});
|
||||
}
|
||||
}
|
||||
success = await apiDelete<boolean>('series/spell/delete', {spellId}, userToken, lang);
|
||||
} else if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
success = await tauri.deleteSpell(spellId, book?.bookId ?? '', Date.now());
|
||||
} else {
|
||||
const requestData = {spellId, bookId: entityId, deletedAt};
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
success = await tauri.deleteSpell(requestData.spellId, requestData.bookId, requestData.deletedAt);
|
||||
} else {
|
||||
success = await System.authDeleteToServer<boolean>('spell/delete', requestData, userToken, lang);
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('delete_spell', {data: requestData});
|
||||
}
|
||||
}
|
||||
success = await apiDelete<boolean>('spell/delete', {spellId}, userToken, lang);
|
||||
}
|
||||
if (!success) {
|
||||
errorMessage(t("spellComponent.errorDeleteSpell"));
|
||||
@@ -573,44 +466,31 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
errorMessage(t("common.unknownError"));
|
||||
}
|
||||
}
|
||||
}, [isSeriesMode, userToken, lang, errorMessage, t, isCurrentlyOffline, book?.localBook, entityId, localSyncedBooks, addToQueue]);
|
||||
|
||||
}, [isSeriesMode, userToken, lang, errorMessage, t]);
|
||||
|
||||
const exportToSeries = useCallback(async function (): Promise<void> {
|
||||
if (!selectedSpell || !selectedSpell.id || !bookSeriesId) return;
|
||||
|
||||
|
||||
try {
|
||||
const seriesSpellData = {
|
||||
seriesId: bookSeriesId,
|
||||
name: selectedSpell.name,
|
||||
description: selectedSpell.description,
|
||||
appearance: selectedSpell.appearance || '',
|
||||
tags: [],
|
||||
powerLevel: selectedSpell.powerLevel || null,
|
||||
components: selectedSpell.components || null,
|
||||
limitations: selectedSpell.limitations || null,
|
||||
notes: selectedSpell.notes || null,
|
||||
};
|
||||
|
||||
let seriesSpellId: string;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
// Mode offline ou livre local → IPC
|
||||
seriesSpellId = await tauri.addSeriesSpell(seriesSpellData);
|
||||
} else {
|
||||
// Mode online → Serveur
|
||||
seriesSpellId = await System.authPostToServer<string>(
|
||||
'series/spell/add',
|
||||
seriesSpellData,
|
||||
userToken,
|
||||
lang
|
||||
);
|
||||
// Si la série a une copie locale → addToQueue
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === bookSeriesId)) {
|
||||
addToQueue('add_series_spell', {data: {...seriesSpellData, id: seriesSpellId}});
|
||||
}
|
||||
}
|
||||
|
||||
const seriesSpellId: string = await apiPost<string>(
|
||||
'series/spell/add',
|
||||
{
|
||||
seriesId: bookSeriesId,
|
||||
name: selectedSpell.name,
|
||||
description: selectedSpell.description,
|
||||
appearance: selectedSpell.appearance || '',
|
||||
tags: [],
|
||||
powerLevel: selectedSpell.powerLevel || null,
|
||||
components: selectedSpell.components || null,
|
||||
limitations: selectedSpell.limitations || null,
|
||||
notes: selectedSpell.notes || null,
|
||||
},
|
||||
userToken,
|
||||
lang
|
||||
);
|
||||
|
||||
if (seriesSpellId) {
|
||||
const updateData = {
|
||||
const updateSuccess: boolean = await apiPut<boolean>('spell/update', {
|
||||
id: selectedSpell.id,
|
||||
name: selectedSpell.name,
|
||||
description: selectedSpell.description,
|
||||
@@ -621,21 +501,8 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
limitations: selectedSpell.limitations,
|
||||
notes: selectedSpell.notes,
|
||||
seriesSpellId: seriesSpellId
|
||||
};
|
||||
|
||||
let updateSuccess: boolean;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
// Mode offline ou livre local → Tauri
|
||||
updateSuccess = await tauri.updateSpell(updateData.id, updateData);
|
||||
} else {
|
||||
// Mode online → Serveur
|
||||
updateSuccess = await System.authPutToServer<boolean>('spell/update', updateData, userToken, lang);
|
||||
// Si le livre a une copie locale → addToQueue
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('update_spell', {data: updateData});
|
||||
}
|
||||
}
|
||||
|
||||
}, userToken, lang);
|
||||
|
||||
if (updateSuccess) {
|
||||
setSelectedSpell({...selectedSpell, seriesSpellId: seriesSpellId});
|
||||
setSpells(function (prev: SpellListItem[]): SpellListItem[] {
|
||||
@@ -662,56 +529,38 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
errorMessage(e.message);
|
||||
}
|
||||
}
|
||||
}, [selectedSpell, bookSeriesId, userToken, lang, successMessage, errorMessage, t, isCurrentlyOffline, book, addToQueue, localSyncedBooks, localSyncedSeries, entityId]);
|
||||
|
||||
}, [selectedSpell, bookSeriesId, userToken, lang, successMessage, errorMessage, t]);
|
||||
|
||||
const importFromSeries = useCallback(async function (seriesSpellId: string): Promise<void> {
|
||||
try {
|
||||
// 1. Récupérer les détails du sort de la série
|
||||
let seriesSpellDetail: SeriesSpellDetailResponse;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
// Mode offline → Tauri pour récupérer les détails du sort de la série locale
|
||||
seriesSpellDetail = await tauri.getSeriesSpellDetail(seriesSpellId) as SeriesSpellDetailResponse;
|
||||
} else {
|
||||
// Mode online → Serveur
|
||||
seriesSpellDetail = await System.authGetQueryToServer<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: seriesSpellId}
|
||||
);
|
||||
}
|
||||
|
||||
const seriesSpellDetail: SeriesSpellDetailResponse = await apiGet<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: seriesSpellId}
|
||||
);
|
||||
|
||||
if (!seriesSpellDetail) return;
|
||||
|
||||
// 2. Créer le sort dans le livre
|
||||
const spellData = {
|
||||
bookId: entityId,
|
||||
spell: {
|
||||
name: seriesSpellDetail.name,
|
||||
description: seriesSpellDetail.description,
|
||||
appearance: seriesSpellDetail.appearance || '',
|
||||
tags: [],
|
||||
powerLevel: seriesSpellDetail.powerLevel,
|
||||
components: seriesSpellDetail.components,
|
||||
limitations: seriesSpellDetail.limitations,
|
||||
notes: seriesSpellDetail.notes,
|
||||
seriesSpellId: seriesSpellId
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let createdSpellId: string;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
// Mode offline ou livre local → Tauri
|
||||
createdSpellId = await tauri.createSpell(spellData.bookId, spellData.spell);
|
||||
const spellImportData: Record<string, unknown> = {
|
||||
bookId: entityId,
|
||||
name: seriesSpellDetail.name,
|
||||
description: seriesSpellDetail.description,
|
||||
appearance: seriesSpellDetail.appearance || '',
|
||||
tags: [],
|
||||
powerLevel: seriesSpellDetail.powerLevel,
|
||||
components: seriesSpellDetail.components,
|
||||
limitations: seriesSpellDetail.limitations,
|
||||
notes: seriesSpellDetail.notes,
|
||||
seriesSpellId: seriesSpellId
|
||||
};
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
createdSpellId = await tauri.createSpell(entityId, spellImportData) as string;
|
||||
} else {
|
||||
// Mode online → Serveur
|
||||
createdSpellId = await System.authPostToServer<string>('spell/add', spellData, userToken, lang);
|
||||
// Si le livre a une copie locale → addToQueue
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('create_spell', {data: {...spellData, id: createdSpellId}});
|
||||
}
|
||||
createdSpellId = await apiPost<string>('spell/add', spellImportData, userToken, lang);
|
||||
}
|
||||
|
||||
|
||||
if (createdSpellId) {
|
||||
const newSpellListItem: SpellListItem = {
|
||||
id: createdSpellId,
|
||||
@@ -731,31 +580,21 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
errorMessage(e.message);
|
||||
}
|
||||
}
|
||||
}, [entityId, userToken, lang, errorMessage, successMessage, t, isCurrentlyOffline, book, addToQueue, localSyncedBooks]);
|
||||
|
||||
}, [entityId, userToken, lang, errorMessage, successMessage, t]);
|
||||
|
||||
const createTag = useCallback(async function (name: string, color: string): Promise<SpellTagProps | null> {
|
||||
try {
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
const addData = {
|
||||
seriesId: entityId,
|
||||
name: name,
|
||||
color: color,
|
||||
};
|
||||
let tagId: string;
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
tagId = await tauri.addSeriesSpellTag(addData);
|
||||
} else {
|
||||
tagId = await System.authPostToServer<string>(
|
||||
'series/spell/tag/add',
|
||||
addData,
|
||||
userToken,
|
||||
lang
|
||||
);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === entityId)) {
|
||||
addToQueue('add_series_spell_tag', {data: {...addData, id: tagId}});
|
||||
}
|
||||
}
|
||||
const tagId: string = await apiPost<string>(
|
||||
'series/spell/tag/add',
|
||||
{
|
||||
seriesId: entityId,
|
||||
name: name,
|
||||
color: color,
|
||||
},
|
||||
userToken,
|
||||
lang
|
||||
);
|
||||
if (tagId) {
|
||||
const newTag: SpellTagProps = {id: tagId, name: name, color: color};
|
||||
setTags(function (prev: SpellTagProps[]): SpellTagProps[] {
|
||||
@@ -764,21 +603,22 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
return newTag;
|
||||
}
|
||||
return null;
|
||||
} else if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
const result = await tauri.createSpellTag(entityId, name, color);
|
||||
const newTag: SpellTagProps = result as SpellTagProps;
|
||||
if (newTag && newTag.id) {
|
||||
setTags(function (prev: SpellTagProps[]): SpellTagProps[] {
|
||||
return [...prev, newTag];
|
||||
});
|
||||
return newTag;
|
||||
}
|
||||
return null;
|
||||
} else {
|
||||
const requestData = {
|
||||
const newTag: SpellTagProps = await apiPost<SpellTagProps>('spell/tag/add', {
|
||||
bookId: entityId,
|
||||
name: name,
|
||||
color: color,
|
||||
};
|
||||
let newTag: SpellTagProps;
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
newTag = await tauri.createSpellTag(requestData.bookId, requestData.name, requestData.color) as SpellTagProps;
|
||||
} else {
|
||||
newTag = await System.authPostToServer<SpellTagProps>('spell/tag/add', requestData, userToken, lang);
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('create_spell_tag', {data: {...requestData, id: newTag?.id}});
|
||||
}
|
||||
}
|
||||
}, userToken, lang);
|
||||
if (newTag && newTag.id) {
|
||||
setTags(function (prev: SpellTagProps[]): SpellTagProps[] {
|
||||
return [...prev, newTag];
|
||||
@@ -793,31 +633,21 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}, [isSeriesMode, entityId, userToken, lang, errorMessage, isCurrentlyOffline, book?.localBook, localSyncedBooks, addToQueue]);
|
||||
|
||||
}, [isSeriesMode, entityId, userToken, lang, errorMessage]);
|
||||
|
||||
const updateTag = useCallback(async function (tagId: string, name: string, color: string): Promise<boolean> {
|
||||
try {
|
||||
let success: boolean;
|
||||
const requestData = {tagId, name, color};
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
success = await tauri.updateSeriesSpellTag(requestData);
|
||||
} else {
|
||||
success = await System.authPutToServer<boolean>('series/spell/tag/update', requestData, userToken, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === entityId)) {
|
||||
addToQueue('update_series_spell_tag', {data: requestData});
|
||||
}
|
||||
}
|
||||
success = await apiPut<boolean>('series/spell/tag/update', {
|
||||
tagId, name, color
|
||||
}, userToken, lang);
|
||||
} else if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
success = await tauri.updateSpellTag(tagId, name, color);
|
||||
} else {
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
success = await tauri.updateSpellTag(requestData.tagId, requestData.name, requestData.color);
|
||||
} else {
|
||||
success = await System.authPutToServer<boolean>('spell/tag/update', requestData, userToken, lang);
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('update_spell_tag', {data: requestData});
|
||||
}
|
||||
}
|
||||
success = await apiPut<boolean>('spell/tag/update', {
|
||||
tagId, name, color
|
||||
}, userToken, lang);
|
||||
}
|
||||
if (!success) {
|
||||
errorMessage(t("spellComponent.updateSuccess"));
|
||||
@@ -835,33 +665,19 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, [isSeriesMode, userToken, lang, errorMessage, t, isCurrentlyOffline, book?.localBook, entityId, localSyncedBooks, addToQueue]);
|
||||
|
||||
}, [isSeriesMode, userToken, lang, errorMessage, t]);
|
||||
|
||||
const deleteTag = useCallback(async function (tagId: string): Promise<boolean> {
|
||||
try {
|
||||
let success: boolean;
|
||||
const deletedAt: number = System.timeStampInSeconds();
|
||||
if (isSeriesMode) {
|
||||
// Series mode - dual logic
|
||||
const deleteData = {tagId, deletedAt};
|
||||
if (isCurrentlyOffline() || localSeries) {
|
||||
success = await tauri.deleteSeriesSpellTag(deleteData.tagId, deleteData.deletedAt);
|
||||
} else {
|
||||
success = await System.authDeleteToServer<boolean>('series/spell/tag/delete', deleteData, userToken, lang);
|
||||
if (localSyncedSeries.find((s: SyncedSeries): boolean => s.id === entityId)) {
|
||||
addToQueue('delete_series_spell_tag', {data: deleteData});
|
||||
}
|
||||
}
|
||||
success = await apiDelete<boolean>('series/spell/tag/delete', {tagId}, userToken, lang);
|
||||
} else if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
success = await tauri.deleteSpellTag(tagId, book?.bookId ?? '', Date.now());
|
||||
} else {
|
||||
const requestData = {tagId, bookId: entityId, deletedAt};
|
||||
if (isCurrentlyOffline() || book?.localBook) {
|
||||
success = await tauri.deleteSpellTag(requestData.tagId, requestData.bookId, requestData.deletedAt);
|
||||
} else {
|
||||
success = await System.authDeleteToServer<boolean>('spell/tag/delete', requestData, userToken, lang);
|
||||
if (localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === entityId)) {
|
||||
addToQueue('delete_spell_tag', {data: requestData});
|
||||
}
|
||||
}
|
||||
success = await apiDelete<boolean>('spell/tag/delete', {
|
||||
tagId, bookId: entityId
|
||||
}, userToken, lang);
|
||||
}
|
||||
if (success) {
|
||||
setTags(function (prev: SpellTagProps[]): SpellTagProps[] {
|
||||
@@ -878,40 +694,35 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}, [isSeriesMode, entityId, userToken, lang, errorMessage, isCurrentlyOffline, book?.localBook, localSyncedBooks, addToQueue]);
|
||||
|
||||
}, [isSeriesMode, entityId, userToken, lang, errorMessage]);
|
||||
|
||||
const handleSyncComplete = useCallback(async function (): Promise<void> {
|
||||
if (selectedSpell?.seriesSpellId) {
|
||||
let seriesSpellResponse: SeriesSpellDetailResponse;
|
||||
if (isCurrentlyOffline() || (isSeriesMode ? localSeries : book?.localBook)) {
|
||||
seriesSpellResponse = await tauri.getSeriesSpellDetail(selectedSpell.seriesSpellId) as SeriesSpellDetailResponse;
|
||||
} else {
|
||||
seriesSpellResponse = await System.authGetQueryToServer<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: selectedSpell.seriesSpellId}
|
||||
);
|
||||
}
|
||||
const seriesSpellResponse: SeriesSpellDetailResponse = await apiGet<SeriesSpellDetailResponse>(
|
||||
'series/spell/detail',
|
||||
userToken,
|
||||
lang,
|
||||
{spellid: selectedSpell.seriesSpellId}
|
||||
);
|
||||
if (seriesSpellResponse) {
|
||||
setSelectedSeriesSpell(seriesSpellResponse);
|
||||
}
|
||||
}
|
||||
}, [selectedSpell?.seriesSpellId, userToken, lang, isCurrentlyOffline, isSeriesMode, localSeries, book?.localBook]);
|
||||
}, [selectedSpell?.seriesSpellId, userToken, lang]);
|
||||
|
||||
const enterDetailMode = useCallback(async function (spell: SpellListItem): Promise<void> {
|
||||
await selectSpell(spell);
|
||||
setViewMode('detail');
|
||||
setSpellBackup(null);
|
||||
}, [selectSpell]);
|
||||
|
||||
|
||||
const enterEditMode = useCallback(function (): void {
|
||||
if (selectedSpell) {
|
||||
setSpellBackup({...selectedSpell});
|
||||
}
|
||||
setViewMode('edit');
|
||||
}, [selectedSpell]);
|
||||
|
||||
|
||||
const exitEditMode = useCallback(async function (save: boolean): Promise<void> {
|
||||
if (save) {
|
||||
const success: boolean = await saveSpell();
|
||||
@@ -932,14 +743,14 @@ export function useSpells(config: UseSpellsConfig): UseSpellsReturn {
|
||||
}
|
||||
setSpellBackup(null);
|
||||
}, [saveSpell, spellBackup]);
|
||||
|
||||
|
||||
const backToList = useCallback(function (): void {
|
||||
setSelectedSpell(null);
|
||||
setSelectedSeriesSpell(null);
|
||||
setSpellBackup(null);
|
||||
setViewMode('list');
|
||||
}, []);
|
||||
|
||||
|
||||
return {
|
||||
spells,
|
||||
seriesSpells,
|
||||
|
||||
Reference in New Issue
Block a user