mirror of
https://github.com/temetro/temetro.git
synced 2026-08-20 23:22:18 +00:00
feat: clinic→wallet record-update push
A clinician can push an updated record to a wallet-linked patient (permanent share). The snapshot is signed with the clinic Ed25519 key and sealed to the wallet's X25519 key — derived from its Ed25519 wallet number via the birational map, verified byte-for-byte against the wallet's own derivation. Stored pending, delivered over the /wallet relay live and on the wallet's next authenticated connect (offline catch-up). The patient approves/denies in-app; the wallet signs its decision, the backend verifies it, and the record is replaced only on approval. Wallet pins the clinic key (TOFU) and warns on change. Backend: walletRecordUpdates table + service, ed25519PubToX25519Hex helper, POST /api/patients/wallet/push, GET .../link/:fileNumber|updates|updates/:id, wallet:update-request / wallet:update-response relay events. Frontend: "Push to wallet" dialog with live status, wallet-link gating on the patient sheet, "Sent updates" list under Settings → Signing, walletPush / walletUpdatesList locale namespaces across all five languages. Bumps to v0.5.0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import { RecordGraph } from "@/components/graph/record-graph";
|
||||
import { PatientDetail } from "@/components/patients/patient-detail";
|
||||
import { ScribeDialog } from "@/components/patients/scribe-dialog";
|
||||
import { TransferPatientDialog } from "@/components/patients/transfer-patient-dialog";
|
||||
import { WalletPushDialog } from "@/components/patients/wallet-push-dialog";
|
||||
import { ConfirmDialog } from "@/components/ui/confirm-dialog";
|
||||
import {
|
||||
Dialog,
|
||||
@@ -32,6 +33,7 @@ import { listPrescriptions, type Prescription } from "@/lib/prescriptions";
|
||||
import { useAiAccess } from "@/lib/ai-policy";
|
||||
import { hasClinicalAccess, useActiveRole } from "@/lib/roles";
|
||||
import { notify } from "@/lib/toast";
|
||||
import { getWalletLink } from "@/lib/wallet-updates";
|
||||
|
||||
type Status = "loading" | "ready" | "not-found";
|
||||
|
||||
@@ -86,6 +88,9 @@ export function PatientDetailSheet({
|
||||
const [status, setStatus] = useState<Status>("loading");
|
||||
const [editOpen, setEditOpen] = useState(false);
|
||||
const [scribeOpen, setScribeOpen] = useState(false);
|
||||
const [walletPushOpen, setWalletPushOpen] = useState(false);
|
||||
// Set once we confirm this patient is linked to a wallet (permanent share).
|
||||
const [walletLinked, setWalletLinked] = useState(false);
|
||||
const [transferOpen, setTransferOpen] = useState(false);
|
||||
const [confirmOpen, setConfirmOpen] = useState(false);
|
||||
// Graph popped out of the sheet into its own dialog (the sheet closes first).
|
||||
@@ -131,6 +136,21 @@ export function PatientDetailSheet({
|
||||
};
|
||||
}, [open, fileNumber]);
|
||||
|
||||
// Whether this patient is wallet-linked (drives the "Push update" button).
|
||||
// Separate from the main load so it re-checks once the role resolves without
|
||||
// refetching the record. Only clinicians can push.
|
||||
useEffect(() => {
|
||||
setWalletLinked(false);
|
||||
if (!open || !fileNumber || !hasClinicalAccess(role)) return;
|
||||
let active = true;
|
||||
getWalletLink(fileNumber)
|
||||
.then(() => active && setWalletLinked(true))
|
||||
.catch(() => {});
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [open, fileNumber, role]);
|
||||
|
||||
const remove = async () => {
|
||||
if (!patient) return;
|
||||
try {
|
||||
@@ -179,6 +199,9 @@ export function PatientDetailSheet({
|
||||
setEditOpen(true);
|
||||
}}
|
||||
onScribe={canScribe ? () => setScribeOpen(true) : undefined}
|
||||
onWalletPush={
|
||||
walletLinked ? () => setWalletPushOpen(true) : undefined
|
||||
}
|
||||
onOpenGraph={() => {
|
||||
onOpenChange(false);
|
||||
setGraphOpen(true);
|
||||
@@ -214,6 +237,14 @@ export function PatientDetailSheet({
|
||||
/>
|
||||
)}
|
||||
|
||||
{patient && (
|
||||
<WalletPushDialog
|
||||
onOpenChange={setWalletPushOpen}
|
||||
open={walletPushOpen}
|
||||
patient={patient}
|
||||
/>
|
||||
)}
|
||||
|
||||
{patient && (
|
||||
<TransferPatientDialog
|
||||
onOpenChange={setTransferOpen}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
Mic,
|
||||
Network,
|
||||
Pencil,
|
||||
Send,
|
||||
Trash2,
|
||||
} from "lucide-react";
|
||||
import { type ReactNode, useEffect, useState } from "react";
|
||||
@@ -181,6 +182,7 @@ export function PatientDetail({
|
||||
patient,
|
||||
onEdit,
|
||||
onScribe,
|
||||
onWalletPush,
|
||||
onTransfer,
|
||||
onDelete,
|
||||
onOpenGraph,
|
||||
@@ -192,6 +194,8 @@ export function PatientDetail({
|
||||
onEdit?: () => void;
|
||||
// Opens the ambient AI visit scribe (record/transcribe → draft note).
|
||||
onScribe?: () => void;
|
||||
// Pushes the record to the patient's wallet (only when wallet-linked).
|
||||
onWalletPush?: () => void;
|
||||
onTransfer?: () => void;
|
||||
onDelete?: () => void;
|
||||
// Pops the record graph out into its own dialog (closing this sheet).
|
||||
@@ -309,6 +313,17 @@ export function PatientDetail({
|
||||
{t("patientCard.edit")}
|
||||
</Button>
|
||||
)}
|
||||
{onWalletPush && (
|
||||
<Button
|
||||
onClick={onWalletPush}
|
||||
size="sm"
|
||||
type="button"
|
||||
variant="outline"
|
||||
>
|
||||
<Send className="size-4" />
|
||||
{t("walletPush.action")}
|
||||
</Button>
|
||||
)}
|
||||
{onDelete && (
|
||||
<Button
|
||||
aria-label={t("patients.delete.action")}
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
"use client";
|
||||
|
||||
import { Check, Loader2, Send, X } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogPanel,
|
||||
DialogPopup,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { ApiError } from "@/lib/api-client";
|
||||
import type { Patient } from "@/lib/patients";
|
||||
import { cn } from "@/lib/utils";
|
||||
import {
|
||||
getWalletUpdate,
|
||||
pushWalletUpdate,
|
||||
type WalletUpdate,
|
||||
} from "@/lib/wallet-updates";
|
||||
|
||||
// The record sections a clinician can flag as changed. The labels double as the
|
||||
// human-readable change summary the patient sees when approving.
|
||||
const SECTION_KEYS = [
|
||||
"demographics",
|
||||
"problems",
|
||||
"medications",
|
||||
"allergies",
|
||||
"labs",
|
||||
"vitals",
|
||||
"visits",
|
||||
] as const;
|
||||
|
||||
type Phase = "compose" | "sent";
|
||||
|
||||
// Push the current record to a wallet-linked patient's app. The patient must
|
||||
// approve it on their phone before their on-device record is replaced.
|
||||
export function WalletPushDialog({
|
||||
patient,
|
||||
open,
|
||||
onOpenChange,
|
||||
}: {
|
||||
patient: Patient;
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [phase, setPhase] = useState<Phase>("compose");
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set());
|
||||
const [note, setNote] = useState("");
|
||||
const [update, setUpdate] = useState<WalletUpdate | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const reset = () => {
|
||||
setPhase("compose");
|
||||
setSelected(new Set());
|
||||
setNote("");
|
||||
setUpdate(null);
|
||||
setBusy(false);
|
||||
setError(null);
|
||||
};
|
||||
|
||||
const handleOpenChange = (next: boolean) => {
|
||||
if (!next) reset();
|
||||
onOpenChange(next);
|
||||
};
|
||||
|
||||
// Poll the update's status until the patient approves/denies (or the dialog
|
||||
// closes). Live pushes usually resolve within seconds.
|
||||
useEffect(() => {
|
||||
if (phase !== "sent" || !update || update.resolvedAt) return;
|
||||
let active = true;
|
||||
const timer = setInterval(async () => {
|
||||
try {
|
||||
const fresh = await getWalletUpdate(update.id);
|
||||
if (!active) return;
|
||||
setUpdate(fresh);
|
||||
if (fresh.resolvedAt) clearInterval(timer);
|
||||
} catch {
|
||||
/* keep polling */
|
||||
}
|
||||
}, 3000);
|
||||
return () => {
|
||||
active = false;
|
||||
clearInterval(timer);
|
||||
};
|
||||
}, [phase, update]);
|
||||
|
||||
const toggle = (key: string) => {
|
||||
setSelected((prev) => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(key)) next.delete(key);
|
||||
else next.add(key);
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
const push = async () => {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const changes = [
|
||||
...[...selected].map((k) => t(`walletPush.sections.${k}`)),
|
||||
...(note.trim() ? [note.trim()] : []),
|
||||
];
|
||||
const created = await pushWalletUpdate({
|
||||
fileNumber: patient.fileNumber,
|
||||
changes,
|
||||
});
|
||||
setUpdate(created);
|
||||
setPhase("sent");
|
||||
} catch (err) {
|
||||
setError(
|
||||
err instanceof ApiError ? err.message : t("walletPush.errors.generic"),
|
||||
);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
const canPush = selected.size > 0 || note.trim().length > 0;
|
||||
const status = update?.status ?? "pending";
|
||||
|
||||
return (
|
||||
<Dialog onOpenChange={handleOpenChange} open={open}>
|
||||
<DialogPopup className="flex max-h-[85dvh] flex-col sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<Send className="size-4 text-primary" />
|
||||
{t("walletPush.title")}
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
{t("walletPush.subtitle", { name: patient.name })}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<DialogPanel className="min-h-0 flex-1 overflow-y-auto">
|
||||
{phase === "compose" ? (
|
||||
<div className="flex flex-col gap-4">
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>{t("walletPush.sectionsLabel")}</Label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{SECTION_KEYS.map((key) => {
|
||||
const on = selected.has(key);
|
||||
return (
|
||||
<button
|
||||
className={cn(
|
||||
"rounded-full border px-3 py-1 text-sm transition-colors",
|
||||
on
|
||||
? "border-primary bg-primary/10 text-foreground"
|
||||
: "border-border text-muted-foreground hover:bg-accent",
|
||||
)}
|
||||
key={key}
|
||||
onClick={() => toggle(key)}
|
||||
type="button"
|
||||
>
|
||||
{t(`walletPush.sections.${key}`)}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label htmlFor="wallet-push-note">
|
||||
{t("walletPush.noteLabel")}
|
||||
</Label>
|
||||
<Textarea
|
||||
className="min-h-20"
|
||||
id="wallet-push-note"
|
||||
onChange={(e) => setNote(e.target.value)}
|
||||
placeholder={t("walletPush.notePlaceholder")}
|
||||
value={note}
|
||||
/>
|
||||
</div>
|
||||
<p className="rounded-lg bg-muted px-3 py-2 text-muted-foreground text-xs">
|
||||
{t("walletPush.notice")}
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex flex-col items-center gap-4 py-6 text-center">
|
||||
{status === "approved" ? (
|
||||
<div className="flex size-12 items-center justify-center rounded-full bg-success/15 text-success">
|
||||
<Check className="size-6" />
|
||||
</div>
|
||||
) : status === "denied" ? (
|
||||
<div className="flex size-12 items-center justify-center rounded-full bg-destructive/15 text-destructive">
|
||||
<X className="size-6" />
|
||||
</div>
|
||||
) : (
|
||||
<Loader2 className="size-8 animate-spin text-muted-foreground" />
|
||||
)}
|
||||
<div className="flex flex-col gap-1">
|
||||
<p className="font-medium text-foreground text-sm">
|
||||
{t(`walletPush.status.${status}.title`)}
|
||||
</p>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{t(`walletPush.status.${status}.body`)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<p className="mt-3 text-destructive text-sm" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</DialogPanel>
|
||||
|
||||
<DialogFooter>
|
||||
{phase === "compose" ? (
|
||||
<>
|
||||
<Button
|
||||
onClick={() => handleOpenChange(false)}
|
||||
type="button"
|
||||
variant="outline"
|
||||
>
|
||||
{t("walletPush.cancel")}
|
||||
</Button>
|
||||
<Button disabled={busy || !canPush} onClick={push} type="button">
|
||||
{busy && <Spinner className="size-4" />}
|
||||
{t("walletPush.send")}
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<Button onClick={() => handleOpenChange(false)} type="button">
|
||||
{t("walletPush.done")}
|
||||
</Button>
|
||||
)}
|
||||
</DialogFooter>
|
||||
</DialogPopup>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
type SharedRecord,
|
||||
type SigningKey,
|
||||
} from "@/lib/signing";
|
||||
import { listWalletUpdates, type WalletUpdate } from "@/lib/wallet-updates";
|
||||
import { notify } from "@/lib/toast";
|
||||
|
||||
function formatDate(iso: string): string {
|
||||
@@ -33,17 +34,23 @@ export function SigningPanel() {
|
||||
const { t } = useTranslation();
|
||||
const [key, setKey] = useState<SigningKey | null>(null);
|
||||
const [records, setRecords] = useState<SharedRecord[]>([]);
|
||||
const [updates, setUpdates] = useState<WalletUpdate[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [rotating, setRotating] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let active = true;
|
||||
Promise.all([getSigningKey(), listSignedRecords().catch(() => [])])
|
||||
.then(([k, r]) => {
|
||||
Promise.all([
|
||||
getSigningKey(),
|
||||
listSignedRecords().catch(() => []),
|
||||
listWalletUpdates().catch(() => []),
|
||||
])
|
||||
.then(([k, r, u]) => {
|
||||
if (!active) return;
|
||||
setKey(k);
|
||||
setRecords(r);
|
||||
setUpdates(u);
|
||||
setError(null);
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -223,6 +230,40 @@ export function SigningPanel() {
|
||||
</SettingsCard>
|
||||
)}
|
||||
</SettingsSection>
|
||||
|
||||
<SettingsSection
|
||||
description={t("walletUpdatesList.description")}
|
||||
title={t("walletUpdatesList.title")}
|
||||
>
|
||||
{updates.length === 0 ? (
|
||||
<SettingsCard className="flex items-center justify-center p-12">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t("walletUpdatesList.none")}
|
||||
</p>
|
||||
</SettingsCard>
|
||||
) : (
|
||||
<SettingsCard className="divide-y divide-border">
|
||||
{updates.map((update) => (
|
||||
<div
|
||||
className="flex items-center justify-between gap-3 px-4 py-3.5"
|
||||
key={update.id}
|
||||
>
|
||||
<div className="min-w-0 space-y-0.5">
|
||||
<p className="truncate text-sm">
|
||||
{update.changes.join(" · ") || `#${update.fileNumber}`}
|
||||
</p>
|
||||
<p className="truncate font-mono text-xs text-muted-foreground">
|
||||
#{update.fileNumber} · {formatDate(update.createdAt)}
|
||||
</p>
|
||||
</div>
|
||||
<Badge variant="secondary">
|
||||
{t(`walletPush.status.${update.status}.title`)}
|
||||
</Badge>
|
||||
</div>
|
||||
))}
|
||||
</SettingsCard>
|
||||
)}
|
||||
</SettingsSection>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user