File size: 4,778 Bytes
07bbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * useEnhancedDialog Hook
 * 
 * Custom hook for presenting EnhancedDialog using imperative API similar to useIonModal
 */

import { useCallback, useRef, useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import { I18nextProvider } from 'react-i18next';
import i18n from '../../i18n/i18n';
import { EnhancedDialogContent } from './EnhancedDialogContent';
import { EnhancedDialogOptions, DismissRole } from './types';

/**
 * Custom hook for presenting EnhancedDialog using imperative API
 * 
 * @param options - Dialog configuration options
 * @returns [present, dismiss] - Functions to show and hide the dialog
 * 
 * @example
 * ```tsx
 * function MyComponent() {
 *   const [present, dismiss] = useEnhancedDialog({
 *     children: <div>Dialog Content</div>,
 *     onConfirm: () => {
 *       console.log('Confirmed');
 *       dismiss();
 *     },
 *     onCancel: () => {
 *       console.log('Cancelled');
 *       dismiss();
 *     }
 *   });
 * 
 *   return <IonButton onClick={present}>Open Dialog</IonButton>;
 * }
 * ```
 */
export const useEnhancedDialog = (options: EnhancedDialogOptions) => {
  const {
    children,
    className,
    backdropDismiss = true,
    showButtons = true,
    confirmLabel = '确定',
    cancelLabel = '取消',
    danger = false,
    disableKeyboardShortcuts = false,
    onDismiss,
    onConfirm,
    onCancel,
  } = options;

  const containerRef = useRef<HTMLDivElement | null>(null);
  const rootRef = useRef<ReactDOM.Root | null>(null);
  const optionsRef = useRef(options);

  // Update options ref when options change
  useEffect(() => {
    optionsRef.current = options;
  }, [options]);

  /**
   * Dismiss the dialog
   * @param role - The role that triggered the dismiss (confirm, cancel, backdrop)
   */
  const dismiss = useCallback((role?: DismissRole) => {
    const currentRoot = rootRef.current;
    const currentContainer = containerRef.current;

    if (currentRoot && currentContainer) {
      // Call appropriate callbacks first
      onDismiss?.(role);
      if (role === 'confirm') {
        onConfirm?.();
      } else if (role === 'cancel' || role === 'backdrop') {
        onCancel?.();
      }

      // Unmount and cleanup asynchronously to avoid race condition
      setTimeout(() => {
        currentRoot.unmount();
        if (currentContainer.parentNode) {
          document.body.removeChild(currentContainer);
        }
        rootRef.current = null;
        containerRef.current = null;
      }, 0);
    }
  }, [onDismiss, onConfirm, onCancel]);

  /**
   * Present the dialog
   */
  const present = useCallback(() => {
    // If already open, update the content instead of creating new
    if (rootRef.current && containerRef.current) {
      rootRef.current.render(
        <I18nextProvider i18n={i18n}>
          <EnhancedDialogContent
            onDismiss={dismiss}
            children={children}
            showButtons={showButtons}
            confirmLabel={confirmLabel}
            cancelLabel={cancelLabel}
            danger={danger}
            className={className}
            backdropDismiss={backdropDismiss}
            disableKeyboardShortcuts={disableKeyboardShortcuts}
          />
        </I18nextProvider>
      );
      return;
    }

    // Create container
    const newContainer = document.createElement('div');
    newContainer.id = 'enhanced-dialog-container-' + Date.now();
    document.body.appendChild(newContainer);

    // Create root and render
    // Wrap with I18nextProvider to support internationalization in dialog content
    const newRoot = ReactDOM.createRoot(newContainer);
    newRoot.render(
      <I18nextProvider i18n={i18n}>
        <EnhancedDialogContent
          onDismiss={dismiss}
          children={children}
          showButtons={showButtons}
          confirmLabel={confirmLabel}
          cancelLabel={cancelLabel}
          danger={danger}
          className={className}
          backdropDismiss={backdropDismiss}
          disableKeyboardShortcuts={disableKeyboardShortcuts}
        />
      </I18nextProvider>
    );

    containerRef.current = newContainer;
    rootRef.current = newRoot;
  }, [children, className, backdropDismiss, showButtons, confirmLabel, cancelLabel, danger, disableKeyboardShortcuts, dismiss]);


  // Cleanup on unmount
  useEffect(() => {
    return () => {
      const currentRoot = rootRef.current;
      const currentContainer = containerRef.current;
      
      if (currentRoot && currentContainer) {
        currentRoot.unmount();
        if (currentContainer.parentNode) {
          document.body.removeChild(currentContainer);
        }
        rootRef.current = null;
        containerRef.current = null;
      }
    };
  }, []);

  return [present, dismiss] as const;
};