2026-06-28 23:19:33 +03:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
|
import { useTranslations } from 'next-intl';
|
|
|
|
|
import { formatApiErrorMessage } from '@/components/shared/formatApiError';
|
2026-07-08 02:53:47 +03:30
|
|
|
import { canEditCases } from '@/components/shared/permissions';
|
2026-06-28 23:19:33 +03:30
|
|
|
import { useAuth } from '@/lib/hooks/useAuth';
|
|
|
|
|
import { useToast } from '@/lib/hooks/useToast';
|
|
|
|
|
import { organizationApi } from '@/lib/api/organization';
|
2026-07-06 20:40:19 +03:30
|
|
|
import { treatmentCatalogApi } from '@/lib/api/treatment-catalog';
|
|
|
|
|
import { treatmentTypeLabelFromCatalog } from '@/components/ui/treatment/treatmentTypeDisplay';
|
2026-06-28 23:19:33 +03:30
|
|
|
import { Button } from '@/components/ui/shared/Button';
|
2026-07-11 17:10:02 +03:30
|
|
|
import { MobileDetailBackButton } from '@/components/ui/shared/MobileDetailBackButton';
|
2026-06-28 23:19:33 +03:30
|
|
|
import { SearchBar } from '@/components/ui/shared/SearchBar';
|
|
|
|
|
import { ToastStack } from '@/components/ui/shared/Toast';
|
2026-07-08 02:53:47 +03:30
|
|
|
import { CaseDetailPanel, CaseTaskProgressBar } from '@/components/ui/lab/CaseDetailPanel';
|
2026-07-07 15:31:09 +03:30
|
|
|
import { LabCaseCommentsPanel } from '@/components/ui/lab/LabCaseCommentsPanel';
|
|
|
|
|
import {
|
2026-07-08 02:53:47 +03:30
|
|
|
formatCaseDateTime,
|
|
|
|
|
formatPatientName,
|
|
|
|
|
} from '@/components/ui/lab/caseDetailUtils';
|
2026-07-07 18:43:10 +03:30
|
|
|
import { treatmentsApi } from '@/lib/api/treatments';
|
2026-07-08 02:53:47 +03:30
|
|
|
import { casesApi } from '@/lib/api/cases';
|
2026-06-28 23:19:33 +03:30
|
|
|
import type { CounterpartItemDto } from '@/lib/api/organization';
|
|
|
|
|
import type { LabCaseDetail, LabCaseListItem, LabTaskStatus } from '@/types/cases';
|
2026-07-06 20:40:19 +03:30
|
|
|
import type { TreatmentCatalogEntry } from '@/types/treatment-catalog';
|
2026-06-28 23:19:33 +03:30
|
|
|
|
|
|
|
|
const PAGE_SIZE = 20;
|
|
|
|
|
|
|
|
|
|
interface ConnectionCaseHistoryContentProps {
|
|
|
|
|
connection: CounterpartItemDto;
|
|
|
|
|
onBack: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ConnectionCaseHistoryContent({
|
|
|
|
|
connection,
|
|
|
|
|
onBack,
|
|
|
|
|
}: ConnectionCaseHistoryContentProps) {
|
|
|
|
|
const t = useTranslations('organizations');
|
|
|
|
|
const tCases = useTranslations('cases');
|
|
|
|
|
const tCommon = useTranslations('common');
|
|
|
|
|
const { currentOrganization, user } = useAuth();
|
|
|
|
|
const { showError, setError, messages: toastMessages } = useToast();
|
|
|
|
|
|
|
|
|
|
const [search, setSearch] = useState('');
|
|
|
|
|
const [page, setPage] = useState(1);
|
|
|
|
|
const [cases, setCases] = useState<LabCaseListItem[]>([]);
|
|
|
|
|
const [pagination, setPagination] = useState({
|
|
|
|
|
page: 1,
|
|
|
|
|
limit: PAGE_SIZE,
|
|
|
|
|
total: 0,
|
|
|
|
|
totalPages: 1,
|
|
|
|
|
});
|
|
|
|
|
const [selectedCaseId, setSelectedCaseId] = useState<string | null>(null);
|
2026-07-11 17:10:02 +03:30
|
|
|
const [mobileDetailOpen, setMobileDetailOpen] = useState(false);
|
2026-06-28 23:19:33 +03:30
|
|
|
const [selectedCase, setSelectedCase] = useState<LabCaseDetail | null>(null);
|
2026-07-06 20:40:19 +03:30
|
|
|
const [treatmentCatalog, setTreatmentCatalog] = useState<TreatmentCatalogEntry[]>([]);
|
2026-06-28 23:19:33 +03:30
|
|
|
const [loadingList, setLoadingList] = useState(false);
|
|
|
|
|
const [loadingDetail, setLoadingDetail] = useState(false);
|
2026-07-08 02:53:47 +03:30
|
|
|
const [updatingImportant, setUpdatingImportant] = useState(false);
|
2026-07-07 17:14:31 +03:30
|
|
|
const [commentCount, setCommentCount] = useState(0);
|
2026-06-28 23:19:33 +03:30
|
|
|
|
|
|
|
|
const locale = user?.language ?? 'en';
|
|
|
|
|
const isClinic = currentOrganization?.type === 'CLINIC';
|
2026-07-08 02:53:47 +03:30
|
|
|
const canEditImportant = !isClinic && canEditCases(currentOrganization);
|
2026-06-28 23:19:33 +03:30
|
|
|
|
|
|
|
|
const tRef = useRef(t);
|
|
|
|
|
tRef.current = t;
|
|
|
|
|
|
|
|
|
|
const treatmentLabel = useCallback(
|
2026-07-06 20:40:19 +03:30
|
|
|
(type: string) => treatmentTypeLabelFromCatalog(type, treatmentCatalog),
|
|
|
|
|
[treatmentCatalog],
|
2026-06-28 23:19:33 +03:30
|
|
|
);
|
|
|
|
|
|
2026-07-06 20:40:19 +03:30
|
|
|
useEffect(() => {
|
|
|
|
|
void treatmentCatalogApi.list().then((r) => setTreatmentCatalog(r.data)).catch(() => {});
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-06-28 23:19:33 +03:30
|
|
|
const statusOptions: { value: LabTaskStatus; label: string }[] = useMemo(
|
|
|
|
|
() => [
|
|
|
|
|
{ value: 'IN_PROGRESS', label: tCases('statusInProgress') },
|
|
|
|
|
{ value: 'COMPLETED', label: tCases('statusCompleted') },
|
|
|
|
|
],
|
|
|
|
|
[tCases],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
|
|
|
|
void (async () => {
|
|
|
|
|
setLoadingList(true);
|
|
|
|
|
setError('');
|
|
|
|
|
try {
|
|
|
|
|
const response = await organizationApi.listConnectionCases(connection.id, {
|
|
|
|
|
q: search.trim() || undefined,
|
|
|
|
|
page,
|
|
|
|
|
limit: PAGE_SIZE,
|
|
|
|
|
});
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
setCases(response.data.items);
|
|
|
|
|
setPagination(response.data.pagination);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
showError(formatApiErrorMessage(error, tRef.current('caseHistoryErrorLoadList')));
|
|
|
|
|
} finally {
|
|
|
|
|
if (!cancelled) setLoadingList(false);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
}, search ? 300 : 0);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
};
|
|
|
|
|
}, [search, page, connection.id, showError, setError]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!selectedCaseId) {
|
|
|
|
|
setSelectedCase(null);
|
2026-07-07 17:14:31 +03:30
|
|
|
setCommentCount(0);
|
2026-06-28 23:19:33 +03:30
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
|
2026-07-07 17:14:31 +03:30
|
|
|
void organizationApi
|
|
|
|
|
.listConnectionCaseComments(connection.id, selectedCaseId)
|
|
|
|
|
.then((r) => {
|
|
|
|
|
if (!cancelled) setCommentCount(r.data.length);
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
if (!cancelled) setCommentCount(0);
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-28 23:19:33 +03:30
|
|
|
void (async () => {
|
|
|
|
|
setLoadingDetail(true);
|
|
|
|
|
setError('');
|
|
|
|
|
try {
|
|
|
|
|
const response = await organizationApi.getConnectionCase(connection.id, selectedCaseId);
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
setSelectedCase(response.data);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
showError(formatApiErrorMessage(error, tRef.current('caseHistoryErrorLoadDetail')));
|
|
|
|
|
setSelectedCase(null);
|
|
|
|
|
} finally {
|
|
|
|
|
if (!cancelled) setLoadingDetail(false);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [selectedCaseId, connection.id, showError, setError]);
|
|
|
|
|
|
2026-07-11 17:10:02 +03:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!selectedCaseId) {
|
|
|
|
|
setMobileDetailOpen(false);
|
|
|
|
|
}
|
|
|
|
|
}, [selectedCaseId]);
|
|
|
|
|
|
2026-07-07 17:14:31 +03:30
|
|
|
function scrollToComments() {
|
|
|
|
|
document.getElementById('case-comments')?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-07 18:43:10 +03:30
|
|
|
const loadClinicAttachmentBlob = useCallback(
|
|
|
|
|
(_caseId: string, attachmentId: string) => treatmentsApi.getAttachmentFileBlob(attachmentId),
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-08 02:53:47 +03:30
|
|
|
async function handleCaseImportantToggle(isImportant: boolean) {
|
|
|
|
|
if (!selectedCaseId || !canEditImportant || !selectedCase) return;
|
2026-07-07 18:43:10 +03:30
|
|
|
|
2026-07-08 02:53:47 +03:30
|
|
|
const previousCase = selectedCase;
|
|
|
|
|
setSelectedCase({ ...selectedCase, isImportant });
|
|
|
|
|
|
|
|
|
|
setUpdatingImportant(true);
|
|
|
|
|
setError('');
|
|
|
|
|
try {
|
|
|
|
|
const response = await casesApi.setCaseImportant(selectedCaseId, isImportant);
|
|
|
|
|
setSelectedCase(response.data);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
setSelectedCase(previousCase);
|
|
|
|
|
showError(formatApiErrorMessage(error, tCases('errorUpdateTask')));
|
|
|
|
|
} finally {
|
|
|
|
|
setUpdatingImportant(false);
|
2026-07-07 18:43:10 +03:30
|
|
|
}
|
2026-07-08 02:53:47 +03:30
|
|
|
}
|
2026-07-07 18:43:10 +03:30
|
|
|
|
2026-06-28 23:19:33 +03:30
|
|
|
return (
|
|
|
|
|
<div className="space-y-6">
|
|
|
|
|
<div>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onBack}
|
|
|
|
|
className="text-sm text-primary hover:opacity-90"
|
|
|
|
|
>
|
|
|
|
|
{t('caseHistoryBackToConnections')}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
2026-07-11 17:10:02 +03:30
|
|
|
<h1 className="text-xl sm:text-2xl font-semibold text-text-primary">
|
2026-06-28 23:19:33 +03:30
|
|
|
{t('caseHistoryTitle', { name: connection.organizationName })}
|
|
|
|
|
</h1>
|
|
|
|
|
<p className="text-sm text-text-secondary mt-1">
|
|
|
|
|
{isClinic ? t('caseHistorySubtitleClinic') : t('caseHistorySubtitleLab')}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<ToastStack {...toastMessages} />
|
|
|
|
|
|
|
|
|
|
<div className="grid gap-4 lg:grid-cols-[minmax(300px,380px)_1fr]">
|
2026-07-11 17:10:02 +03:30
|
|
|
<section
|
|
|
|
|
className={`rounded-lg border border-border bg-surface p-3 sm:p-4 space-y-3 flex flex-col min-h-0 ${
|
|
|
|
|
mobileDetailOpen && selectedCaseId ? 'hidden lg:flex' : 'flex'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
2026-06-28 23:19:33 +03:30
|
|
|
<SearchBar
|
|
|
|
|
embedded
|
|
|
|
|
value={search}
|
|
|
|
|
onChange={(value) => {
|
|
|
|
|
setSearch(value);
|
|
|
|
|
setPage(1);
|
|
|
|
|
}}
|
|
|
|
|
placeholder={tCases('searchPlaceholder')}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className="flex-1 min-h-0">
|
|
|
|
|
{loadingList ? (
|
|
|
|
|
<p className="text-sm text-text-muted">{tCommon('loading')}</p>
|
|
|
|
|
) : cases.length === 0 ? (
|
|
|
|
|
<p className="text-sm text-text-muted">{t('caseHistoryEmpty')}</p>
|
|
|
|
|
) : (
|
|
|
|
|
<ul className="space-y-2 max-h-[55vh] overflow-y-auto pr-1">
|
|
|
|
|
{cases.map((item) => {
|
|
|
|
|
const isActive = item.id === selectedCaseId;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<li key={item.id}>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-07-11 17:10:02 +03:30
|
|
|
onClick={() => {
|
|
|
|
|
setSelectedCaseId(item.id);
|
|
|
|
|
setMobileDetailOpen(true);
|
|
|
|
|
}}
|
2026-06-28 23:19:33 +03:30
|
|
|
className={`w-full rounded-md border px-3 py-2.5 text-left transition-colors ${
|
|
|
|
|
isActive
|
|
|
|
|
? 'border-primary bg-primary/5'
|
|
|
|
|
: 'border-border hover:border-primary/40'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
<div className="font-medium text-text-primary">
|
|
|
|
|
{formatPatientName(item.patient)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-xs text-text-muted mt-0.5">{item.patient.mobile}</div>
|
|
|
|
|
{!isClinic ? (
|
|
|
|
|
<div className="text-xs text-text-muted mt-0.5">{item.clinic.name}</div>
|
|
|
|
|
) : null}
|
|
|
|
|
<div className="text-xs text-text-muted mt-1">
|
2026-07-08 02:53:47 +03:30
|
|
|
{formatCaseDateTime(item.sentAt, locale)}
|
2026-06-28 23:19:33 +03:30
|
|
|
</div>
|
|
|
|
|
<div className="text-xs text-text-muted mt-1 truncate">
|
2026-07-10 15:26:36 +03:30
|
|
|
{item.treatmentType ? treatmentLabel(item.treatmentType) : '—'}
|
2026-06-28 23:19:33 +03:30
|
|
|
</div>
|
|
|
|
|
<div className="mt-2">
|
2026-07-08 02:53:47 +03:30
|
|
|
<CaseTaskProgressBar
|
2026-06-28 23:19:33 +03:30
|
|
|
completed={item.taskProgress.completed}
|
|
|
|
|
total={item.taskProgress.total}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
</li>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</ul>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{pagination.totalPages > 1 ? (
|
|
|
|
|
<div className="flex items-center justify-between gap-2 pt-2 border-t border-border">
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
disabled={page <= 1 || loadingList}
|
|
|
|
|
onClick={() => setPage((p) => Math.max(1, p - 1))}
|
|
|
|
|
>
|
|
|
|
|
{tCases('prevPage')}
|
|
|
|
|
</Button>
|
|
|
|
|
<span className="text-xs text-text-muted text-center">
|
|
|
|
|
{tCases('pageSummary', {
|
|
|
|
|
page: pagination.page,
|
|
|
|
|
totalPages: pagination.totalPages,
|
|
|
|
|
total: pagination.total,
|
|
|
|
|
})}
|
|
|
|
|
</span>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
disabled={page >= pagination.totalPages || loadingList}
|
|
|
|
|
onClick={() => setPage((p) => p + 1)}
|
|
|
|
|
>
|
|
|
|
|
{tCases('nextPage')}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</section>
|
|
|
|
|
|
2026-07-11 17:10:02 +03:30
|
|
|
<section
|
|
|
|
|
className={`rounded-lg border border-border bg-surface p-3 sm:p-4 min-h-[320px] lg:min-h-[420px] ${
|
|
|
|
|
selectedCaseId && !mobileDetailOpen ? 'hidden lg:block' : ''
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{mobileDetailOpen && selectedCaseId ? (
|
|
|
|
|
<MobileDetailBackButton onClick={() => setMobileDetailOpen(false)} />
|
|
|
|
|
) : null}
|
2026-06-28 23:19:33 +03:30
|
|
|
{!selectedCaseId ? (
|
|
|
|
|
<p className="text-sm text-text-muted">{tCases('selectCaseHint')}</p>
|
|
|
|
|
) : loadingDetail || !selectedCase ? (
|
|
|
|
|
<p className="text-sm text-text-muted">{tCommon('loading')}</p>
|
|
|
|
|
) : (
|
2026-07-08 02:53:47 +03:30
|
|
|
<CaseDetailPanel
|
|
|
|
|
labCase={selectedCase}
|
|
|
|
|
locale={locale}
|
|
|
|
|
treatmentLabel={treatmentLabel}
|
|
|
|
|
statusOptions={statusOptions}
|
|
|
|
|
loadAttachmentBlob={loadClinicAttachmentBlob}
|
|
|
|
|
showCommentsButton={isClinic}
|
|
|
|
|
commentCount={commentCount}
|
|
|
|
|
onCommentsClick={scrollToComments}
|
|
|
|
|
canEditImportant={canEditImportant}
|
|
|
|
|
updatingImportant={updatingImportant}
|
|
|
|
|
onImportantChange={(checked) => void handleCaseImportantToggle(checked)}
|
|
|
|
|
headerMetaLines={
|
|
|
|
|
!isClinic ? (
|
2026-06-28 23:19:33 +03:30
|
|
|
<p className="text-sm text-text-muted">
|
|
|
|
|
{tCases('fromClinic', { name: selectedCase.clinic.name })}
|
|
|
|
|
</p>
|
|
|
|
|
) : (
|
|
|
|
|
<p className="text-sm text-text-muted">
|
|
|
|
|
{t('caseHistorySentToLab', { name: connection.organizationName })}
|
|
|
|
|
</p>
|
2026-07-08 02:53:47 +03:30
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
commentsSection={
|
|
|
|
|
isClinic && selectedCaseId ? (
|
|
|
|
|
<section id="case-comments" className="scroll-mt-4 border-t border-border pt-4">
|
|
|
|
|
<LabCaseCommentsPanel
|
2026-07-07 18:43:10 +03:30
|
|
|
caseId={selectedCaseId}
|
2026-07-08 02:53:47 +03:30
|
|
|
canPost
|
|
|
|
|
canToggleVisibility={false}
|
|
|
|
|
loadComments={async () => {
|
|
|
|
|
const r = await organizationApi.listConnectionCaseComments(
|
|
|
|
|
connection.id,
|
|
|
|
|
selectedCaseId,
|
|
|
|
|
);
|
|
|
|
|
setCommentCount(r.data.length);
|
|
|
|
|
return r.data;
|
|
|
|
|
}}
|
|
|
|
|
onPost={async (body) => {
|
|
|
|
|
const r = await organizationApi.addConnectionCaseComment(
|
|
|
|
|
connection.id,
|
|
|
|
|
selectedCaseId,
|
|
|
|
|
body,
|
|
|
|
|
);
|
|
|
|
|
setCommentCount((n) => n + 1);
|
|
|
|
|
return r.data;
|
|
|
|
|
}}
|
|
|
|
|
onError={showError}
|
2026-07-07 18:43:10 +03:30
|
|
|
/>
|
2026-07-08 02:53:47 +03:30
|
|
|
</section>
|
|
|
|
|
) : null
|
|
|
|
|
}
|
|
|
|
|
/>
|
2026-06-28 23:19:33 +03:30
|
|
|
)}
|
|
|
|
|
</section>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|