2026-05-07 03:40:29 +03:30
|
|
|
'use client';
|
|
|
|
|
|
2026-07-13 01:03:26 +03:30
|
|
|
import { useMemo, useState } from 'react';
|
2026-07-14 01:27:11 +03:30
|
|
|
import { useLocale, useTranslations } from 'next-intl';
|
2026-07-13 01:03:26 +03:30
|
|
|
import { Button } from '@/components/ui/shared/Button';
|
|
|
|
|
import { Checkbox } from '@/components/ui/shared/Checkbox';
|
|
|
|
|
import { FORM_SELECT_CLASS } from '@/components/shared/formSelectStyles';
|
2026-06-28 21:45:33 +03:30
|
|
|
import { TreatmentHistoryDetailLine } from '@/components/ui/treatment/TreatmentHistoryDetailLine';
|
2026-07-13 01:03:26 +03:30
|
|
|
import { DetailLabSendBadge } from '@/components/ui/treatment/DetailLabSendBadge';
|
|
|
|
|
import { filterTreatmentHistoryItems } from '@/components/treatment/treatmentHistoryFilters';
|
2026-07-14 01:27:11 +03:30
|
|
|
import { AppDateInput } from '@/components/ui/shared/AppDateInput';
|
|
|
|
|
import { APP_DATE, formatAppDateTime } from '@/lib/i18n/format';
|
2026-07-13 01:03:26 +03:30
|
|
|
import type { LinkedOrganizationOption, PastTreatment } from '@/types/treatment';
|
2026-07-06 20:40:19 +03:30
|
|
|
import type { TreatmentCatalogEntry } from '@/types/treatment-catalog';
|
2026-06-20 14:51:43 +03:30
|
|
|
|
2026-05-07 03:40:29 +03:30
|
|
|
interface PastTreatmentsPanelProps {
|
|
|
|
|
items: PastTreatment[];
|
2026-07-13 01:03:26 +03:30
|
|
|
patientName?: string;
|
|
|
|
|
currentAppointmentId?: string | null;
|
2026-07-06 20:40:19 +03:30
|
|
|
treatmentCatalog: TreatmentCatalogEntry[];
|
2026-07-13 01:03:26 +03:30
|
|
|
labDependentCodes: Set<string>;
|
|
|
|
|
orgs?: LinkedOrganizationOption[];
|
2026-05-07 03:40:29 +03:30
|
|
|
loading?: boolean;
|
2026-06-28 21:45:33 +03:30
|
|
|
selectedPreviewId?: string | null;
|
|
|
|
|
onSelectTreatment?: (treatment: PastTreatment) => void;
|
2026-07-13 22:53:23 +03:30
|
|
|
compact?: boolean;
|
2026-05-07 03:40:29 +03:30
|
|
|
}
|
|
|
|
|
|
2026-07-14 01:27:11 +03:30
|
|
|
function formatHistoryTimestamp(iso: string, locale: string): string {
|
|
|
|
|
return formatAppDateTime(iso, locale, APP_DATE.history);
|
2026-07-13 01:03:26 +03:30
|
|
|
}
|
|
|
|
|
|
2026-05-07 14:20:50 +03:30
|
|
|
export function PastTreatmentsPanel({
|
|
|
|
|
items,
|
2026-07-13 01:03:26 +03:30
|
|
|
patientName,
|
|
|
|
|
currentAppointmentId,
|
2026-07-06 20:40:19 +03:30
|
|
|
treatmentCatalog,
|
2026-07-13 01:03:26 +03:30
|
|
|
labDependentCodes,
|
|
|
|
|
orgs,
|
2026-05-07 14:20:50 +03:30
|
|
|
loading,
|
2026-06-28 21:45:33 +03:30
|
|
|
selectedPreviewId,
|
|
|
|
|
onSelectTreatment,
|
2026-07-13 22:53:23 +03:30
|
|
|
compact = false,
|
2026-05-07 14:20:50 +03:30
|
|
|
}: PastTreatmentsPanelProps) {
|
2026-07-14 01:27:11 +03:30
|
|
|
const locale = useLocale();
|
2026-06-20 14:51:43 +03:30
|
|
|
const t = useTranslations('treatment');
|
2026-07-13 01:03:26 +03:30
|
|
|
const [notShippedOnly, setNotShippedOnly] = useState(false);
|
|
|
|
|
const [filterDate, setFilterDate] = useState('');
|
2026-07-13 22:53:23 +03:30
|
|
|
const [filtersOpen, setFiltersOpen] = useState(false);
|
2026-07-13 01:03:26 +03:30
|
|
|
|
|
|
|
|
const hasActiveFilters = notShippedOnly || Boolean(filterDate);
|
|
|
|
|
|
|
|
|
|
const displayedItems = useMemo(
|
|
|
|
|
() =>
|
2026-07-14 15:09:10 +03:30
|
|
|
filterTreatmentHistoryItems(items, labDependentCodes, {
|
2026-07-13 01:03:26 +03:30
|
|
|
notShippedOnly,
|
|
|
|
|
date: filterDate,
|
2026-07-14 15:09:10 +03:30
|
|
|
}, currentAppointmentId),
|
|
|
|
|
[items, currentAppointmentId, labDependentCodes, notShippedOnly, filterDate],
|
2026-07-13 01:03:26 +03:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function clearFilters() {
|
|
|
|
|
setNotShippedOnly(false);
|
|
|
|
|
setFilterDate('');
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 01:27:11 +03:30
|
|
|
const filterInputClass = `${FORM_SELECT_CLASS} rounded-md py-1.5 text-xs min-w-[9.5rem]`;
|
2026-06-20 14:51:43 +03:30
|
|
|
|
2026-07-13 22:53:23 +03:30
|
|
|
const filtersBlock = (
|
|
|
|
|
<div
|
|
|
|
|
className={`flex flex-wrap items-center gap-x-3 gap-y-2 rounded-[var(--radius-md)] border border-border/60 bg-background-secondary/30 ${
|
|
|
|
|
compact ? 'p-2' : 'p-2.5'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={notShippedOnly}
|
|
|
|
|
onChange={setNotShippedOnly}
|
|
|
|
|
label={t('historyFilterNotShipped')}
|
|
|
|
|
className="text-xs [&_span:last-child]:text-xs shrink-0"
|
|
|
|
|
/>
|
|
|
|
|
<label className="flex items-center gap-1.5 shrink-0">
|
|
|
|
|
<span className="text-xs font-medium text-text-muted whitespace-nowrap">
|
|
|
|
|
{t('historyFilterDate')}
|
|
|
|
|
</span>
|
2026-07-14 01:27:11 +03:30
|
|
|
<AppDateInput
|
2026-07-13 22:53:23 +03:30
|
|
|
value={filterDate}
|
2026-07-14 01:27:11 +03:30
|
|
|
onChange={setFilterDate}
|
2026-07-13 22:53:23 +03:30
|
|
|
className={filterInputClass}
|
2026-07-13 01:03:26 +03:30
|
|
|
/>
|
2026-07-13 22:53:23 +03:30
|
|
|
</label>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={clearFilters}
|
|
|
|
|
disabled={!hasActiveFilters}
|
|
|
|
|
className="shrink-0"
|
|
|
|
|
>
|
|
|
|
|
{t('historyClearFilters')}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-07-13 01:03:26 +03:30
|
|
|
|
2026-07-13 22:53:23 +03:30
|
|
|
const listBlock = (
|
|
|
|
|
<>
|
|
|
|
|
{loading && <p className="text-xs text-text-muted">{t('loadingHistory')}</p>}
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-07-13 01:03:26 +03:30
|
|
|
{!loading && displayedItems.length === 0 && (
|
2026-07-13 22:53:23 +03:30
|
|
|
<p className="text-xs text-text-muted">
|
2026-07-13 01:03:26 +03:30
|
|
|
{hasActiveFilters ? t('historyFilterEmpty') : t('historyEmpty')}
|
|
|
|
|
</p>
|
2026-05-07 03:40:29 +03:30
|
|
|
)}
|
|
|
|
|
|
2026-07-13 22:53:23 +03:30
|
|
|
<div
|
|
|
|
|
className={`space-y-1 overflow-y-auto overscroll-y-contain pr-1 ${
|
|
|
|
|
compact ? 'max-h-[min(220px,32vh)]' : 'max-h-[min(420px,50vh)]'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
2026-07-13 01:03:26 +03:30
|
|
|
{displayedItems.map((treatment) => {
|
2026-06-28 21:45:33 +03:30
|
|
|
const isSelected = selectedPreviewId === treatment.id;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<article
|
|
|
|
|
key={treatment.id}
|
|
|
|
|
role="button"
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
onClick={() => onSelectTreatment?.(treatment)}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
onSelectTreatment?.(treatment);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
className={`
|
|
|
|
|
border rounded-[var(--radius-sm)] px-2 py-1.5 cursor-pointer transition-colors
|
|
|
|
|
focus:outline-none focus-visible:ring-2 focus-visible:ring-primary/45
|
|
|
|
|
${
|
|
|
|
|
isSelected
|
|
|
|
|
? 'border-primary bg-primary/5'
|
|
|
|
|
: 'border-border/60 bg-background-secondary/30 hover:border-border hover:bg-background-secondary/50'
|
|
|
|
|
}
|
|
|
|
|
`}
|
|
|
|
|
>
|
2026-07-13 01:03:26 +03:30
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
|
|
|
<time
|
|
|
|
|
className="text-xs font-medium text-text-primary tabular-nums"
|
|
|
|
|
dateTime={treatment.treatmentAt}
|
|
|
|
|
>
|
2026-07-14 01:27:11 +03:30
|
|
|
{formatHistoryTimestamp(treatment.treatmentAt, locale)}
|
2026-07-13 01:03:26 +03:30
|
|
|
</time>
|
|
|
|
|
</div>
|
2026-05-07 03:40:29 +03:30
|
|
|
|
2026-06-28 21:45:33 +03:30
|
|
|
{treatment.details.length === 0 ? (
|
|
|
|
|
<p className="text-[10px] text-text-muted mt-1">{t('noDetails')}</p>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="mt-1.5 divide-y divide-border/50 border-t border-border/40 pointer-events-none">
|
|
|
|
|
{treatment.details.map((detail, idx) => (
|
2026-07-13 01:03:26 +03:30
|
|
|
<div key={`${detail.clientId ?? detail.id}-${idx}`} className="py-1.5 space-y-1">
|
2026-06-28 21:45:33 +03:30
|
|
|
<TreatmentHistoryDetailLine
|
|
|
|
|
detail={detail}
|
|
|
|
|
detailNumber={idx + 1}
|
2026-07-06 20:40:19 +03:30
|
|
|
treatmentCatalog={treatmentCatalog}
|
2026-06-28 21:45:33 +03:30
|
|
|
/>
|
2026-07-13 01:03:26 +03:30
|
|
|
<DetailLabSendBadge
|
|
|
|
|
detail={detail}
|
|
|
|
|
labDependentCodes={labDependentCodes}
|
|
|
|
|
orgs={orgs}
|
|
|
|
|
className="text-[10px] px-1.5 py-0"
|
|
|
|
|
/>
|
2026-05-19 23:43:43 +03:30
|
|
|
</div>
|
2026-06-28 21:45:33 +03:30
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</article>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
2026-07-13 22:53:23 +03:30
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (compact) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-2 pt-2">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setFiltersOpen((open) => !open)}
|
|
|
|
|
className="text-[10px] font-medium text-primary hover:underline"
|
|
|
|
|
>
|
|
|
|
|
{filtersOpen ? t('historyHideFilters') : t('historyShowFilters')}
|
|
|
|
|
</button>
|
|
|
|
|
{filtersOpen ? filtersBlock : null}
|
|
|
|
|
{listBlock}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="surface-card p-4 space-y-3">
|
|
|
|
|
<div>
|
|
|
|
|
<h3 className="text-sm font-semibold text-text-primary">
|
|
|
|
|
{patientName ? t('historyPatientScope', { patientName }) : t('historyTitle')}
|
|
|
|
|
</h3>
|
|
|
|
|
<p className="text-[11px] text-text-muted mt-0.5">{t('historySubtitle')}</p>
|
|
|
|
|
</div>
|
|
|
|
|
{filtersBlock}
|
|
|
|
|
{listBlock}
|
2026-05-07 03:40:29 +03:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|