55*/
66
77// Base imports
8- import { useEffect , useMemo , useState } from "react"
8+ import { useEffect , useMemo } from "react"
99
1010// 3rd Party Imports
1111import { useDispatch , useSelector } from "react-redux"
@@ -14,6 +14,7 @@ import { useDispatch, useSelector } from "react-redux"
1414import { hexToRgba } from "./components/fla/utils"
1515
1616// Custom components and helpers
17+ import { PRELOAD_LABELS } from "./components/fla/constants.js"
1718import { logEventIds } from "./components/fla/logEventIds.js"
1819
1920import SelectFlightLog from "./components/fla/SelectFlightLog.jsx"
@@ -48,9 +49,6 @@ export default function FLA() {
4849 const customColors = useSelector ( selectCustomColors )
4950 const baseChartData = useSelector ( selectBaseChartData )
5051
51- // Local states
52- const [ chartData , setLocalChartData ] = useState ( { datasets : [ ] } )
53-
5452 /**
5553 * Dispatch the lightweight summary info to Redux
5654 */
@@ -76,13 +74,17 @@ export default function FLA() {
7674 ) ,
7775 )
7876 dispatch ( setBaseChartData ( [ ] ) )
77+ // Fire off preload in the background without blocking
78+ const labelsToPreload = PRELOAD_LABELS [ summary . logType ]
79+ if ( labelsToPreload && labelsToPreload . length > 0 ) {
80+ setTimeout ( ( ) => fetchData ( labelsToPreload ) , 0 )
81+ }
7982 }
8083
8184 // Close file
8285 function closeLogFile ( ) {
8386 dispatch ( setFile ( null ) )
8487 dispatch ( setLogMessages ( null ) )
85- setLocalChartData ( { datasets : [ ] } )
8688 dispatch ( setMessageFilters ( null ) )
8789 dispatch ( setCustomColors ( { } ) )
8890 dispatch ( setUtcAvailable ( false ) )
@@ -93,6 +95,30 @@ export default function FLA() {
9395 dispatch ( setBaseChartData ( [ ] ) )
9496 }
9597
98+ async function fetchData ( labelsToFetch ) {
99+ const newDatasets = await window . ipcRenderer . invoke (
100+ "fla:get-messages" ,
101+ labelsToFetch ,
102+ )
103+ // Unpack and Cache
104+ if ( Array . isArray ( newDatasets ) && newDatasets . length > 0 ) {
105+ const transformed = newDatasets . map ( ( ds ) => {
106+ if ( Array . isArray ( ds ?. data ) ) return ds
107+ const len = Math . min ( ds . x . length , ds . y . length )
108+ const points = new Array ( len )
109+ for ( let i = 0 ; i < len ; i ++ ) {
110+ points [ i ] = { x : ds . x [ i ] , y : ds . y [ i ] }
111+ }
112+ return { label : ds . label , yAxisID : ds . yAxisID , data : points }
113+ } )
114+ // Deduplicate by label: new datasets override old ones
115+ const existing = baseChartData || [ ]
116+ const newLabels = new Set ( transformed . map ( ( ds ) => ds . label ) )
117+ const filteredExisting = existing . filter ( ( ds ) => ! newLabels . has ( ds . label ) )
118+ dispatch ( setBaseChartData ( [ ...filteredExisting , ...transformed ] ) )
119+ }
120+ }
121+
96122 // Step 1: Memoize the calculation of which labels are currently requested.
97123 // This loop only runs when `messageFilters` changes.
98124 const requestedLabels = useMemo ( ( ) => {
@@ -121,19 +147,9 @@ export default function FLA() {
121147
122148 if ( labelsToFetch . length > 0 ) {
123149 console . log ( "Cache miss. Fetching:" , labelsToFetch )
124- const fetchMissingData = async ( ) => {
125- const newDatasets = await window . ipcRenderer . invoke (
126- "fla:get-messages" ,
127- labelsToFetch ,
128- )
129- if ( newDatasets ) {
130- // Dispatch to add the new data to our master cache in Redux
131- dispatch ( setBaseChartData ( [ ...( baseChartData || [ ] ) , ...newDatasets ] ) )
132- }
133- }
134- fetchMissingData ( )
150+ fetchData ( labelsToFetch )
135151 }
136- } , [ requestedLabels , baseChartData , dispatch ] )
152+ } , [ requestedLabels , baseChartData ] )
137153
138154 // Step 3: Memoize the final chart data.
139155 // This filters the master cache and applies colors. It only re-runs if
@@ -153,18 +169,15 @@ export default function FLA() {
153169 } )
154170 } , [ baseChartData , customColors , requestedLabels ] )
155171
156- // Step 4: Update the chart's state.
157- // This is now very simple and just syncs the memoized data to the local state.
158- useEffect ( ( ) => {
159- setLocalChartData ( { datasets : visibleDataWithColors } )
160- } , [ visibleDataWithColors ] )
161-
162172 return (
163173 < Layout currentPage = "fla" >
164174 { messageFilters === null ? (
165175 < SelectFlightLog getLogSummary = { getLogSummary } />
166176 ) : (
167- < MainDisplay closeLogFile = { closeLogFile } chartData = { chartData } />
177+ < MainDisplay
178+ closeLogFile = { closeLogFile }
179+ chartData = { { datasets : visibleDataWithColors } }
180+ />
168181 ) }
169182 </ Layout >
170183 )
0 commit comments