OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
events: fall back when an event's icon name is not in the mapper
Carl-Gerhard Lindesvärdebc68650b841
diff --git a/apps/start/src/components/events/event-icon.test.tsx b/apps/start/src/components/events/event-icon.test.tsxnew file mode 100644index 000000000..5bbc1460f--- /dev/null+++ b/apps/start/src/components/events/event-icon.test.tsx@@ -0,0 +1,45 @@+// @vitest-environment jsdom++// `meta.icon` is a free-text column an operator fills in, so it can name an+// icon `EventIconMapper` does not carry — or be the empty string, which is+// not nullish and therefore survives the `??` chain. Either way the lookup+// used to yield `undefined`, and rendering `undefined` as a component makes+// React throw "Element type is invalid" and discard the whole subtree. On the+// mobile events list (`event-list-item.tsx`) that is the entire page.++import type { EventMeta } from '@openpanel/db';+import { render } from '@testing-library/react';+import { describe, expect, test } from 'vitest';+import { EventIcon } from './event-icon';++function metaWithIcon(icon: string): EventMeta {+ return { icon, color: 'blue' } as EventMeta;+}++describe('EventIcon', () => {+ test('renders a known icon from meta', () => {+ const { container } = render(+ <EventIcon meta={metaWithIcon('HomeIcon')} name="screen_view" />+ );+ expect(container.querySelector('svg')).not.toBeNull();+ });++ test('falls back when meta names an icon the mapper does not carry', () => {+ const { container } = render(+ <EventIcon meta={metaWithIcon('NoSuchIcon')} name="screen_view" />+ );+ expect(container.querySelector('svg')).not.toBeNull();+ });++ test('falls back when meta carries an empty icon name', () => {+ const { container } = render(+ <EventIcon meta={metaWithIcon('')} name="screen_view" />+ );+ expect(container.querySelector('svg')).not.toBeNull();+ });++ test('renders for an event name with no record and no meta', () => {+ const { container } = render(<EventIcon name="an_unseeded_event" />);+ expect(container.querySelector('svg')).not.toBeNull();+ });+});diff --git a/apps/start/src/components/events/event-icon.tsx b/apps/start/src/components/events/event-icon.tsxindex 4330edc11..cdaa0ff87 100644--- a/apps/start/src/components/events/event-icon.tsx+++ b/apps/start/src/components/events/event-icon.tsx@@ -219,19 +219,24 @@ export const EventIconColors = ['slate',];+/** What `EventIconRecords.default` resolves to; also the last-resort lookup. */+const FALLBACK_ICON_NAME = 'BotIcon';+const FALLBACK_COLOR = 'slate';+export function EventIcon({ className, name, size, meta }: EventIconProps) {- const Icon =- EventIconMapper[- meta?.icon ??- EventIconRecords[name]?.icon ??- EventIconRecords.default?.icon ??- ''- ]!;+ const iconName =+ meta?.icon ??+ EventIconRecords[name]?.icon ??+ EventIconRecords.default?.icon ??+ FALLBACK_ICON_NAME;+ // `meta.icon` is operator-entered and may name an icon this mapper does not+ // carry; an undefined component makes React throw the whole tree away.+ const Icon = EventIconMapper[iconName] ?? EventIconMapper[FALLBACK_ICON_NAME];const color =meta?.color ??EventIconRecords[name]?.color ??EventIconRecords.default?.color ??- '';+ FALLBACK_COLOR;return (<div className={cn(`bg-${color}-200`, variants({ size }), className)}>