import { useEffect, useState } from "react"; import { AlertCircle } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../../../../components/ui/dialog.tsx"; import { Button } from "../../../../components/ui/button.tsx"; import { Input } from "../../../../components/ui/input.tsx"; import { Label } from "../../../../components/ui/label.tsx"; import { Textarea } from "../../../../components/ui/textarea.tsx"; import { Alert, AlertDescription } from "../../../../components/ui/alert.tsx"; import { relayServerFailure } from "../../../../utils/fetchFailureMessage"; import type { LabConnection, LabCredentials } from "../../../../hooks/useLabConnections.ts"; import "./DiagnosticsPage.css"; // Write-only: a saved sign-in is never read back, so the boxes always start empty and a save // replaces whatever was held. export type CredentialsTarget = { connection: LabConnection } | undefined; interface Props { target: CredentialsTarget; onClose: () => void; saveCredentials: (id: string, credentials: LabCredentials) => Promise; removeCredentials: (id: string) => Promise; } const EMPTY: LabCredentials = { apiKey: "", username: "", password: "", clientCertificate: "", clientKey: "", sendingFacility: "", }; const NEEDS_NO_SIGN_IN = new Set(["secure-email", "direct-project"]); const LabCredentialsDialog = ({ target, onClose, saveCredentials, removeCredentials }: Props) => { const [draft, setDraft] = useState(EMPTY); const [saving, setSaving] = useState(false); const [confirmingRemove, setConfirmingRemove] = useState(false); const [error, setError] = useState(); useEffect(() => { setDraft(EMPTY); setConfirmingRemove(false); setError(undefined); }, [target]); if (!target) return null; const { connection } = target; const saved = connection.credentialsConfigured === true; const needsNone = NEEDS_NO_SIGN_IN.has(connection.connectionType ?? ""); const mllp = connection.connectionType === "hl7v2-mllp"; const set = (patch: Partial) => setDraft((prev) => ({ ...prev, ...patch })); const run = async (act: () => Promise, fallback: string) => { setSaving(true); setError(undefined); try { await act(); onClose(); } catch (failure) { console.error("Error changing a laboratory sign-in:", failure); setError(relayServerFailure(failure, fallback)); } finally { setSaving(false); } }; const submit = () => { const filled = [draft.apiKey, draft.username, draft.password, draft.clientCertificate, draft.clientKey] .some((value) => value?.trim()); if (!filled) { setError("Enter the sign-in this laboratory gave you"); return; } void run( () => saveCredentials(connection.id as string, draft), "The sign-in was not saved. Please try again.", ); }; const remove = () => void run( () => removeCredentials(connection.id as string), "The sign-in was not removed. Please try again.", ); return ( { if (!open && !saving) onClose(); }}> Sign-in for {connection.name ?? "this laboratory"} {saved ? "Credentials are saved and will not be shown again. Enter new credentials to replace them." : "No sign-in is saved for this laboratory yet."} {error ? ( {error} ) : null} {needsNone ? (

This way of sending needs no sign-in.

) : (
set({ apiKey: e.target.value })} />

If the laboratory gave you a token, that is all it needs. Otherwise enter the username and password it gave you.

set({ username: e.target.value })} />
set({ password: e.target.value })} />
{mllp ? ( <>
set({ sendingFacility: e.target.value })} />