Add enable/disable management for book tools (characters, worlds, and locations)

- Introduced toggling functionality for managing `characters`, `worlds`, and `locations` tool availability per book.
- Updated `CharacterComponent`, `WorldSetting`, and `LocationComponent` with toggle switches for tool enablement.
- Added `book_tools` database table and related schema migration for storing tool settings.
- Extended API calls, models, and IPC handlers to support tool enablement states.
- Localized new strings for English with supporting descriptions and messages.
- Adjusted conditional rendering logic across components to respect tool enablement.
This commit is contained in:
natreex
2026-01-14 17:42:59 -05:00
parent 7215ac5c4f
commit e45a15225b
19 changed files with 782 additions and 341 deletions

View File

@@ -15,6 +15,7 @@ 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 ToggleSwitch from "@/components/form/ToggleSwitch";
interface SubElement {
id: string;
@@ -35,7 +36,12 @@ interface LocationProps {
elements: Element[];
}
export function LocationComponent(props: any, ref: any) {
interface LocationListResponse {
locations: LocationProps[];
enabled: boolean;
}
export function LocationComponent({showToggle = true}: {showToggle?: boolean}, ref: any) {
const t = useTranslations();
const {lang} = useContext<LangContextProps>(LangContext);
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
@@ -43,15 +49,16 @@ export function LocationComponent(props: any, ref: any) {
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
const {session} = useContext(SessionContext);
const {successMessage, errorMessage} = useContext(AlertContext);
const {book} = useContext(BookContext);
const {book, setBook} = useContext(BookContext);
const bookId: string | undefined = book?.bookId;
const token: string = session.accessToken;
const [sections, setSections] = useState<LocationProps[]>([]);
const [newSectionName, setNewSectionName] = useState<string>('');
const [newElementNames, setNewElementNames] = useState<{ [key: string]: string }>({});
const [newSubElementNames, setNewSubElementNames] = useState<{ [key: string]: string }>({});
const [toolEnabled, setToolEnabled] = useState<boolean>(book?.tools?.locations ?? false);
useImperativeHandle(ref, function () {
return {
@@ -62,23 +69,61 @@ export function LocationComponent(props: any, ref: any) {
useEffect((): void => {
getAllLocations().then();
}, []);
async function handleToggleTool(enabled: boolean): Promise<void> {
try {
let response: boolean;
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:tool:update', {
bookId: bookId,
toolName: 'locations',
enabled: enabled
});
} else {
response = await System.authPatchToServer<boolean>('book/tool-setting', {
bookId: bookId,
toolName: 'locations',
enabled: enabled
}, token, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
addToQueue('db:book:tool:update', {
bookId: bookId,
toolName: 'locations',
enabled: enabled
});
}
}
if (response && setBook && book) {
setToolEnabled(enabled);
setBook({...book, tools: {...book.tools, locations: enabled}});
}
} catch (e: unknown) {
if (e instanceof Error) {
errorMessage(e.message);
}
}
}
async function getAllLocations(): Promise<void> {
try {
let response: LocationProps[];
let response: LocationListResponse;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<LocationProps[]>('db:location:all', {bookid: bookId});
response = await window.electron.invoke<LocationListResponse>('db:location:all', {bookid: bookId});
} else {
if (book?.localBook) {
response = await window.electron.invoke<LocationProps[]>('db:location:all', {bookid: bookId});
response = await window.electron.invoke<LocationListResponse>('db:location:all', {bookid: bookId});
} else {
response = await System.authGetQueryToServer<LocationProps[]>(`location/all`, token, lang, {
response = await System.authGetQueryToServer<LocationListResponse>(`location/all`, token, lang, {
bookid: bookId,
});
}
}
if (response && response.length > 0) {
setSections(response);
if (response) {
setSections(response.locations);
setToolEnabled(response.enabled);
if (setBook && book) {
setBook({...book, tools: {...book.tools, locations: response.enabled}});
}
}
} catch (e: unknown) {
if (e instanceof Error) {
@@ -423,140 +468,154 @@ export function LocationComponent(props: any, ref: any) {
return (
<div className="space-y-6">
<div className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-4 border border-secondary/50">
<div className="grid grid-cols-1 gap-4 mb-4">
<InputField
input={
<TextInput
value={newSectionName}
setValue={(e: ChangeEvent<HTMLInputElement>) => setNewSectionName(e.target.value)}
placeholder={t("locationComponent.newSectionPlaceholder")}
/>
}
actionIcon={faPlus}
actionLabel={t("locationComponent.addSectionLabel")}
addButtonCallBack={handleAddSection}
{showToggle && (
<div className="bg-secondary/20 rounded-xl p-4 shadow-inner border border-secondary/30">
<ToggleSwitch
enabled={toolEnabled}
setEnabled={handleToggleTool}
label={t('locationComponent.enableTool')}
description={t('locationComponent.enableToolDescription')}
/>
</div>
</div>
{sections.length > 0 ? (
sections.map((section: LocationProps) => (
<div key={section.id}
className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-4 border border-secondary/50">
<h3 className="text-lg font-semibold text-text-primary mb-4 flex items-center">
<FontAwesomeIcon icon={faMapMarkerAlt} className="mr-2 w-5 h-5"/>
{section.name}
<span
className="ml-2 text-sm bg-dark-background text-text-secondary py-0.5 px-2 rounded-full">
{section.elements.length || 0}
</span>
<button onClick={(): Promise<void> => handleRemoveSection(section.id)}
className="ml-auto bg-dark-background text-text-primary rounded-full p-1.5 hover:bg-secondary transition-colors shadow-md">
<FontAwesomeIcon icon={faTrash} className={'w-5 h-5'}/>
</button>
</h3>
<div className="space-y-4">
{section.elements.length > 0 ? (
section.elements.map((element, elementIndex) => (
<div key={element.id}
className="bg-dark-background rounded-lg p-3 border-l-4 border-primary">
<div className="mb-2">
<InputField
input={
<TextInput
value={element.name}
setValue={(e: ChangeEvent<HTMLInputElement>) =>
handleElementChange(section.id, elementIndex, 'name', e.target.value)
}
placeholder={t("locationComponent.elementNamePlaceholder")}
/>
}
removeButtonCallBack={(): Promise<void> => handleRemoveElement(section.id, elementIndex)}
/>
</div>
<TexteAreaInput
value={element.description}
setValue={(e: React.ChangeEvent<HTMLTextAreaElement>): void => handleElementChange(section.id, elementIndex, 'description', e.target.value)}
placeholder={t("locationComponent.elementDescriptionPlaceholder")}
/>
<div className="mt-4 pt-4 border-t border-secondary/50">
{element.subElements.length > 0 && (
<h4 className="text-sm italic text-text-secondary mb-3">{t("locationComponent.subElementsHeading")}</h4>
)}
{element.subElements.map((subElement: SubElement, subElementIndex: number) => (
<div key={subElement.id}
className="bg-darkest-background rounded-lg p-3 mb-3">
<div className="mb-2">
<InputField
input={
<TextInput
value={subElement.name}
setValue={(e: ChangeEvent<HTMLInputElement>): void =>
handleSubElementChange(section.id, elementIndex, subElementIndex, 'name', e.target.value)
}
placeholder={t("locationComponent.subElementNamePlaceholder")}
/>
}
removeButtonCallBack={(): Promise<void> => handleRemoveSubElement(section.id, elementIndex, subElementIndex)}
/>
</div>
<TexteAreaInput
value={subElement.description}
setValue={(e) =>
handleSubElementChange(section.id, elementIndex, subElementIndex, 'description', e.target.value)
}
placeholder={t("locationComponent.subElementDescriptionPlaceholder")}
/>
</div>
))}
<InputField
input={
<TextInput
value={newSubElementNames[elementIndex] || ''}
setValue={(e: ChangeEvent<HTMLInputElement>) =>
setNewSubElementNames({
...newSubElementNames,
[elementIndex]: e.target.value
})
}
placeholder={t("locationComponent.newSubElementPlaceholder")}
/>
}
addButtonCallBack={(): Promise<void> => handleAddSubElement(section.id, elementIndex)}
/>
</div>
</div>
))
) : (
<div className="text-center py-4 text-text-secondary italic">
{t("locationComponent.noElementAvailable")}
</div>
)}
)}
{toolEnabled && (
<>
<div className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-4 border border-secondary/50">
<div className="grid grid-cols-1 gap-4 mb-4">
<InputField
input={
<TextInput
value={newElementNames[section.id] || ''}
setValue={(e: ChangeEvent<HTMLInputElement>) =>
setNewElementNames({...newElementNames, [section.id]: e.target.value})
}
placeholder={t("locationComponent.newElementPlaceholder")}
value={newSectionName}
setValue={(e: ChangeEvent<HTMLInputElement>) => setNewSectionName(e.target.value)}
placeholder={t("locationComponent.newSectionPlaceholder")}
/>
}
addButtonCallBack={(): Promise<void> => handleAddElement(section.id)}
actionIcon={faPlus}
actionLabel={t("locationComponent.addSectionLabel")}
addButtonCallBack={handleAddSection}
/>
</div>
</div>
))
) : (
<div
className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-8 border border-secondary/50 text-center">
<p className="text-text-secondary mb-4">{t("locationComponent.noSectionAvailable")}</p>
</div>
{sections.length > 0 ? (
sections.map((section: LocationProps) => (
<div key={section.id}
className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-4 border border-secondary/50">
<h3 className="text-lg font-semibold text-text-primary mb-4 flex items-center">
<FontAwesomeIcon icon={faMapMarkerAlt} className="mr-2 w-5 h-5"/>
{section.name}
<span
className="ml-2 text-sm bg-dark-background text-text-secondary py-0.5 px-2 rounded-full">
{section.elements.length || 0}
</span>
<button onClick={(): Promise<void> => handleRemoveSection(section.id)}
className="ml-auto bg-dark-background text-text-primary rounded-full p-1.5 hover:bg-secondary transition-colors shadow-md">
<FontAwesomeIcon icon={faTrash} className={'w-5 h-5'}/>
</button>
</h3>
<div className="space-y-4">
{section.elements.length > 0 ? (
section.elements.map((element, elementIndex) => (
<div key={element.id}
className="bg-dark-background rounded-lg p-3 border-l-4 border-primary">
<div className="mb-2">
<InputField
input={
<TextInput
value={element.name}
setValue={(e: ChangeEvent<HTMLInputElement>) =>
handleElementChange(section.id, elementIndex, 'name', e.target.value)
}
placeholder={t("locationComponent.elementNamePlaceholder")}
/>
}
removeButtonCallBack={(): Promise<void> => handleRemoveElement(section.id, elementIndex)}
/>
</div>
<TexteAreaInput
value={element.description}
setValue={(e: React.ChangeEvent<HTMLTextAreaElement>): void => handleElementChange(section.id, elementIndex, 'description', e.target.value)}
placeholder={t("locationComponent.elementDescriptionPlaceholder")}
/>
<div className="mt-4 pt-4 border-t border-secondary/50">
{element.subElements.length > 0 && (
<h4 className="text-sm italic text-text-secondary mb-3">{t("locationComponent.subElementsHeading")}</h4>
)}
{element.subElements.map((subElement: SubElement, subElementIndex: number) => (
<div key={subElement.id}
className="bg-darkest-background rounded-lg p-3 mb-3">
<div className="mb-2">
<InputField
input={
<TextInput
value={subElement.name}
setValue={(e: ChangeEvent<HTMLInputElement>): void =>
handleSubElementChange(section.id, elementIndex, subElementIndex, 'name', e.target.value)
}
placeholder={t("locationComponent.subElementNamePlaceholder")}
/>
}
removeButtonCallBack={(): Promise<void> => handleRemoveSubElement(section.id, elementIndex, subElementIndex)}
/>
</div>
<TexteAreaInput
value={subElement.description}
setValue={(e) =>
handleSubElementChange(section.id, elementIndex, subElementIndex, 'description', e.target.value)
}
placeholder={t("locationComponent.subElementDescriptionPlaceholder")}
/>
</div>
))}
<InputField
input={
<TextInput
value={newSubElementNames[elementIndex] || ''}
setValue={(e: ChangeEvent<HTMLInputElement>) =>
setNewSubElementNames({
...newSubElementNames,
[elementIndex]: e.target.value
})
}
placeholder={t("locationComponent.newSubElementPlaceholder")}
/>
}
addButtonCallBack={(): Promise<void> => handleAddSubElement(section.id, elementIndex)}
/>
</div>
</div>
))
) : (
<div className="text-center py-4 text-text-secondary italic">
{t("locationComponent.noElementAvailable")}
</div>
)}
<InputField
input={
<TextInput
value={newElementNames[section.id] || ''}
setValue={(e: ChangeEvent<HTMLInputElement>) =>
setNewElementNames({...newElementNames, [section.id]: e.target.value})
}
placeholder={t("locationComponent.newElementPlaceholder")}
/>
}
addButtonCallBack={(): Promise<void> => handleAddElement(section.id)}
/>
</div>
</div>
))
) : (
<div
className="bg-tertiary/90 backdrop-blur-sm rounded-xl shadow-lg p-8 border border-secondary/50 text-center">
<p className="text-text-secondary mb-4">{t("locationComponent.noSectionAvailable")}</p>
</div>
)}
</>
)}
</div>
);