All files / src/components RegisterDialog.tsx

100% Statements 21/21
87.5% Branches 14/16
100% Functions 6/6
100% Lines 18/18

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 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 1394x                                                                                     24x 24x     24x         24x 24x 24x 11x 1x   11x 11x 11x 11x 11x 11x       24x       11x                     26x                                                                                                                
"use client";
 
/**
 * RegisterDialog — a lightweight modal around the wallet approval. Used by the
 * homepage "Register with d-you" CTA and the /patient "Request EHR data"
 * button. No dialog dependency — plain Tailwind overlay with ESC/backdrop close,
 * role="dialog" + aria-modal, and body-scroll lock. z-70 sits above the nav
 * (z-50) and UserMenu dropdown.
 *
 * On the hub, the citizen wallet's register and login run for real
 * (`WalletSignInFlow`): the QR code is the verifier's request and the
 * simulated phone signs in as the demo patient, both with a session, landing
 * on `callbackUrl`. The ePA transfer, and everything in the static export,
 * stays the simulation of `EudiApprovalFlow`, which calls `onComplete`.
 */
import { useEffect, useState } from "react";
import { createPortal } from "react-dom";
import { X, ShieldCheck, ScanLine } from "lucide-react";
import { useT } from "@/lib/i18n/translate";
import {
  EudiApprovalFlow,
  type ApprovalMode,
} from "@/components/wallet/EudiApprovalFlow";
import { WalletSignInFlow } from "@/components/wallet/WalletSignInFlow";
import { IS_STATIC } from "@/lib/static-export";
 
export function RegisterDialog({
  mode = "register",
  title,
  subtitle,
  callbackUrl = "/patient",
  onClose,
  onComplete,
}: {
  mode?: ApprovalMode;
  title: string;
  subtitle?: string;
  /** where a real wallet sign-in lands (on the hub, register and login only) */
  callbackUrl?: string;
  onClose: () => void;
  /** the simulation's end: the ePA transfer, and the static export */
  onComplete: () => void;
}) {
  const t = useT("patient");
  const tc = useT("common");
  // Eyebrow label per approval mode, so the dialog's intent is unmistakable.
  const eyebrow =
    mode === "register"
      ? t("European Health Dataspace · Registration")
      : mode === "login"
        ? t("European Health Dataspace · Sign in")
        : t("Electronic Patient Record · ePA transfer");
  const [mounted, setMounted] = useState(false);
  useEffect(() => setMounted(true), []);
  useEffect(() => {
    const onKey = (e: KeyboardEvent) => {
      Eif (e.key === "Escape") onClose();
    };
    window.addEventListener("keydown", onKey);
    const prev = document.body.style.overflow;
    document.body.style.overflow = "hidden";
    return () => {
      window.removeEventListener("keydown", onKey);
      document.body.style.overflow = prev;
    };
  }, [onClose]);
 
  if (!mounted) return null;
 
  // Portal to <body> so the modal escapes the page's stacking context and sits
  // above the sticky nav (z-50) — true "always on top".
  return createPortal(
    <div
      role="dialog"
      aria-modal="true"
      aria-label={title}
      className="fixed inset-0 z-100 flex items-center justify-center p-4"
      onClick={onClose}
    >
      <div className="absolute inset-0 bg-black/75 backdrop-blur-md" />
      <div
        className="relative z-10 w-full max-w-3xl rounded-2xl border-2 border-(--border) bg-(--surface) ring-1 ring-black/10 shadow-[0_24px_70px_rgba(0,0,0,0.45)] p-6 sm:p-8 max-h-[92vh] overflow-y-auto"
        onClick={(e) => e.stopPropagation()}
      >
        <button
          type="button"
          onClick={onClose}
          aria-label={tc("Close")}
          className="absolute top-3 right-3 grid place-items-center w-9 h-9 rounded-full text-(--text-secondary) hover:bg-(--surface-2) transition-colors z-10"
        >
          <X size={18} />
        </button>
 
        {/* Branded header — makes the registration intent unmistakable */}
        <div className="flex flex-col items-center text-center">
          <span
            className="inline-flex items-center gap-1.5 text-[11px] font-bold uppercase tracking-[0.12em] px-3 py-1 rounded-full mb-3"
            style={{
              background: "rgba(36,113,163,0.12)",
              color: "var(--accent)",
            }}
          >
            <ShieldCheck size={13} /> {eyebrow}
          </span>
          <h2 className="text-2xl font-extrabold text-(--text-primary) leading-tight">
            {title}
          </h2>
          {subtitle && (
            <p className="text-sm text-(--text-secondary) mt-1.5 max-w-md mx-auto">
              {subtitle}
            </p>
          )}
          <span className="inline-flex items-center gap-1.5 mt-3 text-xs font-semibold text-(--text-secondary)">
            <ScanLine size={13} />{" "}
            {t("Scan the QR or approve on your phone, no password")}
          </span>
        </div>
 
        <div className="mt-5">
          {!IS_STATIC && mode !== "ehr" ? (
            <WalletSignInFlow
              mode={mode}
              callbackUrl={callbackUrl}
              onCancel={onClose}
            />
          ) : (
            <EudiApprovalFlow
              mode={mode}
              onComplete={onComplete}
              onCancel={onClose}
            />
          )}
        </div>
      </div>
    </div>,
    document.body,
  );
}