feat(registratura): auto-close monitoring on resolved, inline check, edit tracking

- Auto-deactivate monitoring when status is 'solutionat' or 'respins'
- 'Verifica acum' shows result inline (no page reload/close)
- 'Modifica' button opens edit dialog to fix petitioner/regNumber/date
- Show monitoring section even when inactive (final status visible)
- Display tracking config info (nr, date, petitioner) in detail view

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-30 09:17:18 +03:00
parent 89e7d08d19
commit 5bcf65ff02
3 changed files with 223 additions and 75 deletions
@@ -166,11 +166,21 @@ export function RegistryEntryDetail({
const [previewIndex, setPreviewIndex] = useState<number | null>(null);
const [copiedPath, setCopiedPath] = useState<string | null>(null);
const [monitorConfigOpen, setMonitorConfigOpen] = useState(false);
const [monitorEditMode, setMonitorEditMode] = useState(false);
// Auto-detect if recipient matches a known authority
// Authority for existing tracking or auto-detected from recipient
const trackingAuthority = useMemo(() => {
if (!entry) return undefined;
if (entry.externalStatusTracking) {
return getAuthority(entry.externalStatusTracking.authorityId) ?? undefined;
}
return undefined;
}, [entry]);
// Auto-detect if recipient matches a known authority (only when no tracking)
const matchedAuthority = useMemo(() => {
if (!entry) return undefined;
if (entry.externalStatusTracking?.active) return undefined;
if (entry.externalStatusTracking) return undefined;
if (!entry.recipientRegNumber) return undefined;
return findAuthorityForContact(entry.recipient);
}, [entry]);
@@ -757,14 +767,47 @@ export function RegistryEntryDetail({
)}
{/* ── External status monitoring ── */}
{entry.externalStatusTracking?.active && (
<ExternalStatusSection
entry={entry}
/>
{entry.externalStatusTracking && (
<>
<ExternalStatusSection
entry={entry}
onEdit={() => {
setMonitorEditMode(true);
setMonitorConfigOpen(true);
}}
/>
{trackingAuthority && (
<StatusMonitorConfig
open={monitorConfigOpen && monitorEditMode}
onOpenChange={(open) => {
setMonitorConfigOpen(open);
if (!open) setMonitorEditMode(false);
}}
entry={entry}
authority={trackingAuthority}
editMode
onActivate={async (tracking) => {
try {
await fetch("/api/registratura", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: entry.id,
updates: { externalStatusTracking: tracking },
}),
});
window.location.reload();
} catch {
// Best effort
}
}}
/>
)}
</>
)}
{/* ── Auto-detect: suggest monitoring activation ── */}
{matchedAuthority && !entry.externalStatusTracking?.active && (
{matchedAuthority && !entry.externalStatusTracking && (
<div className="rounded-lg border border-dashed border-blue-300 dark:border-blue-700 bg-blue-50/50 dark:bg-blue-950/20 p-3">
<div className="flex items-start gap-2">
<Radio className="h-4 w-4 text-blue-500 mt-0.5 shrink-0" />
@@ -780,7 +823,10 @@ export function RegistryEntryDetail({
variant="outline"
size="sm"
className="mt-2 h-6 text-xs"
onClick={() => setMonitorConfigOpen(true)}
onClick={() => {
setMonitorEditMode(false);
setMonitorConfigOpen(true);
}}
>
Configureaza monitorizarea
</Button>
@@ -788,12 +834,11 @@ export function RegistryEntryDetail({
</div>
<StatusMonitorConfig
open={monitorConfigOpen}
open={monitorConfigOpen && !monitorEditMode}
onOpenChange={setMonitorConfigOpen}
entry={entry}
authority={matchedAuthority}
onActivate={async (tracking) => {
// Save tracking to entry via API
try {
await fetch("/api/registratura", {
method: "PUT",
@@ -892,26 +937,54 @@ const STATUS_COLORS: Record<ExternalDocStatus, string> = {
necunoscut: "bg-muted text-muted-foreground",
};
function ExternalStatusSection({ entry }: { entry: RegistryEntry }) {
function ExternalStatusSection({
entry,
onEdit,
}: {
entry: RegistryEntry;
onEdit: () => void;
}) {
const tracking = entry.externalStatusTracking;
if (!tracking) return null;
const [checking, setChecking] = useState(false);
const [checkResult, setCheckResult] = useState<{
changed: boolean;
error: string | null;
newStatus?: string;
} | null>(null);
const [showHistory, setShowHistory] = useState(false);
const [liveTracking, setLiveTracking] = useState(tracking);
const authority = getAuthority(tracking.authorityId);
const handleManualCheck = useCallback(async () => {
setChecking(true);
setCheckResult(null);
try {
await fetch("/api/registratura/status-check/single", {
const res = await fetch("/api/registratura/status-check/single", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ entryId: entry.id }),
});
// Reload page to show updated status
window.location.reload();
} catch {
// Ignore — user will see if it worked on reload
const data = (await res.json()) as {
changed: boolean;
error: string | null;
newStatus?: string;
tracking?: typeof tracking;
};
setCheckResult({
changed: data.changed,
error: data.error,
newStatus: data.newStatus,
});
if (data.tracking) {
setLiveTracking(data.tracking);
}
} catch (err) {
setCheckResult({
changed: false,
error: err instanceof Error ? err.message : "Eroare conexiune",
});
} finally {
setChecking(false);
}
@@ -928,80 +1001,121 @@ function ExternalStatusSection({ entry }: { entry: RegistryEntry }) {
return `acum ${days}z`;
};
const t = liveTracking;
return (
<div>
<div className="flex items-center justify-between mb-2">
<h3 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider">
Monitorizare status extern
{!t.active && (
<span className="ml-1.5 text-[10px] font-normal normal-case">(oprita)</span>
)}
</h3>
<Button
variant="ghost"
size="sm"
className="h-6 px-2 text-xs"
onClick={handleManualCheck}
disabled={checking}
>
<RefreshCw className={cn("h-3 w-3 mr-1", checking && "animate-spin")} />
{checking ? "Se verifică..." : "Verifică acum"}
</Button>
<div className="flex gap-1">
<Button
variant="ghost"
size="sm"
className="h-6 px-2 text-xs"
onClick={onEdit}
>
<Pencil className="h-3 w-3 mr-1" />
Modifica
</Button>
{t.active && (
<Button
variant="ghost"
size="sm"
className="h-6 px-2 text-xs"
onClick={handleManualCheck}
disabled={checking}
>
<RefreshCw className={cn("h-3 w-3 mr-1", checking && "animate-spin")} />
{checking ? "Se verifica..." : "Verifica acum"}
</Button>
)}
</div>
</div>
{/* Inline check result */}
{checkResult && (
<div className={cn(
"rounded border px-2.5 py-1.5 text-xs mb-2",
checkResult.error
? "border-red-200 bg-red-50 text-red-700 dark:border-red-800 dark:bg-red-950/30 dark:text-red-400"
: checkResult.changed
? "border-green-200 bg-green-50 text-green-700 dark:border-green-800 dark:bg-green-950/30 dark:text-green-400"
: "border-muted bg-muted/30 text-muted-foreground",
)}>
{checkResult.error
? `Eroare: ${checkResult.error}`
: checkResult.changed
? `Status actualizat: ${EXTERNAL_STATUS_LABELS[checkResult.newStatus as ExternalDocStatus] ?? checkResult.newStatus}`
: "Nicio schimbare detectata"}
</div>
)}
<div className="space-y-2">
{/* Authority + status badge */}
<div className="flex items-center gap-2 flex-wrap">
<span className="text-xs text-muted-foreground">
{authority?.name ?? tracking.authorityId}
{authority?.name ?? t.authorityId}
</span>
<Badge className={cn("text-[10px] px-1.5 py-0", STATUS_COLORS[tracking.semanticStatus])}>
<Badge className={cn("text-[10px] px-1.5 py-0", STATUS_COLORS[t.semanticStatus])}>
<Radio className="mr-0.5 inline h-2.5 w-2.5" />
{EXTERNAL_STATUS_LABELS[tracking.semanticStatus]}
{EXTERNAL_STATUS_LABELS[t.semanticStatus]}
</Badge>
</div>
{/* Last check time */}
{tracking.lastCheckAt && (
{t.lastCheckAt && (
<p className="text-[10px] text-muted-foreground">
Ultima verificare: {relativeTime(tracking.lastCheckAt)}
Ultima verificare: {relativeTime(t.lastCheckAt)}
</p>
)}
{/* Error state */}
{tracking.lastError && (
<p className="text-[10px] text-red-500">{tracking.lastError}</p>
{t.lastError && (
<p className="text-[10px] text-red-500">{t.lastError}</p>
)}
{/* Latest status row */}
{tracking.lastStatusRow && (
{t.lastStatusRow && (
<div className="rounded border bg-muted/30 p-2 text-xs space-y-1">
<div className="flex gap-3">
<span>
<span className="text-muted-foreground">Sursa:</span>{" "}
{tracking.lastStatusRow.sursa}
{t.lastStatusRow.sursa}
</span>
<span>
<span className="text-muted-foreground"></span>{" "}
{tracking.lastStatusRow.destinatie}
{t.lastStatusRow.destinatie}
</span>
</div>
{tracking.lastStatusRow.modRezolvare && (
{t.lastStatusRow.modRezolvare && (
<div>
<span className="text-muted-foreground">Rezolvare:</span>{" "}
<span className="font-medium">{tracking.lastStatusRow.modRezolvare}</span>
<span className="font-medium">{t.lastStatusRow.modRezolvare}</span>
</div>
)}
{tracking.lastStatusRow.comentarii && (
{t.lastStatusRow.comentarii && (
<div className="text-muted-foreground">
{tracking.lastStatusRow.comentarii}
{t.lastStatusRow.comentarii}
</div>
)}
<div className="text-muted-foreground">
{tracking.lastStatusRow.dataVenire} {tracking.lastStatusRow.oraVenire}
{t.lastStatusRow.dataVenire} {t.lastStatusRow.oraVenire}
</div>
</div>
)}
{/* Tracking config info */}
<div className="text-[10px] text-muted-foreground">
Nr: {t.regNumber} | Data: {t.regDate} | Deponent: {t.petitionerName}
</div>
{/* History toggle */}
{tracking.history.length > 0 && (
{t.history.length > 0 && (
<div>
<button
className="flex items-center gap-1 text-[10px] text-muted-foreground hover:text-foreground transition-colors"
@@ -1012,12 +1126,12 @@ function ExternalStatusSection({ entry }: { entry: RegistryEntry }) {
) : (
<ChevronDown className="h-3 w-3" />
)}
Istoric ({tracking.history.length} schimbări)
Istoric ({t.history.length} schimbari)
</button>
{showHistory && (
<div className="mt-1 space-y-1.5 max-h-48 overflow-y-auto">
{[...tracking.history].reverse().map((change, i) => (
{[...t.history].reverse().map((change, i) => (
<div
key={`${change.timestamp}-${i}`}
className="rounded border bg-muted/20 p-1.5 text-[10px]"