@0xchain/header 1.1.0-beta.9 → 1.1.0-beta.91

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.
@@ -1,24 +1,22 @@
1
- const E = {
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 i(e, n) {
7
- const t = new CustomEvent(e, { detail: n });
8
- window.dispatchEvent(t);
6
+ function emitUserEvent(eventName, data) {
7
+ const event = new CustomEvent(eventName, { detail: data });
8
+ window.dispatchEvent(event);
9
9
  }
10
- function r(e, n) {
11
- const t = (o) => {
12
- n(o.detail);
10
+ function onUserEvent(eventName, callback) {
11
+ const handler = (event) => {
12
+ const customEvent = event;
13
+ callback(customEvent.detail);
13
14
  };
14
- return window.addEventListener(e, t), () => window.removeEventListener(e, t);
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
- E as USER_EVENTS,
21
- d as cleanUserEvent,
22
- i as emitUserEvent,
23
- r as onUserEvent
19
+ USER_EVENTS,
20
+ emitUserEvent,
21
+ onUserEvent
24
22
  };
@@ -0,0 +1,52 @@
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 hasTrustedHeaderPoints(userInfo) {
18
+ if (typeof userInfo?.displayBalance === "string" && userInfo.displayBalance.trim() && userInfo.displayBalance !== "0") {
19
+ return true;
20
+ }
21
+ const points = Number(userInfo?.points ?? userInfo?.balance);
22
+ return Number.isFinite(points) && points > 0;
23
+ }
24
+ function pointsTextFromOverview(res) {
25
+ if (!res || typeof res !== "object") {
26
+ return null;
27
+ }
28
+ const root = res;
29
+ const statusCode = Number(root.status?.code);
30
+ if (Number.isFinite(statusCode) && statusCode !== 0 && statusCode !== 200) {
31
+ return null;
32
+ }
33
+ const payload = root.data && typeof root.data === "object" ? root.data : root;
34
+ if (!payload || typeof payload !== "object") {
35
+ return null;
36
+ }
37
+ const data = payload;
38
+ if (typeof data.balance === "undefined" && typeof data.displayBalance !== "string") {
39
+ return null;
40
+ }
41
+ return formatHeaderPoints({
42
+ displayBalance: data.displayBalance,
43
+ points: data.balance
44
+ });
45
+ }
46
+ export {
47
+ HEADER_POINTS_ICON,
48
+ HEADER_POINTS_OVERVIEW_PATH,
49
+ formatHeaderPoints,
50
+ hasTrustedHeaderPoints,
51
+ pointsTextFromOverview
52
+ };
@@ -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.9",
3
+ "version": "1.1.0-beta.91",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -17,34 +17,50 @@
17
17
  "dist",
18
18
  "!**/*.tsbuildinfo"
19
19
  ],
20
- "devDependencies": {
21
- "rollup-plugin-preserve-use-client": "3.0.1",
22
- "@types/js-cookie": "3.0.6"
20
+ "peerDependencies": {
21
+ "antd": "6.1.1",
22
+ "lucide-react": "0.539.0",
23
+ "next": "16.1.6",
24
+ "next-intl": "4.13.0",
25
+ "rc-drawer": "7.3.0",
26
+ "react": "19.1.1",
27
+ "react-dom": "19.1.1",
28
+ "tailwind-merge": "3.3.1",
29
+ "@0xchain/with-login": "1.1.0-beta.91"
23
30
  },
24
- "dependencies": {
31
+ "devDependencies": {
32
+ "@0xchain/next-themes": "1.0.0",
33
+ "@types/js-cookie": "3.0.6",
34
+ "antd": "6.1.1",
25
35
  "js-cookie": "3.0.5",
26
36
  "lucide-react": "0.539.0",
27
- "next-intl": "4.8.2",
28
- "@0xchain/next-themes": "1.0.0",
37
+ "next": "16.1.6",
38
+ "next-intl": "4.13.0",
29
39
  "rc-drawer": "7.3.0",
30
40
  "react": "19.1.1",
31
41
  "react-dom": "19.1.1",
42
+ "rollup-plugin-preserve-use-client": "3.0.1",
32
43
  "tailwind-merge": "3.3.1",
33
- "next": "16.1.6",
34
- "antd": "6.1.1",
35
- "@0xchain/auth": "1.1.0-beta.9",
36
- "@0xchain/better-link": "1.1.0-beta.9",
37
- "@0xchain/image": "1.1.0-beta.9",
38
- "@0xchain/translation": "1.1.0-beta.9",
39
- "@0xchain/link": "1.1.0-beta.9",
40
- "@0xchain/avatar": "1.1.0-beta.9",
41
- "@0xchain/ui": "1.1.0-beta.9",
42
- "@0xchain/theme-toggle": "1.1.0-beta.9",
43
- "@0xchain/iconfont": "1.1.0-beta.9",
44
- "@0xchain/with-login": "1.1.0-beta.9",
45
- "@0xchain/token": "1.1.0-beta.9",
46
- "@0xchain/request": "1.1.0-beta.9",
47
- "@0xchain/tooltip": "1.1.0-beta.9"
44
+ "@0xchain/with-login": "1.1.0-beta.91"
45
+ },
46
+ "dependencies": {
47
+ "@0xchain/auth": "1.1.0-beta.91",
48
+ "@0xchain/iconfont": "1.1.0-beta.91",
49
+ "@0xchain/image": "1.1.0-beta.91",
50
+ "@0xchain/link": "1.1.0-beta.91",
51
+ "@0xchain/request": "1.1.0-beta.91",
52
+ "@0xchain/avatar": "1.1.0-beta.91",
53
+ "@0xchain/theme-toggle": "1.1.0-beta.91",
54
+ "@0xchain/i18n": "1.1.0-beta.91",
55
+ "@0xchain/tooltip": "1.1.0-beta.91",
56
+ "@0xchain/better-link": "1.1.0-beta.91",
57
+ "@0xchain/translation": "1.1.0-beta.91",
58
+ "@0xchain/ui": "1.1.0-beta.91"
59
+ },
60
+ "nx": {
61
+ "tags": [
62
+ "type:feature"
63
+ ]
48
64
  },
49
65
  "publishConfig": {
50
66
  "access": "public"