All files / src/utils useTimeoutWarning.ts

83.33% Statements 25/30
66.66% Branches 12/18
81.81% Functions 9/11
86.2% Lines 25/29

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94          3x       192x     192x 192x 86x       192x           192x             278x 205x   73x             119x                                   119x   119x 239x 239x 19x   220x 76x   144x     119x 205x     119x 119x     119x         119x                
import type { Ref } from "vue";
 
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
 
/** Show the warning banner in the last minute before timeout (ZMSKVR-501). */
export const TIMEOUT_WARNING_WINDOW_MS = 60_000;
 
/** Ticks once per second so countdown text updates without a page reload. */
export function useNowTicker(intervalMs = 1000): Ref<number> {
  const nowMs = ref(Date.now());
  let timer: number | undefined;
 
  onMounted(() => {
    timer = window.setInterval(() => {
      nowMs.value = Date.now();
    }, intervalMs);
  });
 
  onBeforeUnmount(() => {
    if (timer !== undefined) {
      window.clearInterval(timer);
    }
  });
 
  return nowMs;
}
 
export function remainingMsUntil(
  deadlineMs: number | null | undefined,
  nowMs: number
): number | null {
  if (deadlineMs == null || !Number.isFinite(deadlineMs)) {
    return null;
  }
  return Math.max(0, deadlineMs - nowMs);
}
 
/** True while time is left and within the last 60 seconds. */
export function isInTimeoutWarningWindow(
  remainingMs: number | null | undefined
): boolean {
  return (
    remainingMs != null &&
    remainingMs > 0 &&
    remainingMs <= TIMEOUT_WARNING_WINDOW_MS
  );
}
 
/**
 * Active deadline for the timeout warning banner:
 * - Termin (view 1): captcha JWT expiry
 * - Kontakt / Übersicht (views 2–3): reservation end
 * Banner is hidden on Leistung and after booking confirmation.
 */
export function useTimeoutWarning(options: {
  currentView: Ref<number>;
  captchaDeadlineMs: Ref<number | null>;
  reservationDeadlineMs: Ref<number | null>;
}) {
  const nowMs = useNowTicker();
 
  const activeDeadlineMs = computed<number | null>(() => {
    const view = options.currentView.value;
    if (view === 1) {
      return options.captchaDeadlineMs.value;
    }
    if (view === 2 || view === 3) {
      return options.reservationDeadlineMs.value;
    }
    return null;
  });
 
  const remainingMs = computed(() =>
    remainingMsUntil(activeDeadlineMs.value, nowMs.value)
  );
 
  const showTimeoutWarning = computed(() =>
    isInTimeoutWarningWindow(remainingMs.value)
  );
 
  const remainingSeconds = computed(() => {
    if (remainingMs.value == null) return 0;
    return Math.max(1, Math.ceil(remainingMs.value / 1000));
  });
 
  return {
    nowMs,
    activeDeadlineMs,
    remainingMs,
    remainingSeconds,
    showTimeoutWarning,
  };
}