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 | 3x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 3x 3x 3x 1x 69x 14x 69x 69x 69x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 1x 1x 2x 2x 2x 3x 6x 6x 6x 69x 8x 5x 69x 8x 2x 2x 136x 1x 1x 1x | "use client";
import { useCallback, useEffect, useState } from "react";
import Link from "next/link";
import {
AlertTriangle,
Box,
CheckCircle2,
Database,
Loader2,
} from "lucide-react";
import { fetchApi } from "@/lib/api";
import {
DEFAULT_IMPORT_SPE,
IMPORT_SPES,
importViewUrl,
type ExistingImport,
type SpeImportResult,
} from "@/lib/spe-import";
import { useT } from "@/lib/i18n/translate";
/**
* Ends an open transfer in a secure processing environment (#876): the user
* picks the SPE (SPE-1 unless they choose another), the hub imports the
* transfer's data into that workspace's graph, and the result links to the
* 3D overview of the import. A transfer an SPE holds already says so, and a
* second import asks first: replace it in the same SPE, or add it to another.
*/
export default function SpeImport({
transferId,
participantId,
known,
onImported,
onRestart,
}: {
transferId: string;
participantId: string;
/** What the page already knows the SPEs hold; without it the row asks itself. */
known?: ExistingImport[];
/** After an import, so the page can move the row to its new stage. */
onImported?: () => void;
/** Starts a new transfer under the same contract, when the access expired. */
onRestart?: () => void;
}) {
const t = useT("research");
const [speId, setSpeId] = useState<string>(DEFAULT_IMPORT_SPE);
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const [result, setResult] = useState<SpeImportResult | null>(null);
const [existing, setExisting] = useState<ExistingImport[]>([]);
const [confirming, setConfirming] = useState(false);
const [expired, setExpired] = useState(false);
/** Minutes until a workspace that is starting can take the import (ADR-072). */
const [startingIn, setStartingIn] = useState<number | null>(null);
const loadExisting = useCallback(() => {
fetchApi(`/api/spe-imports?ids=${encodeURIComponent(transferId)}`)
.then((r) => (r.ok ? r.json() : { imports: [] }))
.then((b: { imports?: ExistingImport[] }) =>
setExisting(
(b.imports ?? []).filter((i) => i.datasetId === transferId),
),
)
.catch(() => setExisting([]));
}, [transferId]);
useEffect(() => {
if (known) setExisting(known);
else EloadExisting();
}, [known, loadExisting]);
const inChosen = existing.find((e) => e.speId === speId);
const elsewhere = existing.filter((e) => e.speId !== speId);
const runImport = async (overwrite: boolean) => {
setBusy(true);
setError("");
setExpired(false);
setStartingIn(null);
setResult(null);
setConfirming(false);
try {
const res = await fetchApi(
`/api/transfers/${encodeURIComponent(transferId)}/import`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ participantId, speId, overwrite }),
},
);
const body = (await res.json()) as Partial<SpeImportResult> & {
error?: string;
detail?: string;
existing?: ExistingImport;
expired?: boolean;
spe?: { state?: string; etaSeconds?: number };
};
if (!res.ok) {
// Imported in the meantime: show it and ask, as for a known one.
Iif (body.existing) {
setExisting((cur) => [
...cur.filter((e) => e.speId !== body.existing!.speId),
body.existing!,
]);
setConfirming(true);
return;
}
// A stopped workspace was started for this import (Azure, ADR-072).
if (res.status === 503 && body.spe?.state === "starting") {
setStartingIn(
Math.max(1, Math.ceil((body.spe.etaSeconds ?? 240) / 60)),
);
return;
}
setExpired(!!body.expired);
setError([body.error, body.detail].filter(Boolean).join(": "));
return;
}
// The static build answers every POST with {ok: true}; the link and
// the SPE are still the ones the user chose.
setResult({
datasetId: body.datasetId ?? transferId,
speId: body.speId ?? speId,
records: body.records ?? 0,
relationships: body.relationships ?? 0,
byType: body.byType ?? {},
importedAt: body.importedAt ?? "",
pseudonymised: body.pseudonymised ?? true,
untilStopped: body.untilStopped ?? false,
closed: body.closed ?? false,
graphUrl: body.graphUrl ?? importViewUrl(transferId, speId),
});
loadExisting();
onImported?.();
} catch (err) {
setError(String(err));
} finally {
setBusy(false);
}
};
const onImport = () => {
if (existing.length) setConfirming(true);
else void runImport(false);
};
return (
<div data-testid="spe-import" className="mt-3 space-y-2 text-xs">
{existing.length > 0 && !result && (
<ul data-testid="spe-import-existing" className="space-y-1">
{existing.map((e) => (
<li
key={e.speId}
className="flex flex-wrap items-center gap-2 text-(--text-primary)"
>
<CheckCircle2 size={14} className="text-green-500" />
{t("Imported into {spe} on {date}: {records} records.", {
spe: e.speId,
date: e.importedAt.slice(0, 10),
records: String(e.records),
})}
<Link
href={importViewUrl(transferId, e.speId)}
className="flex items-center gap-1 underline text-(--accent)"
>
<Box size={12} />
{t("Show in 3D")}
</Link>
</li>
))}
</ul>
)}
<div className="flex flex-wrap items-center gap-2">
<label
htmlFor={`spe-${transferId}`}
className="text-(--text-secondary)"
>
{t("Import into")}
</label>
<select
id={`spe-${transferId}`}
data-testid="spe-import-target"
value={speId}
onChange={(e) => {
setSpeId(e.target.value);
setConfirming(false);
}}
disabled={busy}
className="px-2 py-1 rounded border border-(--border) bg-(--surface) text-(--text-primary)"
>
{IMPORT_SPES.map((s) => (
<option key={s.id} value={s.id}>
{s.id} ({s.holder})
</option>
))}
</select>
<button
type="button"
data-testid="spe-import-run"
onClick={onImport}
disabled={busy || confirming}
className="flex items-center gap-1 px-2 py-1 rounded bg-(--accent) text-white hover:opacity-90 disabled:opacity-50"
>
{busy ? (
<Loader2 size={12} className="animate-spin" />
) : (
<Database size={12} />
)}
{busy
? t("Importing…")
: existing.length
? t("Import again")
: t("Import into SPE")}
</button>
</div>
{confirming && (
<div
data-testid="spe-import-confirm"
role="alertdialog"
aria-label={t("Import again?")}
className="rounded border border-amber-600/50 bg-amber-900/20 px-2 py-2 text-(--text-primary) space-y-2"
>
<p className="flex items-start gap-1.5">
<AlertTriangle
size={14}
className="mt-0.5 shrink-0 text-amber-500"
/>
{inChosen
? t(
"{spe} holds this transfer already ({records} records, imported on {date}). Overwrite it? Its records are replaced by the data as it is now.",
{
spe: inChosen.speId,
records: String(inChosen.records),
date: inChosen.importedAt.slice(0, 10),
},
)
: t(
"This transfer is in {others} already. Import it into {spe} as well? It is then held twice.",
{
others: elsewhere.map((e) => e.speId).join(", "),
spe: speId,
},
)}
</p>
<div className="flex gap-2">
<button
type="button"
data-testid="spe-import-confirm-yes"
onClick={() => void runImport(!!inChosen)}
className="px-2 py-1 rounded bg-amber-600 text-white hover:opacity-90"
>
{inChosen ? t("Overwrite") : t("Import as well")}
</button>
<button
type="button"
data-testid="spe-import-confirm-no"
onClick={() => setConfirming(false)}
className="px-2 py-1 rounded border border-(--border) text-(--text-primary)"
>
{t("Cancel")}
</button>
</div>
</div>
)}
{startingIn !== null && (
<p
data-testid="spe-import-starting"
className="flex items-center gap-1.5 text-(--text-secondary)"
>
<Loader2 size={12} className="animate-spin" />
{t(
"{spe} is starting. That takes about {minutes} minutes; import again then.",
{ spe: speId, minutes: String(startingIn) },
)}
</p>
)}
{error &&
(expired ? (
<p
data-testid="spe-import-error"
className="flex flex-wrap items-center gap-2 text-(--badge-inactive-text)"
>
{t(
"The access to this transfer has expired. Start a new transfer to get the data again.",
)}
{onRestart && (
<button
type="button"
data-testid="spe-import-restart"
onClick={onRestart}
className="px-2 py-1 rounded bg-(--accent) text-white hover:opacity-90"
>
{t("Start a new transfer")}
</button>
)}
</p>
) : (
<p
data-testid="spe-import-error"
className="text-(--badge-inactive-text)"
>
{t("The import failed: {detail}", { detail: error })}
</p>
))}
{result && (
<div
data-testid="spe-import-done"
className="flex flex-wrap items-center gap-2 rounded border border-green-700/50 bg-green-900/20 px-2 py-1.5 text-(--text-primary)"
>
<CheckCircle2 size={14} className="text-green-500" />
<span>
{t(
"Imported {records} records with {relationships} references into {spe}.",
{
records: String(result.records),
relationships: String(result.relationships),
spe: result.speId,
},
)}
{result.pseudonymised && ` ${t("Patients are pseudonymised.")}`}
{result.untilStopped &&
` ${t(
"The workspace keeps the data in memory until it stops; it is a demo import, not loaded through the Trustee.",
)}`}
{result.closed &&
` ${t(
"The transfer is closed: the access to the provider's data has ended. Start a new transfer to import it again.",
)}`}
</span>
<Link
href={result.graphUrl}
data-testid="spe-import-graph"
className="flex items-center gap-1 underline text-(--accent)"
>
<Box size={12} />
{t("Show in 3D")}
</Link>
</div>
)}
</div>
);
}
|