77 lines
3.9 KiB
TypeScript
77 lines
3.9 KiB
TypeScript
import React from "react";
|
|
import { useDeploymentProfile } from "~/contexts/deployment-profile-context";
|
|
import { formatNumber } from "~/lib/utils";
|
|
|
|
export interface UnitMetricData {
|
|
category: string;
|
|
costI: number;
|
|
capacityI: number;
|
|
revenueI: number;
|
|
}
|
|
|
|
const metricDefinitions = {
|
|
incomeIncrease: { label: "افزایش درآمد", value: (x: UnitMetricData) => x.revenueI, unit: "میلیون ریال" },
|
|
innovationCostReduction: { label: "کاهش هزینه", value: (x: UnitMetricData) => x.costI, unit: "میلیون ریال" },
|
|
productionCapacityIncrease: { label: "افزایش ظرفیت", value: (x: UnitMetricData) => x.capacityI, unit: "تن در سال" },
|
|
} as const;
|
|
|
|
export function UnitSchematic({ data }: { data: UnitMetricData[] }) {
|
|
const { profile, assetUrl } = useDeploymentProfile();
|
|
const metricColors: Record<string, string> = {
|
|
incomeIncrease: profile.theme.primaryColor,
|
|
innovationCostReduction: profile.theme.accentColor,
|
|
productionCapacityIncrease: profile.theme.secondaryColor,
|
|
};
|
|
const isDiamond = profile.dashboard.layout === "diamond-4";
|
|
return <div className={`w-full ${isDiamond ? "h-[650px]" : "h-[500px]"} rounded-xl`}>
|
|
<div dir="ltr" className={`grid grid-cols-5 ${isDiamond ? "grid-rows-5 h-[650px] gap-y-4" : "grid-rows-4 h-[500px] gap-1"} w-full`}>
|
|
{profile.dashboard.units.map((unit, index) => {
|
|
const values = data.find((item) => item.category === unit.dataKey) ??
|
|
{ category: unit.dataKey, costI: 0, capacityI: 0, revenueI: 0 };
|
|
const diamondImagePositions = [
|
|
{ column: 3, row: 2 },
|
|
{ column: 2, row: 3 },
|
|
{ column: 4, row: 3 },
|
|
{ column: 3, row: 4 },
|
|
];
|
|
const diamondInfoPositions = [
|
|
{ column: 3, row: 1 },
|
|
{ column: 1, row: 3 },
|
|
{ column: 5, row: 3 },
|
|
{ column: 3, row: 5 },
|
|
];
|
|
const imagePosition = isDiamond
|
|
? diamondImagePositions[index]
|
|
: { column: unit.imageColumn, row: unit.imageRow };
|
|
const infoPosition = isDiamond
|
|
? diamondInfoPositions[index]
|
|
: { column: unit.infoColumn, row: unit.infoRow };
|
|
return <React.Fragment key={unit.dataKey}>
|
|
<div className="company-item flex flex-col items-center justify-center rounded-lg text-white"
|
|
style={{ gridColumn: imagePosition.column, gridRow: imagePosition.row }}>
|
|
<img src={assetUrl(unit.imageAssetKey)} alt="" className="h-[100px] max-w-[170px] object-contain"/>
|
|
<span dir="rtl">{unit.title}</span>
|
|
</div>
|
|
<div dir="rtl" className="info-box w-full min-w-0 max-w-48 self-center justify-self-center rounded-xl border border-[#3F415A] px-3 py-1 sm:min-w-36 sm:px-4"
|
|
style={{ gridColumn: infoPosition.column, gridRow: infoPosition.row }}>
|
|
{(profile.dashboard.visibleMetrics ?? unit.metrics ?? []).map((metricKey) => {
|
|
const definition = metricDefinitions[metricKey as keyof typeof metricDefinitions];
|
|
if (!definition) return null;
|
|
const label = metricKey === "innovationCostReduction"
|
|
? profile.dashboard.costSeriesLabel
|
|
: definition.label;
|
|
const showDivider = metricKey !== "productionCapacityIncrease";
|
|
return <div key={metricKey} className={`relative flex justify-between gap-2 py-1 ${showDivider ? "border-b" : ""}`}
|
|
style={showDivider ? { borderBottomColor: metricColors[metricKey] ?? "#3F415A" } : undefined}>
|
|
<span className="text-[11px] text-white">{label}:</span>
|
|
<span className="info-value text-[12px] text-white pb-2">{formatNumber(definition.value(values) || 0)}</span>
|
|
<span className="absolute left-0 bottom-0 text-[9px] leading-none text-gray-300">{definition.unit}</span>
|
|
</div>;
|
|
})}
|
|
</div>
|
|
</React.Fragment>;
|
|
})}
|
|
</div>
|
|
</div>;
|
|
}
|