From e2a0990c06f2c533056c9aa13eda893ebe18ed07 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Mon, 8 Jan 2024 14:03:22 -0300 Subject: [PATCH] Removed typesContext --- src/frontend/src/contexts/typesContext.tsx | 105 ------------------ src/frontend/src/stores/flowsManagerStore.ts | 3 +- src/frontend/src/types/typesContext/index.ts | 89 --------------- .../src/types/zustand/flowsManager/index.ts | 5 + 4 files changed, 6 insertions(+), 196 deletions(-) delete mode 100644 src/frontend/src/contexts/typesContext.tsx delete mode 100644 src/frontend/src/types/typesContext/index.ts diff --git a/src/frontend/src/contexts/typesContext.tsx b/src/frontend/src/contexts/typesContext.tsx deleted file mode 100644 index 16deecf00..000000000 --- a/src/frontend/src/contexts/typesContext.tsx +++ /dev/null @@ -1,105 +0,0 @@ -import _ from "lodash"; -import { - createContext, - ReactNode, - useContext, - useEffect, - useState, -} from "react"; -import { getAll, getHealth } from "../controllers/API"; -import useAlertStore from "../stores/alertStore"; -import { APIKindType } from "../types/api"; -import { typesContextType } from "../types/typesContext"; -import { AuthContext } from "./authContext"; - -//context to share types adn functions from nodes to flow - -const initialValue: typesContextType = { - types: {}, - setTypes: () => {}, - templates: {}, - setTemplates: () => {}, - data: {}, - setData: () => {}, - getTypes: () => {}, - setFetchError: () => {}, - fetchError: false, - setFilterEdge: (filter) => {}, - getFilterEdge: [], -}; - -export const typesContext = createContext(initialValue); - -export function TypesProvider({ children }: { children: ReactNode }) { - const [types, setTypes] = useState({}); - const [templates, setTemplates] = useState({}); - const [data, setData] = useState({}); - const [fetchError, setFetchError] = useState(false); - const setLoading = useAlertStore((state) => state.setLoading); - const [getFilterEdge, setFilterEdge] = useState([]); - - async function getTypes(): Promise { - // We will keep a flag to handle the case where the component is unmounted before the API call resolves. - let isMounted = true; - try { - const result = await getAll(); - // Make sure to only update the state if the component is still mounted. - if (isMounted && result?.status === 200) { - setLoading(false); - let { data } = _.cloneDeep(result); - setData((old) => ({ ...old, ...data })); - setTemplates( - Object.keys(data).reduce((acc, curr) => { - Object.keys(data[curr]).forEach((c: keyof APIKindType) => { - //prevent wrong overwriting of the component template by a group of the same type - if (!data[curr][c].flow) acc[c] = data[curr][c]; - }); - return acc; - }, {}) - ); - // Set the types by reducing over the keys of the result data and updating the accumulator. - setTypes( - // Reverse the keys so the tool world does not overlap - Object.keys(data) - .reverse() - .reduce((acc, curr) => { - Object.keys(data[curr]).forEach((c: keyof APIKindType) => { - acc[c] = curr; - // Add the base classes to the accumulator as well. - data[curr][c].base_classes?.forEach((b) => { - acc[b] = curr; - }); - }); - return acc; - }, {}) - ); - } - } catch (error) { - console.error("An error has occurred while fetching types."); - console.log(error); - await getHealth().catch((e) => { - setFetchError(true); - }); - } - } - - return ( - - {children} - - ); -} diff --git a/src/frontend/src/stores/flowsManagerStore.ts b/src/frontend/src/stores/flowsManagerStore.ts index b076f75e4..dfab1a9df 100644 --- a/src/frontend/src/stores/flowsManagerStore.ts +++ b/src/frontend/src/stores/flowsManagerStore.ts @@ -10,8 +10,7 @@ import { } from "../controllers/API"; import { FlowType, NodeDataType } from "../types/flow"; import { FlowState } from "../types/tabs"; -import { UseUndoRedoOptions } from "../types/typesContext"; -import { FlowsManagerStoreType } from "../types/zustand/flowsManager"; +import { FlowsManagerStoreType, UseUndoRedoOptions } from "../types/zustand/flowsManager"; import { addVersionToDuplicates, createFlowComponent, diff --git a/src/frontend/src/types/typesContext/index.ts b/src/frontend/src/types/typesContext/index.ts deleted file mode 100644 index c7314fea0..000000000 --- a/src/frontend/src/types/typesContext/index.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { Edge, Node } from "reactflow"; -import { AlertItemType } from "../alerts"; - -export type alertContextType = { - errorData: { title: string; list?: Array }; - setErrorData: (newState: { title: string; list?: Array }) => void; - errorOpen: boolean; - setErrorOpen: (newState: boolean) => void; - noticeData: { title: string; link?: string }; - setNoticeData: (newState: { title: string; link?: string }) => void; - noticeOpen: boolean; - setNoticeOpen: (newState: boolean) => void; - successData: { title: string }; - setSuccessData: (newState: { title: string }) => void; - successOpen: boolean; - setSuccessOpen: (newState: boolean) => void; - notificationCenter: boolean; - setNotificationCenter: (newState: boolean) => void; - notificationList: Array; - pushNotificationList: (Object: AlertItemType) => void; - clearNotificationList: () => void; - removeFromNotificationList: (index: string) => void; - loading: boolean; - setLoading: (newState: boolean) => void; - modalContextOpen: boolean | null; - setModalContextOpen: (newState: boolean) => void; -}; - -export type darkContextType = { - dark: {}; - setDark: (newState: {}) => void; - stars: number; - setStars: (stars: number) => void; - gradientIndex: number; - setGradientIndex: (index: number) => void; -}; - -export type locationContextType = { - current: Array; - setCurrent: (newState: Array) => void; - isStackedOpen: boolean; - setIsStackedOpen: (newState: boolean) => void; - showSideBar: boolean; - setShowSideBar: (newState: boolean) => void; - extraNavigation: { - title: string; - options?: Array<{ - name: string; - href: string; - icon: React.ElementType; - children?: Array; - }>; - }; - setExtraNavigation: (newState: { - title: string; - options?: Array<{ - name: string; - href: string; - icon: React.ElementType; - children?: Array; - }>; - }) => void; - extraComponent: any; - setExtraComponent: (newState: JSX.Element) => void; -}; - -export type undoRedoContextType = { - undo: () => void; - redo: () => void; - takeSnapshot: () => void; -}; - -export type UseUndoRedoOptions = { - maxHistorySize: number; - enableShortcuts: boolean; -}; - -export type UseUndoRedo = (options?: UseUndoRedoOptions) => { - undo: () => void; - redo: () => void; - takeSnapshot: () => void; - canUndo: boolean; - canRedo: boolean; -}; - -export type HistoryItem = { - nodes: Node[]; - edges: Edge[]; -}; diff --git a/src/frontend/src/types/zustand/flowsManager/index.ts b/src/frontend/src/types/zustand/flowsManager/index.ts index d130c00e6..c90b699a1 100644 --- a/src/frontend/src/types/zustand/flowsManager/index.ts +++ b/src/frontend/src/types/zustand/flowsManager/index.ts @@ -26,3 +26,8 @@ export type FlowsManagerStoreType = { redo: () => void; takeSnapshot: () => void; }; + +export type UseUndoRedoOptions = { + maxHistorySize: number; + enableShortcuts: boolean; +}; \ No newline at end of file