mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-26 12:21:23 +00:00
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import Explorer from "Explorer/Explorer";
|
|
import { Subscription } from "Contracts/DataModels";
|
|
import React from "react";
|
|
import { userContext } from "UserContext";
|
|
import { CopyJobMigrationType } from "../Enums/CopyJobEnums";
|
|
import { CopyJobContextProviderType, CopyJobContextState, CopyJobFlowType } from "../Types/CopyJobTypes";
|
|
|
|
export const CopyJobContext = React.createContext<CopyJobContextProviderType>(null);
|
|
export const useCopyJobContext = (): CopyJobContextProviderType => {
|
|
const context = React.useContext(CopyJobContext);
|
|
if (!context) {
|
|
throw new Error("useCopyJobContext must be used within a CopyJobContextProvider");
|
|
}
|
|
return context;
|
|
};
|
|
|
|
interface CopyJobContextProviderProps {
|
|
children: React.ReactNode;
|
|
explorer: Explorer;
|
|
}
|
|
|
|
const getInitialCopyJobState = (): CopyJobContextState => {
|
|
return {
|
|
jobName: "",
|
|
migrationType: CopyJobMigrationType.Offline,
|
|
source: {
|
|
subscription: {
|
|
subscriptionId: userContext.subscriptionId || "",
|
|
} as Subscription,
|
|
account: userContext.databaseAccount || null,
|
|
databaseId: "",
|
|
containerId: "",
|
|
},
|
|
target: {
|
|
subscriptionId: userContext.subscriptionId || "",
|
|
account: userContext.databaseAccount || null,
|
|
databaseId: "",
|
|
containerId: "",
|
|
},
|
|
sourceReadAccessFromTarget: false,
|
|
};
|
|
};
|
|
|
|
const CopyJobContextProvider: React.FC<CopyJobContextProviderProps> = (props) => {
|
|
const [copyJobState, setCopyJobState] = React.useState<CopyJobContextState>(getInitialCopyJobState());
|
|
const [flow, setFlow] = React.useState<CopyJobFlowType | null>(null);
|
|
const [contextError, setContextError] = React.useState<string | null>(null);
|
|
|
|
const resetCopyJobState = () => {
|
|
setCopyJobState(getInitialCopyJobState());
|
|
};
|
|
|
|
const contextValue: CopyJobContextProviderType = {
|
|
contextError,
|
|
setContextError,
|
|
copyJobState,
|
|
setCopyJobState,
|
|
flow,
|
|
setFlow,
|
|
resetCopyJobState,
|
|
explorer: props.explorer,
|
|
};
|
|
|
|
return <CopyJobContext.Provider value={contextValue}>{props.children}</CopyJobContext.Provider>;
|
|
};
|
|
|
|
export default CopyJobContextProvider;
|