This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into laurent/flow-create-error-handler
- Loading branch information
Showing
7 changed files
with
149 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
createContext, | ||
useContext, | ||
useState, | ||
ReactNode, | ||
useEffect, | ||
} from "react"; | ||
import { useApi } from "@/contexts/ApiContext"; | ||
import { Flow } from "@/types"; | ||
|
||
interface FlowsContextType { | ||
flows: Flow[]; | ||
refetchFlows: () => Promise<Flow[]>; | ||
flowVisibility: Record<string, boolean>; | ||
setFlowVisibility: (flowId: string, visible: boolean) => void; | ||
} | ||
|
||
// Create the context with a default value | ||
const FlowsContext = createContext<FlowsContextType>({ | ||
flows: [], | ||
refetchFlows: async () => [], | ||
flowVisibility: {}, | ||
setFlowVisibility: () => {}, | ||
}); | ||
|
||
// Create a provider component | ||
interface FlowsContextProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const FlowsContextProvider = ({ | ||
children, | ||
}: FlowsContextProviderProps) => { | ||
const { getFlows } = useApi(); | ||
const [flows, setFlows] = useState<Flow[]>([]); | ||
|
||
const [flowVisibility, setFlowVisibility] = useState<Record<string, boolean>>( | ||
{}, | ||
); | ||
|
||
useEffect(() => { | ||
setFlowVisibility((prevState) => | ||
flows.reduce( | ||
(acc, flow) => ({ | ||
...acc, | ||
[flow["flow-id"]]: prevState[flow["flow-id"]] ?? true, | ||
}), | ||
{}, | ||
), | ||
); | ||
}, [flows]); | ||
|
||
useEffect(() => { | ||
getFlows().then(setFlows); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const refetchFlows = async () => { | ||
const newFlows = await getFlows(); | ||
setFlows(newFlows); | ||
return newFlows; | ||
}; | ||
|
||
return ( | ||
<FlowsContext.Provider | ||
value={{ | ||
flows, | ||
refetchFlows, | ||
flowVisibility, | ||
setFlowVisibility: (flowId: string, visible: boolean) => { | ||
setFlowVisibility({ ...flowVisibility, [flowId]: visible }); | ||
}, | ||
}} | ||
> | ||
{children} | ||
</FlowsContext.Provider> | ||
); | ||
}; | ||
|
||
// Create a custom hook to use the context | ||
// eslint-disable-next-line react-refresh/only-export-components | ||
export const useFlowsContext = (): FlowsContextType => { | ||
const context = useContext(FlowsContext); | ||
if (context === undefined) { | ||
throw new Error( | ||
"useFlowsContext must be used within a FlowsContextProvider", | ||
); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters