import { useTranslation } from "react-i18next"; import { RowDto } from "~/modules/rows/repositories/RowDto"; import { EntityWithDetails } from "~/utils/db/entities/entities.db.server"; import { RowWithDetails } from "~/utils/db/entities/rows.db.server"; import RowRelationshipRow from "./RowRelationshipRow"; import { EntitiesApi } from "~/utils/api/.server/EntitiesApi"; import { translateEntityTitle } from "~/utils/i18n/translateLabel"; export default function RowRelationshipRowsByEntity({ entity, item, allEntities, onRelatedRowClick, routes, type, }: { entity: EntityWithDetails; item: RowWithDetails; allEntities: EntityWithDetails[]; onRelatedRowClick?: () => void; routes?: EntitiesApi.Routes; type: "parents" | "children"; }) { const { t, i18n } = useTranslation(); const relatedEntities = type === "children" ? entity.childEntities.filter((f) => f.hiddenIfEmpty) : entity.parentEntities.filter((f) => f.hiddenIfEmpty); let relatedRows: any[] = []; if (type === "children") { relatedRows = item.childRows?.filter((f) => relatedEntities.some((p) => p.id === f.relationshipId)) ?? []; } else { relatedRows = item.parentRows?.filter((f) => relatedEntities.some((p) => p.id === f.relationshipId)) ?? []; } const relatedRowsByEntity: { entity: EntityWithDetails; rows: RowDto[] }[] = []; relatedRows?.forEach((row) => { const entity = type === "children" ? allEntities.find((f) => f.id === row.child.entityId) : allEntities.find((f) => f.id === row.parent.entityId); if (!entity) { return null; } const existing = relatedRowsByEntity.find((f) => f.entity.id === entity.id); if (existing) { existing.rows.push(type === "children" ? row.child : row.parent); } else { relatedRowsByEntity.push({ entity, rows: [type === "children" ? row.child : row.parent] }); } }); return (
{relatedRows.length === 0 ? (
{t("shared.none")}
) : ( )}
); }