All files / src/utils modal-instance.ts

100% Statements 71/71
73.33% Branches 11/15
100% Functions 4/4
100% Lines 71/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 9x 9x 10x 1x 1x 1x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 15x 15x 15x 15x 33x 1x 1x 1x 1x 1x 26x 25x 25x  
/**
 * Modal instance management utilities
 */
 
/**
 * Window interface extension for active modal instance tracking
 */
declare global {
  interface Window {
    __3DS_MODAL_ACTIVE_INSTANCE__?: ThreeDSModalInstance;
  }
}
 
export interface ThreeDSModalInstance {
  readonly instanceId: string;
  close: (result?: { success: boolean; error?: string }) => void;
}
 
/**
 * Cleans up any existing active modal instance
 */
export function cleanupPreviousModal(
  currentInstance: ThreeDSModalInstance,
  log?: (message: string, ...args: unknown[]) => void
): void {
  if (typeof window === 'undefined') return;
 
  const previousModal = window.__3DS_MODAL_ACTIVE_INSTANCE__;
  if (previousModal && previousModal !== currentInstance) {
    log?.(`Cleaning up previous modal instance: ${previousModal.instanceId}`);
    previousModal.close({ success: false, error: 'Replaced by new modal instance' });
  }
}
 
/**
 * Sets a modal instance as the active one
 */
export function setAsActiveModal(
  instance: ThreeDSModalInstance,
  setIsActive?: (value: boolean) => void,
  log?: (message: string, ...args: unknown[]) => void
): void {
  if (typeof window === 'undefined') return;
  window.__3DS_MODAL_ACTIVE_INSTANCE__ = instance;
  setIsActive?.(true);
  log?.(`Set as active modal instance: ${instance.instanceId}`);
}
 
/**
 * Clears a modal instance as active if it's currently active
 */
export function clearActiveModal(
  instance: ThreeDSModalInstance,
  setIsActive?: (value: boolean) => void,
  log?: (message: string, ...args: unknown[]) => void
): void {
  if (typeof window === 'undefined') return;
  if (window.__3DS_MODAL_ACTIVE_INSTANCE__ === instance) {
    delete window.__3DS_MODAL_ACTIVE_INSTANCE__;
    setIsActive?.(false);
    log?.(`Cleared active modal instance: ${instance.instanceId}`);
  }
}
 
/**
 * Checks if a modal instance is still the active one
 */
export function isModalActive(instance: ThreeDSModalInstance): boolean {
  if (typeof window === 'undefined') return true;
  return window.__3DS_MODAL_ACTIVE_INSTANCE__ === instance;
}