(component: React.ComponentType }>, properties: P): Promise;
isActive(): boolean;
}
export const DialogsContext = React.createContext(null);
export const useDialogs = () => {
const dialogs = useContext(DialogsContext);
if (dialogs === null) {
throw new Error("useDialogs can only be called inside of ");
}
return dialogs;
};
export const WithDialogs = ({ children } : { children: React.ReactNode }) => {
const [dialog, setDialog] = useState(null);
type State = "close" | "show" | "run";
const shown = useRef("close");
function transition(expected: State, to: State, arg: React.ReactNode = null) {
if (shown.current !== expected)
throw new Error(`Dialogs.${to}(${JSON.stringify(arg)}) called, but that's only valid ` +
`after .${expected}(), current dialog is ${JSON.stringify(dialog)}.`);
shown.current = to;
setDialog(arg);
}
const Dialogs: Dialogs = {
show: (component: React.ReactNode) => transition("close", "show", component),
close: () => transition("show", "close"),
run: async (component, props) => {
try {
return await new Promise((resolve, reject) => {
transition("close", "run",
React.createElement(component, { ...props, dialogResult: { resolve, reject } }));
});
} finally {
transition("run", "close");
}
},
isActive: () => dialog !== null
};
return (
{children}
{dialog}
);
};