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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | 2x 2x 50x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 31x 28x 27x 2x 2x 2x 2x 25x 25x 27x 27x 27x 117x 25x 25x 25x 24x 25x 1x 117x 26x 117x 24x 117x 1x 1x 1x 1x 1x 1x 1x 1x 1x 117x 4x 4x 4x 4x 4x 4x 4x 4x 117x 1x 1x 1x 1x 1x 1x 1x 117x 67x 117x 25x 91x 91x 1x 1x 1x 4x 49x | "use client";
import { fetchApi } from "@/lib/api";
import { useEffect, useState } from "react";
import {
FlaskConical,
CheckCircle2,
XCircle,
Shield,
ShieldOff,
} from "lucide-react";
import PageIntro from "@/components/PageIntro";
import { useT } from "@/lib/i18n/translate";
import { useOwnRecord } from "@/lib/patient/use-own-record";
interface Program {
studyId: string;
studyName: string;
institution: string;
purpose: string;
description: string;
dataNeeded: string;
status: string;
}
interface Consent {
consentId: string;
studyId: string;
grantedAt: string;
revoked: boolean;
purpose: string;
}
interface OptOutRecord {
optOutId: string;
optedOutAt: string;
source: string | null;
reversedAt: string | null;
}
interface OptOutState {
optedOut: boolean;
since: string | null;
history: OptOutRecord[];
}
/**
* The record an operator (EDC_ADMIN) sees here: P1, the demo cohort's first
* patient, which every seed carries. A patient sees and consents for their
* own: the route refuses any other record with a 403 (#475).
*/
const DEMO_PATIENT_ID = "P1";
/** Why the programmes did not load: refused (401, 403) or failed (anything else). */
type LoadError =
| { kind: "refused" }
| { kind: "failed"; status: number | null };
const day = (iso: string | null) => (iso ? iso.slice(0, 10) : "");
export default function ResearchProgramsPage() {
const t = useT("patient");
const tn = useT("nav");
const [programs, setPrograms] = useState<Program[]>([]);
const [consents, setConsents] = useState<Consent[]>([]);
const [loading, setLoading] = useState(true);
const [donating, setDonating] = useState<string | null>(null);
const [revoking, setRevoking] = useState<string | null>(null);
const [message, setMessage] = useState<string | null>(null);
const [optOut, setOptOut] = useState<OptOutState | null>(null);
const [optOutError, setOptOutError] = useState<string | null>(null);
const [switching, setSwitching] = useState(false);
const [loadError, setLoadError] = useState<LoadError | null>(null);
const subject = useOwnRecord();
const patientId =
subject.kind === "own"
? subject.patientId
: subject.kind === "operator"
? DEMO_PATIENT_ID
: null;
const reload = () => {
if (subject.kind === "pending") return;
fetchApi(
patientId
? `/api/patient/research?patientId=${encodeURIComponent(patientId)}`
: "/api/patient/research",
)
.then(async (r) => {
// A refusal or an error is not an empty list: say which it is.
if (!r.ok) {
setPrograms([]);
setConsents([]);
setLoadError(
r.status === 401 || r.status === 403
? { kind: "refused" }
: { kind: "failed", status: r.status },
);
return;
}
const d = await r.json();
setPrograms(d.programs ?? []);
setConsents(d.consents ?? []);
setLoadError(null);
})
.catch(() => setLoadError({ kind: "failed", status: null }))
.finally(() => setLoading(false));
};
// The opt-out is the signed-in patient's own (Art. 71, ADR-062): the route
// takes the record from the session, so no patientId is sent.
const loadOptOut = () => {
fetchApi("/api/patient/opt-out")
.then(async (r) => {
const d = await r.json();
if (!r.ok) throw new Error(d.error ?? `HTTP ${r.status}`);
setOptOut({
optedOut: Boolean(d.optedOut),
since: d.since ?? null,
history: Array.isArray(d.history) ? d.history : [],
});
setOptOutError(null);
})
.catch((e: Error) => setOptOutError(e.message));
};
// The programmes wait for the session, so they ask for the right record.
useEffect(() => {
reload();
}, [subject.kind, patientId]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
loadOptOut();
}, []); // eslint-disable-line react-hooks/exhaustive-deps
const switchOptOut = async () => {
Iif (!optOut) return;
setSwitching(true);
setMessage(null);
try {
const r = await fetchApi("/api/patient/opt-out", {
method: optOut.optedOut ? "DELETE" : "POST",
headers: { "Content-Type": "application/json" },
body: optOut.optedOut
? undefined
: JSON.stringify({ source: "patient-portal" }),
});
const d = await r.json();
setMessage(
d.message ??
(optOut.optedOut
? t("Your opt-out is reversed.")
: t("You have opted out of secondary use.")),
);
loadOptOut();
} finally {
setSwitching(false);
}
};
const donate = async (studyId: string) => {
setDonating(studyId);
setMessage(null);
try {
const r = await fetchApi("/api/patient/research", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ patientId, studyId }),
});
const d = await r.json();
setMessage(d.message ?? t("EHR donation registered."));
reload();
} finally {
setDonating(null);
}
};
const revoke = async (consent: Consent) => {
setRevoking(consent.consentId);
setMessage(null);
try {
await fetchApi(
`/api/patient/research?consentId=${encodeURIComponent(
consent.consentId,
)}&patientId=${encodeURIComponent(patientId ?? "")}`,
{ method: "DELETE" },
);
setMessage(
t("Consent withdrawn. Nothing new of yours reaches this study."),
);
reload();
} finally {
setRevoking(null);
}
};
const consentedStudyIds = new Set(
consents.filter((c) => !c.revoked).map((c) => c.studyId),
);
return (
<div className="min-h-screen bg-(--bg)">
<div className="max-w-3xl mx-auto px-6 py-10">
<PageIntro
title={tn("Research Programs")}
icon={FlaskConical}
description={t(
"Decide about the secondary use of your health data (EHDS Art. 71) and, separately, which studies you join. Joining a study is your consent (GDPR Art. 9(2)(a)); you can withdraw it at any time (GDPR Art. 7(3)).",
)}
prevStep={{ href: "/patient/profile", label: t("Health Profile") }}
nextStep={{
href: "/patient/insights",
label: tn("Research Insights"),
}}
infoText={t(
"Your data is pseudonymised by the Trust Center before reaching researchers. Individual-level data never leaves the Secure Processing Environment (EHDS Art. 73).",
)}
docLink={{
href: "/docs/user-guide",
label: t("Patient Rights Guide"),
}}
/>
{/* The Art. 71 opt-out from all secondary use (ADR-062) */}
<section
data-testid="opt-out-panel"
aria-labelledby="opt-out-heading"
className="mb-6 rounded-lg border border-teal-700 bg-teal-900/20 p-4"
>
<div className="flex items-start gap-2">
{optOut?.optedOut ? (
<ShieldOff
size={16}
className="mt-0.5 shrink-0 text-amber-800 dark:text-amber-400"
/>
) : (
<Shield
size={16}
className="mt-0.5 shrink-0 text-teal-800 dark:text-teal-400"
/>
)}
<div className="text-xs text-teal-900 dark:text-teal-200 space-y-2">
<h2 id="opt-out-heading" className="text-sm font-semibold">
{t("Secondary use of your health data (EHDS Art. 71)")}
</h2>
<p>
{t(
"Your data may be used for research, innovation, policy and statistics, but only under a permit from a health data access body, pseudonymised, inside a secure processing environment. You can opt out at any time, without giving a reason, and opt back in. The opt-out applies to every new extraction; what a permit received before it is not recalled.",
)}
</p>
{optOut ? (
<>
<p data-testid="opt-out-state" className="font-medium">
{optOut.optedOut
? t("You opted out on {date}.", {
date: day(optOut.since),
})
: t(
"You have not opted out: secondary use under a permit applies.",
)}
</p>
<button
type="button"
data-testid="opt-out-switch"
onClick={switchOptOut}
disabled={switching}
className={`px-3 py-1.5 rounded-sm text-white text-xs font-medium transition-colors disabled:opacity-50 ${
optOut.optedOut
? "bg-teal-700 hover:bg-teal-600"
: "bg-amber-700 hover:bg-amber-600"
}`}
>
{switching
? t("Saving…")
: optOut.optedOut
? t("Opt back in")
: t("Opt out of secondary use")}
</button>
{optOut.history.length > 0 && (
<ul className="list-disc list-inside text-(--text-secondary)">
{optOut.history.map((h) => (
<li key={h.optOutId}>
{h.reversedAt
? t("Opted out {date}, back in {back}", {
date: day(h.optedOutAt),
back: day(h.reversedAt),
})
: t("Opted out {date}, still in effect", {
date: day(h.optedOutAt),
})}
</li>
))}
</ul>
)}
</>
) : (
<p className="text-(--text-secondary)">
{optOutError
? t(
"The opt-out is available to a signed-in patient ({error}).",
{ error: optOutError },
)
: t("Loading your opt-out…")}
</p>
)}
<p className="text-(--text-secondary)">
{t(
"Joining a study below is a separate choice: your consent to that study (GDPR Art. 9(2)(a)), which you can withdraw at any time (GDPR Art. 7(3)).",
)}
</p>
</div>
</div>
</section>
{message && (
<div className="mb-4 rounded-lg bg-green-100 dark:bg-green-900/30 border border-green-300 dark:border-green-700 p-3 text-sm text-green-800 dark:text-green-300">
{message}
</div>
)}
{loading ? (
<div className="text-(--text-secondary) text-sm">
{t("Loading research programs…")}
</div>
) : loadError ? (
<div
role="alert"
data-testid="research-load-error"
className="rounded-lg border border-amber-300 dark:border-amber-700 bg-amber-50 dark:bg-amber-900/20 p-3 text-sm text-amber-900 dark:text-amber-200"
>
{loadError.kind === "refused"
? t(
"You may not see these research programs: a patient sees and consents for their own record only. If your session has ended, sign in again.",
)
: loadError.status
? t(
"The research programs could not be loaded (HTTP {status}). Please try again later.",
{ status: loadError.status },
)
: t(
"The research programs could not be loaded. Please try again later.",
)}
</div>
) : (
<div className="space-y-4">
<h2 className="text-lg font-semibold">
{t("Available Research Programs ({n})", { n: programs.length })}
</h2>
{programs.length === 0 && (
<div className="text-(--text-secondary) text-sm">
{t(
"No research programs found. Seed the JAD stack to populate.",
)}
</div>
)}
{programs.map((prog) => {
const isConsented = consentedStudyIds.has(prog.studyId);
return (
<div
key={prog.studyId}
data-testid="research-program-card"
className={`rounded-xl border p-4 ${
isConsented
? "border-teal-300 dark:border-teal-700 bg-teal-50 dark:bg-teal-900/10"
: "border-(--border) bg-(--surface-2)/50"
}`}
>
<div className="flex items-start justify-between mb-2">
<div>
<div className="font-semibold text-sm">
{prog.studyName}
</div>
<div className="text-xs text-(--text-secondary)">
{prog.institution}
</div>
</div>
{isConsented ? (
<span className="flex items-center gap-1 text-xs text-teal-800 dark:text-teal-400 font-medium">
<CheckCircle2 size={12} /> {t("Donated")}
</span>
) : (
<span className="text-xs text-(--text-secondary) border border-gray-600 rounded-sm px-2 py-0.5">
{prog.status}
</span>
)}
</div>
<p className="text-xs text-(--text-secondary) mb-2">
{prog.description}
</p>
<div className="text-xs text-(--text-secondary) mb-3">
<span className="text-gray-700 dark:text-gray-400">
{t("Data needed:")}{" "}
</span>
{prog.dataNeeded}
</div>
{isConsented ? (
<button
onClick={() => {
const consent = consents.find(
(c) => c.studyId === prog.studyId && !c.revoked,
);
Eif (consent) revoke(consent);
}}
disabled={revoking !== null}
className="flex items-center gap-1.5 text-xs text-red-800 dark:text-red-400 hover:text-red-900 dark:hover:text-red-300 transition-colors disabled:opacity-50"
>
<XCircle size={12} />
{revoking
? t("Withdrawing…")
: t("Withdraw consent (GDPR Art. 7(3))")}
</button>
) : (
<button
onClick={() => donate(prog.studyId)}
disabled={donating !== null}
className="px-3 py-1.5 rounded-sm bg-teal-700 hover:bg-teal-600 text-white text-xs font-medium transition-colors disabled:opacity-50"
>
{donating === prog.studyId
? t("Registering…")
: t("Donate my EHR to this study")}
</button>
)}
</div>
);
})}
{/* Consent history */}
{consents.length > 0 && (
<div className="mt-8">
<h2 className="text-lg font-semibold mb-3">
{t("Consent History")}
</h2>
<table className="text-xs w-full border-collapse">
<thead>
<tr className="border-b border-(--border) text-(--text-secondary)">
<th className="text-left pb-1">{t("Study")}</th>
<th className="text-left pb-1">{t("Granted")}</th>
<th className="text-left pb-1">{t("Status")}</th>
</tr>
</thead>
<tbody>
{consents.map((c) => (
<tr
key={c.consentId}
className="border-b border-(--border)"
>
<td className="py-1.5 pr-3 font-mono">{c.studyId}</td>
<td className="py-1.5 pr-3 text-(--text-secondary)">
{c.grantedAt?.slice(0, 10) || "—"}
</td>
<td className="py-1.5">
{c.revoked ? (
<span className="text-red-800 dark:text-red-400">
{t("Revoked")}
</span>
) : (
<span className="text-teal-800 dark:text-teal-400">
{t("Active")}
</span>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
)}
</div>
</div>
);
}
|