fix(node): don't recreate Vico CartesianChartModelProducer on channel switch (#5160)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
James Rich 2026-04-16 21:40:17 -05:00 committed by GitHub
parent a6a889430b
commit df3b5365f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 10 deletions

View file

@ -159,26 +159,28 @@ fun GenericMetricChart(
*
* @param isEmpty Whether the chart data is empty when true, nothing is rendered.
* @param legendData Legend items shown below the chart.
* @param key Optional key for the [CartesianChartModelProducer] (e.g. a selected channel). Pass a different value to
* recreate the producer.
* @param hiddenSet Indices of hidden legend items (toggleable legend).
* @param onToggle Callback when a legend item is toggled; when null, a read-only legend is rendered.
* @param content Builder lambda receiving the [CartesianChartModelProducer] and a standard `Modifier.weight(1f)`
* suitable for the chart area.
*
* A single [CartesianChartModelProducer] is created per scaffold instance. Vico forbids swapping the producer attached
* to a live [CartesianChartHost] (it throws "A new `CartesianChartModelProducer` was provided…"), so callers must push
* new data through [CartesianChartModelProducer.runTransaction] instead of recreating the producer. Keying the scaffold
* on external state (e.g. a selected channel) caused exactly that crash, so the previous `key` parameter was removed.
*/
@Composable
fun MetricChartScaffold(
isEmpty: Boolean,
legendData: List<LegendData>,
modifier: Modifier = Modifier,
key: Any? = Unit,
hiddenSet: Set<Int> = emptySet(),
onToggle: ((Int) -> Unit)? = null,
content: @Composable ColumnScope.(CartesianChartModelProducer, Modifier) -> Unit,
) {
Column(modifier = modifier) {
if (isEmpty) return@Column
val modelProducer = remember(key) { CartesianChartModelProducer() }
val modelProducer = remember { CartesianChartModelProducer() }
val chartModifier = Modifier.weight(1f).padding(horizontal = 8.dp).padding(bottom = 0.dp)
content(modelProducer, chartModifier)
Legend(

View file

@ -182,12 +182,10 @@ private fun PowerMetricsChart(
selectedX: Double?,
onPointSelected: (Double) -> Unit,
) {
MetricChartScaffold(
isEmpty = telemetries.isEmpty(),
legendData = LEGEND_DATA,
modifier = modifier,
key = selectedChannel,
) { modelProducer, chartModifier ->
MetricChartScaffold(isEmpty = telemetries.isEmpty(), legendData = LEGEND_DATA, modifier = modifier) {
modelProducer,
chartModifier,
->
val currentColor = PowerMetric.CURRENT.color
val voltageColor = PowerMetric.VOLTAGE.color
val marker =