Skip to content

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

dashboard: a missing parameter is a message, not a stack trace

Carl-Gerhard Lindesvärd0a1116851012
diff --git a/apps/start/src/components/chat/tool-handlers.ts b/apps/start/src/components/chat/tool-handlers.ts
index 8612f59d6..f73f526a6 100644
--- a/apps/start/src/components/chat/tool-handlers.ts
+++ b/apps/start/src/components/chat/tool-handlers.ts
@@ -34,15 +34,13 @@ function pushUrl(url: URL): void {
window.dispatchEvent(new PopStateEvent('popstate'));
}
-function applyFilters(input: ApplyFiltersInput): {
- applied: boolean;
- applied_filters: ApplyFiltersInput;
-} {
- if (typeof window === 'undefined') {
- return { applied: false, applied_filters: input };
- }
- const url = new URL(window.location.href);
-
+// Each of these mutates a URL rather than navigating, so one command that
+// changes a range AND a filter results in one history entry and one round of
+// queries instead of two or three.
+function applyFiltersToUrl(
+ url: URL,
+ input: Omit<ApplyFiltersInput, 'range'> & { range?: string }
+): void {
if (input.startDate && input.endDate) {
url.searchParams.set('range', 'custom');
url.searchParams.set('start', input.startDate);
@@ -56,7 +54,42 @@ function applyFilters(input: ApplyFiltersInput): {
if (input.interval) {
url.searchParams.set('overrideInterval', input.interval);
}
+}
+function setPropertyFiltersOnUrl(
+ url: URL,
+ input: {
+ filters: (Omit<PropertyFilter, 'operator'> & { operator?: string })[];
+ }
+): void {
+ if (input.filters.length === 0) {
+ url.searchParams.delete('f');
+ } else {
+ url.searchParams.set('f', serializePropertyFilters(input.filters));
+ }
+}
+
+function setEventNamesFilterOnUrl(
+ url: URL,
+ input: SetEventNamesFilterInput
+): void {
+ if (input.eventNames.length === 0) {
+ url.searchParams.delete('events');
+ } else {
+ // nuqs `parseAsArrayOf(parseAsString)` defaults to comma-separated.
+ url.searchParams.set('events', input.eventNames.join(','));
+ }
+}
+
+function applyFilters(input: ApplyFiltersInput): {
+ applied: boolean;
+ applied_filters: ApplyFiltersInput;
+} {
+ if (typeof window === 'undefined') {
+ return { applied: false, applied_filters: input };
+ }
+ const url = new URL(window.location.href);
+ applyFiltersToUrl(url, input);
pushUrl(url);
return { applied: true, applied_filters: input };
}
@@ -66,7 +99,9 @@ function applyFilters(input: ApplyFiltersInput): {
* `name,operator,value1|value2`, joined by `;`. We URL-encode the
* values to match the parser.
*/
-function serializePropertyFilters(filters: PropertyFilter[]): string {
+function serializePropertyFilters(
+ filters: (Omit<PropertyFilter, 'operator'> & { operator?: string })[]
+): string {
return filters
.map((f) => {
const op = f.operator ?? 'is';
@@ -84,11 +119,7 @@ function setPropertyFilters(input: SetPropertyFiltersInput): {
return { applied: false, count: 0 };
}
const url = new URL(window.location.href);
- if (input.filters.length === 0) {
- url.searchParams.delete('f');
- } else {
- url.searchParams.set('f', serializePropertyFilters(input.filters));
- }
+ setPropertyFiltersOnUrl(url, input);
pushUrl(url);
return { applied: true, count: input.filters.length };
}
@@ -101,12 +132,7 @@ function setEventNamesFilter(input: SetEventNamesFilterInput): {
return { applied: false, count: 0 };
}
const url = new URL(window.location.href);
- if (input.eventNames.length === 0) {
- url.searchParams.delete('events');
- } else {
- // nuqs `parseAsArrayOf(parseAsString)` defaults to comma-separated.
- url.searchParams.set('events', input.eventNames.join(','));
- }
+ setEventNamesFilterOnUrl(url, input);
pushUrl(url);
return { applied: true, count: input.eventNames.length };
}
@@ -118,3 +144,48 @@ export const chatToolHandlers: ChatClientToolHandlers = {
set_event_names_filter: async (input) =>
setEventNamesFilter(input as SetEventNamesFilterInput),
};
+
+/**
+ * Applies every part of one filter command in a single navigation.
+ *
+ * The overview's AI command used to await the three handlers in turn, so a
+ * command like "last 30 days, mobile only" pushed two history entries and ran
+ * every overview query twice.
+ */
+export function applyFilterCommandToUrl(command: {
+ // `range` and `operator` are widened to `string` on purpose.
+ // `overview.runFilterCommand` validates against the canonical `zRange` and
+ // operator list, while the chat tool's own schemas in assistant.constants.ts
+ // hand-list subsets: they are missing `last24h` and `3m` for range, and
+ // `inCohort`/`notInCohort` for operator. Both values are written verbatim
+ // into search params that the overview's own parsers then validate, so the
+ // wider types are correct here — and they keep the drift visible instead of
+ // hiding it behind the cast the tool-handler map used to apply.
+ applyFilters?: (Omit<ApplyFiltersInput, 'range'> & { range?: string }) | null;
+ setPropertyFilters?: {
+ filters: (Omit<PropertyFilter, 'operator'> & { operator?: string })[];
+ } | null;
+ setEventNamesFilter?: SetEventNamesFilterInput | null;
+}): number {
+ if (typeof window === 'undefined') {
+ return 0;
+ }
+ const url = new URL(window.location.href);
+ let applied = 0;
+ if (command.applyFilters) {
+ applyFiltersToUrl(url, command.applyFilters);
+ applied += 1;
+ }
+ if (command.setPropertyFilters) {
+ setPropertyFiltersOnUrl(url, command.setPropertyFilters);
+ applied += 1;
+ }
+ if (command.setEventNamesFilter) {
+ setEventNamesFilterOnUrl(url, command.setEventNamesFilter);
+ applied += 1;
+ }
+ if (applied > 0) {
+ pushUrl(url);
+ }
+ return applied;
+}
diff --git a/apps/start/src/components/cohort/cohort-criteria-builder.tsx b/apps/start/src/components/cohort/cohort-criteria-builder.tsx
index 569af2a90..eb4bd5cd9 100644
--- a/apps/start/src/components/cohort/cohort-criteria-builder.tsx
+++ b/apps/start/src/components/cohort/cohort-criteria-builder.tsx
@@ -28,7 +28,7 @@ export function CohortCriteriaBuilder({
onChange,
}: CohortCriteriaBuilderProps) {
const { projectId } = useAppParams();
- const eventNames = useEventNames({ projectId });
+ const eventNames = useEventNames({ projectId, anyEvents: false });
const handleTypeChange = (type: 'event' | 'property') => {
if (type === 'event') {
diff --git a/apps/start/src/components/overview/overview-ai-command.tsx b/apps/start/src/components/overview/overview-ai-command.tsx
index b8701f4fb..1222c2bb8 100644
--- a/apps/start/src/components/overview/overview-ai-command.tsx
+++ b/apps/start/src/components/overview/overview-ai-command.tsx
@@ -2,7 +2,7 @@ import { useMutation } from '@tanstack/react-query';
import { Loader2Icon, SparklesIcon } from 'lucide-react';
import { useState } from 'react';
import { toast } from 'sonner';
-import { chatToolHandlers } from '../chat/tool-handlers';
+import { applyFilterCommandToUrl } from '../chat/tool-handlers';
import { Input } from '../ui/input';
import { useOverviewOptions } from './useOverviewOptions';
import { useAppParams } from '@/hooks/use-app-params';
@@ -16,7 +16,7 @@ import { cn } from '@/utils/cn';
/**
* Inline AI command bar. Calls the `overview.runFilterCommand`
* mutation, then dispatches the returned URL-mutator actions through
- * the same handlers the chat panel uses (`chatToolHandlers`).
+ * the same URL serialization the chat panel's handlers use.
*
* `className` controls layout — caller passes the width / responsive
* visibility it wants (e.g. compact `hidden w-[280px] md:block` in a
@@ -39,29 +39,19 @@ export function OverviewAICommand({ className }: { className?: string }) {
// Strip null inner fields from applyFilters: the schema uses
// nullable for OpenAI strict-mode compatibility, but the chat
// handler expects undefined to mean "leave this alone".
- let applied = 0;
- if (result.applyFilters) {
- const af = result.applyFilters;
- await chatToolHandlers.apply_filters({
- ...(af.range !== null ? { range: af.range } : {}),
- ...(af.startDate !== null ? { startDate: af.startDate } : {}),
- ...(af.endDate !== null ? { endDate: af.endDate } : {}),
- ...(af.interval !== null ? { interval: af.interval } : {}),
- });
- applied += 1;
- }
- if (result.setPropertyFilters) {
- await chatToolHandlers.set_property_filters(
- result.setPropertyFilters
- );
- applied += 1;
- }
- if (result.setEventNamesFilter) {
- await chatToolHandlers.set_event_names_filter(
- result.setEventNamesFilter
- );
- applied += 1;
- }
+ const af = result.applyFilters;
+ const applied = applyFilterCommandToUrl({
+ applyFilters: af
+ ? {
+ ...(af.range !== null ? { range: af.range } : {}),
+ ...(af.startDate !== null ? { startDate: af.startDate } : {}),
+ ...(af.endDate !== null ? { endDate: af.endDate } : {}),
+ ...(af.interval !== null ? { interval: af.interval } : {}),
+ }
+ : null,
+ setPropertyFilters: result.setPropertyFilters,
+ setEventNamesFilter: result.setEventNamesFilter,
+ });
if (applied === 0) {
toast(
diff --git a/apps/start/src/routes/unsubscribe.tsx b/apps/start/src/routes/unsubscribe.tsx
index edc267cf2..d8aa55b3e 100644
--- a/apps/start/src/routes/unsubscribe.tsx
+++ b/apps/start/src/routes/unsubscribe.tsx
@@ -3,6 +3,7 @@ import { useMutation } from '@tanstack/react-query';
import { createFileRoute, useSearch } from '@tanstack/react-router';
import { useState } from 'react';
import { z } from 'zod';
+import { FullPageEmptyState } from '@/components/full-page-empty-state';
import FullPageLoadingState from '@/components/full-page-loading-state';
import { PublicPageCard } from '@/components/public-page-card';
import { Button, LinkButton } from '@/components/ui/button';
@@ -18,6 +19,14 @@ export const Route = createFileRoute('/unsubscribe')({
component: RouteComponent,
validateSearch: unsubscribeSearchSchema,
pendingComponent: FullPageLoadingState,
+ // An unsubscribe link with a missing or malformed parameter used to render
+ // the raw ZodError issues array at the recipient.
+ errorComponent: () => (
+ <FullPageEmptyState
+ description="This unsubscribe link is missing information or has expired. Open the link from your email again, or change your preferences from your account settings."
+ title="Link not valid"
+ />
+ ),
});
function RouteComponent() {
diff --git a/apps/start/src/routes/widget/badge.tsx b/apps/start/src/routes/widget/badge.tsx
index 20b031bb6..0ddae2734 100644
--- a/apps/start/src/routes/widget/badge.tsx
+++ b/apps/start/src/routes/widget/badge.tsx
@@ -12,6 +12,14 @@ const widgetSearchSchema = z.object({
});
export const Route = createFileRoute('/widget/badge')({
+ // Embedded in someone else's page, so this stays small. Without it a
+ // missing `shareId` fell through to the root handler, which prints
+ // `error.message` — for a ZodError that is the raw issues array.
+ errorComponent: () => (
+ <div className="p-2 text-muted-foreground text-xs">
+ Missing or invalid widget link.
+ </div>
+ ),
component: RouteComponent,
validateSearch: widgetSearchSchema,
});
diff --git a/apps/start/src/routes/widget/counter.tsx b/apps/start/src/routes/widget/counter.tsx
index d745aa62e..65a69cd08 100644
--- a/apps/start/src/routes/widget/counter.tsx
+++ b/apps/start/src/routes/widget/counter.tsx
@@ -14,6 +14,14 @@ const widgetSearchSchema = z.object({
});
export const Route = createFileRoute('/widget/counter')({
+ // Embedded in someone else's page, so this stays small. Without it a
+ // missing `shareId` fell through to the root handler, which prints
+ // `error.message` — for a ZodError that is the raw issues array.
+ errorComponent: () => (
+ <div className="p-2 text-muted-foreground text-xs">
+ Missing or invalid widget link.
+ </div>
+ ),
component: RouteComponent,
validateSearch: widgetSearchSchema,
});
diff --git a/apps/start/src/routes/widget/realtime.tsx b/apps/start/src/routes/widget/realtime.tsx
index 8e1a4cdb3..35cc32590 100644
--- a/apps/start/src/routes/widget/realtime.tsx
+++ b/apps/start/src/routes/widget/realtime.tsx
@@ -34,6 +34,14 @@ const widgetSearchSchema = z.object({
});
export const Route = createFileRoute('/widget/realtime')({
+ // Embedded in someone else's page, so this stays small. Without it a
+ // missing `shareId` fell through to the root handler, which prints
+ // `error.message` — for a ZodError that is the raw issues array.
+ errorComponent: () => (
+ <div className="p-2 text-muted-foreground text-xs">
+ Missing or invalid widget link.
+ </div>
+ ),
component: RouteComponent,
validateSearch: widgetSearchSchema,
});