2026-05-07 03:40:29 +03:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
2026-05-07 03:46:18 +03:30
|
|
|
import { AppointmentsStrip } from '@/components/ui/treatment/AppointmentsStrip';
|
|
|
|
|
import { FdiToothChart } from '@/components/ui/treatment/FdiToothChart';
|
|
|
|
|
import { PastTreatmentsPanel } from '@/components/ui/treatment/PastTreatmentsPanel';
|
2026-05-18 12:31:21 +03:30
|
|
|
import { Checkbox } from '@/components/ui/shared/Checkbox';
|
|
|
|
|
import { Button } from '@/components/ui/shared/Button';
|
|
|
|
|
import { Dropdown } from '@/components/ui/shared/Dropdown';
|
|
|
|
|
import { SearchBar } from '@/components/ui/shared/SearchBar';
|
|
|
|
|
import { Toast } from '@/components/ui/shared/Toast';
|
2026-05-07 03:40:29 +03:30
|
|
|
import {
|
2026-05-19 22:29:29 +03:30
|
|
|
addCalendarDays,
|
|
|
|
|
compareLocalDayStart,
|
|
|
|
|
isSameLocalCalendarDay,
|
|
|
|
|
startOfLocalDay,
|
|
|
|
|
} from '@/components/appointments/appointmentTime';
|
|
|
|
|
import { appointmentsApi } from '@/lib/api/appointments';
|
|
|
|
|
import { treatmentsApi } from '@/lib/api/treatments';
|
2026-05-18 14:08:07 +03:30
|
|
|
import { pickAutoAppointment } from '@/components/shared/treatmentSelection';
|
2026-05-19 22:29:29 +03:30
|
|
|
import { canEditTreatment, canViewTreatment } from '@/components/shared/permissions';
|
|
|
|
|
import { formatApiErrorMessage } from '@/components/shared/formatApiError';
|
2026-05-07 03:40:29 +03:30
|
|
|
import type { Organization } from '@/types/organization';
|
2026-05-19 22:29:29 +03:30
|
|
|
import type { AppointmentRecord } from '@/types/appointment';
|
2026-05-07 03:40:29 +03:30
|
|
|
import type {
|
|
|
|
|
FdiToothId,
|
|
|
|
|
LinkedOrganizationOption,
|
|
|
|
|
PastTreatment,
|
2026-05-19 22:29:29 +03:30
|
|
|
PastTreatmentCase,
|
2026-05-07 03:40:29 +03:30
|
|
|
TreatmentAppointment,
|
|
|
|
|
TreatmentAttachmentMeta,
|
2026-05-19 22:29:29 +03:30
|
|
|
TreatmentCaseDraft,
|
2026-05-07 03:40:29 +03:30
|
|
|
} from '@/types/treatment';
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
function newCase(): TreatmentCaseDraft {
|
2026-05-07 03:40:29 +03:30
|
|
|
return {
|
|
|
|
|
clientId:
|
|
|
|
|
typeof crypto !== 'undefined' && 'randomUUID' in crypto
|
|
|
|
|
? crypto.randomUUID()
|
2026-05-19 22:29:29 +03:30
|
|
|
: `case-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
|
2026-05-07 14:20:50 +03:30
|
|
|
treatmentType: 'consultation',
|
2026-05-07 03:40:29 +03:30
|
|
|
teeth: [],
|
|
|
|
|
comment: '',
|
|
|
|
|
attachmentMetas: [],
|
|
|
|
|
sendToOrganizationIds: [],
|
|
|
|
|
sentAt: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
function mapAppointment(record: AppointmentRecord): TreatmentAppointment {
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
patientId: record.patientId,
|
|
|
|
|
patientFirstName: record.patient.firstName,
|
|
|
|
|
patientLastName: record.patient.lastName,
|
|
|
|
|
providerUserId: record.providerUserId,
|
|
|
|
|
startAt: record.startAt,
|
|
|
|
|
endAt: record.endAt,
|
|
|
|
|
purpose: record.purpose,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mapCaseFromApi(c: PastTreatmentCase): TreatmentCaseDraft {
|
|
|
|
|
return {
|
|
|
|
|
clientId: c.clientId,
|
|
|
|
|
id: c.id,
|
|
|
|
|
treatmentType: c.treatmentType,
|
|
|
|
|
teeth: c.teeth,
|
|
|
|
|
comment: c.notes ?? '',
|
|
|
|
|
attachmentMetas: c.attachmentMetas ?? [],
|
|
|
|
|
sendToOrganizationIds: c.sendToOrganizationIds ?? [],
|
|
|
|
|
sentAt: c.sentAt ?? null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function serializeCases(cases: TreatmentCaseDraft[]) {
|
|
|
|
|
return JSON.stringify(
|
|
|
|
|
cases.map((c) => ({
|
|
|
|
|
clientId: c.clientId,
|
|
|
|
|
id: c.id,
|
|
|
|
|
treatmentType: c.treatmentType,
|
|
|
|
|
teeth: c.teeth,
|
|
|
|
|
comment: c.comment,
|
|
|
|
|
attachmentMetas: c.attachmentMetas,
|
|
|
|
|
sendToOrganizationIds: c.sendToOrganizationIds,
|
|
|
|
|
sentAt: c.sentAt,
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
interface TreatmentWorkspaceProps {
|
|
|
|
|
userId: string;
|
|
|
|
|
currentOrganization: Organization | null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TreatmentWorkspace({ userId, currentOrganization }: TreatmentWorkspaceProps) {
|
2026-05-19 22:29:29 +03:30
|
|
|
const canView = canViewTreatment(currentOrganization);
|
2026-05-07 03:40:29 +03:30
|
|
|
const canEdit = canEditTreatment(currentOrganization);
|
|
|
|
|
|
|
|
|
|
const [stripHidden, setStripHidden] = useState(false);
|
|
|
|
|
|
2026-05-18 12:50:25 +03:30
|
|
|
const todayStart = useMemo(() => startOfLocalDay(new Date()), []);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
const [selectedDay, setSelectedDay] = useState(() => startOfLocalDay(new Date()));
|
|
|
|
|
const [appointments, setAppointments] = useState<TreatmentAppointment[]>([]);
|
|
|
|
|
const [apptsLoading, setApptsLoading] = useState(false);
|
|
|
|
|
const [selectionLocked, setSelectionLocked] = useState(false);
|
|
|
|
|
const [selectedAppointmentId, setSelectedAppointmentId] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const [history, setHistory] = useState<PastTreatment[]>([]);
|
|
|
|
|
const [historyLoading, setHistoryLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
const [orgs, setOrgs] = useState<LinkedOrganizationOption[]>([]);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const [cases, setCases] = useState<TreatmentCaseDraft[]>(() => [newCase()]);
|
|
|
|
|
const [activeCaseId, setActiveCaseId] = useState<string>(() => cases[0].clientId);
|
|
|
|
|
const [savedSnapshot, setSavedSnapshot] = useState<string | null>(null);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
const selectionLockedRef = useRef(selectionLocked);
|
|
|
|
|
selectionLockedRef.current = selectionLocked;
|
|
|
|
|
|
|
|
|
|
const attachmentInputRef = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
|
|
const [saveBusy, setSaveBusy] = useState(false);
|
|
|
|
|
const [sendBusyId, setSendBusyId] = useState<string | null>(null);
|
2026-05-19 22:29:29 +03:30
|
|
|
const [uploadBusy, setUploadBusy] = useState(false);
|
2026-05-07 03:40:29 +03:30
|
|
|
const [banner, setBanner] = useState<string | null>(null);
|
2026-05-19 22:29:29 +03:30
|
|
|
const [errorBanner, setErrorBanner] = useState<string | null>(null);
|
2026-05-07 14:20:50 +03:30
|
|
|
const [organizationSearch, setOrganizationSearch] = useState('');
|
|
|
|
|
const [recentOrganizationIds, setRecentOrganizationIds] = useState<string[]>([]);
|
|
|
|
|
const [reviewTreatment, setReviewTreatment] = useState<PastTreatment | null>(null);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const isDirty = useMemo(() => {
|
|
|
|
|
if (savedSnapshot === null) {
|
|
|
|
|
return cases.length !== 1 || cases[0].comment !== '' || cases[0].teeth.length > 0;
|
|
|
|
|
}
|
|
|
|
|
return serializeCases(cases) !== savedSnapshot;
|
|
|
|
|
}, [cases, savedSnapshot]);
|
|
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
const selectedAppointment = useMemo(
|
|
|
|
|
() => appointments.find((a) => a.id === selectedAppointmentId) ?? null,
|
|
|
|
|
[appointments, selectedAppointmentId],
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-18 12:50:25 +03:30
|
|
|
const isViewingPastDay = useMemo(
|
|
|
|
|
() => compareLocalDayStart(selectedDay, todayStart) < 0,
|
|
|
|
|
[selectedDay, todayStart],
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const canEditTreatmentForDay = canEdit && Boolean(selectedAppointment) && !isViewingPastDay;
|
2026-05-18 12:50:25 +03:30
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const activeCase = useMemo(
|
|
|
|
|
() => cases.find((c) => c.clientId === activeCaseId) ?? cases[0],
|
|
|
|
|
[cases, activeCaseId],
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const selectedTeethSet = useMemo(() => new Set(activeCase.teeth), [activeCase.teeth]);
|
2026-05-07 14:20:50 +03:30
|
|
|
const activeLinkedOrganizations = useMemo(() => orgs.filter((o) => o.active), [orgs]);
|
|
|
|
|
const filteredOrganizations = useMemo(() => {
|
|
|
|
|
const q = organizationSearch.trim().toLowerCase();
|
|
|
|
|
if (!q) return activeLinkedOrganizations;
|
|
|
|
|
return activeLinkedOrganizations.filter((o) => o.name.toLowerCase().includes(q));
|
|
|
|
|
}, [organizationSearch, activeLinkedOrganizations]);
|
|
|
|
|
const recentOrganizations = useMemo(() => {
|
|
|
|
|
if (recentOrganizationIds.length === 0) return [];
|
|
|
|
|
const recentSet = new Set(recentOrganizationIds);
|
|
|
|
|
return activeLinkedOrganizations
|
|
|
|
|
.filter((o) => recentSet.has(o.id))
|
|
|
|
|
.sort((a, b) => recentOrganizationIds.indexOf(a.id) - recentOrganizationIds.indexOf(b.id))
|
|
|
|
|
.slice(0, 3);
|
|
|
|
|
}, [recentOrganizationIds, activeLinkedOrganizations]);
|
2026-05-19 22:29:29 +03:30
|
|
|
|
2026-05-07 14:20:50 +03:30
|
|
|
const currentDraftPreview = useMemo<PastTreatment | null>(() => {
|
|
|
|
|
if (!selectedAppointment) return null;
|
|
|
|
|
return {
|
|
|
|
|
id: 'current-draft',
|
|
|
|
|
patientId: selectedAppointment.patientId,
|
|
|
|
|
title: `Current draft for ${selectedAppointment.patientFirstName} ${selectedAppointment.patientLastName}`,
|
|
|
|
|
treatmentAt: new Date().toISOString(),
|
|
|
|
|
status: 'draft',
|
2026-05-19 22:29:29 +03:30
|
|
|
cases: cases.map((c, idx) => ({
|
|
|
|
|
id: c.id ?? c.clientId ?? `draft-${idx + 1}`,
|
|
|
|
|
clientId: c.clientId,
|
|
|
|
|
treatmentType: c.treatmentType,
|
|
|
|
|
teeth: c.teeth,
|
|
|
|
|
notes: c.comment || null,
|
2026-05-07 14:20:50 +03:30
|
|
|
})),
|
2026-05-19 22:29:29 +03:30
|
|
|
documents: cases.flatMap((c) => c.attachmentMetas),
|
2026-05-07 14:20:50 +03:30
|
|
|
};
|
2026-05-19 22:29:29 +03:30
|
|
|
}, [cases, selectedAppointment]);
|
2026-05-07 14:20:50 +03:30
|
|
|
|
|
|
|
|
const treatmentTypeTextColor = useMemo(() => {
|
2026-05-19 22:29:29 +03:30
|
|
|
const map: Record<TreatmentCaseDraft['treatmentType'], string> = {
|
2026-05-07 14:20:50 +03:30
|
|
|
consultation: '#ddd6fe',
|
|
|
|
|
filling: '#fed7aa',
|
|
|
|
|
endo: '#fecaca',
|
|
|
|
|
visit: '#bae6fd',
|
|
|
|
|
hygiene: '#d9f99d',
|
|
|
|
|
};
|
2026-05-19 22:29:29 +03:30
|
|
|
return map[activeCase.treatmentType];
|
|
|
|
|
}, [activeCase.treatmentType]);
|
|
|
|
|
|
|
|
|
|
const loadDraftForAppointment = useCallback(async (appointmentId: string) => {
|
|
|
|
|
const response = await treatmentsApi.getDraft(appointmentId);
|
|
|
|
|
if (response.data?.cases?.length) {
|
|
|
|
|
const mapped = response.data.cases.map(mapCaseFromApi);
|
|
|
|
|
setCases(mapped);
|
|
|
|
|
setActiveCaseId(mapped[0].clientId);
|
|
|
|
|
setSavedSnapshot(serializeCases(mapped));
|
|
|
|
|
} else {
|
|
|
|
|
const first = newCase();
|
|
|
|
|
setCases([first]);
|
|
|
|
|
setActiveCaseId(first.clientId);
|
|
|
|
|
setSavedSnapshot(serializeCases([first]));
|
|
|
|
|
}
|
|
|
|
|
setOrganizationSearch('');
|
|
|
|
|
setReviewTreatment(null);
|
|
|
|
|
}, []);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setSelectionLocked(false);
|
|
|
|
|
}, [selectedDay]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
setApptsLoading(true);
|
|
|
|
|
void (async () => {
|
|
|
|
|
try {
|
2026-05-19 22:29:29 +03:30
|
|
|
const dayStart = startOfLocalDay(selectedDay);
|
|
|
|
|
const dayEnd = addCalendarDays(dayStart, 1);
|
|
|
|
|
const response = await appointmentsApi.list({
|
|
|
|
|
from: dayStart.toISOString(),
|
|
|
|
|
to: dayEnd.toISOString(),
|
|
|
|
|
});
|
2026-05-07 03:40:29 +03:30
|
|
|
if (cancelled) return;
|
2026-05-19 22:29:29 +03:30
|
|
|
const list = response.data
|
|
|
|
|
.filter((a) => a.providerUserId === userId)
|
|
|
|
|
.map(mapAppointment);
|
2026-05-07 03:40:29 +03:30
|
|
|
setAppointments(list);
|
|
|
|
|
if (!selectionLockedRef.current) {
|
|
|
|
|
setSelectedAppointmentId(pickAutoAppointment(list, selectedDay));
|
|
|
|
|
}
|
2026-05-19 22:29:29 +03:30
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setErrorBanner(formatApiErrorMessage(error, 'Failed to load appointments.'));
|
|
|
|
|
}
|
2026-05-07 03:40:29 +03:30
|
|
|
} finally {
|
|
|
|
|
if (!cancelled) setApptsLoading(false);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [userId, selectedDay]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const today = startOfLocalDay(new Date());
|
|
|
|
|
if (!isSameLocalCalendarDay(selectedDay, today) || selectionLocked) return;
|
|
|
|
|
|
|
|
|
|
const id = window.setInterval(() => {
|
|
|
|
|
setSelectedAppointmentId((prev) => {
|
|
|
|
|
const next = pickAutoAppointment(appointments, selectedDay);
|
|
|
|
|
return next ?? prev;
|
|
|
|
|
});
|
|
|
|
|
}, 60_000);
|
|
|
|
|
|
|
|
|
|
return () => window.clearInterval(id);
|
|
|
|
|
}, [selectedDay, appointments, selectionLocked]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
void (async () => {
|
2026-05-19 22:29:29 +03:30
|
|
|
try {
|
|
|
|
|
const list = await treatmentsApi.listLinkedOrganizations();
|
|
|
|
|
if (!cancelled) setOrgs(list.data);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setErrorBanner(formatApiErrorMessage(error, 'Failed to load linked organizations.'));
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-07 03:40:29 +03:30
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!selectedAppointment) {
|
|
|
|
|
setHistory([]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
setHistoryLoading(true);
|
|
|
|
|
void (async () => {
|
2026-05-19 22:29:29 +03:30
|
|
|
try {
|
|
|
|
|
const response = await treatmentsApi.listPatientHistory(selectedAppointment.patientId);
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setHistory(response.data);
|
|
|
|
|
}
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setErrorBanner(formatApiErrorMessage(error, 'Failed to load treatment history.'));
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (!cancelled) setHistoryLoading(false);
|
2026-05-07 03:40:29 +03:30
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [selectedAppointment?.patientId]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-19 22:29:29 +03:30
|
|
|
if (!selectedAppointment?.id) return;
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
void (async () => {
|
|
|
|
|
try {
|
|
|
|
|
await loadDraftForAppointment(selectedAppointment.id);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (!cancelled) {
|
|
|
|
|
setErrorBanner(formatApiErrorMessage(error, 'Failed to load treatment draft.'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [selectedAppointment?.id, loadDraftForAppointment]);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const fixActiveAfterCasesChange = useCallback((next: TreatmentCaseDraft[]) => {
|
|
|
|
|
setCases(next);
|
|
|
|
|
setActiveCaseId((id) => (next.some((c) => c.clientId === id) ? id : next[0].clientId));
|
2026-05-07 03:40:29 +03:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const toggleTooth = useCallback(
|
|
|
|
|
(fdi: FdiToothId) => {
|
2026-05-19 22:29:29 +03:30
|
|
|
if (!canEditTreatmentForDay) return;
|
|
|
|
|
setCases((prev) =>
|
|
|
|
|
prev.map((c) => {
|
|
|
|
|
if (c.clientId !== activeCaseId) return c;
|
|
|
|
|
const set = new Set(c.teeth);
|
2026-05-07 03:40:29 +03:30
|
|
|
if (set.has(fdi)) set.delete(fdi);
|
|
|
|
|
else set.add(fdi);
|
2026-05-19 22:29:29 +03:30
|
|
|
return { ...c, teeth: [...set].sort() as FdiToothId[] };
|
2026-05-07 03:40:29 +03:30
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-05-19 22:29:29 +03:30
|
|
|
[activeCaseId, canEditTreatmentForDay],
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const confirmDiscardIfDirty = useCallback(() => {
|
|
|
|
|
if (!isDirty) return true;
|
|
|
|
|
return window.confirm('You have unsaved changes. Discard them and continue?');
|
|
|
|
|
}, [isDirty]);
|
|
|
|
|
|
|
|
|
|
const onPickAppointment = useCallback(
|
|
|
|
|
(id: string) => {
|
|
|
|
|
if (!confirmDiscardIfDirty()) return;
|
|
|
|
|
setSelectionLocked(true);
|
|
|
|
|
setSelectedAppointmentId(id);
|
|
|
|
|
},
|
|
|
|
|
[confirmDiscardIfDirty],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onSelectDay = useCallback(
|
|
|
|
|
(day: Date) => {
|
|
|
|
|
if (!confirmDiscardIfDirty()) return;
|
|
|
|
|
setSelectedDay(day);
|
|
|
|
|
},
|
|
|
|
|
[confirmDiscardIfDirty],
|
|
|
|
|
);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
|
|
|
|
const addAttachments = useCallback(
|
2026-05-19 22:29:29 +03:30
|
|
|
async (files: FileList | null) => {
|
|
|
|
|
if (!files?.length || !canEditTreatmentForDay || !selectedAppointment) return;
|
|
|
|
|
setUploadBusy(true);
|
|
|
|
|
setErrorBanner(null);
|
|
|
|
|
try {
|
|
|
|
|
const uploaded = await treatmentsApi.uploadCaseAttachments(
|
|
|
|
|
selectedAppointment.id,
|
|
|
|
|
activeCaseId,
|
|
|
|
|
Array.from(files),
|
|
|
|
|
);
|
|
|
|
|
setCases((prev) =>
|
|
|
|
|
prev.map((c) => {
|
|
|
|
|
if (c.clientId !== activeCaseId) return c;
|
|
|
|
|
return { ...c, attachmentMetas: [...c.attachmentMetas, ...uploaded.data] };
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
setErrorBanner(formatApiErrorMessage(error, 'Failed to upload attachments.'));
|
|
|
|
|
} finally {
|
|
|
|
|
setUploadBusy(false);
|
|
|
|
|
}
|
2026-05-07 03:40:29 +03:30
|
|
|
},
|
2026-05-19 22:29:29 +03:30
|
|
|
[activeCaseId, canEditTreatmentForDay, selectedAppointment],
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const persistDraft = useCallback(async () => {
|
|
|
|
|
if (!selectedAppointment) {
|
|
|
|
|
throw new Error('No appointment selected');
|
|
|
|
|
}
|
|
|
|
|
const response = await treatmentsApi.saveDraft(selectedAppointment.id, {
|
|
|
|
|
cases: cases.map(({ clientId, id, treatmentType, teeth, comment, attachmentMetas }) => ({
|
|
|
|
|
clientId,
|
|
|
|
|
id,
|
|
|
|
|
treatmentType,
|
|
|
|
|
teeth,
|
|
|
|
|
comment,
|
|
|
|
|
attachmentIds: attachmentMetas.map((a) => a.id),
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
const mapped = response.data.cases.map(mapCaseFromApi);
|
|
|
|
|
setCases(mapped);
|
|
|
|
|
setActiveCaseId((prev) => {
|
|
|
|
|
const stillExists = mapped.some((c) => c.clientId === prev);
|
|
|
|
|
return stillExists ? prev : mapped[0]?.clientId ?? prev;
|
|
|
|
|
});
|
|
|
|
|
setSavedSnapshot(serializeCases(mapped));
|
|
|
|
|
return response.data;
|
|
|
|
|
}, [cases, selectedAppointment]);
|
|
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
const handleSaveAll = useCallback(async () => {
|
2026-05-19 22:29:29 +03:30
|
|
|
if (!canEditTreatmentForDay || !selectedAppointment) return;
|
2026-05-07 03:40:29 +03:30
|
|
|
setSaveBusy(true);
|
|
|
|
|
setBanner(null);
|
2026-05-19 22:29:29 +03:30
|
|
|
setErrorBanner(null);
|
2026-05-07 03:40:29 +03:30
|
|
|
try {
|
2026-05-19 22:29:29 +03:30
|
|
|
await persistDraft();
|
|
|
|
|
setBanner('Treatment draft saved. You can send cases later.');
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
setErrorBanner(formatApiErrorMessage(error, 'Failed to save treatment draft.'));
|
2026-05-07 03:40:29 +03:30
|
|
|
} finally {
|
|
|
|
|
setSaveBusy(false);
|
|
|
|
|
}
|
2026-05-19 22:29:29 +03:30
|
|
|
}, [canEditTreatmentForDay, selectedAppointment, persistDraft]);
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
const handleSendCase = useCallback(
|
|
|
|
|
async (treatmentCase: TreatmentCaseDraft) => {
|
|
|
|
|
if (!canEditTreatmentForDay || !selectedAppointment) return;
|
|
|
|
|
const targets = treatmentCase.sendToOrganizationIds.filter((id) =>
|
2026-05-07 03:40:29 +03:30
|
|
|
orgs.some((o) => o.id === id && o.active),
|
|
|
|
|
);
|
|
|
|
|
if (targets.length === 0) {
|
2026-05-19 22:29:29 +03:30
|
|
|
setErrorBanner('Choose at least one active organization to send this case.');
|
2026-05-07 03:40:29 +03:30
|
|
|
return;
|
|
|
|
|
}
|
2026-05-19 22:29:29 +03:30
|
|
|
setSendBusyId(treatmentCase.clientId);
|
2026-05-07 03:40:29 +03:30
|
|
|
setBanner(null);
|
2026-05-19 22:29:29 +03:30
|
|
|
setErrorBanner(null);
|
2026-05-07 03:40:29 +03:30
|
|
|
try {
|
2026-05-19 22:29:29 +03:30
|
|
|
const saved = await persistDraft();
|
|
|
|
|
const serverCase = saved.cases.find((c) => c.clientId === treatmentCase.clientId);
|
|
|
|
|
if (!serverCase?.id) {
|
|
|
|
|
throw new Error('Case must be saved before sending.');
|
|
|
|
|
}
|
|
|
|
|
const response = await treatmentsApi.sendCase(serverCase.id, { organizationIds: targets });
|
|
|
|
|
setCases((prev) => {
|
|
|
|
|
const next = prev.map((c) =>
|
|
|
|
|
c.clientId === treatmentCase.clientId
|
|
|
|
|
? {
|
|
|
|
|
...c,
|
|
|
|
|
id: response.data.id,
|
|
|
|
|
sentAt: response.data.sentAt,
|
|
|
|
|
sendToOrganizationIds: response.data.sendToOrganizationIds,
|
|
|
|
|
}
|
|
|
|
|
: c,
|
|
|
|
|
);
|
|
|
|
|
setSavedSnapshot(serializeCases(next));
|
|
|
|
|
return next;
|
2026-05-07 03:40:29 +03:30
|
|
|
});
|
2026-05-07 14:20:50 +03:30
|
|
|
setRecentOrganizationIds((prev) => {
|
2026-05-19 22:29:29 +03:30
|
|
|
const next = [...targets.filter((id) => id && !prev.includes(id)), ...prev];
|
2026-05-07 14:20:50 +03:30
|
|
|
return next.slice(0, 10);
|
|
|
|
|
});
|
2026-05-19 22:29:29 +03:30
|
|
|
setBanner('Case sent to selected organizations.');
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
setErrorBanner(formatApiErrorMessage(error, 'Failed to send case.'));
|
2026-05-07 03:40:29 +03:30
|
|
|
} finally {
|
|
|
|
|
setSendBusyId(null);
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-05-19 22:29:29 +03:30
|
|
|
[canEditTreatmentForDay, selectedAppointment, orgs, persistDraft, cases],
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
if (!canView) {
|
2026-05-07 03:40:29 +03:30
|
|
|
return (
|
|
|
|
|
<div className="surface-card p-6 max-w-xl">
|
|
|
|
|
<h2 className="text-lg font-semibold text-text-primary">Treatment workspace</h2>
|
|
|
|
|
<p className="text-sm text-text-secondary mt-2">
|
2026-05-19 22:29:29 +03:30
|
|
|
You do not have permission to view the Treatment tab for this organization.
|
2026-05-07 03:40:29 +03:30
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-05-08 14:07:21 +03:30
|
|
|
<div className="relative space-y-6 pb-24">
|
2026-05-07 03:40:29 +03:30
|
|
|
<header className="space-y-1">
|
|
|
|
|
<h1 className="text-2xl font-semibold text-text-primary">Treatment</h1>
|
|
|
|
|
<p className="text-sm text-text-secondary">
|
2026-05-19 22:29:29 +03:30
|
|
|
{canEdit
|
|
|
|
|
? 'Document cases for your appointments, save drafts, and send work to linked organizations.'
|
|
|
|
|
: 'View-only access — you can review appointments and treatment history but cannot edit.'}
|
2026-05-07 03:40:29 +03:30
|
|
|
</p>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<AppointmentsStrip
|
|
|
|
|
stripHidden={stripHidden}
|
|
|
|
|
onToggleStripHidden={() => setStripHidden((s) => !s)}
|
|
|
|
|
selectedDay={selectedDay}
|
2026-05-19 22:29:29 +03:30
|
|
|
onSelectDay={onSelectDay}
|
2026-05-07 03:40:29 +03:30
|
|
|
appointments={appointments}
|
|
|
|
|
selectedAppointmentId={selectedAppointmentId}
|
|
|
|
|
onSelectAppointment={onPickAppointment}
|
|
|
|
|
loading={apptsLoading}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-05-18 12:50:25 +03:30
|
|
|
{isViewingPastDay && (
|
|
|
|
|
<p className="text-sm text-text-secondary rounded-[var(--radius-md)] border border-border/60 bg-background-secondary/50 px-3 py-2">
|
2026-05-19 22:29:29 +03:30
|
|
|
Past days are view-only. You can review appointments and history, but treatment cases
|
2026-05-18 12:50:25 +03:30
|
|
|
cannot be added or changed.
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
<div className="grid grid-cols-1 xl:grid-cols-[minmax(280px,380px)_minmax(0,1fr)] gap-6 items-start">
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
{selectedAppointment ? (
|
|
|
|
|
<div className="surface-card p-4 space-y-1">
|
|
|
|
|
<p className="text-xs uppercase tracking-wide text-text-muted">Selected patient</p>
|
|
|
|
|
<p className="text-lg font-semibold text-text-primary">
|
|
|
|
|
{selectedAppointment.patientFirstName} {selectedAppointment.patientLastName}
|
|
|
|
|
</p>
|
|
|
|
|
<p className="text-xs text-text-secondary">
|
|
|
|
|
Appointment purpose:{' '}
|
|
|
|
|
<span className="capitalize text-text-primary">{selectedAppointment.purpose}</span>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="surface-card p-4 text-sm text-text-muted">
|
|
|
|
|
{apptsLoading ? 'Loading appointments…' : 'Select a day with at least one appointment.'}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-05-07 14:20:50 +03:30
|
|
|
<PastTreatmentsPanel
|
|
|
|
|
items={history}
|
|
|
|
|
loading={historyLoading}
|
|
|
|
|
selectedTreatmentId={reviewTreatment?.id ?? null}
|
|
|
|
|
onSelectTreatment={setReviewTreatment}
|
|
|
|
|
/>
|
|
|
|
|
<div className="surface-card p-4 space-y-3">
|
|
|
|
|
<div className="flex items-center justify-between gap-2">
|
|
|
|
|
<h3 className="text-sm font-semibold text-text-primary">Treatment review</h3>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="primary"
|
|
|
|
|
disabled={!selectedAppointment}
|
|
|
|
|
onClick={() => setReviewTreatment(currentDraftPreview)}
|
|
|
|
|
>
|
|
|
|
|
Preview current treatment
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
{!reviewTreatment && (
|
|
|
|
|
<p className="text-sm text-text-muted">
|
|
|
|
|
Select a treatment from history, or preview the current draft.
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
{reviewTreatment && (
|
|
|
|
|
<div className="border border-border/70 rounded-[var(--radius-md)] p-3 bg-background-secondary/40 space-y-2">
|
|
|
|
|
<div className="flex items-start justify-between gap-2">
|
|
|
|
|
<p className="text-sm font-medium text-text-primary">{reviewTreatment.title}</p>
|
|
|
|
|
<span className="text-xs text-text-muted tabular-nums">
|
|
|
|
|
{new Date(reviewTreatment.treatmentAt).toLocaleDateString()}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-xs text-text-secondary capitalize">Status: {reviewTreatment.status}</p>
|
|
|
|
|
<div className="space-y-2">
|
2026-05-19 22:29:29 +03:30
|
|
|
{reviewTreatment.cases.map((c, idx) => (
|
|
|
|
|
<div key={c.id} className="rounded-[var(--radius-sm)] border border-border/60 px-2.5 py-2">
|
|
|
|
|
<p className="text-xs text-text-primary font-medium">Case {idx + 1}</p>
|
2026-05-07 14:20:50 +03:30
|
|
|
<p className="text-xs text-text-secondary capitalize mt-1">
|
2026-05-19 22:29:29 +03:30
|
|
|
Type: {c.treatmentType}
|
2026-05-07 14:20:50 +03:30
|
|
|
</p>
|
|
|
|
|
<p className="text-xs text-text-secondary">
|
2026-05-19 22:29:29 +03:30
|
|
|
Teeth: {c.teeth.length ? [...c.teeth].sort().join(', ') : 'None selected'}
|
2026-05-07 14:20:50 +03:30
|
|
|
</p>
|
2026-05-19 22:29:29 +03:30
|
|
|
{c.notes && <p className="text-xs text-text-muted mt-1">Notes: {c.notes}</p>}
|
2026-05-07 14:20:50 +03:30
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-xs text-text-muted">
|
|
|
|
|
Attachments: {reviewTreatment.documents.length}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-4 min-w-0">
|
|
|
|
|
<FdiToothChart
|
|
|
|
|
selected={selectedTeethSet}
|
|
|
|
|
onToggle={toggleTooth}
|
2026-05-18 12:50:25 +03:30
|
|
|
disabled={!canEditTreatmentForDay}
|
2026-05-07 03:40:29 +03:30
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className="surface-card p-4 space-y-4">
|
|
|
|
|
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
|
|
|
<div>
|
2026-05-19 22:29:29 +03:30
|
|
|
<h3 className="text-sm font-semibold text-text-primary">Treatment cases</h3>
|
2026-05-07 03:40:29 +03:30
|
|
|
<p className="text-xs text-text-muted mt-0.5">
|
2026-05-19 22:29:29 +03:30
|
|
|
Each case has its own teeth, notes, attachments, and destinations for send.
|
2026-05-07 03:40:29 +03:30
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="primary"
|
2026-05-18 12:50:25 +03:30
|
|
|
disabled={!canEditTreatmentForDay}
|
2026-05-07 03:40:29 +03:30
|
|
|
onClick={() => {
|
2026-05-19 22:29:29 +03:30
|
|
|
const nextCase = newCase();
|
|
|
|
|
fixActiveAfterCasesChange([...cases, nextCase]);
|
|
|
|
|
setActiveCaseId(nextCase.clientId);
|
2026-05-07 03:40:29 +03:30
|
|
|
}}
|
|
|
|
|
>
|
2026-05-19 22:29:29 +03:30
|
|
|
Add case
|
2026-05-07 03:40:29 +03:30
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
2026-05-19 22:29:29 +03:30
|
|
|
{cases.map((c, idx) => (
|
2026-05-07 03:40:29 +03:30
|
|
|
<button
|
2026-05-19 22:29:29 +03:30
|
|
|
key={c.clientId}
|
2026-05-07 03:40:29 +03:30
|
|
|
type="button"
|
2026-05-19 22:29:29 +03:30
|
|
|
onClick={() => setActiveCaseId(c.clientId)}
|
2026-05-07 03:40:29 +03:30
|
|
|
className={`
|
|
|
|
|
rounded-[var(--radius-md)] border px-3 py-1.5 text-sm transition-colors
|
|
|
|
|
focus:outline-none focus-visible:ring-2 focus-visible:ring-primary/45
|
|
|
|
|
${
|
2026-05-19 22:29:29 +03:30
|
|
|
c.clientId === activeCaseId
|
2026-05-07 03:40:29 +03:30
|
|
|
? 'border-primary bg-primary-soft font-medium text-text-primary'
|
|
|
|
|
: 'border-border/70 text-text-secondary hover:border-border hover:bg-background-card/50'
|
|
|
|
|
}
|
|
|
|
|
`}
|
|
|
|
|
>
|
2026-05-19 22:29:29 +03:30
|
|
|
Case {idx + 1}
|
|
|
|
|
{c.sentAt ? ' · sent' : ''}
|
2026-05-07 03:40:29 +03:30
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
{activeCase && (
|
2026-05-07 03:40:29 +03:30
|
|
|
<div className="space-y-4 border border-border/60 rounded-[var(--radius-md)] p-4 bg-background-secondary/30">
|
|
|
|
|
<label className="block text-xs font-medium text-text-secondary">
|
|
|
|
|
Comments
|
|
|
|
|
<textarea
|
2026-05-19 22:29:29 +03:30
|
|
|
value={activeCase.comment}
|
2026-05-07 03:40:29 +03:30
|
|
|
onChange={(e) => {
|
|
|
|
|
const v = e.target.value;
|
2026-05-19 22:29:29 +03:30
|
|
|
setCases((prev) =>
|
|
|
|
|
prev.map((c) => (c.clientId === activeCaseId ? { ...c, comment: v } : c)),
|
2026-05-07 03:40:29 +03:30
|
|
|
);
|
|
|
|
|
}}
|
2026-05-19 22:29:29 +03:30
|
|
|
placeholder="Write clinical notes for this case…"
|
2026-05-07 03:40:29 +03:30
|
|
|
rows={5}
|
2026-05-19 22:29:29 +03:30
|
|
|
disabled={!canEditTreatmentForDay || Boolean(activeCase.sentAt)}
|
2026-05-07 03:40:29 +03:30
|
|
|
className="mt-1.5 w-full rounded-[var(--radius-md)] border border-border bg-background-secondary/90 text-text-primary text-sm px-3 py-2 placeholder:text-text-muted focus:outline-none focus:ring-2 focus:ring-primary/35 resize-y min-h-[120px]"
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
|
2026-05-07 14:20:50 +03:30
|
|
|
<div>
|
|
|
|
|
<Dropdown
|
|
|
|
|
label="Treatment type"
|
2026-05-19 22:29:29 +03:30
|
|
|
value={activeCase.treatmentType}
|
2026-05-07 14:20:50 +03:30
|
|
|
onChange={(e) => {
|
2026-05-19 22:29:29 +03:30
|
|
|
const nextType = e.target.value as TreatmentCaseDraft['treatmentType'];
|
|
|
|
|
setCases((prev) =>
|
|
|
|
|
prev.map((c) =>
|
|
|
|
|
c.clientId === activeCaseId ? { ...c, treatmentType: nextType } : c,
|
2026-05-07 14:20:50 +03:30
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}}
|
2026-05-19 22:29:29 +03:30
|
|
|
disabled={!canEditTreatmentForDay || Boolean(activeCase.sentAt)}
|
2026-05-07 14:20:50 +03:30
|
|
|
className="capitalize"
|
|
|
|
|
style={{ color: treatmentTypeTextColor }}
|
|
|
|
|
>
|
|
|
|
|
<option value="consultation" style={{ color: '#ddd6fe', backgroundColor: '#14253d' }} className="capitalize">consultation</option>
|
|
|
|
|
<option value="filling" style={{ color: '#fed7aa', backgroundColor: '#14253d' }} className="capitalize">filling</option>
|
|
|
|
|
<option value="endo" style={{ color: '#fecaca', backgroundColor: '#14253d' }} className="capitalize">endo</option>
|
|
|
|
|
<option value="visit" style={{ color: '#bae6fd', backgroundColor: '#14253d' }} className="capitalize">visit</option>
|
|
|
|
|
<option value="hygiene" style={{ color: '#d9f99d', backgroundColor: '#14253d' }} className="capitalize">hygiene</option>
|
|
|
|
|
</Dropdown>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
<div>
|
2026-05-19 22:29:29 +03:30
|
|
|
<p className="text-xs font-medium text-text-secondary mb-2">Attachments</p>
|
2026-05-07 03:40:29 +03:30
|
|
|
<input
|
|
|
|
|
ref={attachmentInputRef}
|
2026-05-19 22:29:29 +03:30
|
|
|
id="treatment-case-attachments"
|
2026-05-07 03:40:29 +03:30
|
|
|
type="file"
|
|
|
|
|
multiple
|
2026-05-19 22:29:29 +03:30
|
|
|
disabled={!canEditTreatmentForDay || uploadBusy || Boolean(activeCase.sentAt)}
|
2026-05-07 03:40:29 +03:30
|
|
|
onChange={(e) => {
|
2026-05-19 22:29:29 +03:30
|
|
|
void addAttachments(e.target.files);
|
2026-05-07 03:40:29 +03:30
|
|
|
e.target.value = '';
|
|
|
|
|
}}
|
|
|
|
|
className="sr-only"
|
2026-05-19 22:29:29 +03:30
|
|
|
aria-label="Attach files for this treatment case"
|
2026-05-07 03:40:29 +03:30
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="primary"
|
2026-05-19 22:29:29 +03:30
|
|
|
disabled={!canEditTreatmentForDay || uploadBusy || Boolean(activeCase.sentAt)}
|
|
|
|
|
isLoading={uploadBusy}
|
2026-05-07 03:40:29 +03:30
|
|
|
onClick={() => attachmentInputRef.current?.click()}
|
2026-05-19 22:29:29 +03:30
|
|
|
aria-controls="treatment-case-attachments"
|
2026-05-07 03:40:29 +03:30
|
|
|
>
|
|
|
|
|
Choose files
|
|
|
|
|
</Button>
|
2026-05-19 22:29:29 +03:30
|
|
|
{activeCase.attachmentMetas.length > 0 && (
|
2026-05-07 03:40:29 +03:30
|
|
|
<ul className="mt-2 space-y-1 text-xs text-text-muted">
|
2026-05-19 22:29:29 +03:30
|
|
|
{activeCase.attachmentMetas.map((f) => (
|
2026-05-07 03:40:29 +03:30
|
|
|
<li key={f.id} className="truncate">
|
|
|
|
|
{f.fileName} ({(f.sizeBytes / 1024).toFixed(1)} KB)
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-xs font-medium text-text-secondary mb-2">
|
2026-05-19 22:29:29 +03:30
|
|
|
Send this case to linked organizations
|
2026-05-07 03:40:29 +03:30
|
|
|
</p>
|
2026-05-07 14:20:50 +03:30
|
|
|
<div className="space-y-2 mb-2">
|
|
|
|
|
<SearchBar
|
|
|
|
|
value={organizationSearch}
|
|
|
|
|
onChange={setOrganizationSearch}
|
|
|
|
|
placeholder="Search active organizations..."
|
|
|
|
|
/>
|
|
|
|
|
{recentOrganizations.length > 0 && (
|
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
|
|
|
<span className="text-xs text-text-muted">Recent:</span>
|
|
|
|
|
{recentOrganizations.map((o) => (
|
|
|
|
|
<button
|
|
|
|
|
key={o.id}
|
|
|
|
|
type="button"
|
2026-05-19 22:29:29 +03:30
|
|
|
disabled={!canEditTreatmentForDay || Boolean(activeCase.sentAt)}
|
2026-05-07 14:20:50 +03:30
|
|
|
onClick={() => {
|
2026-05-19 22:29:29 +03:30
|
|
|
setCases((prev) =>
|
|
|
|
|
prev.map((c) => {
|
|
|
|
|
if (c.clientId !== activeCaseId || c.sentAt) return c;
|
|
|
|
|
if (c.sendToOrganizationIds.includes(o.id)) return c;
|
|
|
|
|
return { ...c, sendToOrganizationIds: [...c.sendToOrganizationIds, o.id] };
|
2026-05-07 14:20:50 +03:30
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}}
|
2026-05-19 22:29:29 +03:30
|
|
|
className="text-xs rounded-[var(--radius-sm)] border border-border/70 px-2 py-1 text-text-secondary hover:text-text-primary hover:border-border focus:outline-none focus-visible:ring-2 focus-visible:ring-primary/35 disabled:opacity-50"
|
2026-05-07 14:20:50 +03:30
|
|
|
>
|
|
|
|
|
{o.name}
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-05-07 03:40:29 +03:30
|
|
|
<div className="flex flex-col gap-2">
|
2026-05-07 14:20:50 +03:30
|
|
|
{filteredOrganizations.map((o) => (
|
2026-05-07 03:40:29 +03:30
|
|
|
<Checkbox
|
|
|
|
|
key={o.id}
|
2026-05-19 22:29:29 +03:30
|
|
|
checked={activeCase.sendToOrganizationIds.includes(o.id)}
|
|
|
|
|
disabled={!canEditTreatmentForDay || Boolean(activeCase.sentAt)}
|
2026-05-07 03:40:29 +03:30
|
|
|
onChange={(checked) => {
|
2026-05-19 22:29:29 +03:30
|
|
|
setCases((prev) =>
|
|
|
|
|
prev.map((c) => {
|
|
|
|
|
if (c.clientId !== activeCaseId) return c;
|
|
|
|
|
const next = new Set(c.sendToOrganizationIds);
|
2026-05-07 03:40:29 +03:30
|
|
|
if (checked) next.add(o.id);
|
|
|
|
|
else next.delete(o.id);
|
2026-05-19 22:29:29 +03:30
|
|
|
return { ...c, sendToOrganizationIds: [...next] };
|
2026-05-07 03:40:29 +03:30
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}}
|
2026-05-19 22:29:29 +03:30
|
|
|
label={o.name}
|
2026-05-07 03:40:29 +03:30
|
|
|
/>
|
|
|
|
|
))}
|
2026-05-07 14:20:50 +03:30
|
|
|
{filteredOrganizations.length === 0 && (
|
|
|
|
|
<p className="text-xs text-text-muted">No active organization matches your search.</p>
|
|
|
|
|
)}
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-wrap items-center gap-3 pt-1">
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="primary"
|
2026-05-19 22:29:29 +03:30
|
|
|
disabled={
|
|
|
|
|
!canEditTreatmentForDay ||
|
|
|
|
|
Boolean(activeCase.sentAt) ||
|
|
|
|
|
sendBusyId === activeCase.clientId
|
|
|
|
|
}
|
|
|
|
|
isLoading={sendBusyId === activeCase.clientId}
|
|
|
|
|
onClick={() => void handleSendCase(activeCase)}
|
2026-05-07 03:40:29 +03:30
|
|
|
>
|
2026-05-19 22:29:29 +03:30
|
|
|
Send this case
|
2026-05-07 03:40:29 +03:30
|
|
|
</Button>
|
2026-05-19 22:29:29 +03:30
|
|
|
{activeCase.sentAt && (
|
2026-05-07 03:40:29 +03:30
|
|
|
<span className="text-xs text-text-muted">
|
2026-05-19 22:29:29 +03:30
|
|
|
Sent at {new Date(activeCase.sentAt).toLocaleString()}
|
2026-05-07 03:40:29 +03:30
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
{canEdit && (
|
|
|
|
|
<div className="flex flex-wrap gap-3 pt-2 border-t border-border/60">
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="primary"
|
|
|
|
|
disabled={!canEditTreatmentForDay || saveBusy}
|
|
|
|
|
isLoading={saveBusy}
|
|
|
|
|
onClick={() => void handleSaveAll()}
|
|
|
|
|
>
|
|
|
|
|
Save treatment draft
|
|
|
|
|
</Button>
|
|
|
|
|
<p className="text-xs text-text-muted self-center">
|
|
|
|
|
{isDirty ? 'Unsaved changes' : 'Draft saved'}. Sending is per case and saves first automatically.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2026-05-08 14:07:21 +03:30
|
|
|
|
2026-05-19 22:29:29 +03:30
|
|
|
{(banner || errorBanner) && (
|
2026-05-08 14:07:21 +03:30
|
|
|
<div className="fixed bottom-4 left-0 right-0 z-[70] px-4 pointer-events-none">
|
|
|
|
|
<div className="pointer-events-auto w-full">
|
2026-05-19 22:29:29 +03:30
|
|
|
{banner && <Toast variant="success">{banner}</Toast>}
|
|
|
|
|
{errorBanner && (
|
|
|
|
|
<div className={banner ? 'mt-2' : ''}>
|
|
|
|
|
<Toast variant="danger">{errorBanner}</Toast>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-05-08 14:07:21 +03:30
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|