import { useTranslation } from "react-i18next"; import { getPackageSize, isLiquidContainerPackageType, isTubePackageType, type SharedMedicationOverviewItem, } from "../types"; import { formatDate } from "../utils/formatters"; import { MedicationAvatar } from "./MedicationAvatar"; function formatPackageAmountUnit(medication: SharedMedicationOverviewItem, t: (key: string) => string): string | null { if (isTubePackageType(medication.packageType)) { return t("form.packageAmountUnitG"); } if (isLiquidContainerPackageType(medication.packageType)) { return t("form.packageAmountUnitMl"); } if (medication.packageAmountUnit === "g") { return t("form.packageAmountUnitG"); } if (medication.packageAmountUnit === "ml") { return t("form.packageAmountUnitMl"); } return null; } function formatPackageInfo(medication: SharedMedicationOverviewItem, t: (key: string) => string): string { if (medication.packageType === "blister") { return `${medication.packCount} x ${medication.blistersPerPack} x ${medication.pillsPerBlister}`; } const unitLabel = formatPackageAmountUnit(medication, t); if (unitLabel && medication.packageAmountValue && medication.packageAmountValue > 0) { const sizeLabel = `${medication.packageAmountValue} ${unitLabel}`; return medication.packCount > 1 ? `${medication.packCount} x ${sizeLabel}` : sizeLabel; } const packageSize = getPackageSize(medication); if (packageSize > 0) { return medication.packCount > 1 ? `${medication.packCount} x ${packageSize}` : `${packageSize}`; } return `${Math.max(medication.packCount, 1)}`; } function getOverviewStatus( priority: SharedMedicationOverviewItem["priority"] ): { className: string; labelKey: string } | null { if (priority === null) return null; if (priority === "out-of-stock") { return { className: "danger", labelKey: "status.outOfStock" }; } if (priority === "high") { return { className: "warning", labelKey: "status.lowStock" }; } return { className: "normal", labelKey: "status.normal" }; } export interface SharedMedicationOverviewSectionProps { takenBy: string; sharedBy: string | null; medications: SharedMedicationOverviewItem[]; showTitle?: boolean; onMedicationImageClick?: (imageUrl: string, name: string) => void; } export function SharedMedicationOverviewSection({ takenBy, medications, showTitle = true, onMedicationImageClick, }: SharedMedicationOverviewSectionProps) { const { t } = useTranslation(); const renderMedicationAvatar = (name: string, imageUrl: string | null) => { const isClickable = Boolean(imageUrl && onMedicationImageClick); return (
{ if (imageUrl && onMedicationImageClick) onMedicationImageClick(imageUrl, name); }} onKeyDown={(e) => { if ((e.key === "Enter" || e.key === " ") && imageUrl && onMedicationImageClick) { onMedicationImageClick(imageUrl, name); } }} >
); }; return (
{showTitle ? (

{t("sharedOverview.title", { person: takenBy })}

) : null} {medications.length === 0 ? (

{t("sharedOverview.noMedications")}

) : ( <>
{medications.map((medication) => { const overviewStatus = getOverviewStatus(medication.priority); return ( ); })}
{t("sharedOverview.columns.name")} {t("sharedOverview.columns.package")} {t("sharedOverview.columns.stock")} {t("sharedOverview.columns.daysLeft")} {t("sharedOverview.columns.depletion")} {t("sharedOverview.columns.priority")}
{renderMedicationAvatar(medication.name, medication.imageUrl)}
{medication.name} {medication.genericName ? ( {medication.genericName} ) : null}
{formatPackageInfo(medication, t)} {medication.currentStock === null || medication.capacity === null ? "-" : t("sharedOverview.stock.of", { current: medication.currentStock, capacity: medication.capacity, })} {medication.daysLeft === null ? "-" : medication.daysLeft} {formatDate(medication.depletionDate)} {overviewStatus === null ? ( "-" ) : ( {t(overviewStatus.labelKey)} )}
{medications.map((medication) => { const overviewStatus = getOverviewStatus(medication.priority); return (
{renderMedicationAvatar(medication.name, medication.imageUrl)}
{medication.name} {medication.genericName ? ( {medication.genericName} ) : null}
{t("sharedOverview.columns.package")} {formatPackageInfo(medication, t)} {t("sharedOverview.columns.stock")} {medication.currentStock === null || medication.capacity === null ? "-" : t("sharedOverview.stock.of", { current: medication.currentStock, capacity: medication.capacity, })} {t("sharedOverview.columns.daysLeft")} {medication.daysLeft === null ? "-" : medication.daysLeft} {t("sharedOverview.columns.depletion")} {formatDate(medication.depletionDate)}
{overviewStatus ? ( {t(overviewStatus.labelKey)} ) : null}
); })}
)}
); }