/** StreamSelector — checkbox list of all streams grouped by subprocess. */ import { useEffect, useCallback } from 'react'; import { useProjectStore } from '../../store/projectStore'; import { useAnalysisStore } from '../../store/analysisStore'; import { getStreamInfo } from '../../utils/streamUtils'; import type { Stream } from '../../types/stream'; import ChartHelpButton from '../ui/ChartHelpButton'; import { useTranslation } from 'react-i18next'; export default function StreamSelector() { const { t } = useTranslation(); const processes = useProjectStore((s) => s.state.processes); const selectedStreams = useAnalysisStore((s) => s.selectedStreams); const setSelectedStream = useAnalysisStore((s) => s.setSelectedStream); // Initialize any streams that haven't been seen yet as "selected" by default useEffect(() => { const keysToEnable: string[] = []; processes.forEach((proc, pi) => { (proc.streams ?? []).forEach((_s: Stream, si: number) => { const key = `stream_${pi}_${si}`; // If it's not even in the map yet, we mark it as "to be enabled" if (selectedStreams[key] === undefined) { keysToEnable.push(key); } }); }); if (keysToEnable.length > 0) { // Direct merge into store to avoid overwriting existing selections const next = { ...selectedStreams }; keysToEnable.forEach(k => next[k] = true); useAnalysisStore.setState({ selectedStreams: next }); } }, [processes, selectedStreams]); const handleToggle = useCallback( (key: string) => { setSelectedStream(key, !selectedStreams[key]); }, [selectedStreams, setSelectedStream], ); if (!processes.length) { return
{t('analysis.messages.no_processes')}
; } return (

{t('stream_selector.title')}

{processes.map((proc, pi) => { const streams = proc.streams ?? []; if (streams.length === 0) return null; const procName = proc.name || `${t('stream_selector.subprocess')} ${pi + 1}`; return (
{procName}
{streams.map((stream: Stream, si: number) => { const key = `stream_${pi}_${si}`; const checked = selectedStreams[key] ?? true; const info = getStreamInfo(stream); const streamName = stream.name || `${t('stream_selector.stream')} ${si + 1}`; const parts: string[] = []; if (info.tin !== null) parts.push(`Tin:${info.tin}°C`); if (info.tout !== null) parts.push(`Tout:${info.tout}°C`); if (info.CP !== null) parts.push(`CP:${info.CP.toFixed(2)}`); if (info.Q !== null) parts.push(`Q:${info.Q.toFixed(2)} kW`); if (info.type) parts.push(info.type.includes('Hot') ? '🔴' : '🔵'); return ( ); })}
); })}
); }