@adland/react 0.7.1 → 0.8.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/CHANGELOG.md +13 -0
- package/package.json +7 -2
- package/src/components/Ad.tsx +28 -44
- package/src/index.ts +1 -1
- package/src/types.ts +31 -0
- package/src/utils/constants.ts +5 -0
- package/src/utils/index.ts +22 -0
- package/tsup.config.ts +4 -1
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adland/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -15,6 +15,11 @@
|
|
|
15
15
|
"import": "./dist/index.js",
|
|
16
16
|
"require": "./dist/index.cjs"
|
|
17
17
|
},
|
|
18
|
+
"./types": {
|
|
19
|
+
"types": "./dist/types.d.ts",
|
|
20
|
+
"import": "./dist/types.js",
|
|
21
|
+
"require": "./dist/types.cjs"
|
|
22
|
+
},
|
|
18
23
|
"./package.json": "./package.json"
|
|
19
24
|
},
|
|
20
25
|
"peerDependencies": {
|
|
@@ -28,7 +33,7 @@
|
|
|
28
33
|
"@types/react-dom": "^18.3.1",
|
|
29
34
|
"lucide-react": "0.561.0",
|
|
30
35
|
"tsup": "^8.0.1",
|
|
31
|
-
"@adland/data": "0.
|
|
36
|
+
"@adland/data": "0.11.0"
|
|
32
37
|
},
|
|
33
38
|
"devDependencies": {
|
|
34
39
|
"@typescript-eslint/eslint-plugin": "^7.13.1",
|
package/src/components/Ad.tsx
CHANGED
|
@@ -8,48 +8,9 @@ import BasicAdBody from "./BasicAdBody";
|
|
|
8
8
|
import LinkAdContent from "./content/LinkAdContent";
|
|
9
9
|
import MiniappAdContent from "./content/MiniappAdContent";
|
|
10
10
|
import CastAdContent from "./content/CastAdContent";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Get the base URL for the network
|
|
16
|
-
* Uses relative URL in browser (for local dev), otherwise defaults to production
|
|
17
|
-
*/
|
|
18
|
-
const getBaseUrl = (network: Network): string => {
|
|
19
|
-
if (typeof window !== "undefined") {
|
|
20
|
-
// In browser - use relative URL for local development
|
|
21
|
-
return "";
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (network === "testnet") {
|
|
25
|
-
return "https://testnet.adland.space";
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (network === "mainnet") {
|
|
29
|
-
return "https://app.adland.space";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return "";
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export interface AdProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
36
|
-
/**
|
|
37
|
-
* The owner/creator of the ad
|
|
38
|
-
*/
|
|
39
|
-
owner: string;
|
|
40
|
-
/**
|
|
41
|
-
* Unique identifier for the ad
|
|
42
|
-
*/
|
|
43
|
-
slotId: string;
|
|
44
|
-
/**
|
|
45
|
-
* Network to use for tracking requests (currently only "testnet" is supported)
|
|
46
|
-
*/
|
|
47
|
-
network?: Network;
|
|
48
|
-
/**
|
|
49
|
-
* Optional base URL override. If not provided, uses relative URL in browser or production URL in SSR
|
|
50
|
-
*/
|
|
51
|
-
baseUrl?: string;
|
|
52
|
-
}
|
|
11
|
+
import { AdDataQueryError, AdProps } from "../types";
|
|
12
|
+
import { getBaseUrl } from "../utils";
|
|
13
|
+
import { adlandApiUrl } from "../utils/constants";
|
|
53
14
|
|
|
54
15
|
/**
|
|
55
16
|
* Simple Ad component with built-in view and click tracking
|
|
@@ -78,7 +39,7 @@ export function Ad({
|
|
|
78
39
|
`ad-data-${owner}-${slotId}`,
|
|
79
40
|
async () => {
|
|
80
41
|
const res = await fetch(
|
|
81
|
-
`${
|
|
42
|
+
`${adlandApiUrl}/api/data/owner/${owner.toLowerCase()}/slot/${slotId}`,
|
|
82
43
|
{
|
|
83
44
|
method: "GET",
|
|
84
45
|
headers: {
|
|
@@ -86,7 +47,18 @@ export function Ad({
|
|
|
86
47
|
},
|
|
87
48
|
},
|
|
88
49
|
);
|
|
89
|
-
|
|
50
|
+
|
|
51
|
+
if (!res.ok) {
|
|
52
|
+
throw new Error(AdDataQueryError.NO_AD);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const data = await res.json();
|
|
56
|
+
|
|
57
|
+
if (data.error) {
|
|
58
|
+
throw new Error(data.error);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return data;
|
|
90
62
|
},
|
|
91
63
|
{
|
|
92
64
|
enabled: !!owner && !!slotId,
|
|
@@ -94,6 +66,8 @@ export function Ad({
|
|
|
94
66
|
);
|
|
95
67
|
const networkBaseUrl = baseUrl ?? getBaseUrl(network);
|
|
96
68
|
|
|
69
|
+
console.log({ error, adData });
|
|
70
|
+
|
|
97
71
|
const send = useCallback(
|
|
98
72
|
(type: "view" | "click") => {
|
|
99
73
|
const trackEndpoint = `${networkBaseUrl}/api/analytics/track`;
|
|
@@ -165,6 +139,16 @@ export function Ad({
|
|
|
165
139
|
return <LoadingAdContent />;
|
|
166
140
|
}
|
|
167
141
|
if (error) {
|
|
142
|
+
// @ts-ignore
|
|
143
|
+
if (error instanceof Error) {
|
|
144
|
+
if (error.message === AdDataQueryError.NO_AD) {
|
|
145
|
+
return (
|
|
146
|
+
<EmtpyAdContent
|
|
147
|
+
data={{ url: networkBaseUrl + `${owner}/${slotId}` }}
|
|
148
|
+
/>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
168
152
|
return <ErrorAdContent error={error} />;
|
|
169
153
|
}
|
|
170
154
|
if (adData) {
|
package/src/index.ts
CHANGED
package/src/types.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AdData } from "@adland/data";
|
|
2
|
+
|
|
3
|
+
export type Network = "testnet" | "mainnet";
|
|
4
|
+
|
|
5
|
+
export interface AdProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
6
|
+
/**
|
|
7
|
+
* The owner/creator of the ad
|
|
8
|
+
*/
|
|
9
|
+
owner: string;
|
|
10
|
+
/**
|
|
11
|
+
* Unique identifier for the ad
|
|
12
|
+
*/
|
|
13
|
+
slotId: string;
|
|
14
|
+
/**
|
|
15
|
+
* Network to use for tracking requests (currently only "testnet" is supported)
|
|
16
|
+
*/
|
|
17
|
+
network?: Network;
|
|
18
|
+
/**
|
|
19
|
+
* Optional base URL override. If not provided, uses relative URL in browser or production URL in SSR
|
|
20
|
+
*/
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export enum AdDataQueryError {
|
|
25
|
+
NO_AD = "NO_AD",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface AdDataQueryResponse {
|
|
29
|
+
error?: AdDataQueryError;
|
|
30
|
+
data?: AdData;
|
|
31
|
+
}
|
package/src/utils/constants.ts
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Network } from "../types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get the base URL for the network
|
|
5
|
+
* Uses relative URL in browser (for local dev), otherwise defaults to production
|
|
6
|
+
*/
|
|
7
|
+
export const getBaseUrl = (network: Network): string => {
|
|
8
|
+
if (typeof window !== "undefined") {
|
|
9
|
+
// In browser - use relative URL for local development
|
|
10
|
+
return "";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (network === "testnet") {
|
|
14
|
+
return "https://testnet.adland.space";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (network === "mainnet") {
|
|
18
|
+
return "https://app.adland.space";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return "";
|
|
22
|
+
};
|