feat: auto-close modals when success notification appears

- Add closeOthersSignal to success notification store
- Create useCloseOnSuccessNotification hook for modals to subscribe
- Update TopUpModal to auto-close on success events
- Update InsufficientBalancePrompt to auto-close on success events
- Update Subscription page to close all modals/forms on success events

This ensures that when a WebSocket event indicates successful payment
or subscription purchase, all related modals close automatically,
providing clear feedback to users.
This commit is contained in:
c0mrade
2026-01-30 19:54:29 +03:00
parent 175b5608b2
commit 0be1ef0340
4 changed files with 57 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
import { useEffect } from 'react';
import { create } from 'zustand';
export type SuccessNotificationType =
@@ -25,6 +26,8 @@ export interface SuccessNotificationData {
interface SuccessNotificationState {
isOpen: boolean;
data: SuccessNotificationData | null;
/** Signal that increments when other modals should close */
closeOthersSignal: number;
show: (data: SuccessNotificationData) => void;
hide: () => void;
@@ -33,7 +36,30 @@ interface SuccessNotificationState {
export const useSuccessNotification = create<SuccessNotificationState>((set) => ({
isOpen: false,
data: null,
closeOthersSignal: 0,
show: (data) => set({ isOpen: true, data }),
show: (data) =>
set((state) => ({
isOpen: true,
data,
// Increment signal to tell other modals to close
closeOthersSignal: state.closeOthersSignal + 1,
})),
hide: () => set({ isOpen: false, data: null }),
}));
/**
* Hook that calls onClose when a success notification appears.
* Use this in modals that should auto-close on success events.
*/
export function useCloseOnSuccessNotification(onClose: () => void) {
const closeOthersSignal = useSuccessNotification((state) => state.closeOthersSignal);
useEffect(() => {
// Skip the initial render (signal = 0)
if (closeOthersSignal > 0) {
onClose();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [closeOthersSignal]);
}