Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(dashboard): failed requests on sessions and overview show an error

Carl-Gerhard Lindesvärdc87e619bd22b
diff --git a/apps/start/src/components/overview/overview-metric-card.tsx b/apps/start/src/components/overview/overview-metric-card.tsx
index ddafac6b8..877d2069b 100644
--- a/apps/start/src/components/overview/overview-metric-card.tsx
+++ b/apps/start/src/components/overview/overview-metric-card.tsx
@@ -16,6 +16,7 @@ import { cn } from '@/utils/cn';
import { formatDate as formatAbsoluteDate, timeAgo } from '@/utils/date';
const PRIMARY_COLOR = 'var(--chart-0)';
+const METRIC_ERROR_LABEL = 'Could not load';
export type MetricUnit = '' | 'date' | 'timeAgo' | 'min' | '%' | 'currency';
@@ -36,6 +37,8 @@ interface MetricCardProps {
active?: boolean;
inverted?: boolean;
isLoading?: boolean;
+ /** The metric failed to load: show that instead of a value that reads as real. */
+ isError?: boolean;
/** Interval drives the hover-label date format. */
interval?: IInterval;
/** Range drives the default label ("Last 30 days") and dashed-tail logic. */
@@ -43,7 +46,6 @@ interface MetricCardProps {
}
export function OverviewMetricCard({
- id,
data,
metric,
unit,
@@ -52,6 +54,7 @@ export function OverviewMetricCard({
active,
inverted = false,
isLoading = false,
+ isError = false,
interval = 'day',
range,
}: MetricCardProps) {
@@ -70,6 +73,7 @@ export function OverviewMetricCard({
: (range ? timeWindows[range]?.label : 'Total') || 'Total';
const diff = getPreviousMetric(displayValue, displayPrev);
+ const hasValue = !(isLoading || isError);
return (
<MetricCardShell active={active} onClick={onClick}>
@@ -78,7 +82,7 @@ export function OverviewMetricCard({
<span className="truncate font-medium text-[10px] text-muted-foreground uppercase tracking-wider">
{label}
</span>
- {isLoading ? null : (
+ {hasValue && (
<PreviousDiffIndicatorPure
{...diff}
inverted={inverted}
@@ -87,14 +91,12 @@ export function OverviewMetricCard({
)}
</div>
<div className="mt-1 flex items-baseline gap-0.5 leading-none">
- {isLoading ? (
- <Skeleton className="h-5 w-20" />
- ) : (
- <MetricValue unit={unit} value={displayValue} />
- )}
+ {isLoading && <Skeleton className="h-5 w-20" />}
+ {isError && <span className={VALUE_CLASS}>—</span>}
+ {hasValue && <MetricValue unit={unit} value={displayValue} />}
</div>
<div className="mt-0.5 truncate text-[11px] text-muted-foreground leading-none">
- {displayLabel}
+ {isError ? METRIC_ERROR_LABEL : displayLabel}
</div>
</div>
diff --git a/apps/start/src/components/overview/overview-metrics.tsx b/apps/start/src/components/overview/overview-metrics.tsx
index a29505ba9..5503728f2 100644
--- a/apps/start/src/components/overview/overview-metrics.tsx
+++ b/apps/start/src/components/overview/overview-metrics.tsx
@@ -1,7 +1,7 @@
import type { IInterval } from '@openpanel/core/modules/report/report.constants';
import { useQuery } from '@tanstack/react-query';
import { curveMonotoneX } from '@visx/curve';
-import { DollarSignIcon } from 'lucide-react';
+import { DollarSignIcon, RotateCwIcon } from 'lucide-react';
import { useCallback, useMemo, useState } from 'react';
import { Area } from '../charts/area';
import { ComposedChart } from '../charts/composed-chart';
@@ -16,6 +16,7 @@ import { OPChartTooltip } from '../charts/op-tooltip';
import { XAxis } from '../charts/x-axis';
import { YAxis } from '../charts/y-axis';
import { Skeleton } from '../skeleton';
+import { Button } from '../ui/button';
import { OverviewLiveHistogram } from './overview-live-histogram';
import { OverviewMetricCard } from './overview-metric-card';
import { useOverviewOptions } from '@/components/overview/useOverviewOptions';
@@ -124,6 +125,7 @@ export default function OverviewMetrics({
id={title.key}
interval={interval}
inverted={title.inverted}
+ isError={overviewQuery.isError}
isLoading={overviewQuery.isLoading}
key={title.key}
label={title.title}
@@ -164,7 +166,21 @@ export default function OverviewMetrics({
</div>
<div className="h-[190px] w-full">
{overviewQuery.isLoading && <Skeleton className="h-full w-full" />}
- {!overviewQuery.isLoading && data.length > 0 && (
+ {overviewQuery.isError && (
+ <div className="center-center h-full flex-col gap-3 text-center text-muted-foreground text-sm">
+ We could not load this data. Try again in a moment.
+ <Button
+ icon={RotateCwIcon}
+ loading={overviewQuery.isFetching}
+ onClick={() => overviewQuery.refetch()}
+ size="sm"
+ variant="outline"
+ >
+ Try again
+ </Button>
+ </div>
+ )}
+ {overviewQuery.isSuccess && data.length > 0 && (
<Chart
activeMetric={activeMetric}
data={data}
diff --git a/apps/start/src/components/sessions/table/index.tsx b/apps/start/src/components/sessions/table/index.tsx
index fc7fdf65c..d09ead065 100644
--- a/apps/start/src/components/sessions/table/index.tsx
+++ b/apps/start/src/components/sessions/table/index.tsx
@@ -1,7 +1,30 @@
+import type { IServiceSession } from '@openpanel/core';
import type { UseInfiniteQueryResult } from '@tanstack/react-query';
+import { useNavigate } from '@tanstack/react-router';
+import type { Table } from '@tanstack/react-table';
+import { getCoreRowModel, useReactTable } from '@tanstack/react-table';
+import { useWindowVirtualizer } from '@tanstack/react-virtual';
+import type { TRPCInfiniteData } from '@trpc/tanstack-react-query';
+import { Loader2Icon, RotateCwIcon } from 'lucide-react';
+import { last } from 'ramda';
+import { memo, useCallback, useEffect, useMemo, useRef } from 'react';
+import { useInViewport } from 'react-in-viewport';
import { useLocalStorage } from 'usehooks-ts';
import { useColumns } from './columns';
+import { TableFilterPills } from '@/components/filters/TableFilterPills';
+import { FullPageEmptyState } from '@/components/full-page-empty-state';
+import { FullPageErrorState } from '@/components/full-page-error-state';
+import { Skeleton } from '@/components/skeleton';
+import { Button } from '@/components/ui/button';
+import {
+ AnimatedSearchInput,
+ DataTableToolbarContainer,
+} from '@/components/ui/data-table/data-table-toolbar';
+import { DataTableViewOptions } from '@/components/ui/data-table/data-table-view-options';
+import { useAppParams } from '@/hooks/use-app-params';
+import { useSearchQueryState } from '@/hooks/use-search-query-state';
import type { RouterInputs, RouterOutputs } from '@/trpc/client';
+import { cn } from '@/utils/cn';
// Custom hook for persistent column visibility
const usePersistentColumnVisibility = (columns: any[]) => {
@@ -35,29 +58,7 @@ const usePersistentColumnVisibility = (columns: any[]) => {
};
};
-import type { IServiceSession } from '@openpanel/core';
-import { useNavigate } from '@tanstack/react-router';
-import type { Table } from '@tanstack/react-table';
-import { getCoreRowModel, useReactTable } from '@tanstack/react-table';
-import { useWindowVirtualizer } from '@tanstack/react-virtual';
-import type { TRPCInfiniteData } from '@trpc/tanstack-react-query';
-import { Loader2Icon } from 'lucide-react';
-import { last } from 'ramda';
-import { memo, useCallback, useEffect, useMemo, useRef } from 'react';
-import { useInViewport } from 'react-in-viewport';
-import { TableFilterPills } from '@/components/filters/TableFilterPills';
-import { FullPageEmptyState } from '@/components/full-page-empty-state';
-import { Skeleton } from '@/components/skeleton';
-import {
- AnimatedSearchInput,
- DataTableToolbarContainer,
-} from '@/components/ui/data-table/data-table-toolbar';
-import { DataTableViewOptions } from '@/components/ui/data-table/data-table-view-options';
-import { useAppParams } from '@/hooks/use-app-params';
-import { useSearchQueryState } from '@/hooks/use-search-query-state';
-import { cn } from '@/utils/cn';
-
-type Props = {
+interface Props {
query: UseInfiniteQueryResult<
TRPCInfiniteData<
RouterInputs['session']['list'],
@@ -65,7 +66,7 @@ type Props = {
>,
unknown
>;
-};
+}
const LOADING_DATA = [{}, {}, {}, {}, {}, {}, {}, {}, {}] as IServiceSession[];
const ROW_HEIGHT = 40;
@@ -288,7 +289,7 @@ export const SessionsTable = ({ query }: Props) => {
}
return query.data?.pages?.flatMap((p) => p.items) ?? [];
- }, [query.data]);
+ }, [query.data, isLoading]);
// const { setPage, state: pagination } = useDataTablePagination();
const { columnVisibility, setColumnVisibility } =
@@ -332,6 +333,28 @@ export const SessionsTable = ({ query }: Props) => {
}
}, [inViewport, enterCount, hasNextPage]);
+ const hasFailedWithoutData = query.isError && !query.data;
+ if (hasFailedWithoutData) {
+ return (
+ <>
+ <SessionTableToolbar table={table} />
+ <FullPageErrorState
+ description="We could not load this data. Try again in a moment."
+ title="Something went wrong"
+ >
+ <Button
+ icon={RotateCwIcon}
+ loading={query.isFetching}
+ onClick={() => query.refetch()}
+ variant="outline"
+ >
+ Try again
+ </Button>
+ </FullPageErrorState>
+ </>
+ );
+ }
+
return (
<>
<SessionTableToolbar table={table} />