Portal/rsconcept/frontend/src/features/oss/pages/OssPage/OssEditContext.tsx

298 lines
9.2 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import { createContext, useContext, useEffect, useState } from 'react';
2024-06-07 20:17:03 +03:00
import { urls, useConceptNavigation } from '@/app';
import { useAuthSuspense } from '@/features/auth/backend/useAuth';
import { useDeleteItem } from '@/features/library/backend/useDeleteItem';
import { ILibraryItemEditor, LibraryItemID } from '@/features/library/models/library';
import { useLibrarySearchStore } from '@/features/library/stores/librarySearch';
import { RSTabID } from '@/features/rsform/pages/RSFormPage/RSEditContext';
import { UserRole } from '@/features/users/models/user';
import { useDialogsStore } from '@/stores/dialogs';
import { usePreferencesStore } from '@/stores/preferences';
2025-01-15 23:03:23 +03:00
import { useRoleStore } from '@/stores/role';
2024-08-02 11:17:27 +03:00
import { PARAMETER } from '@/utils/constants';
import { prompts } from '@/utils/labels';
2024-06-07 20:17:03 +03:00
import { useInputUpdate } from '../../backend/useInputUpdate';
import { useOperationCreate } from '../../backend/useOperationCreate';
import { useOperationDelete } from '../../backend/useOperationDelete';
import { useOperationUpdate } from '../../backend/useOperationUpdate';
import { useOssSuspense } from '../../backend/useOSS';
import { useRelocateConstituents } from '../../backend/useRelocateConstituents';
import { useUpdatePositions } from '../../backend/useUpdatePositions';
import { IOperationPosition, IOperationSchema, OperationID, OperationType } from '../../models/oss';
import { calculateInsertPosition } from '../../models/ossAPI';
2025-01-26 22:24:34 +03:00
export enum OssTabID {
CARD = 0,
GRAPH = 1
}
2024-08-02 11:17:27 +03:00
export interface ICreateOperationPrompt {
2024-09-16 19:38:24 +03:00
defaultX: number;
defaultY: number;
2024-08-02 11:17:27 +03:00
inputs: OperationID[];
positions: IOperationPosition[];
callback: (newID: OperationID) => void;
}
export interface IOssEditContext extends ILibraryItemEditor {
2025-01-26 22:24:34 +03:00
schema: IOperationSchema;
2024-07-23 23:03:58 +03:00
selected: OperationID[];
2024-06-07 20:17:03 +03:00
2025-01-23 19:41:31 +03:00
isOwned: boolean;
2024-06-07 20:17:03 +03:00
isMutable: boolean;
isAttachedToOSS: boolean;
2024-06-07 20:17:03 +03:00
2024-07-26 00:33:22 +03:00
showTooltip: boolean;
setShowTooltip: (newValue: boolean) => void;
2024-07-26 00:33:22 +03:00
2025-01-26 22:24:34 +03:00
navigateTab: (tab: OssTabID) => void;
navigateOperationSchema: (target: OperationID) => void;
2024-06-07 20:17:03 +03:00
2025-01-26 22:24:34 +03:00
deleteSchema: () => void;
2024-07-23 23:03:58 +03:00
setSelected: React.Dispatch<React.SetStateAction<OperationID[]>>;
canDelete: (target: OperationID) => boolean;
2025-01-26 22:24:34 +03:00
promptCreateOperation: (props: ICreateOperationPrompt) => void;
promptDeleteOperation: (target: OperationID, positions: IOperationPosition[]) => void;
2024-07-28 21:29:46 +03:00
promptEditInput: (target: OperationID, positions: IOperationPosition[]) => void;
2024-07-29 16:55:48 +03:00
promptEditOperation: (target: OperationID, positions: IOperationPosition[]) => void;
2024-10-28 23:55:12 +03:00
promptRelocateConstituents: (target: OperationID | undefined, positions: IOperationPosition[]) => void;
2024-06-07 20:17:03 +03:00
}
const OssEditContext = createContext<IOssEditContext | null>(null);
export const useOssEdit = () => {
const context = useContext(OssEditContext);
if (context === null) {
2024-12-12 21:58:07 +03:00
throw new Error('useOssEdit has to be used within <OssEditState>');
2024-06-07 20:17:03 +03:00
}
return context;
};
interface OssEditStateProps {
2025-01-23 19:41:31 +03:00
itemID: LibraryItemID;
2024-06-07 20:17:03 +03:00
}
2025-01-26 22:24:34 +03:00
export const OssEditState = ({ itemID, children }: React.PropsWithChildren<OssEditStateProps>) => {
2024-07-24 18:11:28 +03:00
const router = useConceptNavigation();
const adminMode = usePreferencesStore(state => state.adminMode);
2025-01-15 23:03:23 +03:00
const role = useRoleStore(state => state.role);
const adjustRole = useRoleStore(state => state.adjustRole);
const setSearchLocation = useLibrarySearchStore(state => state.setLocation);
const searchLocation = useLibrarySearchStore(state => state.location);
const { user } = useAuthSuspense();
2025-01-26 22:24:34 +03:00
const { schema } = useOssSuspense({ itemID: itemID });
2025-01-23 19:41:31 +03:00
const isOwned = !!user.id && user.id === schema.owner;
2025-01-26 22:24:34 +03:00
const isMutable = role > UserRole.READER && !schema.read_only;
2024-06-07 20:17:03 +03:00
2024-07-26 00:33:22 +03:00
const [showTooltip, setShowTooltip] = useState(true);
2025-01-26 22:24:34 +03:00
const [selected, setSelected] = useState<OperationID[]>([]);
2024-08-02 11:17:27 +03:00
const showEditInput = useDialogsStore(state => state.showChangeInputSchema);
const showEditOperation = useDialogsStore(state => state.showEditOperation);
const showDeleteOperation = useDialogsStore(state => state.showDeleteOperation);
const showRelocateConstituents = useDialogsStore(state => state.showRelocateConstituents);
const showCreateOperation = useDialogsStore(state => state.showCreateOperation);
2025-01-26 22:24:34 +03:00
const { deleteItem } = useDeleteItem();
const { updatePositions } = useUpdatePositions();
const { operationCreate } = useOperationCreate();
const { operationDelete } = useOperationDelete();
const { operationUpdate } = useOperationUpdate();
const { relocateConstituents } = useRelocateConstituents();
const { inputUpdate } = useInputUpdate();
2024-07-20 18:26:32 +03:00
useEffect(
2024-06-07 20:17:03 +03:00
() =>
2025-01-15 23:03:23 +03:00
adjustRole({
2025-01-26 22:24:34 +03:00
isOwner: isOwned,
isEditor: !!user.id && schema.editors.includes(user.id),
isStaff: user.is_staff,
2025-01-15 23:03:23 +03:00
adminMode: adminMode
2024-06-07 20:17:03 +03:00
}),
2025-01-26 22:24:34 +03:00
[schema, adjustRole, isOwned, user, adminMode]
2024-06-07 20:17:03 +03:00
);
2025-01-26 22:24:34 +03:00
function navigateTab(tab: OssTabID) {
const url = urls.oss_props({
id: schema.id,
tab: tab
});
router.push(url);
}
2024-06-07 20:17:03 +03:00
function navigateOperationSchema(target: OperationID) {
const node = schema.operationByID.get(target);
if (!node?.result) {
return;
}
router.push(urls.schema_props({ id: node.result, tab: RSTabID.CST_LIST }));
}
2024-07-24 18:11:28 +03:00
2025-01-26 22:24:34 +03:00
function deleteSchema() {
if (!window.confirm(prompts.deleteOSS)) {
2025-01-26 22:24:34 +03:00
return;
}
deleteItem(schema.id, () => {
if (searchLocation === schema.location) {
setSearchLocation('');
}
router.push(urls.library);
});
2025-01-26 22:24:34 +03:00
}
function promptCreateOperation({ defaultX, defaultY, inputs, positions, callback }: ICreateOperationPrompt) {
showCreateOperation({
oss: schema,
onCreate: data => {
const target = calculateInsertPosition(schema, data.item_data.operation_type, data.arguments ?? [], positions, {
x: defaultX,
y: defaultY
});
data.positions = positions;
data.item_data.position_x = target.x;
data.item_data.position_y = target.y;
operationCreate({ itemID: schema.id, data }, operation => {
if (callback) {
setTimeout(() => callback(operation.id), PARAMETER.refreshTimeout);
2024-07-23 23:03:58 +03:00
}
});
2025-01-26 22:24:34 +03:00
},
initialInputs: inputs
});
}
2025-01-26 22:24:34 +03:00
function canDelete(target: OperationID) {
const operation = schema.operationByID.get(target);
if (!operation) {
return false;
}
if (operation.operation_type === OperationType.INPUT) {
return true;
}
return schema.graph.expandOutputs([target]).length === 0;
}
2025-01-26 22:24:34 +03:00
function promptEditOperation(target: OperationID, positions: IOperationPosition[]) {
const operation = schema.operationByID.get(target);
if (!operation) {
return;
}
showEditOperation({
oss: schema,
target: operation,
onSubmit: data => {
data.positions = positions;
operationUpdate({ itemID: schema.id, data });
}
2025-01-26 22:24:34 +03:00
});
}
2025-01-26 22:24:34 +03:00
function promptDeleteOperation(target: OperationID, positions: IOperationPosition[]) {
const operation = schema.operationByID.get(target);
if (!operation) {
return;
}
showDeleteOperation({
target: operation,
onSubmit: (targetID, keepConstituents, deleteSchema) => {
operationDelete({
itemID: schema.id,
data: {
target: targetID,
positions: positions,
keep_constituents: keepConstituents,
delete_schema: deleteSchema
}
});
2024-10-28 23:55:12 +03:00
}
2025-01-26 22:24:34 +03:00
});
}
2025-01-26 22:24:34 +03:00
function promptEditInput(target: OperationID, positions: IOperationPosition[]) {
const operation = schema.operationByID.get(target);
if (!operation) {
return;
}
showEditInput({
oss: schema,
target: operation,
onSubmit: (target, newInput) => {
inputUpdate({
itemID: schema.id,
data: {
target: target,
positions: positions,
input: newInput ?? null
}
});
}
2025-01-26 22:24:34 +03:00
});
}
2025-01-26 22:24:34 +03:00
function promptRelocateConstituents(target: OperationID | undefined, positions: IOperationPosition[]) {
const operation = target ? schema.operationByID.get(target) : undefined;
showRelocateConstituents({
oss: schema,
initialTarget: operation,
onSubmit: data => {
if (
positions.every(item => {
const operation = schema.operationByID.get(item.id)!;
return operation.position_x === item.position_x && operation.position_y === item.position_y;
})
) {
relocateConstituents({ itemID: schema.id, data });
2025-01-26 22:24:34 +03:00
} else {
updatePositions(
{
isSilent: true,
2025-01-26 22:24:34 +03:00
itemID: schema.id, //
positions: positions
},
() => relocateConstituents({ itemID: schema.id, data })
2025-01-26 22:24:34 +03:00
);
}
}
2025-01-26 22:24:34 +03:00
});
}
2024-06-07 20:17:03 +03:00
return (
2024-12-12 21:58:07 +03:00
<OssEditContext
2024-06-07 20:17:03 +03:00
value={{
2025-01-26 22:24:34 +03:00
schema,
2024-07-23 23:03:58 +03:00
selected,
2025-01-26 22:24:34 +03:00
navigateTab,
deleteSchema,
2024-07-26 00:33:22 +03:00
showTooltip,
setShowTooltip,
2025-01-23 19:41:31 +03:00
isOwned,
2024-06-07 20:17:03 +03:00
isMutable,
isAttachedToOSS: false,
2024-06-07 20:17:03 +03:00
2024-07-23 23:03:58 +03:00
setSelected,
2024-07-20 18:26:32 +03:00
2025-01-26 22:24:34 +03:00
navigateOperationSchema,
2024-07-23 23:03:58 +03:00
promptCreateOperation,
canDelete,
promptDeleteOperation,
2024-07-29 16:55:48 +03:00
promptEditInput,
2024-07-29 22:30:24 +03:00
promptEditOperation,
2024-10-23 15:18:46 +03:00
promptRelocateConstituents
2024-06-07 20:17:03 +03:00
}}
>
{children}
2024-12-12 21:58:07 +03:00
</OssEditContext>
2024-06-07 20:17:03 +03:00
);
};