d2852df976
Decided during manual testing: the legacy drop-inside zone had zero height, so drops only worked before/after a node and moves happened by accident. Sections are re-parented via Edit, as before. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
58 lines
2.4 KiB
TypeScript
58 lines
2.4 KiB
TypeScript
import * as React from 'react';
|
|
import { confirmModal } from '@tnsu/ui-kit';
|
|
import { IObjectsRepository } from './objectsRepository';
|
|
import { getChangedParentId, getDeleteConfirmMessage, getErrorText } from './objectsRules';
|
|
import { IObjectModalTarget, ObjectNode } from './objectsTypes';
|
|
import { OBJECTS_TEXTS } from './texts';
|
|
|
|
interface IActionsDeps {
|
|
repository: IObjectsRepository;
|
|
projectTitle: string | undefined;
|
|
reload: () => void;
|
|
}
|
|
|
|
export interface IObjectActions {
|
|
/** Ошибка удаления (раньше — alert). */
|
|
actionError: string | undefined;
|
|
/** Создание / правка из модалки. Ошибку бросает дальше — модалка покажет её у себя. */
|
|
saveObject: (target: IObjectModalTarget, title: string, parentValue: string) => Promise<void>;
|
|
deleteObject: (node: ObjectNode) => Promise<void>;
|
|
}
|
|
|
|
/** Пишущие действия над деревом объектов. После каждого успешного действия дерево перечитывается. */
|
|
export function useObjectActions({ repository, projectTitle, reload }: IActionsDeps): IObjectActions {
|
|
const [actionError, setActionError] = React.useState<string | undefined>(undefined);
|
|
|
|
const saveObject = async (target: IObjectModalTarget, title: string, parentValue: string): Promise<void> => {
|
|
const { mode, node } = target;
|
|
// Как в старом коде: объект привязывается к проекту по названию (текстовое поле project).
|
|
const project = projectTitle ?? '';
|
|
if (mode === 'addRoot') {
|
|
await repository.createObject(project, title, undefined);
|
|
} else if (mode === 'add' && node) {
|
|
await repository.createObject(project, title, node.id);
|
|
} else if (node) {
|
|
await repository.updateObject(node.id, title, getChangedParentId(node, parentValue));
|
|
}
|
|
reload();
|
|
};
|
|
|
|
const deleteObject = async (node: ObjectNode): Promise<void> => {
|
|
const isConfirmed = await confirmModal({
|
|
message: getDeleteConfirmMessage(node),
|
|
confirmText: OBJECTS_TEXTS.menu.delete,
|
|
danger: true
|
|
});
|
|
if (!isConfirmed) return;
|
|
setActionError(undefined);
|
|
try {
|
|
await repository.deleteObject(node.id);
|
|
reload();
|
|
} catch (error) {
|
|
setActionError(OBJECTS_TEXTS.deleteError(getErrorText(error)));
|
|
}
|
|
};
|
|
|
|
return { actionError, saveObject, deleteObject };
|
|
}
|