Fix: Make component search by name instead type to avoid delete on side bar not working

This commit is contained in:
igorrCarvalho 2023-11-22 19:15:46 -03:00
commit 51ff8eb032
3 changed files with 23 additions and 42 deletions

View file

@ -81,7 +81,7 @@ const FlowsContextInitialValue: FlowsContextType = {
position: { x: number; y: number; paneX?: number; paneY?: number } position: { x: number; y: number; paneX?: number; paneY?: number }
) => {}, ) => {},
saveComponent: async (component: NodeDataType) => "", saveComponent: async (component: NodeDataType) => "",
deleteComponent: (id: string, key: string) => {}, deleteComponent: (key: string) => {},
version: "", version: "",
}; };
@ -147,7 +147,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
} }
function processFlows(DbData: FlowType[], skipUpdate = true) { function processFlows(DbData: FlowType[], skipUpdate = true) {
let storeComponents: { [key: string]: APIClassType } = {}; let savedComponents: { [key: string]: APIClassType } = {};
DbData.forEach((flow: FlowType) => { DbData.forEach((flow: FlowType) => {
try { try {
if (!flow.data) { if (!flow.data) {
@ -156,7 +156,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
if (flow.data && flow.is_component) { if (flow.data && flow.is_component) {
(flow.data.nodes[0].data as NodeDataType).node!.display_name = (flow.data.nodes[0].data as NodeDataType).node!.display_name =
flow.name; flow.name;
storeComponents[ savedComponents[
createRandomKey( createRandomKey(
(flow.data.nodes[0].data as NodeDataType).type, (flow.data.nodes[0].data as NodeDataType).type,
uid() uid()
@ -170,7 +170,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
} }
}); });
setData((prev) => { setData((prev) => {
prev["saved_components"] = cloneDeep(storeComponents); prev["saved_components"] = cloneDeep(savedComponents);
return prev; return prev;
}); });
} }
@ -505,20 +505,25 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
? processDataFromFlow(flow) ? processDataFromFlow(flow)
: { nodes: [], edges: [], viewport: { zoom: 1, x: 0, y: 0 } }; : { nodes: [], edges: [], viewport: { zoom: 1, x: 0, y: 0 } };
// Create a new flow with a default name if no flow is provided. // Create a new flow with a default name if no flow is provided.
const newFlow = createNewFlow(flowData, flow!); const newFlow = createNewFlow(flowData, flow!);
const flowName = addVersionToDuplicates(newFlow, flows);
newFlow.name = flowName; const newName = addVersionToDuplicates(newFlow, flows);
newFlow.name = newName;
try { try {
const { id } = await saveFlowToDatabase(newFlow); const { id } = await saveFlowToDatabase(newFlow);
// Change the id to the new id. // Change the id to the new id.
newFlow.id = id; newFlow.id = id;
// Add the new flow to the list of flows. // Add the new flow to the list of flows.
refreshFlows(); addFlowToLocalState(newFlow);
// Return the id // Return the id
return id; return id;
@ -641,39 +646,16 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
function saveComponent(component: NodeDataType) { function saveComponent(component: NodeDataType) {
component.node!.official = false; component.node!.official = false;
let key = component.type;
if (data["custom_components"][key] !== undefined) {
let increment: number;
component.type = createRandomKey(key, uid());
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})`;
}
}
return addFlow(true, createFlowComponent(component, version)); return addFlow(true, createFlowComponent(component, version));
} }
function deleteComponent(id: string, key: string) { function deleteComponent(key: string) {
let componentFlow = flows.find( let componentFlow = flows.find(
(componentFlow) => (componentFlow) =>
componentFlow.is_component && componentFlow.is_component &&
(componentFlow.data?.nodes[0].data as NodeDataType).type === key componentFlow.name === key
); );
if (componentFlow) { if (componentFlow) {
removeFlow(componentFlow.id); removeFlow(componentFlow.id);
} }

View file

@ -53,8 +53,7 @@ export default function SidebarDraggableComponent({
break; break;
case "delete": case "delete":
deleteComponent( deleteComponent(
autoLogin ? "auto" : userData?.id!, display_name
removeCountFromString(itemName)
); );
break; break;
} }

View file

@ -45,7 +45,7 @@ export type FlowsContextType = {
setTweak: (tweak: tweakType) => tweakType | void; setTweak: (tweak: tweakType) => tweakType | void;
getTweak: tweakType; getTweak: tweakType;
saveComponent: (component: NodeDataType) => Promise<String | undefined>; saveComponent: (component: NodeDataType) => Promise<String | undefined>;
deleteComponent: (id: string, key: string) => void; deleteComponent: (key: string) => void;
version: string; version: string;
}; };