fix(app): harden account and workflow state
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { createContext, useCallback, useContext, useMemo, useState } from "react";
|
||||
import React, { createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
|
||||
import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, Typography } from "@mui/material";
|
||||
|
||||
type ConfirmOptions = {
|
||||
@@ -15,12 +15,12 @@ type ConfirmContextValue = {
|
||||
|
||||
type ConfirmState = ConfirmOptions & {
|
||||
open: boolean;
|
||||
resolver?: (value: boolean) => void;
|
||||
};
|
||||
|
||||
const ConfirmContext = createContext<ConfirmContextValue | null>(null);
|
||||
|
||||
export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
||||
const resolverRef = useRef<((value: boolean) => void) | null>(null);
|
||||
const [state, setState] = useState<ConfirmState>({
|
||||
open: false,
|
||||
message: "",
|
||||
@@ -31,14 +31,15 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
||||
});
|
||||
|
||||
const closeWith = useCallback((value: boolean) => {
|
||||
setState((prev) => {
|
||||
prev.resolver?.(value);
|
||||
return { ...prev, open: false, resolver: undefined };
|
||||
});
|
||||
const resolve = resolverRef.current;
|
||||
resolverRef.current = null;
|
||||
setState((prev) => ({ ...prev, open: false }));
|
||||
resolve?.(value);
|
||||
}, []);
|
||||
|
||||
const confirm = useCallback((options: ConfirmOptions) => {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
resolverRef.current = resolve;
|
||||
setState({
|
||||
open: true,
|
||||
title: options.title ?? "Confirm action",
|
||||
@@ -46,7 +47,6 @@ export function ConfirmProvider({ children }: { children: React.ReactNode }) {
|
||||
confirmLabel: options.confirmLabel ?? "Confirm",
|
||||
cancelLabel: options.cancelLabel ?? "Cancel",
|
||||
destructive: options.destructive ?? false,
|
||||
resolver: resolve,
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user