import type { DecrementPayload, IdentifyPayload, IncrementPayload, OpenPanelMethodNames, OpenPanelOptions, TrackProperties, } from '@openpanel/web'; import { getInitSnippet } from '@openpanel/web'; // adding .js next/script import fixes an issues // with esm and nextjs (when using pages dir) import Script from 'next/script.js'; // biome-ignore lint/correctness/noUnusedImports: nextjs requires this import React from 'react'; export * from '@openpanel/web'; const CDN_URL = 'https://openpanel.dev/op1.js'; type OpenPanelComponentProps = Omit & { profileId?: string; /** @deprecated Use `scriptUrl` instead. */ cdnUrl?: string; scriptUrl?: string; filter?: string; globalProperties?: Record; strategy?: 'beforeInteractive' | 'afterInteractive' | 'lazyOnload' | 'worker'; }; const stringify = (obj: unknown) => { if (typeof obj === 'object' && obj !== null && obj !== undefined) { const entries = Object.entries(obj).map(([key, value]) => { if (key === 'filter') { return `"${key}":${value}`; } return `"${key}":${JSON.stringify(value)}`; }); return `{${entries.join(',')}}`; } return JSON.stringify(obj); }; export function OpenPanelComponent({ profileId, cdnUrl, scriptUrl, globalProperties, strategy = 'afterInteractive', ...options }: OpenPanelComponentProps) { const methods: { name: OpenPanelMethodNames; value: unknown }[] = [ { name: 'init', value: { ...options, sdk: 'nextjs', sdkVersion: process.env.NEXTJS_VERSION!, }, }, ]; if (profileId) { methods.push({ name: 'identify', value: { profileId, }, }); } if (globalProperties) { methods.push({ name: 'setGlobalProperties', value: globalProperties, }); } const appendVersion = (url: string) => { if (url.endsWith('.js')) { return `${url}?v=${process.env.NEXTJS_VERSION!}`; } return url; }; return ( <>