reuse a bunch of stuff
This commit is contained in:
parent
c0a2387f76
commit
e256d13c92
14 changed files with 1593 additions and 138 deletions
0
packages/react/CHANGELOG.md
Normal file
0
packages/react/CHANGELOG.md
Normal file
21
packages/react/LICENSE
Normal file
21
packages/react/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2023 Sumbit Labs Ltd.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
0
packages/react/README.md
Normal file
0
packages/react/README.md
Normal file
41
packages/react/package.json
Normal file
41
packages/react/package.json
Normal file
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"name": "@aptabase/react",
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"description": "React SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"require": "./dist/index.cjs",
|
||||
"import": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts"
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/aptabase/aptabase-js.git",
|
||||
"directory": "packages/react"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/aptabase/aptabase-js/issues"
|
||||
},
|
||||
"homepage": "https://github.com/aptabase/aptabase-js",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "tsup"
|
||||
},
|
||||
"files": [
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"dependencies": {
|
||||
"@aptabase/web": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsup": "7.2.0"
|
||||
}
|
||||
}
|
39
packages/react/src/index.tsx
Normal file
39
packages/react/src/index.tsx
Normal file
|
@ -0,0 +1,39 @@
|
|||
'use client';
|
||||
|
||||
import { init, trackEvent, type AptabaseOptions } from '@aptabase/web';
|
||||
import { createContext, useContext, useEffect } from 'react';
|
||||
|
||||
type ContextProps = {
|
||||
appKey?: string;
|
||||
} & AptabaseOptions;
|
||||
|
||||
export type AptabaseClient = {
|
||||
trackEvent: typeof trackEvent;
|
||||
};
|
||||
|
||||
const AptabaseContext = createContext<ContextProps>({});
|
||||
|
||||
type Props = {
|
||||
appKey: string;
|
||||
options?: AptabaseOptions;
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
export function AptabaseProvider({ appKey, options, children }: Props) {
|
||||
useEffect(() => {
|
||||
init(appKey, options);
|
||||
}, [appKey, options]);
|
||||
|
||||
return <AptabaseContext.Provider value={{ appKey, ...options }}>{children}</AptabaseContext.Provider>;
|
||||
}
|
||||
|
||||
export function useAptabase(): AptabaseClient {
|
||||
const ctx = useContext(AptabaseContext);
|
||||
if (!ctx) {
|
||||
throw new Error(
|
||||
'useAptabase must be used within AptabaseProvider. Did you forget to wrap your app in <AptabaseProvider>?',
|
||||
);
|
||||
}
|
||||
|
||||
return { trackEvent };
|
||||
}
|
15
packages/react/tsconfig.json
Normal file
15
packages/react/tsconfig.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es6",
|
||||
"strict": true,
|
||||
"moduleResolution": "node",
|
||||
"esModuleInterop": true,
|
||||
"jsx": "react-jsx",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"types": ["@types"]
|
||||
},
|
||||
"declaration": true,
|
||||
"declarationDir": "./dist"
|
||||
}
|
||||
}
|
10
packages/react/tsup.config.ts
Normal file
10
packages/react/tsup.config.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { defineConfig } from 'tsup';
|
||||
|
||||
export default defineConfig({
|
||||
entry: ['src/index.tsx'],
|
||||
format: ['cjs', 'esm'],
|
||||
dts: true,
|
||||
splitting: false,
|
||||
sourcemap: true,
|
||||
clean: true,
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue