@adland/react 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js ADDED
@@ -0,0 +1,34 @@
1
+ const { resolve } = require("node:path");
2
+
3
+ const project = resolve(process.cwd(), "tsconfig.json");
4
+
5
+ /** @type {import("eslint").Linter.Config} */
6
+ module.exports = {
7
+ extends: ["eslint:recommended", "turbo"],
8
+ plugins: ["only-warn"],
9
+ globals: {
10
+ React: true,
11
+ JSX: true,
12
+ },
13
+ env: {
14
+ node: true,
15
+ },
16
+ settings: {
17
+ "import/resolver": {
18
+ typescript: {
19
+ project,
20
+ },
21
+ },
22
+ },
23
+ ignorePatterns: [
24
+ // Ignore dotfiles
25
+ ".*.js",
26
+ "node_modules/",
27
+ "dist/",
28
+ ],
29
+ overrides: [
30
+ {
31
+ files: ["*.js?(x)", "*.ts?(x)"],
32
+ },
33
+ ],
34
+ };
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 dan5py (git@dan5py.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @adland/react
2
+
3
+ A simple React component library for tracking ad views and clicks with built-in support for Farcaster MiniApp SDK & AdLand ads.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @adland/react
9
+ # or
10
+ pnpm add @adland/react
11
+ # or
12
+ yarn add @adland/react
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import { Ad } from "@adland/react";
19
+
20
+ function App() {
21
+ return (
22
+ <Ad owner="0x123..." adId="ad-1">
23
+ <img src="ad.jpg" alt="Advertisement" />
24
+ </Ad>
25
+ );
26
+ }
27
+ ```
28
+
29
+ ## Features
30
+
31
+ - 🎯 **Simple API**: Minimal configuration required
32
+ - 📊 **View & Click Tracking**: Automatic view tracking with IntersectionObserver and click tracking
33
+ - 🔐 **Farcaster SDK Integration**: Built-in support for Farcaster MiniApp SDK's `quickAuth` for authenticated requests
34
+ - 🛡️ **Session-based Deduplication**: Prevents duplicate view tracking within the same browser session
35
+ - 📦 **TypeScript Support**: Full TypeScript definitions included
36
+
37
+ ## API
38
+
39
+ ### `<Ad />` Component
40
+
41
+ | Prop | Type | Default | Description |
42
+ |------|------|---------|-------------|
43
+ | `owner` | `string` | **required** | The owner/creator address of the ad |
44
+ | `adId` | `string` | **required** | Unique identifier for the ad |
45
+ | `children` | `ReactNode` | **required** | The content to display in the ad |
46
+ | `trackEndpoint` | `string` | `"/api/analytics/track"` | Custom endpoint for tracking requests |
47
+
48
+ ## License
49
+
50
+ MIT
51
+
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@adland/react",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "type": "module",
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "peerDependencies": {
21
+ "react": "^18.0.0 || ^19.0.0",
22
+ "react-dom": "^18.0.0 || ^19.0.0"
23
+ },
24
+ "dependencies": {
25
+ "@farcaster/miniapp-sdk": "0.2.1"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^20",
29
+ "@types/react": "^19.1.16",
30
+ "@types/react-dom": "^18.3.1",
31
+ "@typescript-eslint/eslint-plugin": "^7.13.1",
32
+ "@typescript-eslint/parser": "^7.13.1",
33
+ "@vercel/style-guide": "^6.0.0",
34
+ "eslint": "^8",
35
+ "eslint-config-turbo": "2.0.6",
36
+ "eslint-plugin-only-warn": "^1.1.0",
37
+ "tsup": "^8.0.1",
38
+ "typescript": "^5.4.5"
39
+ },
40
+ "scripts": {
41
+ "build": "tsup",
42
+ "dev": "tsup --watch",
43
+ "test:dev": "pnpm --filter @adland/react-dev dev",
44
+ "dev:app": "cd dev && pnpm dev",
45
+ "lint": "eslint ."
46
+ }
47
+ }
@@ -0,0 +1,96 @@
1
+ import { useEffect, useRef, useCallback } from "react";
2
+ import { sendTrackRequest } from "../utils/sdk";
3
+
4
+ export interface AdProps extends React.HTMLAttributes<HTMLDivElement> {
5
+ /**
6
+ * The owner/creator of the ad
7
+ */
8
+ owner: string;
9
+ /**
10
+ * Unique identifier for the ad
11
+ */
12
+ adId: string;
13
+ /**
14
+ * The content to display in the ad
15
+ */
16
+ children: React.ReactNode;
17
+ /**
18
+ * Custom endpoint for tracking requests (default: "/api/analytics/track")
19
+ */
20
+ trackEndpoint?: string;
21
+ }
22
+
23
+ /**
24
+ * Simple Ad component with built-in view and click tracking
25
+ * Uses Farcaster MiniApp SDK for authenticated tracking requests
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * <Ad owner="0x123..." adId="ad-1">
30
+ * <img src="ad-image.jpg" alt="Advertisement" />
31
+ * </Ad>
32
+ * ```
33
+ */
34
+ export function Ad({
35
+ owner,
36
+ adId,
37
+ children,
38
+ trackEndpoint = "/api/analytics/track",
39
+ ...props
40
+ }: AdProps) {
41
+ const ref = useRef<HTMLDivElement>(null);
42
+
43
+ const send = useCallback(
44
+ (type: "view" | "click") => {
45
+ sendTrackRequest(trackEndpoint, {
46
+ type,
47
+ adId,
48
+ adOwner: owner,
49
+ }).catch((error: unknown) => {
50
+ console.error(`[@adland/react] Failed to track ${type}:`, error);
51
+ });
52
+ },
53
+ [adId, owner, trackEndpoint],
54
+ );
55
+
56
+ useEffect(() => {
57
+ const el = ref.current;
58
+ if (!el) return;
59
+
60
+ const key = `ad_view_${adId}`;
61
+
62
+ const obs = new IntersectionObserver(
63
+ (entries) => {
64
+ const entry = entries[0];
65
+ if (!entry?.isIntersecting) return;
66
+
67
+ const already = sessionStorage.getItem(key);
68
+ if (already) {
69
+ obs.unobserve(el);
70
+ return;
71
+ }
72
+
73
+ sessionStorage.setItem(key, "1");
74
+ send("view");
75
+ obs.unobserve(el);
76
+ },
77
+ {
78
+ threshold: 0.15, // more forgiving
79
+ },
80
+ );
81
+
82
+ obs.observe(el);
83
+ return () => obs.disconnect();
84
+ }, [adId, send]);
85
+
86
+ // CLICK tracking
87
+ const onClick = useCallback(() => {
88
+ send("click");
89
+ }, [send]);
90
+
91
+ return (
92
+ <div ref={ref} onClick={onClick} {...props}>
93
+ {children}
94
+ </div>
95
+ );
96
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ // Main exports
2
+ export { Ad } from "./components/Ad";
3
+ export type { AdProps } from "./components/Ad";
4
+
5
+ // Utility exports
6
+ export {
7
+ isSdkReady,
8
+ sendTrackRequest,
9
+ checkSdkActionsReady,
10
+ } from "./utils/sdk";
@@ -0,0 +1,101 @@
1
+ import { sdk } from "@farcaster/miniapp-sdk";
2
+
3
+ /**
4
+ * Checks if the SDK is ready and initialized
5
+ */
6
+ export function isSdkReady(): boolean {
7
+ try {
8
+ // Check if SDK exists
9
+ if (!sdk) {
10
+ console.error("[@adland/react] SDK is not available");
11
+ return false;
12
+ }
13
+
14
+ // Check if quickAuth is available
15
+ if (!sdk.quickAuth) {
16
+ console.error(
17
+ "[@adland/react] SDK quickAuth is not available. Make sure the SDK is properly initialized.",
18
+ );
19
+ return false;
20
+ }
21
+
22
+ return true;
23
+ } catch (error) {
24
+ console.error("[@adland/react] Error checking SDK readiness:", error);
25
+ return false;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Sends a tracking request using the SDK's quickAuth
31
+ */
32
+ export async function sendTrackRequest(
33
+ url: string,
34
+ data: {
35
+ type: "view" | "click";
36
+ adOwner: string;
37
+ adId: string;
38
+ },
39
+ ): Promise<Response> {
40
+ if (!isSdkReady()) {
41
+ console.error(
42
+ "[@adland/react] SDK is not ready. Cannot send tracking request.",
43
+ );
44
+ return Promise.reject(
45
+ new Error(
46
+ "[@adland/react] SDK is not ready. Cannot send tracking request.",
47
+ ),
48
+ );
49
+ }
50
+
51
+ try {
52
+ if (!sdk.quickAuth) {
53
+ throw new Error("[@adland/react] SDK quickAuth is not available");
54
+ }
55
+
56
+ return await sdk.quickAuth.fetch(url, {
57
+ method: "POST",
58
+ headers: {
59
+ "Content-Type": "application/json",
60
+ "x-auth-type": "farcaster",
61
+ },
62
+ body: JSON.stringify(data),
63
+ });
64
+ } catch (error) {
65
+ console.error("[@adland/react] Error sending track request:", error);
66
+ throw error;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Checks if SDK actions are ready
72
+ */
73
+ export async function checkSdkActionsReady(): Promise<boolean> {
74
+ try {
75
+ if (!sdk) {
76
+ console.error("[@adland/react] SDK is not available");
77
+ return false;
78
+ }
79
+
80
+ if (!sdk.actions) {
81
+ console.error("[@adland/react] SDK actions are not available");
82
+ return false;
83
+ }
84
+
85
+ // Check if ready() function exists
86
+ if (typeof sdk.actions.ready !== "function") {
87
+ console.error(
88
+ "[@adland/react] SDK actions.ready() is not available. Make sure the SDK is properly initialized.",
89
+ );
90
+ return false;
91
+ }
92
+
93
+ return true;
94
+ } catch (error) {
95
+ console.error(
96
+ "[@adland/react] Error checking SDK actions readiness:",
97
+ error,
98
+ );
99
+ return false;
100
+ }
101
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "declarationMap": true,
6
+ "esModuleInterop": true,
7
+ "incremental": false,
8
+ "isolatedModules": true,
9
+ "lib": ["es2022", "DOM", "DOM.Iterable"],
10
+ "module": "ESNext",
11
+ "moduleDetection": "force",
12
+ "moduleResolution": "bundler",
13
+ "noUncheckedIndexedAccess": true,
14
+ "resolveJsonModule": true,
15
+ "skipLibCheck": true,
16
+ "strict": true,
17
+ "target": "ES2022",
18
+ "jsx": "react-jsx",
19
+ "baseUrl": ".",
20
+ "paths": {
21
+ "@adland/react/*": ["./src/*"]
22
+ },
23
+ "outDir": "./dist"
24
+ },
25
+ "include": ["src"],
26
+ "exclude": ["node_modules", "dist", "dev"]
27
+ }
28
+
package/tsup.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["cjs", "esm"],
6
+ dts: true,
7
+ splitting: false,
8
+ sourcemap: true,
9
+ clean: true,
10
+ external: ["react", "react-dom", "@farcaster/miniapp-sdk"],
11
+ });