Skip to content

Commit 2322f93

Browse files
committed
Fix: reload widgets when coming back on screen if the props have changed
Previously they would only refresh if they were on screen when filters changed, or after the periodic delay. This caused stale widgets when filtering and then scrolling
1 parent 682b457 commit 2322f93

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

apps/webapp/app/routes/resources.metric.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,19 @@ export function MetricWidget({
173173
const [response, setResponse] = useState<MetricWidgetActionResponse | null>(null);
174174
const [isLoading, setIsLoading] = useState(false);
175175
const abortControllerRef = useRef<AbortController | null>(null);
176+
const isDirtyRef = useRef(false);
176177

177178
// Track the latest props so the submit callback always uses fresh values
178179
// without needing to be recreated (which would cause useInterval to re-register listeners).
179180
const propsRef = useRef(props);
180181
propsRef.current = props;
181182

182183
const submit = useCallback(() => {
183-
// Skip fetching if the widget is not visible on screen
184-
if (!isVisibleRef.current) return;
184+
if (!isVisibleRef.current) {
185+
isDirtyRef.current = true;
186+
return;
187+
}
188+
isDirtyRef.current = false;
185189

186190
// Abort any in-flight request for this widget
187191
abortControllerRef.current?.abort();
@@ -225,7 +229,7 @@ export function MetricWidget({
225229
// When a widget scrolls into view and has no data yet, trigger a load.
226230
const { ref: visibilityRef, isVisibleRef } = useElementVisibility({
227231
onVisibilityChange: (visible) => {
228-
if (visible && !response) {
232+
if (visible && (!response || isDirtyRef.current)) {
229233
submit();
230234
}
231235
},

0 commit comments

Comments
 (0)