fix(treatment): show only past plans in history rail and refresh after lab send.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-14 15:09:10 +03:30
parent a7ba33fb68
commit b7682a2d1c
4 changed files with 38 additions and 37 deletions

View File

@@ -501,12 +501,19 @@ export function TreatmentWorkspace({
const previewTreatment = useMemo(() => {
if (!selectedPreviewId) return currentDraftPreview;
return (
const fromHistory =
history.find((item) => item.id === selectedPreviewId) ??
historyPanelItems.find((item) => item.id === selectedPreviewId) ??
currentDraftPreview
);
}, [selectedPreviewId, history, historyPanelItems, currentDraftPreview]);
historyPanelItems.find((item) => item.id === selectedPreviewId);
if (
workspaceMode === 'live' &&
currentDraftPreview?.appointmentId &&
(selectedPreviewId === currentDraftPreview.id ||
fromHistory?.appointmentId === currentDraftPreview.appointmentId)
) {
return currentDraftPreview;
}
return fromHistory ?? currentDraftPreview;
}, [selectedPreviewId, history, historyPanelItems, currentDraftPreview, workspaceMode]);
const isBrowsing = selectedPreviewId !== null;
@@ -1039,23 +1046,33 @@ export function TreatmentWorkspace({
const ok = workspaceModeRef.current === 'live' ? await flushDraftSave() : true;
if (!ok) return false;
const isHistorical = isTreatmentDayHistorical(treatment.treatmentAt, todayStart);
let treatmentToLoad = treatment;
try {
const draftResponse = await treatmentsApi.getDraft(treatment.appointmentId);
if (draftResponse.data) {
treatmentToLoad = draftResponse.data;
}
} catch {
// Fall back to the history snapshot when draft cannot be loaded.
}
const isHistorical = isTreatmentDayHistorical(treatmentToLoad.treatmentAt, todayStart);
setWorkspaceMode(isHistorical ? 'historical' : 'live');
setSelectedPreviewId(null);
const nextDay = startOfLocalDay(new Date(treatment.treatmentAt));
const nextDay = startOfLocalDay(new Date(treatmentToLoad.treatmentAt));
setSelectedDay((prev) => (compareLocalDayStart(prev, nextDay) === 0 ? prev : nextDay));
setSelectionLocked(true);
setSelectedAppointmentId(treatment.appointmentId);
setSelectedAppointmentId(treatmentToLoad.appointmentId ?? treatment.appointmentId);
skipNextGetDraftRef.current = true;
draftHydratingRef.current = true;
hydrateFromTreatment(treatment);
hydrateFromTreatment(treatmentToLoad);
draftHydratingRef.current = false;
if (focusDetailClientId) {
setActiveDetailId(focusDetailClientId);
const mappedLabCases = withoutEmptyLabCaseDrafts(
(treatment.labCases ?? []).map(mapLabCaseDraftFromApi),
(treatmentToLoad.labCases ?? []).map(mapLabCaseDraftFromApi),
);
const linked = mappedLabCases.find(
(lc) => !lc.sentAt && lc.detailClientId === focusDetailClientId,
@@ -1510,6 +1527,7 @@ export function TreatmentWorkspace({
notifyTabBadgesChanged();
if (selectedAppointment.patientId) {
void refreshPatientLabCases(selectedAppointment.patientId);
void refreshHistory(selectedAppointment.patientId);
}
} catch (error: unknown) {
showError(getUserFacingError(error, tErrors, t('errorSendCase')));
@@ -1523,6 +1541,7 @@ export function TreatmentWorkspace({
persistDraft,
persistLabCases,
refreshPatientLabCases,
refreshHistory,
showSuccess,
showError,
t,
@@ -1675,7 +1694,6 @@ export function TreatmentWorkspace({
>
<PastTreatmentsPanel
items={historyPanelItems}
currentDraft={null}
currentAppointmentId={selectedAppointmentId}
treatmentCatalog={treatmentCatalog}
labDependentCodes={labDependentCodes}