OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
dashboard: tell a failed query apart from an empty one
Carl-Gerhard Lindesvärdce090a6ed007
diff --git a/apps/start/src/components/clients/table/index.tsx b/apps/start/src/components/clients/table/index.tsxindex 5bb5a4a94..32d6c895f 100644--- a/apps/start/src/components/clients/table/index.tsx+++ b/apps/start/src/components/clients/table/index.tsx@@ -14,7 +14,7 @@ interface Props {export const ClientsTable = ({ query }: Props) => {const columns = useColumns();- const { data, isLoading } = query;+ const { data, isLoading, isError } = query;const { table } = useTable({name: 'clients',@@ -35,7 +35,7 @@ export const ClientsTable = ({ query }: Props) => {Create client</Button></DataTableToolbar>- <DataTable loading={isLoading} table={table} />+ <DataTable error={isError} loading={isLoading} table={table} /></>);};diff --git a/apps/start/src/components/groups/table/index.tsx b/apps/start/src/components/groups/table/index.tsxindex b38e0481a..943608238 100644--- a/apps/start/src/components/groups/table/index.tsx+++ b/apps/start/src/components/groups/table/index.tsx@@ -39,7 +39,7 @@ const LOADING_DATA = [export const GroupsTable = memo(({ query, pageSize = PAGE_SIZE, toolbarLeft }: Props) => {- const { data, isLoading } = query;+ const { data, isLoading, isError } = query;const columns = useGroupColumns();const { setPage, state: pagination } = useDataTablePagination(pageSize);@@ -90,13 +90,20 @@ export const GroupsTable = memo(description:'Groups represent companies, teams, or other entities that events belong to.',}}+ error={isError}loading={isLoading}table={table}/></>);},- arePropsEqual(['query.isLoading', 'query.data', 'pageSize', 'toolbarLeft'])+ arePropsEqual([+ 'query.isLoading',+ 'query.isError',+ 'query.data',+ 'pageSize',+ 'toolbarLeft',+ ]));function GroupsTableToolbar({diff --git a/apps/start/src/components/notifications/table/index.tsx b/apps/start/src/components/notifications/table/index.tsxindex 34cfe383f..a0c8a34bc 100644--- a/apps/start/src/components/notifications/table/index.tsx+++ b/apps/start/src/components/notifications/table/index.tsx@@ -14,7 +14,7 @@ type Props = {export const NotificationsTable = ({ query }: Props) => {const columns = useColumns();- const { data, isLoading } = query;+ const { data, isLoading, isError } = query;const { table } = useTable({name: 'notifications',columns,@@ -26,7 +26,7 @@ export const NotificationsTable = ({ query }: Props) => {return (<><DataTableToolbar table={table} />- <DataTable loading={isLoading} table={table} />+ <DataTable error={isError} loading={isLoading} table={table} /></>);};diff --git a/apps/start/src/components/pages/table/index.tsx b/apps/start/src/components/pages/table/index.tsxindex 63e3045f3..2b3549a41 100644--- a/apps/start/src/components/pages/table/index.tsx+++ b/apps/start/src/components/pages/table/index.tsx@@ -124,6 +124,7 @@ export function PagesTable({ projectId }: PagesTableProps) {? `No pages found matching "${debouncedSearch}"`: 'Integrate our web SDK to your site to get pages here.',}}+ error={pagesQuery.isError}loading={pagesQuery.isLoading}onRowClick={(row) => {if (!isGscConnected) {diff --git a/apps/start/src/components/profiles/table/index.tsx b/apps/start/src/components/profiles/table/index.tsxindex 7e3c5c2af..8f2c4e8bd 100644--- a/apps/start/src/components/profiles/table/index.tsx+++ b/apps/start/src/components/profiles/table/index.tsx@@ -33,7 +33,7 @@ const LOADING_DATA = [{}, {}, {}, {}, {}, {}, {}, {}, {}] as IServiceProfile[];export const ProfilesTable = memo(({ type, query, pageSize = PAGE_SIZE }: Props) => {- const { data, isLoading } = query;+ const { data, isLoading, isError } = query;const columns = useColumns(type);const navigate = useNavigate();const { organizationId, projectId } = useAppParams();@@ -99,6 +99,7 @@ export const ProfilesTable = memo(title: 'No profiles',description: "Looks like you haven't identified any profiles yet.",}}+ error={isError}loading={isLoading}onRowClick={handleRowClick}table={table}@@ -106,7 +107,13 @@ export const ProfilesTable = memo(</>);},- arePropsEqual(['query.isLoading', 'query.data', 'type', 'pageSize'])+ arePropsEqual([+ 'query.isLoading',+ 'query.isError',+ 'query.data',+ 'type',+ 'pageSize',+ ]));function ProfileTableToolbar({ table }: { table: Table<IServiceProfile> }) {diff --git a/apps/start/src/components/settings/invites/index.tsx b/apps/start/src/components/settings/invites/index.tsxindex 885199907..511f4bb8e 100644--- a/apps/start/src/components/settings/invites/index.tsx+++ b/apps/start/src/components/settings/invites/index.tsx@@ -16,7 +16,7 @@ type Props = CommonProps;export const InvitesTable = ({ query }: Props) => {const columns = useColumns();- const { data, isLoading } = query;+ const { data, isLoading, isError } = query;const { table } = useTable({name: 'invites',columns,@@ -36,7 +36,7 @@ export const InvitesTable = ({ query }: Props) => {Invite user</Button></DataTableToolbar>- <DataTable loading={isLoading} table={table} />+ <DataTable error={isError} loading={isLoading} table={table} /></>);};diff --git a/apps/start/src/components/settings/members/index.tsx b/apps/start/src/components/settings/members/index.tsxindex f7d2492c9..11bf75136 100644--- a/apps/start/src/components/settings/members/index.tsx+++ b/apps/start/src/components/settings/members/index.tsx@@ -13,7 +13,7 @@ type Props = CommonProps;export const MembersTable = ({ query }: Props) => {const columns = useColumns();- const { data, isLoading } = query;+ const { data, isLoading, isError } = query;const { table } = useTable({name: 'members',columns,@@ -25,7 +25,7 @@ export const MembersTable = ({ query }: Props) => {return (<><DataTableToolbar table={table} />- <DataTable loading={isLoading} table={table} />+ <DataTable error={isError} loading={isLoading} table={table} /></>);};diff --git a/apps/start/src/components/ui/data-table/data-table.test.tsx b/apps/start/src/components/ui/data-table/data-table.test.tsxnew file mode 100644index 000000000..60314f42a--- /dev/null+++ b/apps/start/src/components/ui/data-table/data-table.test.tsx@@ -0,0 +1,75 @@+// @vitest-environment jsdom++// An error and an empty result both produce zero rows, so the table used to+// render "nothing here yet" for a failed query — a 500 from `profile.list`+// read as "Looks like you haven't identified any profiles yet" (ISSUES.md H11).++import {+ type ColumnDef,+ getCoreRowModel,+ useReactTable,+} from '@tanstack/react-table';+import { cleanup, render, screen } from '@testing-library/react';+import { afterEach, describe, expect, test } from 'vitest';+import { DataTable } from './data-table';++interface Row {+ name: string;+}++const COLUMNS: ColumnDef<Row>[] = [+ { accessorKey: 'name', header: 'Name', cell: ({ row }) => row.original.name },+];++const EMPTY = {+ title: 'No profiles',+ description: "Looks like you haven't identified any profiles yet.",+};++function Harness({ error, rows }: { error?: boolean; rows: Row[] }) {+ const table = useReactTable({+ data: rows,+ columns: COLUMNS,+ getCoreRowModel: getCoreRowModel(),+ // `src/types/data-table.ts` declares a custom `isWithinRange` filter, so+ // TableOptions requires it. This table does no filtering; the same stub+ // appears in report-table.tsx and groups/table/index.tsx.+ filterFns: { isWithinRange: () => true },+ });+ return <DataTable empty={EMPTY} error={error} table={table} />;+}++// Match on the descriptions: both states render a title in an <h1>, so a+// title match is ambiguous.+const EMPTY_TEXT = /haven't identified any profiles/;+const ERROR_TEXT = /could not load this data/;++const count = (pattern: RegExp) => screen.queryAllByText(pattern).length;++// `screen` queries the whole document and vitest.config.ts registers no setup+// file, so without this each render stacks on the last one's DOM.+afterEach(cleanup);++describe('DataTable', () => {+ test('renders the empty state when the query simply returned nothing', () => {+ render(<Harness rows={[]} />);+ expect([count(EMPTY_TEXT), count(ERROR_TEXT)]).toEqual([1, 0]);+ });++ test('renders an error instead of the empty state when the query failed', () => {+ render(<Harness error rows={[]} />);+ expect([count(EMPTY_TEXT), count(ERROR_TEXT)]).toEqual([0, 1]);+ });++ test('an error wins over rows that are still in the cache', () => {+ render(<Harness error rows={[{ name: 'stale row' }]} />);+ expect(count(ERROR_TEXT)).toBe(1);+ expect(screen.queryByText('stale row')).toBeNull();+ });++ test('renders rows normally', () => {+ render(<Harness rows={[{ name: 'ada' }]} />);+ expect(screen.queryByText('ada')).not.toBeNull();+ expect([count(EMPTY_TEXT), count(ERROR_TEXT)]).toEqual([0, 0]);+ });+});diff --git a/apps/start/src/components/ui/data-table/data-table.tsx b/apps/start/src/components/ui/data-table/data-table.tsxindex f45b6cfd5..d68c00d04 100644--- a/apps/start/src/components/ui/data-table/data-table.tsx+++ b/apps/start/src/components/ui/data-table/data-table.tsx@@ -10,6 +10,7 @@ import {} from '../table';import { getCommonPinningStyles } from './data-table-helpers';import { FullPageEmptyState } from '@/components/full-page-empty-state';+import { FullPageErrorState } from '@/components/full-page-error-state';import { FloatingPagination } from '@/components/pagination-floating';import { Skeleton } from '@/components/skeleton';import { cn } from '@/utils/cn';@@ -18,6 +19,12 @@ export interface DataTableProps<TData> {table: ITable<TData>;className?: string;loading?: boolean;+ /**+ * A failed query, not an empty one. Without this the table renders its+ * empty state for both, so a 500 from `profile.list` reads as "you haven't+ * identified any profiles yet" (ISSUES.md H11).+ */+ error?: boolean;empty?: {title: string;description: string;@@ -35,6 +42,7 @@ declare module '@tanstack/react-table' {export function DataTable<TData>({table,loading,+ error,className,onRowClick,empty = {@@ -75,7 +83,19 @@ export function DataTable<TData>({))}</TableHeader><TableBody>- {table.getRowModel().rows?.length ? (+ {error ? (+ <TableRow>+ <TableCell+ className="h-24 text-center"+ colSpan={table.getAllColumns().length}+ >+ <FullPageErrorState+ description="We could not load this data. Try again in a moment."+ title="Something went wrong"+ />+ </TableCell>+ </TableRow>+ ) : table.getRowModel().rows?.length ? (table.getRowModel().rows.map((row) => (<TableRowclassName={onRowClick ? 'cursor-pointer' : undefined}diff --git a/apps/start/src/routes/_app.$organizationId.$projectId.references.tsx b/apps/start/src/routes/_app.$organizationId.$projectId.references.tsxindex 2b36ae8cf..e53f1116b 100644--- a/apps/start/src/routes/_app.$organizationId.$projectId.references.tsx+++ b/apps/start/src/routes/_app.$organizationId.$projectId.references.tsx@@ -185,7 +185,7 @@ function Component() {<span className="sm:hidden">Reference</span></Button></DataTableToolbar>- <DataTable loading={loading} table={table} />+ <DataTable error={query.isError} loading={loading} table={table} /></PageContainer>);}