@0xchain/header 1.1.0-beta.10 → 1.1.0-beta.100
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/dist/index.js +43 -45
- package/dist/lib/Drawer.js +29 -25
- package/dist/lib/Explorer.js +61 -50
- package/dist/lib/Logo.js +61 -46
- package/dist/lib/Nav.js +178 -132
- package/dist/lib/PointsEntry.js +121 -0
- package/dist/lib/Products.js +102 -0
- package/dist/lib/Right.js +24 -16
- package/dist/lib/Setting/SettingBody.js +44 -36
- package/dist/lib/Setting/index.js +36 -32
- package/dist/lib/User/Logout.js +13 -13
- package/dist/lib/User/MainTrigger.js +33 -23
- package/dist/lib/User/Mobile.js +22 -22
- package/dist/lib/User/Pc.js +24 -22
- package/dist/lib/User/SignIn.js +16 -15
- package/dist/lib/User/index.js +27 -20
- package/dist/lib/authProfile.js +34 -0
- package/dist/lib/context.js +15 -15
- package/dist/lib/events.js +13 -15
- package/dist/lib/headerPoints.js +102 -0
- package/dist/lib/headerSource.js +13 -0
- package/dist/lib/loadHeaderBarUser.js +42 -0
- package/dist/lib/useHeaderUser.js +44 -0
- package/package.json +41 -24
package/dist/lib/events.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
const
|
|
1
|
+
const USER_EVENTS = {
|
|
2
2
|
PROFILE_UPDATED: "user-profile-updated",
|
|
3
3
|
LOGGED_IN: "user-logged-in",
|
|
4
4
|
LOGGED_OUT: "user-logged-out"
|
|
5
5
|
};
|
|
6
|
-
function
|
|
7
|
-
const
|
|
8
|
-
window.dispatchEvent(
|
|
6
|
+
function emitUserEvent(eventName, data) {
|
|
7
|
+
const event = new CustomEvent(eventName, { detail: data });
|
|
8
|
+
window.dispatchEvent(event);
|
|
9
9
|
}
|
|
10
|
-
function
|
|
11
|
-
const
|
|
12
|
-
|
|
10
|
+
function onUserEvent(eventName, callback) {
|
|
11
|
+
const handler = (event) => {
|
|
12
|
+
const customEvent = event;
|
|
13
|
+
callback(customEvent.detail);
|
|
13
14
|
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
function d(e, n) {
|
|
17
|
-
window.removeEventListener(e, n);
|
|
15
|
+
window.addEventListener(eventName, handler);
|
|
16
|
+
return () => window.removeEventListener(eventName, handler);
|
|
18
17
|
}
|
|
19
18
|
export {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
r as onUserEvent
|
|
19
|
+
USER_EVENTS,
|
|
20
|
+
emitUserEvent,
|
|
21
|
+
onUserEvent
|
|
24
22
|
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const HEADER_POINTS_OVERVIEW_PATH = "/api/account/points/overview";
|
|
2
|
+
const HEADER_POINTS_ICON = `${process.env.NEXT_PUBLIC_CDN_URL || ""}/uploads/2026/08/31/6a952e3b78f30.png`;
|
|
3
|
+
function formatHeaderPoints(userInfo) {
|
|
4
|
+
const display = userInfo?.displayBalance;
|
|
5
|
+
if (typeof display === "string" && display.trim()) {
|
|
6
|
+
return display.trim();
|
|
7
|
+
}
|
|
8
|
+
const points = Number(userInfo?.points ?? userInfo?.balance);
|
|
9
|
+
if (!Number.isFinite(points) || points < 0) {
|
|
10
|
+
return "0";
|
|
11
|
+
}
|
|
12
|
+
if (points > 99999) {
|
|
13
|
+
return "99999";
|
|
14
|
+
}
|
|
15
|
+
return String(Math.trunc(points));
|
|
16
|
+
}
|
|
17
|
+
function headerPointsShowAmount(userInfo) {
|
|
18
|
+
return Boolean(userInfo?.id || userInfo?.userId);
|
|
19
|
+
}
|
|
20
|
+
function hasTrustedHeaderPoints(userInfo) {
|
|
21
|
+
if (userInfo?.headerPointsSeeded === true) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (typeof userInfo?.displayBalance === "string" && userInfo.displayBalance.trim() && userInfo.displayBalance !== "0") {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
const points = Number(userInfo?.points ?? userInfo?.balance);
|
|
28
|
+
return Number.isFinite(points) && points > 0;
|
|
29
|
+
}
|
|
30
|
+
function overviewPayload(res) {
|
|
31
|
+
if (!res || typeof res !== "object") return null;
|
|
32
|
+
const root = res;
|
|
33
|
+
const statusCode = Number(root.status?.code);
|
|
34
|
+
if (Number.isFinite(statusCode) && statusCode !== 0 && statusCode !== 200) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const payload = root.data && typeof root.data === "object" ? root.data : root;
|
|
38
|
+
if (!payload || typeof payload !== "object") return null;
|
|
39
|
+
return payload;
|
|
40
|
+
}
|
|
41
|
+
function mergePointsOverviewIntoUser(user, overviewRes) {
|
|
42
|
+
const text = pointsTextFromOverview(overviewRes);
|
|
43
|
+
if (text == null) return user;
|
|
44
|
+
const data = overviewPayload(overviewRes);
|
|
45
|
+
return {
|
|
46
|
+
...user,
|
|
47
|
+
points: data?.balance ?? user.points,
|
|
48
|
+
balance: data?.balance ?? user.balance,
|
|
49
|
+
displayBalance: typeof data?.displayBalance === "string" ? data.displayBalance : text,
|
|
50
|
+
headerPointsSeeded: true
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async function fetchHeaderPointsOverview(options) {
|
|
54
|
+
const base = options.accountBaseUrl.replace(/\/$/, "");
|
|
55
|
+
if (!base || !options.accessToken.trim()) return null;
|
|
56
|
+
const fetcher = options.fetcher ?? fetch;
|
|
57
|
+
try {
|
|
58
|
+
const response = await fetcher(`${base}/api/account/points/overview`, {
|
|
59
|
+
method: "GET",
|
|
60
|
+
cache: "no-store",
|
|
61
|
+
headers: {
|
|
62
|
+
Authorization: `Bearer ${options.accessToken}`
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
if (!response.ok) return null;
|
|
66
|
+
return await response.json();
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function pointsTextFromOverview(res) {
|
|
72
|
+
if (!res || typeof res !== "object") {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const root = res;
|
|
76
|
+
const statusCode = Number(root.status?.code);
|
|
77
|
+
if (Number.isFinite(statusCode) && statusCode !== 0 && statusCode !== 200) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
const payload = root.data && typeof root.data === "object" ? root.data : root;
|
|
81
|
+
if (!payload || typeof payload !== "object") {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const data = payload;
|
|
85
|
+
if (typeof data.balance === "undefined" && typeof data.displayBalance !== "string") {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return formatHeaderPoints({
|
|
89
|
+
displayBalance: data.displayBalance,
|
|
90
|
+
points: data.balance
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
HEADER_POINTS_ICON,
|
|
95
|
+
HEADER_POINTS_OVERVIEW_PATH,
|
|
96
|
+
fetchHeaderPointsOverview,
|
|
97
|
+
formatHeaderPoints,
|
|
98
|
+
hasTrustedHeaderPoints,
|
|
99
|
+
headerPointsShowAmount,
|
|
100
|
+
mergePointsOverviewIntoUser,
|
|
101
|
+
pointsTextFromOverview
|
|
102
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var HeaderSource = /* @__PURE__ */ ((HeaderSource2) => {
|
|
2
|
+
HeaderSource2["EXPLORER"] = "explorer";
|
|
3
|
+
HeaderSource2["CHAT"] = "chat";
|
|
4
|
+
HeaderSource2["DEFAULT"] = "default";
|
|
5
|
+
return HeaderSource2;
|
|
6
|
+
})(HeaderSource || {});
|
|
7
|
+
function isChatHeaderSource(source) {
|
|
8
|
+
return source === "chat";
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
HeaderSource,
|
|
12
|
+
isChatHeaderSource
|
|
13
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import request from "@0xchain/request";
|
|
2
|
+
import { envCookieName } from "@0xchain/token";
|
|
3
|
+
import { fetchHeaderPointsOverview, mergePointsOverviewIntoUser } from "./headerPoints.js";
|
|
4
|
+
function resolveAccountApiBaseUrl() {
|
|
5
|
+
return (process.env.AUTH_ACCOUNT_INNER_URL || process.env.NEXT_PUBLIC_AUTH_URL || "").replace(/\/$/, "");
|
|
6
|
+
}
|
|
7
|
+
function readAccessToken(cookieStore) {
|
|
8
|
+
const keys = [
|
|
9
|
+
envCookieName("access_token"),
|
|
10
|
+
envCookieName("id_token")
|
|
11
|
+
];
|
|
12
|
+
for (const key of keys) {
|
|
13
|
+
const value = cookieStore.get(key)?.value;
|
|
14
|
+
if (value) return value;
|
|
15
|
+
}
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
async function loadHeaderBarUserData(cookieStore) {
|
|
19
|
+
const accessToken = readAccessToken(cookieStore);
|
|
20
|
+
const accountBaseUrl = resolveAccountApiBaseUrl();
|
|
21
|
+
const [profileRes, overviewRes] = await Promise.all([
|
|
22
|
+
request.get("/v2/users/profile", {
|
|
23
|
+
baseURL: process.env.AUTH_INNER_API_URL
|
|
24
|
+
}).catch((error) => {
|
|
25
|
+
console.error(error);
|
|
26
|
+
return null;
|
|
27
|
+
}),
|
|
28
|
+
fetchHeaderPointsOverview({
|
|
29
|
+
accountBaseUrl,
|
|
30
|
+
accessToken
|
|
31
|
+
})
|
|
32
|
+
]);
|
|
33
|
+
let user = profileRes?.data || {};
|
|
34
|
+
if (overviewRes) {
|
|
35
|
+
user = mergePointsOverviewIntoUser(user, overviewRes);
|
|
36
|
+
}
|
|
37
|
+
return user;
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
loadHeaderBarUserData,
|
|
41
|
+
resolveAccountApiBaseUrl
|
|
42
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useState, useCallback, useEffect } from "react";
|
|
3
|
+
import { getClientSession, getRuntimeLoginState } from "@0xchain/auth";
|
|
4
|
+
import { hasProfileIdentity, fetchHeaderProfile } from "./authProfile.js";
|
|
5
|
+
import { onUserEvent, USER_EVENTS } from "./events.js";
|
|
6
|
+
import { useIsChaindigg } from "./context.js";
|
|
7
|
+
function useHeaderUser(user) {
|
|
8
|
+
const isChaindigg = useIsChaindigg();
|
|
9
|
+
const [userKey, setUserKey] = useState("");
|
|
10
|
+
const [userInfo, setUserInfo] = useState(user);
|
|
11
|
+
const hasUserProfile = hasProfileIdentity(userInfo);
|
|
12
|
+
const updateUserInfo = useCallback(async () => {
|
|
13
|
+
try {
|
|
14
|
+
const profile = await fetchHeaderProfile();
|
|
15
|
+
setUserInfo(profile || {});
|
|
16
|
+
setUserKey((/* @__PURE__ */ new Date()).getTime().toString());
|
|
17
|
+
return profile;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error(error);
|
|
20
|
+
setUserInfo({});
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}, []);
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
setUserInfo(user);
|
|
26
|
+
}, [user]);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (isChaindigg) return;
|
|
29
|
+
const cleanProfileUpdated = onUserEvent(USER_EVENTS.PROFILE_UPDATED, updateUserInfo);
|
|
30
|
+
const cleanLoggedIn = onUserEvent(USER_EVENTS.LOGGED_IN, updateUserInfo);
|
|
31
|
+
const cookieLoggedIn = getClientSession().isLoggedIn;
|
|
32
|
+
if (!hasUserProfile && (cookieLoggedIn || getRuntimeLoginState() === true)) {
|
|
33
|
+
updateUserInfo();
|
|
34
|
+
}
|
|
35
|
+
return () => {
|
|
36
|
+
cleanProfileUpdated();
|
|
37
|
+
cleanLoggedIn();
|
|
38
|
+
};
|
|
39
|
+
}, [hasUserProfile, isChaindigg, updateUserInfo]);
|
|
40
|
+
return { userInfo, hasUserProfile, userKey };
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
useHeaderUser
|
|
44
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0xchain/header",
|
|
3
|
-
"version": "1.1.0-beta.
|
|
3
|
+
"version": "1.1.0-beta.100",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -17,36 +17,53 @@
|
|
|
17
17
|
"dist",
|
|
18
18
|
"!**/*.tsbuildinfo"
|
|
19
19
|
],
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@0xchain/with-login": "1.1.0-beta.74",
|
|
22
|
+
"antd": "6.1.1",
|
|
23
|
+
"lucide-react": "0.539.0",
|
|
24
|
+
"next": "16.1.6",
|
|
25
|
+
"next-intl": "4.13.0",
|
|
26
|
+
"rc-drawer": "7.3.0",
|
|
27
|
+
"react": "19.1.1",
|
|
28
|
+
"react-dom": "19.1.1",
|
|
29
|
+
"tailwind-merge": "3.3.1"
|
|
23
30
|
},
|
|
24
|
-
"
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@0xchain/next-themes": "1.0.0",
|
|
33
|
+
"@0xchain/with-login": "1.1.0-beta.74",
|
|
34
|
+
"@types/js-cookie": "3.0.6",
|
|
35
|
+
"antd": "6.1.1",
|
|
25
36
|
"js-cookie": "3.0.5",
|
|
26
37
|
"lucide-react": "0.539.0",
|
|
27
|
-
"next
|
|
28
|
-
"
|
|
38
|
+
"next": "16.1.6",
|
|
39
|
+
"next-intl": "4.13.0",
|
|
29
40
|
"rc-drawer": "7.3.0",
|
|
30
41
|
"react": "19.1.1",
|
|
31
42
|
"react-dom": "19.1.1",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"@0xchain/
|
|
37
|
-
"@0xchain/
|
|
38
|
-
"@0xchain/
|
|
39
|
-
"@0xchain/link": "1.1.0-beta.
|
|
40
|
-
"@0xchain/
|
|
41
|
-
"@0xchain/
|
|
42
|
-
"@0xchain/
|
|
43
|
-
"@0xchain/
|
|
44
|
-
"@0xchain/
|
|
45
|
-
"@0xchain/
|
|
46
|
-
"@0xchain/
|
|
47
|
-
"@0xchain/
|
|
43
|
+
"rollup-plugin-preserve-use-client": "3.0.1",
|
|
44
|
+
"tailwind-merge": "3.3.1"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@0xchain/auth": "1.1.0-beta.74",
|
|
48
|
+
"@0xchain/token": "1.1.0-beta.74",
|
|
49
|
+
"@0xchain/avatar": "1.1.0-beta.74",
|
|
50
|
+
"@0xchain/better-link": "1.1.0-beta.74",
|
|
51
|
+
"@0xchain/i18n": "1.1.0-beta.74",
|
|
52
|
+
"@0xchain/iconfont": "1.1.0-beta.74",
|
|
53
|
+
"@0xchain/image": "1.1.0-beta.74",
|
|
54
|
+
"@0xchain/link": "1.1.0-beta.74",
|
|
55
|
+
"@0xchain/request": "1.1.0-beta.74",
|
|
56
|
+
"@0xchain/theme-toggle": "1.1.0-beta.74",
|
|
57
|
+
"@0xchain/tooltip": "1.1.0-beta.74",
|
|
58
|
+
"@0xchain/translation": "1.1.0-beta.74",
|
|
59
|
+
"@0xchain/ui": "1.1.0-beta.74"
|
|
60
|
+
},
|
|
61
|
+
"nx": {
|
|
62
|
+
"tags": [
|
|
63
|
+
"type:feature"
|
|
64
|
+
]
|
|
48
65
|
},
|
|
49
66
|
"publishConfig": {
|
|
50
67
|
"access": "public"
|
|
51
68
|
}
|
|
52
|
-
}
|
|
69
|
+
}
|