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,17 +1,24 @@
|
||||
'use client'
|
||||
import React, {lazy, Suspense, useRef} from 'react';
|
||||
import {faPen, faSave} from '@fortawesome/free-solid-svg-icons';
|
||||
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
|
||||
import {faSpinner} from '@fortawesome/free-solid-svg-icons';
|
||||
import {useTranslations} from 'next-intl';
|
||||
import PanelHeader from '@/components/PanelHeader';
|
||||
import React, {lazy, Suspense, useContext, useRef, useState} from 'react';
|
||||
import {Save} from 'lucide-react';
|
||||
import {useTranslations} from '@/lib/i18n';
|
||||
import SectionHeader from "@/components/ui/SectionHeader";
|
||||
import IconButton from "@/components/ui/IconButton";
|
||||
import PulseLoader from '@/components/ui/PulseLoader';
|
||||
import ToggleSwitch from "@/components/form/ToggleSwitch";
|
||||
import {BookContext, BookContextProps} from "@/context/BookContext";
|
||||
import {SessionContext, SessionContextProps} from "@/context/SessionContext";
|
||||
import {AlertContext, AlertContextProps} from "@/context/AlertContext";
|
||||
import {LangContext, LangContextProps} from "@/context/LangContext";
|
||||
import {apiPatch} from "@/lib/api/client";
|
||||
import {SettingRef} from "@/lib/types/settings";
|
||||
import {BookProps} from "@/lib/types/book";
|
||||
|
||||
// Lazy loaded components - avec ref (anciens)
|
||||
const BasicInformationSetting = lazy(function () {
|
||||
return import('./BasicInformationSetting');
|
||||
});
|
||||
const GuideLineSetting = lazy(function () {
|
||||
return import('./guide-line/GuideLineSetting');
|
||||
return import('./guideline/GuideLineSetting');
|
||||
});
|
||||
const StorySetting = lazy(function () {
|
||||
return import('./story/StorySetting');
|
||||
@@ -19,8 +26,6 @@ const StorySetting = lazy(function () {
|
||||
const QuillSenseSetting = lazy(function () {
|
||||
return import('./quillsense/QuillSenseSetting');
|
||||
});
|
||||
|
||||
// Lazy loaded components - sans ref (nouveaux avec leur propre header)
|
||||
const WorldSettings = lazy(function () {
|
||||
return import('./world/settings/WorldSettings');
|
||||
});
|
||||
@@ -37,28 +42,41 @@ const ExportSetting = lazy(function () {
|
||||
return import('./ExportSetting');
|
||||
});
|
||||
|
||||
function LoadingSpinner(): React.JSX.Element {
|
||||
return (
|
||||
<div className="flex items-center justify-center py-12">
|
||||
<FontAwesomeIcon icon={faSpinner} className="w-8 h-8 text-primary animate-spin"/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface BookSettingOptionProps {
|
||||
setting: string;
|
||||
}
|
||||
|
||||
interface SettingRef {
|
||||
handleSave: () => Promise<void>;
|
||||
}
|
||||
|
||||
// Settings qui gèrent leur propre save (pas de bouton save parent)
|
||||
const selfManagedSettings: string[] = ['characters', 'spells', 'world', 'worlds', 'locations', 'export'];
|
||||
|
||||
type ToolName = 'worlds' | 'locations' | 'characters' | 'spells' | 'quillsense';
|
||||
|
||||
const toggleableSettings: Record<string, ToolName> = {
|
||||
'world': 'worlds',
|
||||
'worlds': 'worlds',
|
||||
'locations': 'locations',
|
||||
'characters': 'characters',
|
||||
'spells': 'spells',
|
||||
'quillsense': 'quillsense',
|
||||
};
|
||||
|
||||
function getInitialToolEnabled(setting: string, book: BookProps | null): boolean {
|
||||
const toolName: ToolName | undefined = toggleableSettings[setting];
|
||||
if (!toolName) return false;
|
||||
if (toolName === 'quillsense') return book?.quillsenseEnabled ?? false;
|
||||
return book?.tools?.[toolName] ?? false;
|
||||
}
|
||||
|
||||
export default function BookSettingOption({setting}: BookSettingOptionProps): React.JSX.Element {
|
||||
const t = useTranslations();
|
||||
const settingRef = useRef<SettingRef>(null);
|
||||
const settingRef: React.RefObject<SettingRef | null> = useRef<SettingRef>(null);
|
||||
const {book, setBook}: BookContextProps = useContext<BookContextProps>(BookContext);
|
||||
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
|
||||
const {errorMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
|
||||
const {lang}: LangContextProps = useContext<LangContextProps>(LangContext);
|
||||
const userToken: string = session?.accessToken ?? '';
|
||||
|
||||
const isToggleable: boolean = setting in toggleableSettings;
|
||||
const [toolEnabled, setToolEnabled] = useState<boolean>(getInitialToolEnabled(setting, book));
|
||||
|
||||
const showSaveButton: boolean = !selfManagedSettings.includes(setting);
|
||||
|
||||
@@ -86,47 +104,91 @@ export default function BookSettingOption({setting}: BookSettingOptionProps): Re
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
async function handleToggleTool(enabled: boolean): Promise<void> {
|
||||
const toolName: ToolName | undefined = toggleableSettings[setting];
|
||||
if (!toolName) return;
|
||||
try {
|
||||
const result: boolean = await apiPatch<boolean>('book/tool-setting', {
|
||||
bookId: book?.bookId,
|
||||
toolName: toolName,
|
||||
enabled: enabled
|
||||
}, userToken, lang);
|
||||
if (result && setBook && book) {
|
||||
setToolEnabled(enabled);
|
||||
if (toolName === 'quillsense') {
|
||||
setBook({...book, quillsenseEnabled: enabled});
|
||||
} else {
|
||||
setBook({
|
||||
...book,
|
||||
tools: {
|
||||
characters: toolName === 'characters' ? enabled : (book.tools?.characters ?? false),
|
||||
worlds: toolName === 'worlds' ? enabled : (book.tools?.worlds ?? false),
|
||||
locations: toolName === 'locations' ? enabled : (book.tools?.locations ?? false),
|
||||
spells: toolName === 'spells' ? enabled : (book.tools?.spells ?? false),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error) {
|
||||
errorMessage(e.message);
|
||||
} else {
|
||||
errorMessage(t('bookSettingOption.unknownError'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSaveClick(): Promise<void> {
|
||||
if (settingRef.current?.handleSave) {
|
||||
await settingRef.current.handleSave();
|
||||
}
|
||||
}
|
||||
|
||||
function renderHeaderActions(): React.JSX.Element {
|
||||
return (
|
||||
<div className="flex items-center gap-3">
|
||||
{isToggleable && (
|
||||
<ToggleSwitch
|
||||
checked={toolEnabled}
|
||||
onChange={handleToggleTool}
|
||||
size="sm"
|
||||
/>
|
||||
)}
|
||||
{showSaveButton && (
|
||||
<IconButton icon={Save} variant="primary" onClick={handleSaveClick}/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="px-6">
|
||||
<div className="sticky top-0 z-10 bg-tertiary pt-6 pb-4">
|
||||
<PanelHeader
|
||||
icon={faPen}
|
||||
badge="BI"
|
||||
<div className="sticky top-0 z-10 bg-darkest-background pt-6 pb-4">
|
||||
<SectionHeader
|
||||
title={renderTitle()}
|
||||
description=""
|
||||
secondActionCallback={showSaveButton ? handleSaveClick : undefined}
|
||||
callBackAction={showSaveButton ? handleSaveClick : undefined}
|
||||
secondActionIcon={showSaveButton ? faSave : undefined}
|
||||
actions={renderHeaderActions()}
|
||||
/>
|
||||
</div>
|
||||
<div className="bg-secondary/10 rounded-xl p-1 mb-6">
|
||||
<Suspense fallback={<LoadingSpinner/>}>
|
||||
<div className="mb-6">
|
||||
<Suspense fallback={<PulseLoader/>}>
|
||||
{setting === 'basic-information' && <BasicInformationSetting ref={settingRef}/>}
|
||||
{setting === 'guide-line' && <GuideLineSetting ref={settingRef}/>}
|
||||
{setting === 'story' && <StorySetting ref={settingRef}/>}
|
||||
{setting === 'quillsense' && <QuillSenseSetting ref={settingRef}/>}
|
||||
{setting === 'quillsense' && <QuillSenseSetting ref={settingRef} toolEnabled={toolEnabled}/>}
|
||||
{(setting === 'world' || setting === 'worlds') && (
|
||||
<WorldSettings entityType="book" showToggle={true}/>
|
||||
<WorldSettings entityType="book" toolEnabled={toolEnabled}/>
|
||||
)}
|
||||
{setting === 'locations' && (
|
||||
<LocationSettings entityType="book" showToggle={true}/>
|
||||
<LocationSettings entityType="book" toolEnabled={toolEnabled}/>
|
||||
)}
|
||||
{setting === 'characters' && (
|
||||
<CharacterSettings entityType="book" showToggle={true}/>
|
||||
<CharacterSettings entityType="book" toolEnabled={toolEnabled}/>
|
||||
)}
|
||||
{setting === 'spells' && (
|
||||
<SpellSettings entityType="book" showToggle={true}/>
|
||||
)}
|
||||
{setting === 'export' && (
|
||||
<ExportSetting/>
|
||||
<SpellSettings entityType="book" toolEnabled={toolEnabled}/>
|
||||
)}
|
||||
{setting === 'export' && <ExportSetting/>}
|
||||
{!['basic-information', 'guide-line', 'story', 'world', 'worlds', 'locations', 'characters', 'spells', 'quillsense', 'export'].includes(setting) && (
|
||||
<div className="text-text-secondary py-4 text-center">
|
||||
{t("bookSettingOption.notAvailable")}
|
||||
|
||||
Reference in New Issue
Block a user