@blocklet/aigne-hub 0.2.14 → 0.2.16
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/lib/cjs/api/call/index.js +1 -0
- package/lib/cjs/api/user.js +32 -0
- package/lib/cjs/api/utils/util.js +39 -0
- package/lib/cjs/components/credit/alert.js +4 -3
- package/lib/cjs/components/index.js +3 -1
- package/lib/cjs/components/user-credit-card.js +97 -0
- package/lib/cjs/utils/util.js +24 -0
- package/lib/esm/api/call/index.js +1 -0
- package/lib/esm/api/user.js +26 -0
- package/lib/esm/api/utils/util.js +31 -0
- package/lib/esm/components/credit/alert.js +2 -1
- package/lib/esm/components/index.js +1 -0
- package/lib/esm/components/user-credit-card.js +92 -0
- package/lib/esm/utils/util.js +18 -0
- package/lib/types/api/call/index.d.ts +1 -0
- package/lib/types/api/user.d.ts +19 -0
- package/lib/types/api/utils/util.d.ts +2 -0
- package/lib/types/components/index.d.ts +1 -0
- package/lib/types/components/user-credit-card.d.ts +11 -0
- package/lib/types/utils/util.d.ts +1 -0
- package/package.json +14 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getUserInfo = getUserInfo;
|
|
7
|
+
const error_1 = require("@blocklet/error");
|
|
8
|
+
const axios_1 = __importDefault(require("axios"));
|
|
9
|
+
const ufo_1 = require("ufo");
|
|
10
|
+
const util_1 = require("./utils/util");
|
|
11
|
+
async function getUserInfo({ baseUrl, accessKey, }) {
|
|
12
|
+
let finalBaseUrl = (0, util_1.getPrefix)();
|
|
13
|
+
try {
|
|
14
|
+
if (baseUrl) {
|
|
15
|
+
const tmp = new URL(baseUrl);
|
|
16
|
+
if (tmp.origin !== window.location.origin) {
|
|
17
|
+
finalBaseUrl = await (0, util_1.getRemoteBaseUrl)(baseUrl);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
catch (err) {
|
|
22
|
+
console.warn('Failed to parse baseUrl:', err);
|
|
23
|
+
throw new Error(`Failed to parse baseUrl: ${(0, error_1.formatError)(err)}`);
|
|
24
|
+
}
|
|
25
|
+
return axios_1.default
|
|
26
|
+
.get((0, ufo_1.joinURL)(finalBaseUrl, '/api/user/info'), {
|
|
27
|
+
headers: {
|
|
28
|
+
Authorization: `Bearer ${accessKey}`,
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
.then((res) => res.data);
|
|
32
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getPrefix = exports.getRemoteBaseUrl = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const ufo_1 = require("ufo");
|
|
9
|
+
const AIGNE_HUB_DID = 'z8ia3xzq2tMq8CRHfaXj1BTYJyYnEcHbqP8cJ';
|
|
10
|
+
const getRemoteBaseUrl = async (url) => {
|
|
11
|
+
const tmp = new URL(url);
|
|
12
|
+
if (tmp.origin === window.location.origin) {
|
|
13
|
+
return (0, exports.getPrefix)();
|
|
14
|
+
}
|
|
15
|
+
const scriptUrl = (0, ufo_1.joinURL)(tmp.origin, '__blocklet__.js?type=json');
|
|
16
|
+
const blockletInfo = await axios_1.default.get(scriptUrl).then((res) => res.data);
|
|
17
|
+
const componentId = ((blockletInfo === null || blockletInfo === void 0 ? void 0 : blockletInfo.componentId) || '').split('/').pop();
|
|
18
|
+
if (componentId === AIGNE_HUB_DID) {
|
|
19
|
+
return (0, ufo_1.joinURL)(tmp.origin, blockletInfo.prefix || '/');
|
|
20
|
+
}
|
|
21
|
+
const component = ((blockletInfo === null || blockletInfo === void 0 ? void 0 : blockletInfo.componentMountPoints) || []).find((x) => (x === null || x === void 0 ? void 0 : x.did) === AIGNE_HUB_DID);
|
|
22
|
+
return component ? (0, ufo_1.joinURL)(tmp.origin, component.mountPoint) : url;
|
|
23
|
+
};
|
|
24
|
+
exports.getRemoteBaseUrl = getRemoteBaseUrl;
|
|
25
|
+
const getPrefix = () => {
|
|
26
|
+
var _a, _b, _c, _d;
|
|
27
|
+
const prefix = ((_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || '/';
|
|
28
|
+
const baseUrl = (_b = window.location) === null || _b === void 0 ? void 0 : _b.origin; // required when use payment feature cross origin
|
|
29
|
+
const componentId = (((_c = window.blocklet) === null || _c === void 0 ? void 0 : _c.componentId) || '').split('/').pop();
|
|
30
|
+
if (componentId === AIGNE_HUB_DID) {
|
|
31
|
+
return (0, ufo_1.joinURL)(baseUrl, prefix);
|
|
32
|
+
}
|
|
33
|
+
const component = (((_d = window.blocklet) === null || _d === void 0 ? void 0 : _d.componentMountPoints) || []).find((x) => (x === null || x === void 0 ? void 0 : x.did) === AIGNE_HUB_DID);
|
|
34
|
+
if (component) {
|
|
35
|
+
return (0, ufo_1.joinURL)(baseUrl, component.mountPoint);
|
|
36
|
+
}
|
|
37
|
+
return (0, ufo_1.joinURL)(baseUrl, prefix);
|
|
38
|
+
};
|
|
39
|
+
exports.getPrefix = getPrefix;
|
|
@@ -5,15 +5,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
7
|
const context_1 = require("@arcblock/ux/lib/Locale/context");
|
|
8
|
+
const error_1 = require("@blocklet/error");
|
|
8
9
|
const material_1 = require("@mui/material");
|
|
9
|
-
const
|
|
10
|
+
const error_2 = require("../../api/error");
|
|
10
11
|
const withLocaleProvider_1 = __importDefault(require("../../utils/withLocaleProvider"));
|
|
11
12
|
const button_1 = __importDefault(require("./button"));
|
|
12
13
|
function CreditErrorAlert({ error, ...props }) {
|
|
13
14
|
const { t } = (0, context_1.useLocaleContext)();
|
|
14
|
-
const isCreditError = (error === null || error === void 0 ? void 0 : error.type) ===
|
|
15
|
+
const isCreditError = (error === null || error === void 0 ? void 0 : error.type) === error_2.CreditErrorType.NOT_ENOUGH;
|
|
15
16
|
if (!isCreditError) {
|
|
16
|
-
return ((0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", ...props, children: (
|
|
17
|
+
return ((0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "error", ...props, children: (0, error_1.formatError)(error) || t('unknownError') }));
|
|
17
18
|
}
|
|
18
19
|
return ((0, jsx_runtime_1.jsxs)(material_1.Alert, { severity: "warning", ...props, sx: {
|
|
19
20
|
px: 1,
|
|
@@ -18,7 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
18
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
19
|
};
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.FormLabel = exports.Table = exports.Switch = exports.CreditErrorAlert = exports.CreditBalance = exports.CreditButton = exports.SubscribeErrorAlert = exports.SubscribeButton = exports.Conversation = exports.ImagePreview = exports.LoadingImage = void 0;
|
|
21
|
+
exports.FormLabel = exports.UserCreditCard = exports.Table = exports.Switch = exports.CreditErrorAlert = exports.CreditBalance = exports.CreditButton = exports.SubscribeErrorAlert = exports.SubscribeButton = exports.Conversation = exports.ImagePreview = exports.LoadingImage = void 0;
|
|
22
22
|
var loading_image_1 = require("./loading-image");
|
|
23
23
|
Object.defineProperty(exports, "LoadingImage", { enumerable: true, get: function () { return __importDefault(loading_image_1).default; } });
|
|
24
24
|
var image_preview_1 = require("./image-preview");
|
|
@@ -41,5 +41,7 @@ var switch_button_1 = require("./switch-button");
|
|
|
41
41
|
Object.defineProperty(exports, "Switch", { enumerable: true, get: function () { return __importDefault(switch_button_1).default; } });
|
|
42
42
|
var table_1 = require("./table");
|
|
43
43
|
Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return __importDefault(table_1).default; } });
|
|
44
|
+
var user_credit_card_1 = require("./user-credit-card");
|
|
45
|
+
Object.defineProperty(exports, "UserCreditCard", { enumerable: true, get: function () { return __importDefault(user_credit_card_1).default; } });
|
|
44
46
|
var form_label_1 = require("./form-label");
|
|
45
47
|
Object.defineProperty(exports, "FormLabel", { enumerable: true, get: function () { return __importDefault(form_label_1).default; } });
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
7
|
+
const context_1 = require("@arcblock/ux/lib/Locale/context");
|
|
8
|
+
const Toast_1 = __importDefault(require("@arcblock/ux/lib/Toast"));
|
|
9
|
+
const UserCard_1 = __importDefault(require("@arcblock/ux/lib/UserCard"));
|
|
10
|
+
const error_1 = require("@blocklet/error");
|
|
11
|
+
const material_1 = require("@mui/material");
|
|
12
|
+
const ahooks_1 = require("ahooks");
|
|
13
|
+
const user_1 = require("../api/user");
|
|
14
|
+
const util_1 = require("../utils/util");
|
|
15
|
+
const withLocaleProvider_1 = __importDefault(require("../utils/withLocaleProvider"));
|
|
16
|
+
function UserCreditCard({ baseUrl, accessKey, onSuccess = () => { }, onError = () => { }, mode = 'default', render = () => null, }) {
|
|
17
|
+
var _a, _b, _c, _d, _e, _f;
|
|
18
|
+
const { t } = (0, context_1.useLocaleContext)();
|
|
19
|
+
const { data: userInfoData, loading } = (0, ahooks_1.useRequest)(() => (0, user_1.getUserInfo)({ baseUrl, accessKey }), {
|
|
20
|
+
refreshDeps: [baseUrl, accessKey],
|
|
21
|
+
onError: (err) => {
|
|
22
|
+
Toast_1.default.error((0, error_1.formatError)(err));
|
|
23
|
+
onError === null || onError === void 0 ? void 0 : onError(err);
|
|
24
|
+
},
|
|
25
|
+
onSuccess: (data) => {
|
|
26
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
if (loading) {
|
|
30
|
+
return ((0, jsx_runtime_1.jsxs)(material_1.Box, { sx: { p: 2, textAlign: 'center' }, children: [(0, jsx_runtime_1.jsx)(material_1.CircularProgress, { size: 24 }), (0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "body2", color: "text.secondary", sx: { mt: 1 }, children: t('loadingUserInfo') })] }));
|
|
31
|
+
}
|
|
32
|
+
if (!userInfoData) {
|
|
33
|
+
return ((0, jsx_runtime_1.jsx)(material_1.Box, { sx: { p: 2 }, children: (0, jsx_runtime_1.jsx)(material_1.Alert, { severity: "info", sx: { fontSize: '0.875rem' }, children: t('noUserData') }) }));
|
|
34
|
+
}
|
|
35
|
+
if (mode === 'custom') {
|
|
36
|
+
return render(userInfoData);
|
|
37
|
+
}
|
|
38
|
+
return ((0, jsx_runtime_1.jsxs)(material_1.Stack, { sx: { p: 2, border: '1px solid', borderColor: 'divider', borderRadius: 2, width: '100%' }, spacing: 2, className: "user-credit-card", children: [(0, jsx_runtime_1.jsxs)(material_1.Stack, { direction: "row", justifyContent: "space-between", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "body2", color: "text.secondary", children: t('username') }), (0, jsx_runtime_1.jsx)(UserCard_1.default, { user: userInfoData.user, showHoverCard: true, popupShowDid: true, sx: {
|
|
39
|
+
border: 'none',
|
|
40
|
+
p: 0,
|
|
41
|
+
minWidth: 0,
|
|
42
|
+
}, avatarProps: {
|
|
43
|
+
size: 24,
|
|
44
|
+
} })] }), (userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.enableCredit) ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsxs)(material_1.Stack, { direction: "row", justifyContent: "space-between", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "body2", color: "text.secondary", children: t('creditBalance') }), (0, jsx_runtime_1.jsxs)(material_1.Typography, { variant: "h6", fontWeight: "bold", children: [(0, util_1.formatNumber)(((_a = userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.creditBalance) === null || _a === void 0 ? void 0 : _a.balance) || '0'), " ", (_c = (_b = userInfoData.currency) === null || _b === void 0 ? void 0 : _b.symbol) !== null && _c !== void 0 ? _c : 'AHC'] })] }), ((_d = userInfoData.creditBalance) === null || _d === void 0 ? void 0 : _d.pendingCredit) && Number(userInfoData.creditBalance.pendingCredit) > 0 && ((0, jsx_runtime_1.jsxs)(material_1.Stack, { direction: "row", justifyContent: "space-between", alignItems: "center", children: [(0, jsx_runtime_1.jsx)(material_1.Typography, { variant: "body2", color: "text.secondary", children: t('pendingCredit') }), (0, jsx_runtime_1.jsxs)(material_1.Typography, { variant: "h6", fontWeight: "bold", sx: { color: 'error.main' }, children: [(0, util_1.formatNumber)(userInfoData.creditBalance.pendingCredit), " ", (_f = (_e = userInfoData.currency) === null || _e === void 0 ? void 0 : _e.symbol) !== null && _f !== void 0 ? _f : 'AHC'] })] })), (0, jsx_runtime_1.jsxs)(material_1.Stack, { direction: "row", spacing: 1.5, sx: { mt: 1 }, children: [(userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.paymentLink) && ((0, jsx_runtime_1.jsx)(material_1.Button, { variant: "outlined", onClick: () => {
|
|
45
|
+
window.open((userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.paymentLink) || '', '_blank');
|
|
46
|
+
}, sx: {
|
|
47
|
+
flex: 1,
|
|
48
|
+
}, children: t('recharge') })), (0, jsx_runtime_1.jsx)(material_1.Button, { variant: "text", onClick: () => {
|
|
49
|
+
window.open((userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.profileLink) || '', '_blank');
|
|
50
|
+
}, sx: {
|
|
51
|
+
borderColor: 'primary.main',
|
|
52
|
+
color: 'primary.main',
|
|
53
|
+
}, children: t('manage') })] })] })) : ((0, jsx_runtime_1.jsxs)(material_1.Alert, { severity: "info", sx: {
|
|
54
|
+
textAlign: 'left',
|
|
55
|
+
}, children: [t('creditNotEnabled'), (0, jsx_runtime_1.jsx)(material_1.Typography, { sx: {
|
|
56
|
+
display: 'inline-block',
|
|
57
|
+
fontSize: 'inherit',
|
|
58
|
+
lineHeight: 'inherit',
|
|
59
|
+
ml: 1,
|
|
60
|
+
a: {
|
|
61
|
+
textDecoration: 'none',
|
|
62
|
+
},
|
|
63
|
+
'& .MuiButtonBase-root': { p: 0, minWidth: 'auto' },
|
|
64
|
+
}, children: (0, jsx_runtime_1.jsx)(material_1.Link, { href: (userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.profileLink) || '', target: "_blank", children: t('viewProfile') }) })] }))] }));
|
|
65
|
+
}
|
|
66
|
+
exports.default = (0, withLocaleProvider_1.default)(UserCreditCard, {
|
|
67
|
+
translations: {
|
|
68
|
+
en: {
|
|
69
|
+
username: 'User',
|
|
70
|
+
creditBalance: 'Credit Balance',
|
|
71
|
+
pendingCredit: 'Outstanding Charges',
|
|
72
|
+
recharge: 'Buy Credits',
|
|
73
|
+
manage: 'Manage',
|
|
74
|
+
creditNotEnabled: 'AIGNE Hub Credit billing is not enabled. You can use AI services directly.',
|
|
75
|
+
loadingUserInfo: 'Loading user information...',
|
|
76
|
+
fetchUserInfoFailed: 'Failed to fetch user information',
|
|
77
|
+
unknownUser: 'Unknown User',
|
|
78
|
+
noUserData: 'No user data available',
|
|
79
|
+
retry: 'Retry',
|
|
80
|
+
viewProfile: 'View Profile',
|
|
81
|
+
},
|
|
82
|
+
zh: {
|
|
83
|
+
username: '用户',
|
|
84
|
+
creditBalance: '信用额度',
|
|
85
|
+
pendingCredit: '待结清额度',
|
|
86
|
+
recharge: '购买额度',
|
|
87
|
+
manage: '管理额度',
|
|
88
|
+
creditNotEnabled: 'AIGNE Hub 未启用 Credit 计费功能,您可以直接使用AI服务。',
|
|
89
|
+
loadingUserInfo: '正在加载用户信息...',
|
|
90
|
+
fetchUserInfoFailed: '获取用户信息失败',
|
|
91
|
+
unknownUser: '未知用户',
|
|
92
|
+
noUserData: '暂无用户数据',
|
|
93
|
+
retry: '重试',
|
|
94
|
+
viewProfile: '查看用户信息',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.formatNumber = formatNumber;
|
|
7
|
+
const trimEnd_1 = __importDefault(require("lodash/trimEnd"));
|
|
8
|
+
const numbro_1 = __importDefault(require("numbro"));
|
|
9
|
+
function formatNumber(n, precision = 6, trim = true, thousandSeparated = true) {
|
|
10
|
+
if (!n || n === '0') {
|
|
11
|
+
return '0';
|
|
12
|
+
}
|
|
13
|
+
const num = (0, numbro_1.default)(n);
|
|
14
|
+
const options = {
|
|
15
|
+
thousandSeparated,
|
|
16
|
+
...((precision || precision === 0) && { mantissa: precision }),
|
|
17
|
+
};
|
|
18
|
+
const result = num.format(options);
|
|
19
|
+
if (!trim) {
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
const [left, right] = result.split('.');
|
|
23
|
+
return right ? [left, (0, trimEnd_1.default)(right, '0')].filter(Boolean).join('.') : left;
|
|
24
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { formatError } from '@blocklet/error';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import { joinURL } from 'ufo';
|
|
4
|
+
import { getPrefix, getRemoteBaseUrl } from './utils/util';
|
|
5
|
+
export async function getUserInfo({ baseUrl, accessKey, }) {
|
|
6
|
+
let finalBaseUrl = getPrefix();
|
|
7
|
+
try {
|
|
8
|
+
if (baseUrl) {
|
|
9
|
+
const tmp = new URL(baseUrl);
|
|
10
|
+
if (tmp.origin !== window.location.origin) {
|
|
11
|
+
finalBaseUrl = await getRemoteBaseUrl(baseUrl);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
catch (err) {
|
|
16
|
+
console.warn('Failed to parse baseUrl:', err);
|
|
17
|
+
throw new Error(`Failed to parse baseUrl: ${formatError(err)}`);
|
|
18
|
+
}
|
|
19
|
+
return axios
|
|
20
|
+
.get(joinURL(finalBaseUrl, '/api/user/info'), {
|
|
21
|
+
headers: {
|
|
22
|
+
Authorization: `Bearer ${accessKey}`,
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
.then((res) => res.data);
|
|
26
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { joinURL } from 'ufo';
|
|
3
|
+
const AIGNE_HUB_DID = 'z8ia3xzq2tMq8CRHfaXj1BTYJyYnEcHbqP8cJ';
|
|
4
|
+
export const getRemoteBaseUrl = async (url) => {
|
|
5
|
+
const tmp = new URL(url);
|
|
6
|
+
if (tmp.origin === window.location.origin) {
|
|
7
|
+
return getPrefix();
|
|
8
|
+
}
|
|
9
|
+
const scriptUrl = joinURL(tmp.origin, '__blocklet__.js?type=json');
|
|
10
|
+
const blockletInfo = await axios.get(scriptUrl).then((res) => res.data);
|
|
11
|
+
const componentId = ((blockletInfo === null || blockletInfo === void 0 ? void 0 : blockletInfo.componentId) || '').split('/').pop();
|
|
12
|
+
if (componentId === AIGNE_HUB_DID) {
|
|
13
|
+
return joinURL(tmp.origin, blockletInfo.prefix || '/');
|
|
14
|
+
}
|
|
15
|
+
const component = ((blockletInfo === null || blockletInfo === void 0 ? void 0 : blockletInfo.componentMountPoints) || []).find((x) => (x === null || x === void 0 ? void 0 : x.did) === AIGNE_HUB_DID);
|
|
16
|
+
return component ? joinURL(tmp.origin, component.mountPoint) : url;
|
|
17
|
+
};
|
|
18
|
+
export const getPrefix = () => {
|
|
19
|
+
var _a, _b, _c, _d;
|
|
20
|
+
const prefix = ((_a = window.blocklet) === null || _a === void 0 ? void 0 : _a.prefix) || '/';
|
|
21
|
+
const baseUrl = (_b = window.location) === null || _b === void 0 ? void 0 : _b.origin; // required when use payment feature cross origin
|
|
22
|
+
const componentId = (((_c = window.blocklet) === null || _c === void 0 ? void 0 : _c.componentId) || '').split('/').pop();
|
|
23
|
+
if (componentId === AIGNE_HUB_DID) {
|
|
24
|
+
return joinURL(baseUrl, prefix);
|
|
25
|
+
}
|
|
26
|
+
const component = (((_d = window.blocklet) === null || _d === void 0 ? void 0 : _d.componentMountPoints) || []).find((x) => (x === null || x === void 0 ? void 0 : x.did) === AIGNE_HUB_DID);
|
|
27
|
+
if (component) {
|
|
28
|
+
return joinURL(baseUrl, component.mountPoint);
|
|
29
|
+
}
|
|
30
|
+
return joinURL(baseUrl, prefix);
|
|
31
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
3
|
+
import { formatError } from '@blocklet/error';
|
|
3
4
|
import { Alert, Stack } from '@mui/material';
|
|
4
5
|
import { CreditErrorType } from '../../api/error';
|
|
5
6
|
import withLocaleProvider from '../../utils/withLocaleProvider';
|
|
@@ -8,7 +9,7 @@ function CreditErrorAlert({ error, ...props }) {
|
|
|
8
9
|
const { t } = useLocaleContext();
|
|
9
10
|
const isCreditError = (error === null || error === void 0 ? void 0 : error.type) === CreditErrorType.NOT_ENOUGH;
|
|
10
11
|
if (!isCreditError) {
|
|
11
|
-
return (_jsx(Alert, { severity: "error", ...props, children: (error
|
|
12
|
+
return (_jsx(Alert, { severity: "error", ...props, children: formatError(error) || t('unknownError') }));
|
|
12
13
|
}
|
|
13
14
|
return (_jsxs(Alert, { severity: "warning", ...props, sx: {
|
|
14
15
|
px: 1,
|
|
@@ -11,4 +11,5 @@ export { default as CreditBalance } from './credit/balance';
|
|
|
11
11
|
export { default as CreditErrorAlert } from './credit/alert';
|
|
12
12
|
export { default as Switch } from './switch-button';
|
|
13
13
|
export { default as Table } from './table';
|
|
14
|
+
export { default as UserCreditCard } from './user-credit-card';
|
|
14
15
|
export { default as FormLabel } from './form-label';
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
3
|
+
import Toast from '@arcblock/ux/lib/Toast';
|
|
4
|
+
import UserCard from '@arcblock/ux/lib/UserCard';
|
|
5
|
+
import { formatError } from '@blocklet/error';
|
|
6
|
+
import { Alert, Box, Button, CircularProgress, Link, Stack, Typography } from '@mui/material';
|
|
7
|
+
import { useRequest } from 'ahooks';
|
|
8
|
+
import { getUserInfo } from '../api/user';
|
|
9
|
+
import { formatNumber } from '../utils/util';
|
|
10
|
+
import withLocaleProvider from '../utils/withLocaleProvider';
|
|
11
|
+
function UserCreditCard({ baseUrl, accessKey, onSuccess = () => { }, onError = () => { }, mode = 'default', render = () => null, }) {
|
|
12
|
+
var _a, _b, _c, _d, _e, _f;
|
|
13
|
+
const { t } = useLocaleContext();
|
|
14
|
+
const { data: userInfoData, loading } = useRequest(() => getUserInfo({ baseUrl, accessKey }), {
|
|
15
|
+
refreshDeps: [baseUrl, accessKey],
|
|
16
|
+
onError: (err) => {
|
|
17
|
+
Toast.error(formatError(err));
|
|
18
|
+
onError === null || onError === void 0 ? void 0 : onError(err);
|
|
19
|
+
},
|
|
20
|
+
onSuccess: (data) => {
|
|
21
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
if (loading) {
|
|
25
|
+
return (_jsxs(Box, { sx: { p: 2, textAlign: 'center' }, children: [_jsx(CircularProgress, { size: 24 }), _jsx(Typography, { variant: "body2", color: "text.secondary", sx: { mt: 1 }, children: t('loadingUserInfo') })] }));
|
|
26
|
+
}
|
|
27
|
+
if (!userInfoData) {
|
|
28
|
+
return (_jsx(Box, { sx: { p: 2 }, children: _jsx(Alert, { severity: "info", sx: { fontSize: '0.875rem' }, children: t('noUserData') }) }));
|
|
29
|
+
}
|
|
30
|
+
if (mode === 'custom') {
|
|
31
|
+
return render(userInfoData);
|
|
32
|
+
}
|
|
33
|
+
return (_jsxs(Stack, { sx: { p: 2, border: '1px solid', borderColor: 'divider', borderRadius: 2, width: '100%' }, spacing: 2, className: "user-credit-card", children: [_jsxs(Stack, { direction: "row", justifyContent: "space-between", alignItems: "center", children: [_jsx(Typography, { variant: "body2", color: "text.secondary", children: t('username') }), _jsx(UserCard, { user: userInfoData.user, showHoverCard: true, popupShowDid: true, sx: {
|
|
34
|
+
border: 'none',
|
|
35
|
+
p: 0,
|
|
36
|
+
minWidth: 0,
|
|
37
|
+
}, avatarProps: {
|
|
38
|
+
size: 24,
|
|
39
|
+
} })] }), (userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.enableCredit) ? (_jsxs(_Fragment, { children: [_jsxs(Stack, { direction: "row", justifyContent: "space-between", alignItems: "center", children: [_jsx(Typography, { variant: "body2", color: "text.secondary", children: t('creditBalance') }), _jsxs(Typography, { variant: "h6", fontWeight: "bold", children: [formatNumber(((_a = userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.creditBalance) === null || _a === void 0 ? void 0 : _a.balance) || '0'), " ", (_c = (_b = userInfoData.currency) === null || _b === void 0 ? void 0 : _b.symbol) !== null && _c !== void 0 ? _c : 'AHC'] })] }), ((_d = userInfoData.creditBalance) === null || _d === void 0 ? void 0 : _d.pendingCredit) && Number(userInfoData.creditBalance.pendingCredit) > 0 && (_jsxs(Stack, { direction: "row", justifyContent: "space-between", alignItems: "center", children: [_jsx(Typography, { variant: "body2", color: "text.secondary", children: t('pendingCredit') }), _jsxs(Typography, { variant: "h6", fontWeight: "bold", sx: { color: 'error.main' }, children: [formatNumber(userInfoData.creditBalance.pendingCredit), " ", (_f = (_e = userInfoData.currency) === null || _e === void 0 ? void 0 : _e.symbol) !== null && _f !== void 0 ? _f : 'AHC'] })] })), _jsxs(Stack, { direction: "row", spacing: 1.5, sx: { mt: 1 }, children: [(userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.paymentLink) && (_jsx(Button, { variant: "outlined", onClick: () => {
|
|
40
|
+
window.open((userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.paymentLink) || '', '_blank');
|
|
41
|
+
}, sx: {
|
|
42
|
+
flex: 1,
|
|
43
|
+
}, children: t('recharge') })), _jsx(Button, { variant: "text", onClick: () => {
|
|
44
|
+
window.open((userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.profileLink) || '', '_blank');
|
|
45
|
+
}, sx: {
|
|
46
|
+
borderColor: 'primary.main',
|
|
47
|
+
color: 'primary.main',
|
|
48
|
+
}, children: t('manage') })] })] })) : (_jsxs(Alert, { severity: "info", sx: {
|
|
49
|
+
textAlign: 'left',
|
|
50
|
+
}, children: [t('creditNotEnabled'), _jsx(Typography, { sx: {
|
|
51
|
+
display: 'inline-block',
|
|
52
|
+
fontSize: 'inherit',
|
|
53
|
+
lineHeight: 'inherit',
|
|
54
|
+
ml: 1,
|
|
55
|
+
a: {
|
|
56
|
+
textDecoration: 'none',
|
|
57
|
+
},
|
|
58
|
+
'& .MuiButtonBase-root': { p: 0, minWidth: 'auto' },
|
|
59
|
+
}, children: _jsx(Link, { href: (userInfoData === null || userInfoData === void 0 ? void 0 : userInfoData.profileLink) || '', target: "_blank", children: t('viewProfile') }) })] }))] }));
|
|
60
|
+
}
|
|
61
|
+
export default withLocaleProvider(UserCreditCard, {
|
|
62
|
+
translations: {
|
|
63
|
+
en: {
|
|
64
|
+
username: 'User',
|
|
65
|
+
creditBalance: 'Credit Balance',
|
|
66
|
+
pendingCredit: 'Outstanding Charges',
|
|
67
|
+
recharge: 'Buy Credits',
|
|
68
|
+
manage: 'Manage',
|
|
69
|
+
creditNotEnabled: 'AIGNE Hub Credit billing is not enabled. You can use AI services directly.',
|
|
70
|
+
loadingUserInfo: 'Loading user information...',
|
|
71
|
+
fetchUserInfoFailed: 'Failed to fetch user information',
|
|
72
|
+
unknownUser: 'Unknown User',
|
|
73
|
+
noUserData: 'No user data available',
|
|
74
|
+
retry: 'Retry',
|
|
75
|
+
viewProfile: 'View Profile',
|
|
76
|
+
},
|
|
77
|
+
zh: {
|
|
78
|
+
username: '用户',
|
|
79
|
+
creditBalance: '信用额度',
|
|
80
|
+
pendingCredit: '待结清额度',
|
|
81
|
+
recharge: '购买额度',
|
|
82
|
+
manage: '管理额度',
|
|
83
|
+
creditNotEnabled: 'AIGNE Hub 未启用 Credit 计费功能,您可以直接使用AI服务。',
|
|
84
|
+
loadingUserInfo: '正在加载用户信息...',
|
|
85
|
+
fetchUserInfoFailed: '获取用户信息失败',
|
|
86
|
+
unknownUser: '未知用户',
|
|
87
|
+
noUserData: '暂无用户数据',
|
|
88
|
+
retry: '重试',
|
|
89
|
+
viewProfile: '查看用户信息',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import trimEnd from 'lodash/trimEnd';
|
|
2
|
+
import numbro from 'numbro';
|
|
3
|
+
export function formatNumber(n, precision = 6, trim = true, thousandSeparated = true) {
|
|
4
|
+
if (!n || n === '0') {
|
|
5
|
+
return '0';
|
|
6
|
+
}
|
|
7
|
+
const num = numbro(n);
|
|
8
|
+
const options = {
|
|
9
|
+
thousandSeparated,
|
|
10
|
+
...((precision || precision === 0) && { mantissa: precision }),
|
|
11
|
+
};
|
|
12
|
+
const result = num.format(options);
|
|
13
|
+
if (!trim) {
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
const [left, right] = result.split('.');
|
|
17
|
+
return right ? [left, trimEnd(right, '0')].filter(Boolean).join('.') : left;
|
|
18
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { User } from '@arcblock/ux/lib/type';
|
|
2
|
+
import type { TPaymentCurrency } from '@blocklet/payment-js';
|
|
3
|
+
export interface UserInfoResult {
|
|
4
|
+
user: User;
|
|
5
|
+
enableCredit: boolean;
|
|
6
|
+
creditBalance: {
|
|
7
|
+
balance: string;
|
|
8
|
+
total: string;
|
|
9
|
+
grantCount: number;
|
|
10
|
+
pendingCredit: string;
|
|
11
|
+
} | null;
|
|
12
|
+
paymentLink: string | null;
|
|
13
|
+
currency?: TPaymentCurrency;
|
|
14
|
+
profileLink: string;
|
|
15
|
+
}
|
|
16
|
+
export declare function getUserInfo({ baseUrl, accessKey, }: {
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
accessKey: string;
|
|
19
|
+
}): Promise<UserInfoResult>;
|
|
@@ -9,4 +9,5 @@ export { default as CreditBalance } from './credit/balance';
|
|
|
9
9
|
export { default as CreditErrorAlert } from './credit/alert';
|
|
10
10
|
export { default as Switch } from './switch-button';
|
|
11
11
|
export { default as Table } from './table';
|
|
12
|
+
export { default as UserCreditCard } from './user-credit-card';
|
|
12
13
|
export { default as FormLabel } from './form-label';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UserInfoResult } from '../api/user';
|
|
2
|
+
interface UserCreditCardProps {
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
accessKey: string;
|
|
5
|
+
onSuccess?: (userInfo: UserInfoResult) => void;
|
|
6
|
+
onError?: (error: Error) => void;
|
|
7
|
+
mode?: 'default' | 'custom';
|
|
8
|
+
render?: (userInfo: UserInfoResult) => React.ReactNode | null;
|
|
9
|
+
}
|
|
10
|
+
declare const _default: import("react").ComponentType<UserCreditCardProps>;
|
|
11
|
+
export default _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function formatNumber(n: number | string, precision?: number, trim?: boolean, thousandSeparated?: boolean): string | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/aigne-hub",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.16",
|
|
4
4
|
"description": "The react.js component library for AIGNE Hub",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -46,6 +46,16 @@
|
|
|
46
46
|
"types": "./lib/types/components/*.d.ts",
|
|
47
47
|
"import": "./lib/esm/components/*.js",
|
|
48
48
|
"require": "./lib/cjs/components/*.js"
|
|
49
|
+
},
|
|
50
|
+
"./utils": {
|
|
51
|
+
"types": "./lib/types/utils/index.d.ts",
|
|
52
|
+
"import": "./lib/esm/utils/index.js",
|
|
53
|
+
"require": "./lib/cjs/utils/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./utils/*": {
|
|
56
|
+
"types": "./lib/types/utils/*.d.ts",
|
|
57
|
+
"import": "./lib/esm/utils/*.js",
|
|
58
|
+
"require": "./lib/cjs/utils/*.js"
|
|
49
59
|
}
|
|
50
60
|
},
|
|
51
61
|
"typesVersions": {
|
|
@@ -85,8 +95,9 @@
|
|
|
85
95
|
"dependencies": {
|
|
86
96
|
"@arcblock/did": "^1.21.0",
|
|
87
97
|
"@arcblock/ux": "^3.0.33",
|
|
98
|
+
"@blocklet/error": "0.2.5",
|
|
88
99
|
"@blocklet/logger": "^1.16.46",
|
|
89
|
-
"@blocklet/payment-js": "^1.19.
|
|
100
|
+
"@blocklet/payment-js": "^1.19.8",
|
|
90
101
|
"@blocklet/sdk": "^1.16.46",
|
|
91
102
|
"@emotion/css": "^11.13.5",
|
|
92
103
|
"@emotion/react": "^11.14.0",
|
|
@@ -109,6 +120,7 @@
|
|
|
109
120
|
"json-stable-stringify": "^1.1.1",
|
|
110
121
|
"lodash": "^4.17.21",
|
|
111
122
|
"nanoid": "^5.0.7",
|
|
123
|
+
"numbro": "^2.5.0",
|
|
112
124
|
"openai": "^4.56.0",
|
|
113
125
|
"react-markdown": "^9.0.1",
|
|
114
126
|
"react-photo-view": "^1.2.6",
|