fix(tabsContext.tsx): remove unused imports and variables to improve code cleanliness
feat(tabsContext.tsx): add saveComponent and deleteComponent functions to TabsContext to handle saving and deleting custom components fix(typesContext.tsx): remove saveComponent and deleteComponent functions from typesContext as they are now handled in TabsContext fix(sideBarDraggableComponent/index.tsx): remove unused imports and variables to improve code cleanliness feat(sideBarDraggableComponent/index.tsx): add deleteComponent function from TabsContext to handle deleting custom components fix(nodeToolbarComponent/index.tsx): remove unused imports and variables to improve code cleanliness feat(nodeToolbarComponent/index.tsx): add saveComponent function from TabsContext to handle saving custom components fix(types/index.ts): import NodeDataType from flow to fix type error in TabsContextType
This commit is contained in:
parent
40ee81144f
commit
e91e24ee99
6 changed files with 79 additions and 75 deletions
|
|
@ -32,13 +32,20 @@ import { TabsContextType, TabsState } from "../types/tabs";
|
||||||
import {
|
import {
|
||||||
addVersionToDuplicates,
|
addVersionToDuplicates,
|
||||||
checkOldEdgesHandles,
|
checkOldEdgesHandles,
|
||||||
|
createFlowComponent,
|
||||||
scapeJSONParse,
|
scapeJSONParse,
|
||||||
scapedJSONStringfy,
|
scapedJSONStringfy,
|
||||||
updateEdgesHandleIds,
|
updateEdgesHandleIds,
|
||||||
updateIds,
|
updateIds,
|
||||||
updateTemplate,
|
updateTemplate,
|
||||||
} from "../utils/reactflowUtils";
|
} from "../utils/reactflowUtils";
|
||||||
import { getRandomDescription, getRandomName } from "../utils/utils";
|
import {
|
||||||
|
IncrementObjectKey,
|
||||||
|
getRandomDescription,
|
||||||
|
getRandomName,
|
||||||
|
getSetFromObject,
|
||||||
|
removeCountFromString,
|
||||||
|
} from "../utils/utils";
|
||||||
import { alertContext } from "./alertContext";
|
import { alertContext } from "./alertContext";
|
||||||
import { AuthContext } from "./authContext";
|
import { AuthContext } from "./authContext";
|
||||||
import { typesContext } from "./typesContext";
|
import { typesContext } from "./typesContext";
|
||||||
|
|
@ -73,6 +80,8 @@ const TabsContextInitialValue: TabsContextType = {
|
||||||
selection: { nodes: any; edges: any },
|
selection: { nodes: any; edges: any },
|
||||||
position: { x: number; y: number; paneX?: number; paneY?: number }
|
position: { x: number; y: number; paneX?: number; paneY?: number }
|
||||||
) => {},
|
) => {},
|
||||||
|
saveComponent: (component: NodeDataType, id: string) => {},
|
||||||
|
deleteComponent: (id: string, key: string) => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const TabsContext = createContext<TabsContextType>(
|
export const TabsContext = createContext<TabsContextType>(
|
||||||
|
|
@ -90,7 +99,8 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
|
|
||||||
const [flows, setFlows] = useState<Array<FlowType>>([]);
|
const [flows, setFlows] = useState<Array<FlowType>>([]);
|
||||||
const [id, setId] = useState(uid());
|
const [id, setId] = useState(uid());
|
||||||
const { templates, reactFlowInstance, setData } = useContext(typesContext);
|
const { templates, reactFlowInstance, setData, data } =
|
||||||
|
useContext(typesContext);
|
||||||
const [lastCopiedSelection, setLastCopiedSelection] = useState<{
|
const [lastCopiedSelection, setLastCopiedSelection] = useState<{
|
||||||
nodes: any;
|
nodes: any;
|
||||||
edges: any;
|
edges: any;
|
||||||
|
|
@ -647,6 +657,64 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function saveComponent(component: NodeDataType, id: string) {
|
||||||
|
let key = component.type;
|
||||||
|
if (data["custom_components"][key] !== undefined) {
|
||||||
|
let { newKey, increment } = IncrementObjectKey(
|
||||||
|
data["custom_components"],
|
||||||
|
key
|
||||||
|
);
|
||||||
|
key = newKey;
|
||||||
|
component.type = newKey;
|
||||||
|
let componentNodes: { [key: string]: APIClassType } = {};
|
||||||
|
Object.keys(data["custom_components"]).forEach((key) => {
|
||||||
|
componentNodes[key] = data["custom_components"][key];
|
||||||
|
});
|
||||||
|
const display_nameSet = getSetFromObject(componentNodes, "display_name");
|
||||||
|
if (display_nameSet.has(component.node?.display_name!)) {
|
||||||
|
increment = 1;
|
||||||
|
while (
|
||||||
|
display_nameSet.has(
|
||||||
|
removeCountFromString(component.node?.display_name!) +
|
||||||
|
` (${increment})`
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
increment++;
|
||||||
|
}
|
||||||
|
component.node!.display_name =
|
||||||
|
removeCountFromString(component.node?.display_name!) +
|
||||||
|
` (${increment})`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
component.node!.official = false;
|
||||||
|
saveFlowToDatabase(createFlowComponent(component));
|
||||||
|
setData((prev) => {
|
||||||
|
let newData = { ...prev };
|
||||||
|
//clone to prevent reference erro
|
||||||
|
newData["custom_components"][key] = _.cloneDeep({
|
||||||
|
...component.node,
|
||||||
|
official: false,
|
||||||
|
});
|
||||||
|
return newData;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteComponent(id: string, key: string) {
|
||||||
|
let flow = flows.find(
|
||||||
|
(flow) =>
|
||||||
|
flow.is_component &&
|
||||||
|
(flow.data?.nodes[0].data as NodeDataType).type === key
|
||||||
|
);
|
||||||
|
if (flow) {
|
||||||
|
removeFlow(flow.id);
|
||||||
|
setData((prev) => {
|
||||||
|
let newData = _.cloneDeep(prev);
|
||||||
|
delete newData["custom_components"][key];
|
||||||
|
return newData;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const [isBuilt, setIsBuilt] = useState(false);
|
const [isBuilt, setIsBuilt] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -676,6 +744,8 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
getTweak,
|
getTweak,
|
||||||
setTweak,
|
setTweak,
|
||||||
isLoading,
|
isLoading,
|
||||||
|
saveComponent,
|
||||||
|
deleteComponent,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,9 @@ import {
|
||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { Edge, Node, ReactFlowInstance } from "reactflow";
|
import { Edge, Node, ReactFlowInstance } from "reactflow";
|
||||||
import { getAll, getHealth, saveFlowToDatabase } from "../controllers/API";
|
import { getAll, getHealth } from "../controllers/API";
|
||||||
import { APIClassType, APIKindType } from "../types/api";
|
import { APIKindType } from "../types/api";
|
||||||
import { NodeDataType } from "../types/flow";
|
|
||||||
import { typesContextType } from "../types/typesContext";
|
import { typesContextType } from "../types/typesContext";
|
||||||
import { createFlowComponent } from "../utils/reactflowUtils";
|
|
||||||
import {
|
|
||||||
getSetFromObject,
|
|
||||||
IncrementObjectKey,
|
|
||||||
removeCountFromString,
|
|
||||||
} from "../utils/utils";
|
|
||||||
import { alertContext } from "./alertContext";
|
import { alertContext } from "./alertContext";
|
||||||
import { AuthContext } from "./authContext";
|
import { AuthContext } from "./authContext";
|
||||||
|
|
||||||
|
|
@ -36,8 +29,6 @@ const initialValue: typesContextType = {
|
||||||
fetchError: false,
|
fetchError: false,
|
||||||
setFilterEdge: (filter) => {},
|
setFilterEdge: (filter) => {},
|
||||||
getFilterEdge: [],
|
getFilterEdge: [],
|
||||||
saveComponent: (component: NodeDataType, key: string) => {},
|
|
||||||
deleteComponent: (id: string, key: string) => {},
|
|
||||||
deleteEdge: () => {},
|
deleteEdge: () => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -134,62 +125,10 @@ export function TypesProvider({ children }: { children: ReactNode }) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveComponent(component: NodeDataType, id: string) {
|
|
||||||
let key = component.type;
|
|
||||||
if (data["custom_components"][key] !== undefined) {
|
|
||||||
let { newKey, increment } = IncrementObjectKey(
|
|
||||||
data["custom_components"],
|
|
||||||
key
|
|
||||||
);
|
|
||||||
key = newKey;
|
|
||||||
component.type = newKey;
|
|
||||||
let componentNodes: { [key: string]: APIClassType } = {};
|
|
||||||
Object.keys(data["custom_components"]).forEach((key) => {
|
|
||||||
componentNodes[key] = data["custom_components"][key];
|
|
||||||
});
|
|
||||||
const display_nameSet = getSetFromObject(componentNodes, "display_name");
|
|
||||||
if (display_nameSet.has(component.node?.display_name!)) {
|
|
||||||
increment = 1;
|
|
||||||
while (
|
|
||||||
display_nameSet.has(
|
|
||||||
removeCountFromString(component.node?.display_name!) +
|
|
||||||
` (${increment})`
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
increment++;
|
|
||||||
}
|
|
||||||
component.node!.display_name =
|
|
||||||
removeCountFromString(component.node?.display_name!) +
|
|
||||||
` (${increment})`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
component.node!.official = false;
|
|
||||||
saveFlowToDatabase(createFlowComponent(component));
|
|
||||||
setData((prev) => {
|
|
||||||
let newData = { ...prev };
|
|
||||||
//clone to prevent reference erro
|
|
||||||
newData["custom_components"][key] = _.cloneDeep({
|
|
||||||
...component.node,
|
|
||||||
official: false,
|
|
||||||
});
|
|
||||||
return newData;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function deleteComponent(id: string, key: string) {
|
|
||||||
setData((prev) => {
|
|
||||||
let newData = _.cloneDeep(prev);
|
|
||||||
delete newData["custom_components"][key];
|
|
||||||
return newData;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<typesContext.Provider
|
<typesContext.Provider
|
||||||
value={{
|
value={{
|
||||||
deleteEdge,
|
deleteEdge,
|
||||||
deleteComponent,
|
|
||||||
saveComponent,
|
|
||||||
types,
|
types,
|
||||||
setTypes,
|
setTypes,
|
||||||
reactFlowInstance,
|
reactFlowInstance,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import {
|
||||||
} from "../../../../../components/ui/select-custom";
|
} from "../../../../../components/ui/select-custom";
|
||||||
import { AuthContext } from "../../../../../contexts/authContext";
|
import { AuthContext } from "../../../../../contexts/authContext";
|
||||||
import { TabsContext } from "../../../../../contexts/tabsContext";
|
import { TabsContext } from "../../../../../contexts/tabsContext";
|
||||||
import { typesContext } from "../../../../../contexts/typesContext";
|
|
||||||
import { APIClassType } from "../../../../../types/api";
|
import { APIClassType } from "../../../../../types/api";
|
||||||
import {
|
import {
|
||||||
createFlowComponent,
|
createFlowComponent,
|
||||||
|
|
@ -36,8 +35,7 @@ export default function SidebarDraggableComponent({
|
||||||
official: boolean;
|
official: boolean;
|
||||||
}) {
|
}) {
|
||||||
const open = useRef(false);
|
const open = useRef(false);
|
||||||
const { deleteComponent } = useContext(typesContext);
|
const { getNodeId, deleteComponent } = useContext(TabsContext);
|
||||||
const { getNodeId } = useContext(TabsContext);
|
|
||||||
const { autoLogin, userData } = useContext(AuthContext);
|
const { autoLogin, userData } = useContext(AuthContext);
|
||||||
|
|
||||||
function handleSelectChange(value: string) {
|
function handleSelectChange(value: string) {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ import {
|
||||||
} from "../../../../components/ui/select-custom";
|
} from "../../../../components/ui/select-custom";
|
||||||
import { AuthContext } from "../../../../contexts/authContext";
|
import { AuthContext } from "../../../../contexts/authContext";
|
||||||
import { TabsContext } from "../../../../contexts/tabsContext";
|
import { TabsContext } from "../../../../contexts/tabsContext";
|
||||||
import { typesContext } from "../../../../contexts/typesContext";
|
|
||||||
import EditNodeModal from "../../../../modals/EditNodeModal";
|
import EditNodeModal from "../../../../modals/EditNodeModal";
|
||||||
import { nodeToolbarPropsType } from "../../../../types/components";
|
import { nodeToolbarPropsType } from "../../../../types/components";
|
||||||
import {
|
import {
|
||||||
|
|
@ -61,8 +60,7 @@ export default function NodeToolbarComponent({
|
||||||
const isMinimal = canMinimize();
|
const isMinimal = canMinimize();
|
||||||
const isGroup = data.node?.flow ? true : false;
|
const isGroup = data.node?.flow ? true : false;
|
||||||
|
|
||||||
const { paste } = useContext(TabsContext);
|
const { paste, saveComponent } = useContext(TabsContext);
|
||||||
const { saveComponent } = useContext(typesContext);
|
|
||||||
const reactFlowInstance = useReactFlow();
|
const reactFlowInstance = useReactFlow();
|
||||||
const [showModalAdvanced, setShowModalAdvanced] = useState(false);
|
const [showModalAdvanced, setShowModalAdvanced] = useState(false);
|
||||||
const [selectedValue, setSelectedValue] = useState("");
|
const [selectedValue, setSelectedValue] = useState("");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { tweakType } from "../components";
|
import { tweakType } from "../components";
|
||||||
import { FlowType } from "../flow";
|
import { FlowType, NodeDataType } from "../flow";
|
||||||
|
|
||||||
export type TabsContextType = {
|
export type TabsContextType = {
|
||||||
saveFlow: (flow: FlowType, silent?: boolean) => Promise<void>;
|
saveFlow: (flow: FlowType, silent?: boolean) => Promise<void>;
|
||||||
|
|
@ -36,6 +36,8 @@ export type TabsContextType = {
|
||||||
setLastCopiedSelection: (selection: { nodes: any; edges: any }) => void;
|
setLastCopiedSelection: (selection: { nodes: any; edges: any }) => void;
|
||||||
setTweak: (tweak: tweakType) => tweakType | void;
|
setTweak: (tweak: tweakType) => tweakType | void;
|
||||||
getTweak: tweakType;
|
getTweak: tweakType;
|
||||||
|
saveComponent: (component: NodeDataType, id: string) => void;
|
||||||
|
deleteComponent: (id: string, key: string) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TabsState = {
|
export type TabsState = {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import { Edge, Node, ReactFlowInstance } from "reactflow";
|
import { Edge, Node, ReactFlowInstance } from "reactflow";
|
||||||
import { AlertItemType } from "../alerts";
|
import { AlertItemType } from "../alerts";
|
||||||
import { APIClassType, APIDataType } from "../api";
|
import { APIClassType, APIDataType } from "../api";
|
||||||
import { NodeDataType } from "../flow";
|
|
||||||
|
|
||||||
const types: { [char: string]: string } = {};
|
const types: { [char: string]: string } = {};
|
||||||
const template: { [char: string]: APIClassType } = {};
|
const template: { [char: string]: APIClassType } = {};
|
||||||
|
|
@ -22,8 +21,6 @@ export type typesContextType = {
|
||||||
setFilterEdge: (newState) => void;
|
setFilterEdge: (newState) => void;
|
||||||
getFilterEdge: any[];
|
getFilterEdge: any[];
|
||||||
deleteEdge: (idx: string | Array<string>) => void;
|
deleteEdge: (idx: string | Array<string>) => void;
|
||||||
saveComponent: (component: NodeDataType, key: string) => void;
|
|
||||||
deleteComponent: (id: string, key: string) => void;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type alertContextType = {
|
export type alertContextType = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue