fix(widgets): race conditions in useLiveStats and stale value in integration select
- Add generation guard to prevent late SSE events from writing stale data after systemId/integration changes - Sort integrationIds for stable key (avoids unnecessary resubscribes) - Include integrationKey and systemId in flush interval deps - Preserve current form value in WidgetIntegrationSelectInput when it's missing from loaded options (matches WidgetDynamicSelectInput pattern)
This commit is contained in:
2 files changed
+23
-10
No files matched your search
@@ -22,6 +22,11 @@ export const WidgetIntegrationSelectInput = ({
|
||||
const integrationIds = form.values.integrationIds as string[];
|
||||
|
||||
const { data: selectData = [], isPending, isError } = options.useOptions(integrationIds);
|
||||
const currentValue = form.getInputProps(`options.${property}`).value as string;
|
||||
const data =
|
||||
currentValue && !selectData.some((opt) => opt.value === currentValue)
|
||||
? [...selectData, { value: currentValue, label: currentValue }]
|
||||
: selectData;
|
||||
|
||||
if (integrationIds.length === 0) {
|
||||
return (
|
||||
@@ -39,7 +44,7 @@ export const WidgetIntegrationSelectInput = ({
|
||||
clearable={options.clearable}
|
||||
searchable={options.searchable}
|
||||
nothingFoundMessage={t("noResults")}
|
||||
data={selectData}
|
||||
data={data}
|
||||
disabled={isError}
|
||||
error={(isError && t("loadError")) || undefined}
|
||||
{...form.getInputProps(`options.${property}`)}
|
||||
|
||||
@@ -30,10 +30,12 @@ export const useLiveStats = (integrationIds: string[], systemId: string, enabled
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
const bufferRef = useRef<LiveStatsData | null>(null);
|
||||
const dirtyRef = useRef(false);
|
||||
const generationRef = useRef(0);
|
||||
|
||||
const integrationKey = integrationIds.join(",");
|
||||
const integrationKey = [...integrationIds].sort().join(",");
|
||||
|
||||
useEffect(() => {
|
||||
generationRef.current += 1;
|
||||
setData(null);
|
||||
setError(null);
|
||||
bufferRef.current = null;
|
||||
@@ -42,13 +44,17 @@ export const useLiveStats = (integrationIds: string[], systemId: string, enabled
|
||||
|
||||
const onData = useCallback(
|
||||
(incoming: { stats: BeszelSystemStatsRecord; containerStats: BeszelContainerStatsRecord | null }) => {
|
||||
setError(null);
|
||||
const isFirst = !bufferRef.current;
|
||||
bufferRef.current = appendToBuffer(bufferRef.current, incoming);
|
||||
dirtyRef.current = true;
|
||||
if (isFirst) {
|
||||
setData(bufferRef.current);
|
||||
}
|
||||
const gen = generationRef.current;
|
||||
queueMicrotask(() => {
|
||||
if (generationRef.current !== gen) return;
|
||||
setError(null);
|
||||
const isFirst = !bufferRef.current;
|
||||
bufferRef.current = appendToBuffer(bufferRef.current, incoming);
|
||||
dirtyRef.current = true;
|
||||
if (isFirst) {
|
||||
setData(bufferRef.current);
|
||||
}
|
||||
});
|
||||
},
|
||||
[],
|
||||
);
|
||||
@@ -66,13 +72,15 @@ export const useLiveStats = (integrationIds: string[], systemId: string, enabled
|
||||
|
||||
useEffect(() => {
|
||||
if (!subscriptionEnabled) return;
|
||||
const gen = generationRef.current;
|
||||
const id = setInterval(() => {
|
||||
if (generationRef.current !== gen) return;
|
||||
if (!dirtyRef.current || !bufferRef.current) return;
|
||||
dirtyRef.current = false;
|
||||
setData(bufferRef.current);
|
||||
}, FLUSH_INTERVAL_MS);
|
||||
return () => clearInterval(id);
|
||||
}, [subscriptionEnabled]);
|
||||
}, [subscriptionEnabled, integrationKey, systemId]);
|
||||
|
||||
clientApi.widget.beszel.subscribeSystemStats.useSubscription(subscriptionInput, {
|
||||
enabled: subscriptionEnabled,
|
||||
|
||||
Reference in new issue
Block a user