Fixed refreshing flows on refresh page

This commit is contained in:
Lucas Oliveira 2023-11-22 16:47:38 -03:00
commit 0d785c923a
4 changed files with 21 additions and 77 deletions

View file

@ -10,7 +10,6 @@ import {
} from "react"; } from "react";
import { Edge, Node, ReactFlowJsonObject, addEdge } from "reactflow"; import { Edge, Node, ReactFlowJsonObject, addEdge } from "reactflow";
import ShortUniqueId from "short-unique-id"; import ShortUniqueId from "short-unique-id";
import { skipNodeUpdate } from "../constants/constants";
import { import {
deleteFlowFromDatabase, deleteFlowFromDatabase,
downloadFlowsFromDatabase, downloadFlowsFromDatabase,
@ -101,8 +100,7 @@ export function FlowsProvider({ 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, data } = const { reactFlowInstance, setData, data } = useContext(typesContext);
useContext(typesContext);
const [lastCopiedSelection, setLastCopiedSelection] = useState<{ const [lastCopiedSelection, setLastCopiedSelection] = useState<{
nodes: any; nodes: any;
edges: any; edges: any;
@ -123,7 +121,6 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
} }
function refreshFlows() { function refreshFlows() {
if (Object.keys(templates).length > 0) {
setIsLoading(true); setIsLoading(true);
getTabsDataFromDB().then((DbData) => { getTabsDataFromDB().then((DbData) => {
if (DbData) { if (DbData) {
@ -135,15 +132,14 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
} }
}); });
} }
}
useEffect(() => { useEffect(() => {
// If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks. // If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks.
if (getAuthentication() === true && Object.keys(templates).length > 0) { if (getAuthentication() === true) {
// get data from db // get data from db
refreshFlows(); refreshFlows();
} }
}, [getAuthentication(), templates, tabId]); }, [getAuthentication(), tabId]);
function getTabsDataFromDB() { function getTabsDataFromDB() {
//get tabs from db //get tabs from db
@ -174,12 +170,8 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
} }
}); });
setData((prev) => { setData((prev) => {
let newData = _.cloneDeep(prev); prev["saved_components"] = cloneDeep(storeComponents);
return prev;
const customComponent = newData["custom_components"]["CustomComponent"];
newData["custom_components"] = cloneDeep(storeComponents);
newData["custom_components"]["CustomComponent"] = customComponent;
return newData;
}); });
} }
@ -204,27 +196,6 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
node.data.node!.documentation = template["documentation"]; node.data.node!.documentation = template["documentation"];
} }
function processFlowNodes(flow: FlowType) {
if (!flow.data || !flow.data.nodes) return;
flow.data.nodes.forEach((node: NodeType) => {
if (node.data.node?.flow) return;
if (skipNodeUpdate.includes(node.data.type)) return;
const template = templates[node.data.type];
if (!template) {
return;
}
if (Object.keys(template["template"]).length > 0) {
updateDisplay_name(node, template);
updateNodeBaseClasses(node, template);
//update baseclasses in edges
updateNodeEdges(flow, node, template);
updateNodeDescription(node, template);
updateNodeTemplate(node, template);
updateNodeDocumentation(node, template);
}
});
}
function updateNodeBaseClasses(node: NodeType, template: APIClassType) { function updateNodeBaseClasses(node: NodeType, template: APIClassType) {
node.data.node!.base_classes = template["base_classes"]; node.data.node!.base_classes = template["base_classes"];
} }
@ -592,36 +563,6 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
}); });
}; };
const updateNodes = (nodes: Node[], edges: Edge[]) => {
nodes.forEach((node) => {
if (node.data.node?.flow) return;
if (skipNodeUpdate.includes(node.data.type)) return;
const template = templates[node.data.type];
if (!template) {
setErrorData({ title: `Unknown node type: ${node.data.type}` });
return;
}
if (Object.keys(template["template"]).length > 0) {
node.data.node.base_classes = template["base_classes"];
edges.forEach((edge) => {
let sourceHandleObject: sourceHandleType = scapeJSONParse(
edge.sourceHandle!
);
if (edge.source === node.id) {
let newSourceHandle = sourceHandleObject;
newSourceHandle.baseClasses.concat(template["base_classes"]);
edge.sourceHandle = scapedJSONStringfy(newSourceHandle);
}
});
node.data.node.description = template["description"];
node.data.node.template = updateTemplate(
template["template"] as unknown as APITemplateType,
node.data.node.template as APITemplateType
);
}
});
};
const createNewFlow = ( const createNewFlow = (
flowData: ReactFlowJsonObject | null, flowData: ReactFlowJsonObject | null,
flow: FlowType flow: FlowType

View file

@ -61,7 +61,7 @@ export function TypesProvider({ children }: { children: ReactNode }) {
if (isMounted && result?.status === 200) { if (isMounted && result?.status === 200) {
setLoading(false); setLoading(false);
let { data } = _.cloneDeep(result); let { data } = _.cloneDeep(result);
setData(data); setData((old) => ({ ...old, ...data }));
setTemplates( setTemplates(
Object.keys(data).reduce((acc, curr) => { Object.keys(data).reduce((acc, curr) => {
Object.keys(data[curr]).forEach((c: keyof APIKindType) => { Object.keys(data[curr]).forEach((c: keyof APIKindType) => {

View file

@ -34,9 +34,9 @@ export default function ComponentsComponent({
new Date(a?.updated_at!).getTime() new Date(a?.updated_at!).getTime()
); );
} else if (a?.updated_at && !b?.updated_at) { } else if (a?.updated_at && !b?.updated_at) {
return -1;
} else if (!a?.updated_at && b?.updated_at) {
return 1; return 1;
} else if (!a?.updated_at && b?.updated_at) {
return -1;
} else { } else {
return ( return (
new Date(b?.date_created!).getTime() - new Date(b?.date_created!).getTime() -

View file

@ -170,6 +170,7 @@ export const nodeColors: { [char: string]: string } = {
agents: "#903BBE", agents: "#903BBE",
tools: "#FF3434", tools: "#FF3434",
memories: "#F5B85A", memories: "#F5B85A",
saved_components: "#a5B85A",
advanced: "#000000", advanced: "#000000",
chat: "#198BF6", chat: "#198BF6",
thought: "#272541", thought: "#272541",
@ -194,6 +195,7 @@ export const nodeNames: { [char: string]: string } = {
agents: "Agents", agents: "Agents",
tools: "Tools", tools: "Tools",
memories: "Memories", memories: "Memories",
saved_components: "Saved Components",
advanced: "Advanced", advanced: "Advanced",
chat: "Chat", chat: "Chat",
embeddings: "Embeddings", embeddings: "Embeddings",
@ -257,6 +259,7 @@ export const nodeIconsLucide: iconsType = {
advanced: Laptop2, advanced: Laptop2,
chat: MessageCircle, chat: MessageCircle,
embeddings: Fingerprint, embeddings: Fingerprint,
saved_components: Save,
documentloaders: Paperclip, documentloaders: Paperclip,
vectorstores: Layers, vectorstores: Layers,
toolkits: Hammer, toolkits: Hammer,