move sdk packages to its own folder and rename api & dashboard
This commit is contained in:
116
packages/sdks/nextjs/index.tsx
Normal file
116
packages/sdks/nextjs/index.tsx
Normal file
@@ -0,0 +1,116 @@
|
||||
import Script from 'next/script';
|
||||
|
||||
import type {
|
||||
MixanEventOptions,
|
||||
MixanWebOptions,
|
||||
PostEventPayload,
|
||||
UpdateProfilePayload,
|
||||
} from '@mixan/web';
|
||||
|
||||
const CDN_URL = 'http://localhost:3002/op.js';
|
||||
|
||||
type OpenpanelMethods =
|
||||
| 'ctor'
|
||||
| 'event'
|
||||
| 'setProfile'
|
||||
| 'setProfileId'
|
||||
| 'increment'
|
||||
| 'decrement'
|
||||
| 'clear';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
op: {
|
||||
q?: [string, ...any[]];
|
||||
(method: OpenpanelMethods, ...args: any[]): void;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
type OpenpanelProviderProps = MixanWebOptions & {
|
||||
profileId?: string;
|
||||
cdnUrl?: string;
|
||||
};
|
||||
|
||||
export function OpenpanelProvider({
|
||||
profileId,
|
||||
cdnUrl,
|
||||
...options
|
||||
}: OpenpanelProviderProps) {
|
||||
const events: { name: OpenpanelMethods; value: unknown }[] = [
|
||||
{ name: 'ctor', value: options },
|
||||
];
|
||||
if (profileId) {
|
||||
events.push({ name: 'setProfileId', value: profileId });
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<Script src={cdnUrl ?? CDN_URL} async defer />
|
||||
<Script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `window.op = window.op || function(...args) {(window.op.q = window.op.q || []).push(args)};
|
||||
${events
|
||||
.map((event) => {
|
||||
return `window.op('${event.name}', ${JSON.stringify(event.value)});`;
|
||||
})
|
||||
.join('\n')}`,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
interface SetProfileIdProps {
|
||||
value?: string;
|
||||
}
|
||||
|
||||
export function SetProfileId({ value }: SetProfileIdProps) {
|
||||
return (
|
||||
<>
|
||||
<Script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `window.op('setProfileId', '${value}');`,
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function trackEvent(
|
||||
name: string,
|
||||
data?: PostEventPayload['properties']
|
||||
) {
|
||||
window.op('event', name, data);
|
||||
}
|
||||
|
||||
export function trackScreenView(data?: PostEventPayload['properties']) {
|
||||
trackEvent('screen_view', data);
|
||||
}
|
||||
|
||||
export function setProfile(data?: UpdateProfilePayload) {
|
||||
window.op('setProfile', data);
|
||||
}
|
||||
|
||||
export function setProfileId(profileId: string) {
|
||||
window.op('setProfileId', profileId);
|
||||
}
|
||||
|
||||
export function increment(
|
||||
property: string,
|
||||
value: number,
|
||||
options?: MixanEventOptions
|
||||
) {
|
||||
window.op('increment', property, value, options);
|
||||
}
|
||||
|
||||
export function decrement(
|
||||
property: string,
|
||||
value: number,
|
||||
options?: MixanEventOptions
|
||||
) {
|
||||
window.op('decrement', property, value, options);
|
||||
}
|
||||
|
||||
export function clear() {
|
||||
window.op('clear');
|
||||
}
|
||||
35
packages/sdks/nextjs/package.json
Normal file
35
packages/sdks/nextjs/package.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "@mixan/nextjs",
|
||||
"version": "0.0.1",
|
||||
"module": "index.ts",
|
||||
"scripts": {
|
||||
"build": "rm -rf dist && tsup",
|
||||
"build-for-openpanel": "pnpm build && cp dist/cdn.global.js ../../apps/public/public/op.js && cp dist/cdn.global.js ../../apps/test/public/op.js",
|
||||
"lint": "eslint .",
|
||||
"format": "prettier --check \"**/*.{mjs,ts,md,json}\"",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mixan/sdk": "workspace:*",
|
||||
"@mixan/web": "workspace:*"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"next": "^13.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@mixan/eslint-config": "workspace:*",
|
||||
"@mixan/prettier-config": "workspace:*",
|
||||
"@mixan/tsconfig": "workspace:*",
|
||||
"eslint": "^8.48.0",
|
||||
"prettier": "^3.0.3",
|
||||
"tsup": "^7.2.0",
|
||||
"typescript": "^5.2.2"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"root": true,
|
||||
"extends": [
|
||||
"@mixan/eslint-config/base"
|
||||
]
|
||||
},
|
||||
"prettier": "@mixan/prettier-config"
|
||||
}
|
||||
6
packages/sdks/nextjs/tsconfig.json
Normal file
6
packages/sdks/nextjs/tsconfig.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "@mixan/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
||||
9
packages/sdks/nextjs/tsup.config.ts
Normal file
9
packages/sdks/nextjs/tsup.config.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { defineConfig } from 'tsup';
|
||||
|
||||
import config from '@mixan/tsconfig/tsup.config.json' assert { type: 'json' };
|
||||
|
||||
export default defineConfig({
|
||||
...(config as any),
|
||||
entry: ['index.ts'],
|
||||
format: ['cjs', 'esm'],
|
||||
});
|
||||
Reference in New Issue
Block a user