Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor(web): Fix no-null-render: ChartLoadingState.tsx (#18225)

Ben Bachem0b952ba0ffa0
diff --git a/web/src/__tests__/chart-loading-state.clienttest.tsx b/web/src/__tests__/chart-loading-state.clienttest.tsx
index 9987890085..14228ca228 100644
--- a/web/src/__tests__/chart-loading-state.clienttest.tsx
+++ b/web/src/__tests__/chart-loading-state.clienttest.tsx
@@ -15,7 +15,7 @@ describe("ChartLoadingState", () => {
test("keeps legacy pending state spinner-only without loading copy or hint", () => {
const hintDelayMs = 1375;
const { container } = render(
- <ChartLoadingState isLoading={true} hintDelayMs={hintDelayMs} />,
+ <ChartLoadingState hintDelayMs={hintDelayMs} />,
);
expect(
@@ -37,7 +37,7 @@ describe("ChartLoadingState", () => {
test("keeps legacy pending state spinner-only when loading toggles off and on again", () => {
const hintDelayMs = 825;
const { rerender, container } = render(
- <ChartLoadingState isLoading={true} hintDelayMs={hintDelayMs} />,
+ <ChartLoadingState hintDelayMs={hintDelayMs} />,
);
act(() => {
@@ -46,11 +46,11 @@ describe("ChartLoadingState", () => {
expect(container.querySelector("svg")).toBeInTheDocument();
expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();
- rerender(<ChartLoadingState isLoading={false} hintDelayMs={hintDelayMs} />);
+ rerender(<></>);
expect(screen.queryByRole("status")).not.toBeInTheDocument();
expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();
- rerender(<ChartLoadingState isLoading={true} hintDelayMs={hintDelayMs} />);
+ rerender(<ChartLoadingState hintDelayMs={hintDelayMs} />);
expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();
act(() => {
@@ -60,13 +60,39 @@ describe("ChartLoadingState", () => {
expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();
});
+ test("resets progress and hint delays after unmounting", () => {
+ const { rerender } = render(
+ <ChartLoadingState progress={null} hintDelayMs={1500} />,
+ );
+
+ act(() => {
+ vi.advanceTimersByTime(1500);
+ });
+ expect(screen.getByRole("progressbar")).toBeInTheDocument();
+ expect(screen.getByText(SLOW_QUERY_HINT_TEXT)).toBeInTheDocument();
+
+ rerender(<></>);
+ expect(screen.queryByRole("status")).not.toBeInTheDocument();
+
+ rerender(<ChartLoadingState progress={null} hintDelayMs={1500} />);
+ expect(screen.queryByRole("progressbar")).not.toBeInTheDocument();
+ expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();
+
+ act(() => {
+ vi.advanceTimersByTime(1000);
+ });
+ expect(screen.getByRole("progressbar")).toBeInTheDocument();
+ expect(screen.queryByText(SLOW_QUERY_HINT_TEXT)).not.toBeInTheDocument();
+
+ act(() => {
+ vi.advanceTimersByTime(500);
+ });
+ expect(screen.getByText(SLOW_QUERY_HINT_TEXT)).toBeInTheDocument();
+ });
+
test("shows hint immediately without spinner for overload state", () => {
const { container } = render(
- <ChartLoadingState
- isLoading={true}
- showSpinner={false}
- showHintImmediately={true}
- />,
+ <ChartLoadingState showSpinner={false} showHintImmediately={true} />,
);
expect(screen.getByText(SLOW_QUERY_HINT_TEXT)).toBeInTheDocument();
@@ -83,7 +109,6 @@ describe("ChartLoadingState", () => {
render(
<ChartLoadingState
- isLoading={true}
showHintImmediately={true}
showSpinner={false}
hintText={customHint}
@@ -99,7 +124,6 @@ describe("ChartLoadingState", () => {
render(
<ChartLoadingState
- isLoading={true}
showHintImmediately={true}
showSpinner={false}
onRetry={onRetry}
@@ -112,9 +136,7 @@ describe("ChartLoadingState", () => {
});
test("shows only the spinner for the first second before swapping to the loading bar", () => {
- const { container } = render(
- <ChartLoadingState isLoading={true} progress={null} />,
- );
+ const { container } = render(<ChartLoadingState progress={null} />);
expect(container.querySelector("svg")).toBeInTheDocument();
expect(screen.queryByRole("progressbar")).not.toBeInTheDocument();
@@ -137,7 +159,6 @@ describe("ChartLoadingState", () => {
test("renders query progress details when progress is provided after the spinner phase", () => {
const { container } = render(
<ChartLoadingState
- isLoading={true}
progress={{
read_rows: 1_779_300_000,
total_rows_to_read: 2_924_500_000,
@@ -161,7 +182,7 @@ describe("ChartLoadingState", () => {
test("renders an indeterminate progress state while query progress is pending", () => {
const progress = null;
- render(<ChartLoadingState isLoading={true} progress={progress} />);
+ render(<ChartLoadingState progress={progress} />);
act(() => {
vi.advanceTimersByTime(1000);
@@ -179,9 +200,7 @@ describe("ChartLoadingState", () => {
});
test("renders the delayed warning below the loading bar section", () => {
- render(
- <ChartLoadingState isLoading={true} progress={null} hintDelayMs={1500} />,
- );
+ render(<ChartLoadingState progress={null} hintDelayMs={1500} />);
act(() => {
vi.advanceTimersByTime(1000);
@@ -204,12 +223,7 @@ describe("ChartLoadingState", () => {
test("prioritizes the loading bar in tight layouts once the spinner phase ends", () => {
render(
- <ChartLoadingState
- isLoading={true}
- progress={null}
- layout="tight"
- hintDelayMs={1500}
- />,
+ <ChartLoadingState progress={null} layout="tight" hintDelayMs={1500} />,
);
act(() => {
@@ -227,9 +241,7 @@ describe("ChartLoadingState", () => {
});
test("does not render skeleton preview chrome in the default layout", () => {
- const { container } = render(
- <ChartLoadingState isLoading={true} progress={null} />,
- );
+ const { container } = render(<ChartLoadingState progress={null} />);
expect(container.querySelector(".rounded-xl")).not.toBeInTheDocument();
});
diff --git a/web/src/features/monitors/components/MonitorChartPreview.tsx b/web/src/features/monitors/components/MonitorChartPreview.tsx
index 799d00bc39..29fd818663 100644
--- a/web/src/features/monitors/components/MonitorChartPreview.tsx
+++ b/web/src/features/monitors/components/MonitorChartPreview.tsx
@@ -182,16 +182,17 @@ export const MonitorChartPreview = ({
// before the real result (or the leading point) arrives.
isLoading={queryResult.isPending || scalarResult.isPending}
/>
- <ChartLoadingState
- isLoading={queryResult.isError}
- showSpinner={false}
- showHintImmediately
- layout="compact"
- hintText={
- queryResult.error?.message ?? RESOURCE_LIMIT_ERROR_MESSAGE
- }
- className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
- />
+ {queryResult.isError && (
+ <ChartLoadingState
+ showSpinner={false}
+ showHintImmediately
+ layout="compact"
+ hintText={
+ queryResult.error?.message ?? RESOURCE_LIMIT_ERROR_MESSAGE
+ }
+ className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
+ />
+ )}
</div>
</CardContent>
</Card>
diff --git a/web/src/features/widgets/chart-library/ChartLoadingState.tsx b/web/src/features/widgets/chart-library/ChartLoadingState.tsx
index 3880af9284..c83d4e9fb9 100644
--- a/web/src/features/widgets/chart-library/ChartLoadingState.tsx
+++ b/web/src/features/widgets/chart-library/ChartLoadingState.tsx
@@ -1,5 +1,5 @@
/* eslint-disable no-nested-ternary */
-/* eslint-disable @repo/no-style-props, @repo/no-null-render */
+/* eslint-disable @repo/no-style-props */
import { useEffect, useState } from "react";
import { cn } from "@/src/utils/tailwind";
import { SLOW_QUERY_HINT_TEXT } from "@langfuse/shared";
@@ -12,7 +12,6 @@ const DEFAULT_HINT_DELAY_MS = 2000;
const PROGRESS_REVEAL_DELAY_MS = 1000;
type ChartLoadingStateProps = {
- isLoading: boolean;
className?: string;
hintClassName?: string;
spinnerLabel?: string;
@@ -27,7 +26,6 @@ type ChartLoadingStateProps = {
};
export function ChartLoadingState({
- isLoading,
className,
hintClassName,
spinnerLabel = "Loading chart data",
@@ -43,14 +41,9 @@ export function ChartLoadingState({
const [showHint, setShowHint] = useState(false);
const [showProgressPhase, setShowProgressPhase] = useState(false);
const shouldShowProgress = progress !== undefined;
- const isPendingProgressState = isLoading && showSpinner && shouldShowProgress;
+ const isPendingProgressState = showSpinner && shouldShowProgress;
useEffect(() => {
- if (!isLoading) {
- setShowHint(false);
- return;
- }
-
const timeoutId = window.setTimeout(() => {
setShowHint(true);
}, hintDelayMs);
@@ -58,7 +51,7 @@ export function ChartLoadingState({
return () => {
window.clearTimeout(timeoutId);
};
- }, [hintDelayMs, isLoading]);
+ }, [hintDelayMs]);
useEffect(() => {
if (!isPendingProgressState) {
@@ -75,10 +68,6 @@ export function ChartLoadingState({
};
}, [isPendingProgressState]);
- if (!isLoading) {
- return null;
- }
-
const shouldShowHint = showHintImmediately || showHint;
const isCompact = layout !== "default";
const isTight = layout === "tight";
diff --git a/web/src/features/widgets/components/DashboardWidget.tsx b/web/src/features/widgets/components/DashboardWidget.tsx
index cd79d51c41..b54423a0de 100644
--- a/web/src/features/widgets/components/DashboardWidget.tsx
+++ b/web/src/features/widgets/components/DashboardWidget.tsx
@@ -776,7 +776,6 @@ export function DashboardWidget({
{!queryValidation.valid ? (
<div className="relative min-h-0 flex-1">
<ChartLoadingState
- isLoading={true}
showSpinner={false}
showHintImmediately={true}
hintText={queryValidation.reason}
@@ -818,16 +817,17 @@ export function DashboardWidget({
)}
/>
</div>
- <ChartLoadingState
- isLoading={chartLoadingState.isLoading}
- showSpinner={chartLoadingState.showSpinner}
- showHintImmediately={chartLoadingState.showHintImmediately}
- hintText={chartLoadingState.hintText}
- onRetry={queryResult.isError ? handleRetry : undefined}
- progress={loadingProgress}
- layout={loadingStateLayout}
- className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
- />
+ {chartLoadingState.isLoading && (
+ <ChartLoadingState
+ showSpinner={chartLoadingState.showSpinner}
+ showHintImmediately={chartLoadingState.showHintImmediately}
+ hintText={chartLoadingState.hintText}
+ onRetry={queryResult.isError ? handleRetry : undefined}
+ progress={loadingProgress}
+ layout={loadingStateLayout}
+ className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
+ />
+ )}
</div>
)}
</div>
diff --git a/web/src/features/widgets/components/InlineWidget.tsx b/web/src/features/widgets/components/InlineWidget.tsx
index 88eaa3d9da..4f149e3edf 100644
--- a/web/src/features/widgets/components/InlineWidget.tsx
+++ b/web/src/features/widgets/components/InlineWidget.tsx
@@ -433,16 +433,17 @@ export function WidgetContent({
zeroBaseline={zeroBaseline}
emptyState={emptyState}
/>
- <ChartLoadingState
- isLoading={chartLoadingState.isLoading}
- showSpinner={chartLoadingState.showSpinner}
- showHintImmediately={chartLoadingState.showHintImmediately}
- hintText={chartLoadingState.hintText}
- onRetry={queryResult.isError ? handleRetry : undefined}
- progress={loadingProgress}
- layout={layoutHint}
- className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
- />
+ {chartLoadingState.isLoading && (
+ <ChartLoadingState
+ showSpinner={chartLoadingState.showSpinner}
+ showHintImmediately={chartLoadingState.showHintImmediately}
+ hintText={chartLoadingState.hintText}
+ onRetry={queryResult.isError ? handleRetry : undefined}
+ progress={loadingProgress}
+ layout={layoutHint}
+ className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
+ />
+ )}
</div>
);
}
diff --git a/web/src/features/widgets/components/WidgetForm.tsx b/web/src/features/widgets/components/WidgetForm.tsx
index 9cb1098f9e..c2a35af959 100644
--- a/web/src/features/widgets/components/WidgetForm.tsx
+++ b/web/src/features/widgets/components/WidgetForm.tsx
@@ -1156,14 +1156,15 @@ export function WidgetForm({
selectedAggregation,
)}
/>
- <ChartLoadingState
- isLoading={chartLoadingState.isLoading}
- showSpinner={chartLoadingState.showSpinner}
- showHintImmediately={chartLoadingState.showHintImmediately}
- hintText={chartLoadingState.hintText}
- progress={loadingProgress}
- className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
- />
+ {chartLoadingState.isLoading && (
+ <ChartLoadingState
+ showSpinner={chartLoadingState.showSpinner}
+ showHintImmediately={chartLoadingState.showHintImmediately}
+ hintText={chartLoadingState.hintText}
+ progress={loadingProgress}
+ className="bg-background/80 absolute inset-0 z-20 backdrop-blur-xs"
+ />
+ )}
</div>
</div>
) : (
@@ -1171,7 +1172,6 @@ export function WidgetForm({
<div className="flex h-[300px] items-center justify-center">
{chartLoadingState.isLoading ? (
<ChartLoadingState
- isLoading={chartLoadingState.isLoading}
showSpinner={chartLoadingState.showSpinner}
showHintImmediately={chartLoadingState.showHintImmediately}
hintText={chartLoadingState.hintText}