2026-07-08 02:53:47 +03:30
|
|
|
'use client';
|
|
|
|
|
|
2026-07-13 02:09:49 +03:30
|
|
|
import { useEffect, useMemo, useState, type ReactNode } from 'react';
|
2026-07-08 02:53:47 +03:30
|
|
|
import { useTranslations } from 'next-intl';
|
|
|
|
|
import { MessageSquare } from 'lucide-react';
|
|
|
|
|
import { Badge } from '@/components/ui/shared/Badge';
|
|
|
|
|
import { Button } from '@/components/ui/shared/Button';
|
|
|
|
|
import { Checkbox } from '@/components/ui/shared/Checkbox';
|
|
|
|
|
import { CaseToothChartPanel } from '@/components/ui/lab/CaseToothChartPanel';
|
2026-07-13 15:47:32 +03:30
|
|
|
import { FORM_SELECT_CLASS } from '@/components/shared/formSelectStyles';
|
2026-07-08 02:53:47 +03:30
|
|
|
import { LabCaseAttachmentPreview } from '@/components/ui/lab/LabCaseAttachmentPreview';
|
|
|
|
|
import { LabCaseAttachmentsDialog } from '@/components/ui/lab/LabCaseAttachmentsDialog';
|
2026-07-14 21:07:05 +03:30
|
|
|
import {
|
|
|
|
|
LabCaseShareQrDialog,
|
|
|
|
|
LabCaseShareQrThumb,
|
|
|
|
|
} from '@/components/ui/lab/LabCaseShareQrDialog';
|
2026-07-12 21:08:29 +03:30
|
|
|
import { labTaskStatusVariant } from '@/components/lab/labTaskStatusDisplay';
|
2026-07-13 16:30:36 +03:30
|
|
|
import { LabCaseDueDateBadge } from '@/components/lab/LabCaseDueDateBadge';
|
2026-07-08 02:53:47 +03:30
|
|
|
import {
|
|
|
|
|
formatToothList,
|
2026-07-13 02:09:49 +03:30
|
|
|
prosthesisTypeBadgeStyleFromCatalog,
|
2026-07-12 21:08:29 +03:30
|
|
|
} from '@/components/treatment/prosthesisTypeDisplay';
|
2026-07-13 02:09:49 +03:30
|
|
|
import { prosthesisCatalogApi } from '@/lib/api/prosthesis-catalog';
|
2026-07-08 02:53:47 +03:30
|
|
|
import {
|
|
|
|
|
buildCaseProsthesisRows,
|
|
|
|
|
formatCaseDateTime,
|
|
|
|
|
formatPatientName,
|
|
|
|
|
latestCaseAttachment,
|
2026-07-12 21:08:29 +03:30
|
|
|
} from '@/components/lab/caseDetailUtils';
|
2026-07-13 15:47:32 +03:30
|
|
|
import type { AssignableTaskStaff, LabCaseDetail, LabTaskStatus } from '@/types/cases';
|
2026-07-13 02:09:49 +03:30
|
|
|
import type { ProsthesisCatalogEntry } from '@/types/treatment-catalog';
|
2026-07-08 02:53:47 +03:30
|
|
|
|
|
|
|
|
function CaseTaskProgressBar({ completed, total }: { completed: number; total: number }) {
|
|
|
|
|
const pct = total > 0 ? Math.round((completed / total) * 100) : 0;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-1">
|
|
|
|
|
<div className="flex items-center justify-between text-xs text-text-muted">
|
|
|
|
|
<span>
|
|
|
|
|
{completed}/{total}
|
|
|
|
|
</span>
|
|
|
|
|
<span>{pct}%</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="h-1.5 rounded-full bg-border overflow-hidden">
|
|
|
|
|
<div
|
|
|
|
|
className="h-full rounded-full bg-primary transition-all duration-300"
|
|
|
|
|
style={{ width: `${pct}%` }}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CaseDetailPanelProps {
|
|
|
|
|
labCase: LabCaseDetail;
|
|
|
|
|
locale: string;
|
|
|
|
|
treatmentLabel: (type: string) => string;
|
|
|
|
|
statusOptions: { value: LabTaskStatus; label: string }[];
|
|
|
|
|
loadAttachmentBlob: (caseId: string, attachmentId: string) => Promise<Blob>;
|
|
|
|
|
/** Extra lines below patient mobile (e.g. connection-specific clinic/lab line). */
|
|
|
|
|
headerMetaLines?: ReactNode;
|
|
|
|
|
showCommentsButton?: boolean;
|
|
|
|
|
commentCount?: number;
|
|
|
|
|
onCommentsClick?: () => void;
|
|
|
|
|
canEditImportant?: boolean;
|
|
|
|
|
updatingImportant?: boolean;
|
|
|
|
|
onImportantChange?: (checked: boolean) => void;
|
|
|
|
|
commentsSection?: ReactNode;
|
2026-07-13 15:47:32 +03:30
|
|
|
assignableStaff?: AssignableTaskStaff[];
|
|
|
|
|
canAssignTasks?: boolean;
|
|
|
|
|
assigningTaskId?: string | null;
|
|
|
|
|
onAssignTask?: (taskId: string, assigneeUserId: string | null) => void;
|
2026-07-08 02:53:47 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function CaseDetailPanel({
|
|
|
|
|
labCase,
|
|
|
|
|
locale,
|
|
|
|
|
treatmentLabel,
|
|
|
|
|
statusOptions,
|
|
|
|
|
loadAttachmentBlob,
|
|
|
|
|
headerMetaLines,
|
|
|
|
|
showCommentsButton = false,
|
|
|
|
|
commentCount = 0,
|
|
|
|
|
onCommentsClick,
|
|
|
|
|
canEditImportant = false,
|
|
|
|
|
updatingImportant = false,
|
|
|
|
|
onImportantChange,
|
|
|
|
|
commentsSection,
|
2026-07-13 15:47:32 +03:30
|
|
|
assignableStaff = [],
|
|
|
|
|
canAssignTasks = false,
|
|
|
|
|
assigningTaskId = null,
|
|
|
|
|
onAssignTask,
|
2026-07-08 02:53:47 +03:30
|
|
|
}: CaseDetailPanelProps) {
|
|
|
|
|
const t = useTranslations('cases');
|
|
|
|
|
const [attachmentsDialogOpen, setAttachmentsDialogOpen] = useState(false);
|
2026-07-14 21:07:05 +03:30
|
|
|
const [shareQrDialogOpen, setShareQrDialogOpen] = useState(false);
|
2026-07-13 02:09:49 +03:30
|
|
|
const [prosthesisCatalog, setProsthesisCatalog] = useState<ProsthesisCatalogEntry[]>([]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
void prosthesisCatalogApi
|
|
|
|
|
.list()
|
|
|
|
|
.then((response) => setProsthesisCatalog(response.data))
|
|
|
|
|
.catch(() => {});
|
|
|
|
|
}, []);
|
2026-07-08 02:53:47 +03:30
|
|
|
|
|
|
|
|
const prosthesisRows = useMemo(() => buildCaseProsthesisRows(labCase), [labCase]);
|
|
|
|
|
const previewAttachment = useMemo(() => latestCaseAttachment(labCase), [labCase]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-4">
|
2026-07-11 17:10:02 +03:30
|
|
|
<header className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between border-b border-border pb-3">
|
2026-07-08 02:53:47 +03:30
|
|
|
<div className="min-w-0 flex-1 space-y-1">
|
2026-07-13 16:30:36 +03:30
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
|
|
|
<h2 className="text-lg font-semibold text-text-primary">
|
|
|
|
|
{formatPatientName(labCase.patient)}
|
|
|
|
|
</h2>
|
|
|
|
|
<LabCaseDueDateBadge dueDate={labCase.dueDate} locale={locale} />
|
|
|
|
|
</div>
|
2026-07-08 02:53:47 +03:30
|
|
|
{!canEditImportant && labCase.isImportant ? (
|
|
|
|
|
<Badge variant="warning" fixedWidth={false} className="mt-1">
|
|
|
|
|
{t('importantLabel')}
|
|
|
|
|
</Badge>
|
|
|
|
|
) : null}
|
|
|
|
|
<p className="text-sm text-text-muted">
|
|
|
|
|
{t('patientMobile')}: {labCase.patient.mobile}
|
|
|
|
|
</p>
|
|
|
|
|
{headerMetaLines}
|
|
|
|
|
<p className="text-sm text-text-muted">
|
|
|
|
|
{t('sentAt', { date: formatCaseDateTime(labCase.sentAt, locale) })}
|
|
|
|
|
</p>
|
|
|
|
|
<div className="pt-1 max-w-xs">
|
|
|
|
|
<p className="text-sm text-text-muted mb-1">
|
|
|
|
|
{t('taskProgressLabel', {
|
|
|
|
|
completed: labCase.taskProgress.completed,
|
|
|
|
|
total: labCase.taskProgress.total,
|
|
|
|
|
})}
|
|
|
|
|
</p>
|
|
|
|
|
<CaseTaskProgressBar
|
|
|
|
|
completed={labCase.taskProgress.completed}
|
|
|
|
|
total={labCase.taskProgress.total}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-07-14 21:07:05 +03:30
|
|
|
<div className="flex w-full sm:w-auto shrink-0 flex-col items-end gap-2">
|
2026-07-08 02:53:47 +03:30
|
|
|
{canEditImportant ? (
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={labCase.isImportant ?? false}
|
|
|
|
|
disabled={updatingImportant}
|
|
|
|
|
label={t('markCaseImportant')}
|
|
|
|
|
className="shrink-0"
|
|
|
|
|
onChange={(checked) => onImportantChange?.(checked)}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
2026-07-14 21:07:05 +03:30
|
|
|
{showCommentsButton && onCommentsClick ? (
|
|
|
|
|
<Button type="button" variant="outline" size="sm" onClick={onCommentsClick}>
|
|
|
|
|
<MessageSquare className="h-4 w-4 me-1.5" />
|
|
|
|
|
{commentCount > 0
|
|
|
|
|
? t('commentsCount', { count: commentCount })
|
|
|
|
|
: t('showComments')}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
{labCase.shareUrl || (previewAttachment && labCase.attachments.length > 0) ? (
|
|
|
|
|
<div className="flex flex-row items-center gap-2 shrink-0">
|
|
|
|
|
{previewAttachment && labCase.attachments.length > 0 ? (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setAttachmentsDialogOpen(true)}
|
|
|
|
|
className="aspect-square w-24 sm:w-32 cursor-pointer rounded-[var(--radius-md)] border border-border/60 overflow-hidden transition-colors hover:border-primary/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary"
|
|
|
|
|
title={previewAttachment.fileName}
|
|
|
|
|
aria-label={t('viewAttachments')}
|
|
|
|
|
>
|
|
|
|
|
<LabCaseAttachmentPreview
|
|
|
|
|
caseId={labCase.id}
|
|
|
|
|
attachment={previewAttachment}
|
|
|
|
|
loadBlob={loadAttachmentBlob}
|
|
|
|
|
className="h-full w-full"
|
|
|
|
|
/>
|
|
|
|
|
</button>
|
|
|
|
|
) : null}
|
|
|
|
|
{labCase.shareUrl ? (
|
|
|
|
|
<LabCaseShareQrThumb
|
|
|
|
|
shareUrl={labCase.shareUrl}
|
|
|
|
|
onClick={() => setShareQrDialogOpen(true)}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
2026-07-08 02:53:47 +03:30
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<CaseToothChartPanel
|
2026-07-10 15:26:36 +03:30
|
|
|
details={labCase.detail ? [{ teeth: labCase.detail.teeth }] : []}
|
2026-07-08 02:53:47 +03:30
|
|
|
prosthesisRows={prosthesisRows}
|
2026-07-13 02:09:49 +03:30
|
|
|
prosthesisCatalog={prosthesisCatalog}
|
2026-07-08 02:53:47 +03:30
|
|
|
className="w-full"
|
|
|
|
|
/>
|
|
|
|
|
|
2026-07-10 15:26:36 +03:30
|
|
|
{labCase.detail ? (
|
2026-07-08 02:53:47 +03:30
|
|
|
<div className="space-y-2">
|
|
|
|
|
<h3 className="text-sm font-medium text-text-primary">{t('treatmentDetails')}</h3>
|
2026-07-10 15:26:36 +03:30
|
|
|
<div className="rounded-md bg-background border border-border p-2 text-sm">
|
|
|
|
|
<div className="font-medium">{treatmentLabel(labCase.detail.treatmentType)}</div>
|
|
|
|
|
<div className="text-text-muted">
|
|
|
|
|
{t('teethLabel')}: {labCase.detail.teeth.join(', ') || '—'}
|
|
|
|
|
</div>
|
|
|
|
|
{labCase.detail.comment ? (
|
|
|
|
|
<div className="text-text-muted mt-1">{labCase.detail.comment}</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
2026-07-08 02:53:47 +03:30
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
<h3 className="text-sm font-medium text-text-primary">{t('tasksByTooth')}</h3>
|
|
|
|
|
{labCase.tasksByTooth.length === 0 ? (
|
|
|
|
|
<p className="text-sm text-text-muted">{t('noTasks')}</p>
|
|
|
|
|
) : (
|
2026-07-13 02:09:49 +03:30
|
|
|
labCase.tasksByTooth.map((group) => (
|
2026-07-08 02:53:47 +03:30
|
|
|
<div
|
|
|
|
|
key={`${group.treatmentDetailId}-${group.prosthesisTypeCode}`}
|
|
|
|
|
className="rounded-md border border-border p-3 space-y-2"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
|
|
|
<Badge
|
|
|
|
|
truncate
|
|
|
|
|
title={group.prosthesisTypeLabel}
|
2026-07-13 02:09:49 +03:30
|
|
|
style={prosthesisTypeBadgeStyleFromCatalog(
|
|
|
|
|
group.prosthesisTypeCode,
|
|
|
|
|
prosthesisCatalog,
|
|
|
|
|
)}
|
2026-07-08 02:53:47 +03:30
|
|
|
>
|
|
|
|
|
{group.prosthesisTypeLabel}
|
|
|
|
|
</Badge>
|
|
|
|
|
<span className="text-sm font-medium text-text-primary">
|
|
|
|
|
{t('toothGroupTitle', {
|
|
|
|
|
teeth: formatToothList(group.teeth),
|
|
|
|
|
prosthesis: group.prosthesisTypeLabel,
|
|
|
|
|
})}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<ul className="space-y-2">
|
|
|
|
|
{group.tasks.map((task) => (
|
2026-07-13 15:47:32 +03:30
|
|
|
<li key={task.id} className="rounded bg-background px-2 py-1.5 text-sm">
|
|
|
|
|
<div className="flex flex-wrap items-center gap-x-2 gap-y-1">
|
|
|
|
|
<span className="min-w-0 flex-1 font-medium text-text-primary">
|
2026-07-08 02:53:47 +03:30
|
|
|
{task.stepOrder}. {task.stepLabel}
|
|
|
|
|
</span>
|
2026-07-13 15:47:32 +03:30
|
|
|
<span className="text-[11px] text-text-muted shrink-0">
|
|
|
|
|
{task.lastStatusChangedBy
|
|
|
|
|
? t('lastUpdatedBy', { name: task.lastStatusChangedBy.name })
|
|
|
|
|
: t('lastUpdatedUnknown')}
|
|
|
|
|
{task.lastStatusChangedAt
|
|
|
|
|
? ` · ${formatCaseDateTime(task.lastStatusChangedAt, locale)}`
|
|
|
|
|
: ''}
|
|
|
|
|
</span>
|
|
|
|
|
{canAssignTasks && onAssignTask ? (
|
|
|
|
|
<select
|
|
|
|
|
value={task.assignee?.id ?? ''}
|
|
|
|
|
disabled={assigningTaskId === task.id}
|
|
|
|
|
onChange={(e) =>
|
|
|
|
|
onAssignTask(task.id, e.target.value ? e.target.value : null)
|
|
|
|
|
}
|
|
|
|
|
aria-label={t('assigneeLabel')}
|
2026-07-14 01:27:11 +03:30
|
|
|
className={`${FORM_SELECT_CLASS} max-w-[9.5rem] rounded-md py-0.5 text-xs`}
|
2026-07-13 15:47:32 +03:30
|
|
|
>
|
|
|
|
|
<option value="">{t('assigneeUnassigned')}</option>
|
|
|
|
|
{assignableStaff.map((staff) => (
|
|
|
|
|
<option key={staff.id} value={staff.id}>
|
|
|
|
|
{staff.name}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
|
|
|
|
) : null}
|
2026-07-08 02:53:47 +03:30
|
|
|
<Badge variant={labTaskStatusVariant(task.status)} fixedWidth={false}>
|
|
|
|
|
{statusOptions.find((opt) => opt.value === task.status)?.label ??
|
|
|
|
|
task.status}
|
|
|
|
|
</Badge>
|
|
|
|
|
</div>
|
|
|
|
|
</li>
|
|
|
|
|
))}
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{commentsSection}
|
|
|
|
|
|
|
|
|
|
<LabCaseAttachmentsDialog
|
|
|
|
|
open={attachmentsDialogOpen}
|
|
|
|
|
onClose={() => setAttachmentsDialogOpen(false)}
|
|
|
|
|
caseId={labCase.id}
|
|
|
|
|
attachments={labCase.attachments}
|
|
|
|
|
loadBlob={loadAttachmentBlob}
|
|
|
|
|
/>
|
2026-07-14 21:07:05 +03:30
|
|
|
|
|
|
|
|
{labCase.shareUrl ? (
|
|
|
|
|
<LabCaseShareQrDialog
|
|
|
|
|
open={shareQrDialogOpen}
|
|
|
|
|
onClose={() => setShareQrDialogOpen(false)}
|
|
|
|
|
shareUrl={labCase.shareUrl}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
2026-07-08 02:53:47 +03:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { CaseTaskProgressBar };
|