Refactor: move get-field-title and sort-fields to customNodes folder
This commit is contained in:
parent
7547f0a178
commit
05859becd4
6 changed files with 55 additions and 169 deletions
|
|
@ -1,119 +0,0 @@
|
|||
import { APIDataType, TemplateVariableType } from "../../../../../types/api";
|
||||
import {
|
||||
groupedObjType,
|
||||
nodeGroupedObjType,
|
||||
} from "../../../../../types/components";
|
||||
import { NodeType } from "../../../../../types/flow";
|
||||
|
||||
export default function groupByFamily(
|
||||
data: APIDataType,
|
||||
baseClasses: string,
|
||||
left: boolean,
|
||||
flow?: NodeType[],
|
||||
): groupedObjType[] {
|
||||
const baseClassesSet = new Set(baseClasses.split("\n"));
|
||||
let arrOfPossibleInputs: Array<{
|
||||
category: string;
|
||||
nodes: nodeGroupedObjType[];
|
||||
full: boolean;
|
||||
display_name?: string;
|
||||
}> = [];
|
||||
let arrOfPossibleOutputs: Array<{
|
||||
category: string;
|
||||
nodes: nodeGroupedObjType[];
|
||||
full: boolean;
|
||||
display_name?: string;
|
||||
}> = [];
|
||||
let checkedNodes = new Map();
|
||||
const excludeTypes = new Set(["bool", "float", "code", "file", "int"]);
|
||||
|
||||
const checkBaseClass = (template: TemplateVariableType) => {
|
||||
return (
|
||||
template.type &&
|
||||
template.show &&
|
||||
((!excludeTypes.has(template.type) &&
|
||||
baseClassesSet.has(template.type)) ||
|
||||
(template.input_types &&
|
||||
template.input_types.some((inputType) =>
|
||||
baseClassesSet.has(inputType),
|
||||
)))
|
||||
);
|
||||
};
|
||||
|
||||
if (flow) {
|
||||
// se existir o flow
|
||||
for (const node of flow) {
|
||||
// para cada node do flow
|
||||
if (node!.data!.node!.flow || !node!.data!.node!.template) break; // não faz nada se o node for um group
|
||||
const nodeData = node.data;
|
||||
|
||||
const foundNode = checkedNodes.get(nodeData.type); // verifica se o tipo do node já foi checado
|
||||
checkedNodes.set(nodeData.type, {
|
||||
hasBaseClassInTemplate:
|
||||
foundNode?.hasBaseClassInTemplate ||
|
||||
Object.values(nodeData.node!.template).some(checkBaseClass),
|
||||
hasBaseClassInBaseClasses:
|
||||
foundNode?.hasBaseClassInBaseClasses ||
|
||||
nodeData.node!.base_classes.some((baseClass) =>
|
||||
baseClassesSet.has(baseClass),
|
||||
), //seta como anterior ou verifica se o node tem base class
|
||||
displayName: nodeData.node?.display_name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const [d, nodes] of Object.entries(data)) {
|
||||
let tempInputs: nodeGroupedObjType[] = [],
|
||||
tempOutputs: nodeGroupedObjType[] = [];
|
||||
|
||||
for (const [n, node] of Object.entries(nodes!)) {
|
||||
let foundNode = checkedNodes.get(n);
|
||||
|
||||
if (!foundNode) {
|
||||
foundNode = {
|
||||
hasBaseClassInTemplate: Object.values(node!.template).some(
|
||||
checkBaseClass,
|
||||
),
|
||||
hasBaseClassInBaseClasses: node!.base_classes.some((baseClass) =>
|
||||
baseClassesSet.has(baseClass),
|
||||
),
|
||||
displayName: node?.display_name,
|
||||
};
|
||||
}
|
||||
|
||||
if (foundNode.hasBaseClassInTemplate)
|
||||
tempInputs.push({ node: n, displayName: foundNode.displayName });
|
||||
if (foundNode.hasBaseClassInBaseClasses)
|
||||
tempOutputs.push({ node: n, displayName: foundNode.displayName });
|
||||
}
|
||||
|
||||
const totalNodes = Object.keys(nodes!).length;
|
||||
|
||||
if (tempInputs.length)
|
||||
arrOfPossibleInputs.push({
|
||||
category: d,
|
||||
nodes: tempInputs,
|
||||
full: tempInputs.length === totalNodes,
|
||||
});
|
||||
if (tempOutputs.length)
|
||||
arrOfPossibleOutputs.push({
|
||||
category: d,
|
||||
nodes: tempOutputs,
|
||||
full: tempOutputs.length === totalNodes,
|
||||
});
|
||||
}
|
||||
|
||||
return left
|
||||
? arrOfPossibleOutputs.map((output) => ({
|
||||
family: output.category,
|
||||
type: output.full
|
||||
? ""
|
||||
: output.nodes.map((item) => item.node).join(", "),
|
||||
display_name: "",
|
||||
}))
|
||||
: arrOfPossibleInputs.map((input) => ({
|
||||
family: input.category,
|
||||
type: input.full ? "" : input.nodes.map((item) => item.node).join(", "),
|
||||
display_name: input.nodes.map((item) => item.displayName).join(", "),
|
||||
}));
|
||||
}
|
||||
|
|
@ -27,8 +27,10 @@ import { validationStatusType } from "../../types/components";
|
|||
import { NodeDataType } from "../../types/flow";
|
||||
import { handleKeyDown, scapedJSONStringfy } from "../../utils/reactflowUtils";
|
||||
import { nodeColors, nodeIconsLucide } from "../../utils/styleUtils";
|
||||
import { classNames, cn, getFieldTitle, sortFields } from "../../utils/utils";
|
||||
import { classNames, cn } from "../../utils/utils";
|
||||
import ParameterComponent from "./components/parameterComponent";
|
||||
import getFieldTitle from "../utils/get-field-title";
|
||||
import sortFields from "../utils/sort-fields";
|
||||
|
||||
export default function GenericNode({
|
||||
data,
|
||||
|
|
|
|||
10
src/frontend/src/customNodes/utils/get-field-title.tsx
Normal file
10
src/frontend/src/customNodes/utils/get-field-title.tsx
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { APITemplateType } from "../../types/api";
|
||||
|
||||
export default function getFieldTitle(
|
||||
template: APITemplateType,
|
||||
templateField: string,
|
||||
): string {
|
||||
return template[templateField].display_name
|
||||
? template[templateField].display_name!
|
||||
: template[templateField].name ?? templateField;
|
||||
}
|
||||
40
src/frontend/src/customNodes/utils/sort-fields.tsx
Normal file
40
src/frontend/src/customNodes/utils/sort-fields.tsx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { priorityFields } from "../../constants/constants";
|
||||
|
||||
export default function sortFields(a, b, fieldOrder) {
|
||||
// Early return for empty fields
|
||||
if (!a && !b) return 0;
|
||||
if (!a) return 1;
|
||||
if (!b) return -1;
|
||||
|
||||
// Normalize the case to ensure case-insensitive comparison
|
||||
const normalizedFieldA = a.toLowerCase();
|
||||
const normalizedFieldB = b.toLowerCase();
|
||||
|
||||
const aIsPriority = priorityFields.has(normalizedFieldA);
|
||||
const bIsPriority = priorityFields.has(normalizedFieldB);
|
||||
|
||||
// Sort by priority
|
||||
if (aIsPriority && !bIsPriority) return -1;
|
||||
if (!aIsPriority && bIsPriority) return 1;
|
||||
|
||||
// Check if either field is in the fieldOrder array
|
||||
const indexOfA = fieldOrder.indexOf(normalizedFieldA);
|
||||
const indexOfB = fieldOrder.indexOf(normalizedFieldB);
|
||||
|
||||
// If both fields are in fieldOrder, sort by their order in the array
|
||||
if (indexOfA !== -1 && indexOfB !== -1) {
|
||||
return indexOfA - indexOfB;
|
||||
}
|
||||
|
||||
// If only one of the fields is in fieldOrder, that field comes first
|
||||
if (indexOfA !== -1) {
|
||||
return -1;
|
||||
}
|
||||
if (indexOfB !== -1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Default case for fields not in priorityFields and not found in fieldOrder
|
||||
// You might want to sort them alphabetically or in another specific manner
|
||||
return a.localeCompare(b);
|
||||
}
|
||||
|
|
@ -36,8 +36,9 @@ import {
|
|||
unselectAllNodesType,
|
||||
updateEdgesHandleIdsType,
|
||||
} from "../types/utils/reactflowUtils";
|
||||
import { createRandomKey, getFieldTitle, toTitleCase } from "./utils";
|
||||
import { createRandomKey, toTitleCase } from "./utils";
|
||||
import { DESCRIPTIONS } from "../flow_constants";
|
||||
import getFieldTitle from "../customNodes/utils/get-field-title";
|
||||
const uid = new ShortUniqueId({ length: 5 });
|
||||
|
||||
export function checkChatInput(nodes: Node[]) {
|
||||
|
|
|
|||
|
|
@ -346,54 +346,6 @@ export function getSetFromObject(obj: object, key?: string): Set<string> {
|
|||
return set;
|
||||
}
|
||||
|
||||
export function getFieldTitle(
|
||||
template: APITemplateType,
|
||||
templateField: string,
|
||||
): string {
|
||||
return template[templateField].display_name
|
||||
? template[templateField].display_name!
|
||||
: template[templateField].name ?? templateField;
|
||||
}
|
||||
|
||||
export function sortFields(a, b, fieldOrder) {
|
||||
// Early return for empty fields
|
||||
if (!a && !b) return 0;
|
||||
if (!a) return 1;
|
||||
if (!b) return -1;
|
||||
|
||||
// Normalize the case to ensure case-insensitive comparison
|
||||
const normalizedFieldA = a.toLowerCase();
|
||||
const normalizedFieldB = b.toLowerCase();
|
||||
|
||||
const aIsPriority = priorityFields.has(normalizedFieldA);
|
||||
const bIsPriority = priorityFields.has(normalizedFieldB);
|
||||
|
||||
// Sort by priority
|
||||
if (aIsPriority && !bIsPriority) return -1;
|
||||
if (!aIsPriority && bIsPriority) return 1;
|
||||
|
||||
// Check if either field is in the fieldOrder array
|
||||
const indexOfA = fieldOrder.indexOf(normalizedFieldA);
|
||||
const indexOfB = fieldOrder.indexOf(normalizedFieldB);
|
||||
|
||||
// If both fields are in fieldOrder, sort by their order in the array
|
||||
if (indexOfA !== -1 && indexOfB !== -1) {
|
||||
return indexOfA - indexOfB;
|
||||
}
|
||||
|
||||
// If only one of the fields is in fieldOrder, that field comes first
|
||||
if (indexOfA !== -1) {
|
||||
return -1;
|
||||
}
|
||||
if (indexOfB !== -1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Default case for fields not in priorityFields and not found in fieldOrder
|
||||
// You might want to sort them alphabetically or in another specific manner
|
||||
return a.localeCompare(b);
|
||||
}
|
||||
|
||||
export function freezeObject(obj: any) {
|
||||
if (!obj) return obj;
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue