Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | 123x 123x 9x 9x 9x 9x 9x 123x 123x 109x 109x 25x 9x 9x 109x 42x 42x 42x 42x 123x 1x 5x 2x 570x 3453x 123x | import React from "react";
import {
Card,
CardHeader,
CardTitle,
CardContent,
CardDescription,
} from "@/ahuora-design-system/ui/card";
type TipState = {
// raw anchor point (inside SVG)
x: number;
y: number;
// rendered React node
node: React.ReactNode;
// fixed width, height will be measured
w: number;
h?: number;
} | null;
export function useHenHoverCard() {
const [tip, setTip] = React.useState<TipState>(null);
// show the hovercard near mouse position.
const show = React.useCallback(
(
event: React.MouseEvent,
node: React.ReactNode,
opts?: { width?: number; offset?: number }
) => {
const svg = (event.currentTarget as Element).closest("svg");
Iif (!svg) return;
const r = svg.getBoundingClientRect();
const offset = opts?.offset ?? 12;
setTip({
// convert from viewport coords to coords inside svg
x: event.clientX - r.left + offset,
y: event.clientY - r.top + offset,
node,
// keep width in a sensible range
w: Math.max(160, Math.min(360, opts?.width ?? 260)),
h: undefined, // unknwon till first paint (dependent on contents)
});
},
[]
);
// hide the hovercard
const hide = React.useCallback(() => setTip(null), []);
// this is the layer that ACTUALLY renders inside the svg.
// uses <foreignObject> so regular html card can be put in svg space.
const HoverLayer = React.useCallback(
({ svgWidth, svgHeight }: { svgWidth: number; svgHeight: number }) => {
const measureRef = React.useRef<HTMLDivElement>(null);
// After first paint, measure the content height and store it
React.useLayoutEffect(() => {
if (!tip || tip.h || !measureRef.current) return;
const h = measureRef.current.scrollHeight;
setTip((prev) => (prev ? { ...prev, h: Math.ceil(h) } : prev));
}, [tip]);
if (!tip) return null;
// If we haven't measured yet, use a tiny height just to trigger layout
const measuredH = tip.h ?? 1;
// Keep the card fully insdie the svg bounds
const clampedX = Math.min(tip.x, Math.max(0, svgWidth - tip.w));
const clampedY = Math.min(tip.y, Math.max(0, svgHeight - measuredH));
return (
<foreignObject
x={clampedX}
y={clampedY}
width={tip.w}
height={measuredH}
style={{ pointerEvents: "none" }}
>
{/* Wrapper div is what we measure */}
<div ref={measureRef}>
<Card className="pointer-events-none cursor-default p-3 shadow-xl rounded-md">
{tip.node}
</Card>
</div>
</foreignObject>
);
},
[tip]
);
// helpers to build common tooltip bodies.
const buildContent = {
// card for pinch
pinch: (p: { hotPinch?: number; coldPinch?: number }) => (
<>
<CardHeader className="p-0">
<CardTitle className="text-sm" aria-label="hover-card-pinch">Pinch Temperatures</CardTitle>
</CardHeader>
<CardContent className="p-0 text-xs text-muted-foreground space-y-2">
<section>
<p>Hot pinch: {fmt(p.hotPinch)} °C</p>
<p>Cold pinch: {fmt(p.coldPinch)} °C</p>
</section>
</CardContent>
</>
),
// card for streams/segments
stream: (p: {
label: string;
subtitle?: string;
temps?: { inlet?: number; outlet?: number; delta?: number };
dH?: number;
heatFlow?: number;
pressureDrop?: number;
composition?: Array<{ name: string; value: number; unit?: string }>;
}) => (
<>
<CardHeader className="p-0">
<CardTitle className="text-sm" aria-label="hover-card-stream">{p.label}</CardTitle>
{p.subtitle && (
<CardDescription className="mt-0.5">{p.subtitle}</CardDescription>
)}
</CardHeader>
<CardContent className="p-0 pt-2 text-xs text-muted-foreground space-y-2">
<section>
<div className="font-medium text-sm text-foreground mb-1">
Temperatures
</div>
<div className="text-xs">
<p>Inlet: {fmt(p.temps?.inlet)} °C</p>
<p>Outlet: {fmt(p.temps?.outlet)} °C</p>
<p>ΔT: {fmt(p.temps?.delta)} °C</p>
</div>
</section>
<section className="pt-2 border-t">
<div className="font-medium text-sm text-foreground mb-1">
Enthalpy Change
</div>
<p>{fmt(p.dH)} kJ/kmol</p>
</section>
<section className="pt-2 border-t">
<div className="font-medium text-sm text-foreground mb-1">
Useful Heat Energy
</div>
<p>{fmt(p.heatFlow)} kW</p>
</section>
{p.composition?.length ? (
<section className="pt-2 border-t">
<div className="font-medium text-sm text-foreground mb-1">
Composition (mass flow)
</div>
<ul className="list-disc pl-5 space-y-0.5">
{p.composition.map((c, i) => (
<li key={`${c.name}-${i}`}>
{c.name}: {fmt(c.value)} {c.unit ?? ""}
</li>
))}
</ul>
</section>
) : null}
</CardContent>
</>
),
//Card for Heater/Cooler nodes.
nonHxNode: (p: {
label: string;
kind?: string; // "Heater" | "Cooler" | "Unit operation"
temps?: { inlet?: number; outlet?: number; delta?: number };
duty?: number;
pressureDrop?: number;
meta?: string;
}) => (
<>
<CardHeader className="p-0">
<CardTitle className="text-sm" aria-label="hover-card-non-hx-node">{p.label}</CardTitle>
</CardHeader>
<CardContent className="p-0 pt-2 text-xs text-muted-foreground space-y-2">
<section>
<div className="font-medium text-sm text-foreground mb-1">
Temperatures
</div>
<div className="text-xs">
<p>Before: {fmt(p.temps?.inlet)} °C</p>
<p>After: {fmt(p.temps?.outlet)} °C</p>
</div>
</section>
<section className="pt-2 border-t">
<div className="font-medium text-sm text-foreground mb-1">
{p.kind === "Heater"
? "Heat Added"
: p.kind === "Cooler"
? "Heat Removed"
: "Heat Flow"}
{console.log("p.kind", p.kind)}
</div>
<p>{fmt(p.duty)} kW</p>
</section>
</CardContent>
</>
),
// Card for HX nodes.
hxNode: (p: {
title: string;
hot: {
before?: number;
after?: number;
load?: number;
transfer?: number;
area?: number;
};
cold: {
before?: number;
after?: number;
load?: number;
transfer?: number;
area?: number;
};
overall?: { transfer?: number; area?: number };
}) => (
<>
<CardHeader className="p-0">
<CardTitle className="text-sm" aria-label="hover-card-hx-node">{p.title}</CardTitle>
{/* <CardDescription className="mt-0.5">Heat Exchanger</CardDescription> */}
</CardHeader>
<CardContent className="p-0 pt-2 text-xs text-muted-foreground space-y-2">
<section>
<div className="font-medium text-sm text-foreground mb-1">
Hot side
</div>
<div className="text-xs">
<p>Temperature Before: {fmt(p.hot.before)} °C</p>
<p>Temperature After: {fmt(p.hot.after)} °C</p>
{/* <p>Heat Load: {fmt(p.hot.load)} kW</p> */}
{/* <p>Heat Transfer: {fmt(p.hot.transfer)} kW</p> */}
{/* <p>Area: {fmt(p.hot.area)} m²</p> */}
</div>
</section>
<section className="pt-2 border-t">
<div className="font-medium text-sm text-foreground mb-1">
Cold side
</div>
<p>Temperature Before: {fmt(p.cold.before)} °C</p>
<p>Temperature After: {fmt(p.cold.after)} °C</p>
{/* <p>Heat Load: {fmt(p.cold.load)} kW</p> */}
{/* <p>Heat Transfer: {fmt(p.cold.transfer)} kW</p> */}
{/* <p>Area: {fmt(p.cold.area)} m²</p> */}
</section>
<section className="pt-2 border-t">
<div className="font-medium text-sm text-foreground mb-1">
Overall
</div>
<p>Heat Transfer: {fmt(p.overall?.transfer)} kW</p>
<p>Area: {fmt(p.overall?.area)} m²</p>
</section>
</CardContent>
</>
),
};
// Helper for fixing values to 2d.p. or show "-" when values are null/undefined.
function fmt(v?: number) {
return v == null || Number.isNaN(Number(v)) ? "—" : Number(v).toFixed(2);
}
return { HoverLayer, show, hide, buildContent };
}
|