better queue handling for the confirm modal

This commit is contained in:
Nate Kelley 2025-09-30 11:18:24 -06:00
parent ed80370bc6
commit eeb4ffe8ba
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
1 changed files with 19 additions and 6 deletions

View File

@ -26,12 +26,27 @@ interface QueuedModal<T = unknown> extends Omit<ConfirmProps<T>, 'onOk' | 'onCan
onClose: () => void; onClose: () => void;
onOk: () => Promise<void>; onOk: () => Promise<void>;
onCancel?: () => Promise<void>; onCancel?: () => Promise<void>;
closing?: boolean;
} }
export const useConfirmModalContext = () => { export const useConfirmModalContext = () => {
const [modalQueue, setModalQueue] = useState<QueuedModal[]>([]); const [modalQueue, setModalQueue] = useState<QueuedModal[]>([]);
const currentModal = modalQueue[0]; // Get the first modal in the queue const currentModal = modalQueue[0]; // Get the first modal in the queue
// Common handler to close modal and then remove from queue after animation
const closeModalWithDelay = () => {
// Mark modal as closing (triggers close animation)
setModalQueue((prev) => {
if (prev.length === 0) return prev;
return [{ ...prev[0], closing: true }, ...prev.slice(1)];
});
// Remove from queue after animation completes
setTimeout(() => {
setModalQueue((prev) => prev.slice(1));
}, 250);
};
const openConfirmModal = <T = unknown, C = unknown>( const openConfirmModal = <T = unknown, C = unknown>(
props: ConfirmProps<T> props: ConfirmProps<T>
): Promise<T | undefined> | T => { ): Promise<T | undefined> | T => {
@ -47,8 +62,7 @@ export const useConfirmModalContext = () => {
} catch (error) { } catch (error) {
reject(error); reject(error);
} finally { } finally {
// Remove the current modal from the queue closeModalWithDelay();
setModalQueue((prev) => prev.slice(1));
} }
}, },
onCancel: onCancel:
@ -60,14 +74,13 @@ export const useConfirmModalContext = () => {
} catch (error) { } catch (error) {
reject(error); reject(error);
} finally { } finally {
// Remove the current modal from the queue closeModalWithDelay();
setModalQueue((prev) => prev.slice(1));
} }
} }
: undefined, : undefined,
onClose: () => { onClose: () => {
resolve(undefined); resolve(undefined);
setModalQueue((prev) => prev.slice(1)); closeModalWithDelay();
}, },
}; };
@ -79,7 +92,7 @@ export const useConfirmModalContext = () => {
return currentModal return currentModal
? { ? {
...currentModal, ...currentModal,
open: true, open: !currentModal.closing,
} }
: { : {
...defaultConfirmModalProps, ...defaultConfirmModalProps,