Skip to content

Commit

Permalink
fix: re-implement logic to correctly save or update flow in the appro…
Browse files Browse the repository at this point in the history
…priate folder on autologin = false (langflow-ai#2610)

♻️ (frontend): remove unused imports and variables in cardComponent
✨ (authContext): add setAllFlows and setSelectedFolder to AuthProvider
✨ (undrawCards): add folderIdUrl for better URL handling
✨ (collectionCard): add folder URL handling for navigation
✨ (use-delete-multiple): add setAllFlows and setSelectedFolder to handleDeleteMultiple
✨ (componentsComponent): add isLoadingFolders and setSelectedFolder
✨ (tabsComponent): add folder URL handling for tab navigation
✨ (flowsManagerStore): add folder URL handling in saveFlowDebounce

✨ (foldersStore.tsx): add setSelectedFolder method to manage selected folder state
♻️ (foldersStore.tsx): move setIsLoadingFolders call to improve loading state management
✨ (flowsManager/index.ts): add folderId parameter to saveFlow and saveFlowDebounce methods for better flow management
✨ (folders/index.ts): add setSelectedFolder method to FoldersStoreType for better folder state management

(cherry picked from commit 0df06c0)
  • Loading branch information
Cristhianzl authored and nicoloboschi committed Jul 11, 2024
1 parent 9189882 commit 5380492
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/frontend/src/components/cardComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
import { Checkbox } from "../ui/checkbox";
import { FormControl, FormField } from "../ui/form";
import Loading from "../ui/loading";
import DragCardComponent from "./components/dragCardComponent";
import useDataEffect from "./hooks/use-data-effect";
import useInstallComponent from "./hooks/use-handle-install";
import useLikeComponent from "./hooks/use-handle-like";
Expand Down Expand Up @@ -56,7 +55,6 @@ export default function CollectionCardComponent({
control?: Control<any, any>;
is_component?: boolean;
}) {
const addFlow = useFlowsManagerStore((state) => state.addFlow);
const setSuccessData = useAlertStore((state) => state.setSuccessData);
const setErrorData = useAlertStore((state) => state.setErrorData);
const setValidApiKey = useStoreStore((state) => state.updateValidApiKey);
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/contexts/authContext.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import useFlowsManagerStore from "@/stores/flowsManagerStore";
import { createContext, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import Cookies from "universal-cookie";
Expand Down Expand Up @@ -56,6 +57,8 @@ export function AuthProvider({ children }): React.ReactElement {
);
const checkHasStore = useStoreStore((state) => state.checkHasStore);
const fetchApiData = useStoreStore((state) => state.fetchApiData);
const setAllFlows = useFlowsManagerStore((state) => state.setAllFlows);
const setSelectedFolder = useFolderStore((state) => state.setSelectedFolder);

useEffect(() => {
const storedAccessToken = cookies.get("access_token_lf");
Expand Down Expand Up @@ -105,6 +108,8 @@ export function AuthProvider({ children }): React.ReactElement {
setUserData(null);
setAccessToken(null);
setIsAuthenticated(false);
setAllFlows([]);
setSelectedFolder(null);
navigate("/login");
} catch (error) {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export default function UndrawCardComponent({
const location = useLocation();
const folderId = location?.state?.folderId;
const setFolderUrl = useFolderStore((state) => state.setFolderUrl);
const myCollectionId = useFolderStore((state) => state.myCollectionId);

const folderIdUrl = folderId || myCollectionId || "";

function selectImage() {
switch (flow.name) {
Expand Down Expand Up @@ -108,7 +111,7 @@ export default function UndrawCardComponent({
updateIds(flow.data!);
addFlow(true, flow).then((id) => {
setFolderUrl(folderId ?? "");
navigate(`/flow/${id}${folderId ? `/folder/${folderId}` : ""}`);
navigate(`/flow/${id}/folder/${folderIdUrl}`);
});
}}
className="h-64 w-80 cursor-pointer bg-background pt-4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useFolderStore } from "@/stores/foldersStore";
import React from "react";
import { Link, useNavigate } from "react-router-dom";
import CollectionCardComponent from "../../../../../../components/cardComponent";
Expand All @@ -9,9 +10,15 @@ const CollectionCard = ({ item, type, isLoading, control }) => {
const editFlowLink = `/flow/${item.id}`;
const editFlowButtonTestId = `edit-flow-button-${item.id}`;

const folderUrl = useFolderStore((state) => state.folderUrl);
const myCollectionIdFolder = useFolderStore((state) => state.myCollectionId);

const hasFolderUrl = folderUrl != null && folderUrl !== "";
const currentFolderUrl = hasFolderUrl ? folderUrl : myCollectionIdFolder;

const handleClick = () => {
if (!isComponent) {
navigate(editFlowLink);
navigate(editFlowLink, { state: { folderId: currentFolderUrl } });
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FolderType } from "@/pages/MainPage/entities";
import { FlowType } from "@/types/flow";
import { useCallback } from "react";

const useDeleteMultipleFlows = (
Expand All @@ -10,10 +12,15 @@ const useDeleteMultipleFlows = (
getFolderById: (id: string) => void,
setSuccessData: (data: { title: string }) => void,
setErrorData: (data: { title: string; list: string[] }) => void,
setAllFlows: (flows: FlowType[]) => void,
setSelectedFolder: (folder: FolderType | null) => void,
) => {
const handleDeleteMultiple = useCallback(() => {
removeFlow(selectedFlowsComponentsCards)
.then(() => {
setAllFlows([]);
setSelectedFolder(null);

resetFilter();
getFoldersApi(true);
if (!folderId || folderId === myCollectionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export default function ComponentsComponent({
const getFoldersApi = useFolderStore((state) => state.getFoldersApi);
const setFolderUrl = useFolderStore((state) => state.setFolderUrl);
const addFlow = useFlowsManagerStore((state) => state.addFlow);
const isLoadingFolders = useFolderStore((state) => state.isLoadingFolders);
const setSelectedFolder = useFolderStore((state) => state.setSelectedFolder);

const cardTypes = useMemo(() => {
if (window.location.pathname.includes("components")) {
Expand Down Expand Up @@ -159,6 +161,8 @@ export default function ComponentsComponent({
getFolderById,
setSuccessData,
setErrorData,
setAllFlows,
setSelectedFolder,
);

useSelectedFlows(entireFormValues, setSelectedFlowsComponentsCards);
Expand Down Expand Up @@ -196,11 +200,13 @@ export default function ComponentsComponent({
>
<div className="flex h-full w-full flex-col justify-between">
<div className="flex w-full flex-col gap-4">
{!isLoading && data?.length === 0 ? (
{!isLoading && !isLoadingFolders && data?.length === 0 ? (
<EmptyComponent />
) : (
<div className="grid w-full gap-4 md:grid-cols-2 lg:grid-cols-2">
{isLoading === false && data?.length > 0 ? (
{isLoading === false &&
data?.length > 0 &&
isLoadingFolders === false ? (
<>
{data?.map((item) => (
<FormProvider {...methods} key={item.id}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useFolderStore } from "../../../../../../stores/foldersStore";

Expand All @@ -17,6 +17,10 @@ const TabsSearchComponent = ({
}: TabsSearchComponentProps) => {
const navigate = useNavigate();
const folderUrl = useFolderStore((state) => state.folderUrl);
const myCollectionIdFolder = useFolderStore((state) => state.myCollectionId);

const hasFolderUrl = folderUrl != null && folderUrl !== "";
const currentFolderUrl = hasFolderUrl ? folderUrl : myCollectionIdFolder;

const changeLocation = (tabOption) => {
const location = window.location.pathname;
Expand All @@ -33,7 +37,7 @@ const TabsSearchComponent = ({
break;
}

navigate(newLocation, { state: { folderId: folderUrl } });
navigate(newLocation, { state: { folderId: currentFolderUrl } });
};

useEffect(() => {
Expand Down
8 changes: 8 additions & 0 deletions src/frontend/src/stores/flowsManagerStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AxiosError } from "axios";
import { cloneDeep } from "lodash";
import pDebounce from "p-debounce";
import { useLocation } from "react-router-dom";
import { Edge, Node, Viewport, XYPosition } from "reactflow";
import { create } from "zustand";
import { SAVE_DEBOUNCE_TIME } from "../constants/constants";
Expand Down Expand Up @@ -132,6 +133,13 @@ const useFlowsManagerStore = create<FlowsManagerStoreType>((set, get) => ({
return get().saveFlowDebounce(flow, silent); // call the debounced function directly
},
saveFlowDebounce: pDebounce((flow: FlowType, silent?: boolean) => {
const folderUrl = useFolderStore.getState().folderUrl;
const hasFolderUrl = folderUrl != null && folderUrl !== "";

flow.folder_id = hasFolderUrl
? useFolderStore.getState().folderUrl
: useFolderStore.getState().myCollectionId ?? "";

set({ saveLoading: true });
return new Promise<void>((resolve, reject) => {
updateFlowInDatabase(flow)
Expand Down
7 changes: 4 additions & 3 deletions src/frontend/src/stores/foldersStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const useFolderStore = create<FoldersStoreType>((set, get) => ({
folders: [],
getFoldersApi: (refetch = false, startupApplication: boolean = false) => {
return new Promise<void>((resolve, reject) => {
get().setIsLoadingFolders(true);
if (get()?.folders.length === 0 || refetch === true) {
get().setIsLoadingFolders(true);
getFolders().then(
async (res) => {
const foldersWithoutStarterProjects = res?.filter(
Expand All @@ -36,18 +36,18 @@ export const useFolderStore = create<FoldersStoreType>((set, get) => ({

const { refreshFlows } = useFlowsManagerStore.getState();
const { getTypes } = useTypesStore.getState();
const { setIsLoadingFolders } = get();

if (refetch) {
if (startupApplication) {
await refreshFlows();
await getTypes();
get().setIsLoadingFolders(false);
} else {
refreshFlows();
getTypes();
get().setIsLoadingFolders(false);
}
}
setIsLoadingFolders(false);

resolve();
},
Expand Down Expand Up @@ -104,6 +104,7 @@ export const useFolderStore = create<FoldersStoreType>((set, get) => ({
}
},
selectedFolder: null,
setSelectedFolder: (folder) => set(() => ({ selectedFolder: folder })),
loadingById: false,
getMyCollectionFolder: () => {
const folders = get().folders;
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/src/types/zustand/flowsManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ export type FlowsManagerStoreType = {
isLoading: boolean;
setIsLoading: (isLoading: boolean) => void;
refreshFlows: () => Promise<void>;
saveFlow: (flow: FlowType, silent?: boolean) => Promise<void> | undefined;
saveFlow: (
flow: FlowType,
silent?: boolean,
folderId?: string,
) => Promise<void> | undefined;
saveFlowDebounce: (
flow: FlowType,
silent?: boolean,
folderId?: string,
) => Promise<void> | undefined;
autoSaveCurrentFlow: (
nodes: Node[],
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/types/zustand/folders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type FoldersStoreType = {
isLoadingFolders: boolean;
setIsLoadingFolders: (isLoadingFolders: boolean) => void;
selectedFolder: FolderType | null;
setSelectedFolder: (folder: FolderType | null) => void;
getFolderById: (id: string) => void;
getMyCollectionFolder: () => void;
myCollectionFlows: FolderType | null;
Expand Down

0 comments on commit 5380492

Please sign in to comment.