changed useFlow declaration from destructuring to selector to improve performance

This commit is contained in:
Lucas Oliveira 2024-01-05 11:49:07 -03:00
commit 503e7766ba
12 changed files with 49 additions and 27 deletions

View file

@ -72,7 +72,10 @@ export default function ParameterComponent({
const updateNodeInternals = useUpdateNodeInternals(); const updateNodeInternals = useUpdateNodeInternals();
const [position, setPosition] = useState(0); const [position, setPosition] = useState(0);
const { tabId, flows } = useContext(FlowsContext); const { tabId, flows } = useContext(FlowsContext);
const { nodes, edges, setNode } = useFlow(); const nodes = useFlow((state) => state.nodes);
const edges = useFlow((state) => state.edges);
const setNode = useFlow((state) => state.setNode);
const flow = flows.find((flow) => flow.id === tabId)?.data?.nodes ?? null; const flow = flows.find((flow) => flow.id === tabId)?.data?.nodes ?? null;

View file

@ -30,7 +30,8 @@ export default function GenericNode({
yPos: number; yPos: number;
}): JSX.Element { }): JSX.Element {
const { types } = useContext(typesContext); const { types } = useContext(typesContext);
const { deleteNode, setNode } = useFlow(); const deleteNode = useFlow((state) => state.deleteNode);
const setNode = useFlow((state) => state.setNode);
const name = nodeIconsLucide[data.type] ? data.type : types[data.type]; const name = nodeIconsLucide[data.type] ? data.type : types[data.type];
const [inputName, setInputName] = useState(false); const [inputName, setInputName] = useState(false);
const [nodeName, setNodeName] = useState(data.node!.display_name); const [nodeName, setNodeName] = useState(data.node!.display_name);

View file

@ -26,7 +26,8 @@ export default function BuildTrigger({
}): JSX.Element { }): JSX.Element {
const { updateSSEData, isBuilding, setIsBuilding, sseData } = useSSE(); const { updateSSEData, isBuilding, setIsBuilding, sseData } = useSSE();
const { setTabsState, saveFlow } = useContext(FlowsContext); const { setTabsState, saveFlow } = useContext(FlowsContext);
const { nodes, edges } = useFlow(); const nodes = useFlow((state) => state.nodes);
const edges = useFlow((state) => state.edges);
const { setErrorData, setSuccessData } = useContext(alertContext); const { setErrorData, setSuccessData } = useContext(alertContext);
const [isIconTouched, setIsIconTouched] = useState(false); const [isIconTouched, setIsIconTouched] = useState(false);
const eventClick = isBuilding ? "pointer-events-none" : ""; const eventClick = isBuilding ? "pointer-events-none" : "";

View file

@ -14,7 +14,9 @@ import useFlow from "../../stores/flowManagerStore";
export default function Chat({ flow }: ChatType): JSX.Element { export default function Chat({ flow }: ChatType): JSX.Element {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [canOpen, setCanOpen] = useState(false); const [canOpen, setCanOpen] = useState(false);
const { isBuilt, setIsBuilt, isPending } = useFlow(); const isBuilt = useFlow((state) => state.isBuilt);
const setIsBuilt = useFlow((state) => state.setIsBuilt);
const isPending = useFlow((state) => state.isPending);
const { tabsState } = const { tabsState } =
useContext(FlowsContext); useContext(FlowsContext);

View file

@ -55,7 +55,8 @@ export default function CodeTabsComponent({
const [openAccordion, setOpenAccordion] = useState<string[]>([]); const [openAccordion, setOpenAccordion] = useState<string[]>([]);
const {dark} = useDarkStore(); const {dark} = useDarkStore();
const { setNodes } = useFlow(); const setNodes = useFlow((state) => state.setNodes);
const [errorDuplicateKey, setErrorDuplicateKey] = useState(false); const [errorDuplicateKey, setErrorDuplicateKey] = useState(false);
useEffect(() => { useEffect(() => {

View file

@ -104,7 +104,11 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
const { setData } = useContext(typesContext); const { setData } = useContext(typesContext);
const [tabsState, setTabsState] = useState<FlowsState>({}); const [tabsState, setTabsState] = useState<FlowsState>({});
const {nodes, edges, paste, setPending, reactFlowInstance} = useFlow(); const nodes = useFlow((state) => state.nodes);
const edges = useFlow((state) => state.edges);
const reactFlowInstance = useFlow((state) => state.reactFlowInstance);
const setPending = useFlow((state) => state.setPending);
const paste = useFlow((state) => state.paste);
function refreshFlows() { function refreshFlows() {
setIsLoading(true); setIsLoading(true);

View file

@ -32,7 +32,10 @@ export function UndoRedoProvider({ children }) {
const { tabId, flows } = const { tabId, flows } =
useContext(FlowsContext); useContext(FlowsContext);
const {setNodes, setEdges, nodes, edges} = useFlow(); const setNodes = useFlow((state) => state.setNodes);
const setEdges = useFlow((state) => state.setEdges);
const nodes = useFlow((state) => state.nodes);
const edges = useFlow((state) => state.edges);
const [past, setPast] = useState<HistoryItem[][]>(flows.map(() => [])); const [past, setPast] = useState<HistoryItem[][]>(flows.map(() => []));
const [future, setFuture] = useState<HistoryItem[][]>(flows.map(() => [])); const [future, setFuture] = useState<HistoryItem[][]>(flows.map(() => []));

View file

@ -57,7 +57,10 @@ const EditNodeModal = forwardRef(
) => { ) => {
const [myData, setMyData] = useState(data); const [myData, setMyData] = useState(data);
const { setPending, edges, setNode } = useFlow(); const setPending = useFlow((state) => state.setPending);
const edges = useFlow((state) => state.edges);
const setNode = useFlow((state) => state.setNode);
const { setModalContextOpen } = useContext(alertContext); const { setModalContextOpen } = useContext(alertContext);
function changeAdvanced(n) { function changeAdvanced(n) {

View file

@ -39,7 +39,8 @@ export default function FormModal({
flow: FlowType; flow: FlowType;
}): JSX.Element { }): JSX.Element {
const { tabsState, setTabsState } = useContext(FlowsContext); const { tabsState, setTabsState } = useContext(FlowsContext);
const { nodes, edges } = useFlow(); const nodes = useFlow((state) => state.nodes);
const edges = useFlow((state) => state.edges);
const [chatValue, setChatValue] = useState(() => { const [chatValue, setChatValue] = useState(() => {
try { try {
const { formKeysData } = tabsState[flow.id]; const { formKeysData } = tabsState[flow.id];

View file

@ -63,22 +63,20 @@ export default function Page({
const { takeSnapshot } = useContext(undoRedoContext); const { takeSnapshot } = useContext(undoRedoContext);
const { const reactFlowInstance = useFlow((state) => state.reactFlowInstance);
reactFlowInstance, const setReactFlowInstance = useFlow((state) => state.setReactFlowInstance);
setReactFlowInstance, const nodes = useFlow((state) => state.nodes);
nodes, const edges = useFlow((state) => state.edges);
edges, const onNodesChange = useFlow((state) => state.onNodesChange);
onNodesChange, const onEdgesChange = useFlow((state) => state.onEdgesChange);
onEdgesChange, const onConnect = useFlow((state) => state.onConnect);
onConnect, const setNodes = useFlow((state) => state.setNodes);
setNodes, const setEdges = useFlow((state) => state.setEdges);
setEdges, const deleteNode = useFlow((state) => state.deleteNode);
deleteNode, const deleteEdge = useFlow((state) => state.deleteEdge);
deleteEdge, const setPending = useFlow((state) => state.setPending);
setPending, const isPending = useFlow((state) => state.isPending);
isPending, const paste = useFlow((state) => state.paste);
paste,
} = useFlow();
const position = useRef({ x: 0, y: 0 }); const position = useRef({ x: 0, y: 0 });
const [lastSelection, setLastSelection] = const [lastSelection, setLastSelection] =

View file

@ -35,7 +35,8 @@ export default function ExtraSidebar(): JSX.Element {
const hasApiKey = useStoreStore((state) => state.hasApiKey); const hasApiKey = useStoreStore((state) => state.hasApiKey);
const validApiKey = useStoreStore((state) => state.validApiKey); const validApiKey = useStoreStore((state) => state.validApiKey);
const { isBuilt, isPending } = useFlow(); const isBuilt = useFlow((state) => state.isBuilt);
const isPending = useFlow((state) => state.isPending);
const { setErrorData } = useContext(alertContext); const { setErrorData } = useContext(alertContext);
const [dataFilter, setFilterData] = useState(data); const [dataFilter, setFilterData] = useState(data);
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");

View file

@ -66,7 +66,11 @@ 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, nodes, edges, setNodes, setEdges } = useFlow(); const paste = useFlow((state) => state.paste);
const nodes = useFlow((state) => state.nodes);
const edges = useFlow((state) => state.edges);
const setNodes = useFlow((state) => state.setNodes);
const setEdges = useFlow((state) => state.setEdges);
const { saveComponent, flows, version } = useContext(FlowsContext); const { saveComponent, flows, version } = useContext(FlowsContext);
const { takeSnapshot } = useContext(undoRedoContext); const { takeSnapshot } = useContext(undoRedoContext);