All files / src/components HealthDetailModal.tsx

96.96% Statements 32/33
77.27% Branches 17/22
90% Functions 9/10
96.66% Lines 29/30

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 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267                                                          2x             15x 15x 15x 15x   15x 15x 15x 15x                                     8x 8x 8x 4x 1x   4x 4x 4x 4x 4x 4x       8x   4x                                                                                                         9x 9x 9x 9x 9x                                                                                                                                     15x                                                                     21x                                                      
"use client";
 
/**
 * HealthDetailModal — the clickable detail view for a personal-health dashboard
 * card (fitness / labs / nutrition). Shows ~3-month weekly trends (TrendChart)
 * with an improving/declining chip, plus the weekly plan for nutrition.
 * Synthetic, illustrative data from journey-config.
 */
import { useEffect, useState } from "react";
import { createPortal } from "react-dom";
import {
  X,
  TrendingUp,
  TrendingDown,
  CalendarDays,
  Target,
  Check,
  Leaf,
  Wheat,
  Drumstick,
} from "lucide-react";
import { TrendChart } from "@/components/charts/TrendChart";
import type {
  PersonalHealthSource,
  TrendSeries,
  WeeklyGoal,
} from "@/lib/journey-config";
 
/** Maps a weekly-goal icon key to its lucide glyph. */
const GOAL_ICON: Record<WeeklyGoal["icon"], typeof Leaf> = {
  plants: Leaf,
  fibre: Wheat,
  protein: Drumstick,
};
 
function deltaChip(s: TrendSeries) {
  const first = s.points[0];
  const last = s.points[s.points.length - 1];
  const rising = last >= first;
  const improving = rising === (s.goodDirection === "up");
  const pct =
    first === 0 ? 0 : Math.round(((last - first) / Math.abs(first)) * 100);
  const Icon = rising ? TrendingUp : TrendingDown;
  const colour = improving ? "#1E8449" : "#CA6F1E";
  return (
    <span
      className="inline-flex items-center gap-1 text-xs font-semibold px-2 py-0.5 rounded-full"
      style={{ color: colour, background: `${colour}1a` }}
    >
      <Icon size={12} />
      {pct > 0 ? "+" : ""}
      {pct}% · 3 mo
    </span>
  );
}
 
export function HealthDetailModal({
  source,
  onClose,
}: {
  source: PersonalHealthSource;
  onClose: () => void;
}) {
  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;
 
  return createPortal(
    <div
      role="dialog"
      aria-modal="true"
      aria-label={`${source.title} detail`}
      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-2xl rounded-2xl border-2 border-[var(--border)] bg-[var(--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="Close"
          className="absolute top-3 right-3 grid place-items-center w-9 h-9 rounded-full text-[var(--text-secondary)] hover:bg-[var(--surface-2)] transition-colors"
        >
          <X size={18} />
        </button>
 
        <div className="flex items-center gap-3 mb-1 pr-8">
          <span
            className="w-2.5 h-8 rounded-full shrink-0"
            style={{ background: source.brand }}
          />
          <div>
            <h2 className="text-xl font-extrabold text-[var(--text-primary)] leading-tight">
              {source.title}
            </h2>
            <p className="text-xs text-[var(--text-secondary)]">
              {source.source}
            </p>
          </div>
        </div>
        <p className="text-sm text-[var(--text-secondary)] mb-5">
          {source.detail}
        </p>
 
        {/* Weekly nutrition goals — plant diversity / fibre / protein */}
        {source.weeklyGoals && (
          <div className="mb-5">
            <p className="flex items-center gap-1.5 text-sm font-bold text-[var(--text-primary)] mb-2">
              <Target
                size={15}
                aria-hidden="true"
                style={{ color: source.brand }}
              />
              Weekly goals
            </p>
            <div className="grid grid-cols-1 sm:grid-cols-3 gap-2">
              {source.weeklyGoals.map((g) => {
                const Icon = GOAL_ICON[g.icon];
                const met = g.current >= g.target;
                const ratio = Math.min(1, g.current / g.target);
                const accent = met ? "#1E8449" : "#CA6F1E";
                return (
                  <div
                    key={g.label}
                    className="rounded-xl border border-[var(--border)] bg-[var(--surface-2)] p-3"
                  >
                    <div className="flex items-center justify-between mb-1">
                      <span className="inline-flex items-center gap-1.5 text-xs font-bold text-[var(--text-primary)]">
                        <Icon
                          size={14}
                          aria-hidden="true"
                          style={{ color: accent }}
                        />
                        {g.label}
                      </span>
                      {/* Status chip in BOTH states — text cue, not colour alone (WCAG 1.4.1) */}
                      <span
                        className="inline-flex items-center gap-0.5 text-[10px] font-semibold px-1.5 py-0.5 rounded-full whitespace-nowrap"
                        style={{ color: accent, background: `${accent}1a` }}
                      >
                        {met ? (
                          <>
                            <Check size={10} aria-hidden="true" /> met
                          </>
                        ) : (
                          `${g.target - g.current} to go`
                        )}
                      </span>
                    </div>
                    <div className="flex items-baseline gap-1">
                      <span className="text-lg font-black text-[var(--text-primary)] tabular-nums">
                        {g.current}
                      </span>
                      <span className="text-xs text-[var(--text-secondary)]">
                        / {g.target} {g.unit}
                      </span>
                    </div>
                    <div
                      role="progressbar"
                      aria-valuenow={Math.min(g.current, g.target)}
                      aria-valuemin={0}
                      aria-valuemax={g.target}
                      aria-valuetext={`${g.current} of ${g.target} ${g.unit}`}
                      aria-label={`${g.label}: ${g.current} of ${g.target} ${
                        g.unit
                      } — ${
                        met ? "goal met" : `${g.target - g.current} to go`
                      }`}
                      className="h-1.5 rounded-full bg-[var(--border)] mt-2 overflow-hidden"
                    >
                      <div
                        className="h-full rounded-full transition-all"
                        style={{ width: `${ratio * 100}%`, background: accent }}
                      />
                    </div>
                    <p className="text-[10px] text-[var(--text-secondary)] mt-1">
                      {g.note}
                    </p>
                  </div>
                );
              })}
            </div>
          </div>
        )}
 
        {/* 3-month weekly trends */}
        <div className="space-y-4">
          {source.trends.map((s) => (
            <div
              key={s.label}
              className="rounded-xl border border-[var(--border)] bg-[var(--surface-2)] p-4"
            >
              <div className="flex items-baseline justify-between gap-3 mb-2">
                <div className="flex items-baseline gap-2">
                  <span className="text-sm font-bold text-[var(--text-primary)]">
                    {s.label}
                  </span>
                  <span className="text-lg font-black text-[var(--text-primary)]">
                    {s.current}
                  </span>
                  <span className="text-xs text-[var(--text-secondary)]">
                    {s.unit}
                  </span>
                </div>
                {deltaChip(s)}
              </div>
              <TrendChart points={s.points} color={s.color} />
              <p className="text-[10px] text-[var(--text-secondary)] mt-1 text-right">
                last 12 weeks
              </p>
            </div>
          ))}
        </div>
 
        {/* Nutrition weekly plan */}
        {source.weeklyPlan && (
          <div className="mt-5">
            <p className="flex items-center gap-1.5 text-sm font-bold text-[var(--text-primary)] mb-2">
              <CalendarDays size={15} style={{ color: source.brand }} />
              This week&rsquo;s plan
            </p>
            <div className="grid grid-cols-1 sm:grid-cols-2 gap-2">
              {source.weeklyPlan.map((d) => (
                <div
                  key={d.day}
                  className="flex items-center justify-between gap-3 rounded-lg border border-[var(--border)] bg-[var(--surface-2)] px-3 py-2"
                >
                  <span className="text-xs font-bold text-[var(--text-primary)] w-9 shrink-0">
                    {d.day}
                  </span>
                  <span className="text-xs text-[var(--text-secondary)] flex-1 truncate">
                    {d.focus}
                  </span>
                  <span className="text-xs font-semibold text-[var(--text-primary)] tabular-nums">
                    {d.kcal.toLocaleString()} kcal
                  </span>
                </div>
              ))}
            </div>
          </div>
        )}
 
        <p className="text-[11px] text-[var(--text-secondary)] mt-5 text-center">
          Synthetic · illustrative — not medical advice.
        </p>
      </div>
    </div>,
    document.body,
  );
}