|
1 | 1 | import React, { useState, useEffect } from 'react'; |
2 | | -import { Checkbox, Button, Spin } from 'antd'; |
| 2 | +import { Checkbox, Button } from 'antd'; |
3 | 3 | import { css } from '@emotion/react'; |
4 | 4 | import { AVAILABLE_METRICS, type MetricMetadata } from '../../lib/types'; |
5 | 5 | import { listMetrics } from '../../api/client'; |
@@ -125,29 +125,29 @@ const selectorStyle = css` |
125 | 125 | } |
126 | 126 | `; |
127 | 127 |
|
| 128 | +let cachedMetrics: MetricMetadata[] | null = null; |
| 129 | + |
128 | 130 | export const MetricSelector: React.FC<MetricSelectorProps> = ({ |
129 | 131 | selectedMetrics, |
130 | 132 | onToggleMetric, |
131 | 133 | loadFromAPI = false, |
132 | 134 | }) => { |
133 | | - const [metrics, setMetrics] = useState<MetricMetadata[]>(AVAILABLE_METRICS); |
134 | | - const [loading, setLoading] = useState(false); |
| 135 | + const [metrics, setMetrics] = useState<MetricMetadata[]>(cachedMetrics ?? AVAILABLE_METRICS); |
135 | 136 |
|
136 | 137 | useEffect(() => { |
137 | | - if (loadFromAPI) { |
138 | | - setLoading(true); |
139 | | - listMetrics() |
140 | | - .then((apiMetrics) => { |
| 138 | + if (!loadFromAPI || cachedMetrics) return; |
| 139 | + let cancelled = false; |
| 140 | + listMetrics() |
| 141 | + .then((apiMetrics) => { |
| 142 | + if (!cancelled) { |
| 143 | + cachedMetrics = apiMetrics; |
141 | 144 | setMetrics(apiMetrics); |
142 | | - }) |
143 | | - .catch((error) => { |
144 | | - console.error('Failed to load metrics from API, using fallback:', error); |
145 | | - setMetrics(AVAILABLE_METRICS); |
146 | | - }) |
147 | | - .finally(() => { |
148 | | - setLoading(false); |
149 | | - }); |
150 | | - } |
| 145 | + } |
| 146 | + }) |
| 147 | + .catch((error) => { |
| 148 | + console.error('Failed to load metrics from API, using fallback:', error); |
| 149 | + }); |
| 150 | + return () => { cancelled = true; }; |
151 | 151 | }, [loadFromAPI]); |
152 | 152 |
|
153 | 153 | const categorizedMetrics = metrics.reduce( |
@@ -175,14 +175,6 @@ export const MetricSelector: React.FC<MetricSelectorProps> = ({ |
175 | 175 | }); |
176 | 176 | }; |
177 | 177 |
|
178 | | - if (loading) { |
179 | | - return ( |
180 | | - <div css={selectorStyle} style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '200px' }}> |
181 | | - <Spin tip="Loading metrics..." /> |
182 | | - </div> |
183 | | - ); |
184 | | - } |
185 | | - |
186 | 178 | return ( |
187 | 179 | <div css={selectorStyle}> |
188 | 180 | <div className="metric-categories"> |
|
0 commit comments