@blocklet/ui-react 2.9.87 → 2.9.88

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,9 +1,12 @@
1
1
  import { ReactElement } from 'react';
2
- export default function AddComponent({ componentDid, resourceType, resourceDid, autoClose, onComplete, onClose, render, }: {
2
+ export default function AddComponent({ componentDid, resourceType, resourceDid, grantedRoles, autoClose, selectedMeta, storeUrl, onComplete, onClose, render, }: {
3
3
  componentDid: string;
4
- resourceType: string;
5
- resourceDid: string;
4
+ resourceType?: string;
5
+ resourceDid?: string;
6
6
  autoClose: boolean;
7
+ selectedMeta?: any;
8
+ storeUrl?: string;
9
+ grantedRoles?: string[];
7
10
  onComplete: () => void;
8
11
  onClose: () => void;
9
12
  render?: ({ onClick, loading }: {
@@ -1,26 +1,33 @@
1
1
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
- import { useEffect } from "react";
2
+ import { useEffect, useRef, useContext } from "react";
3
3
  import Button from "@arcblock/ux/lib/Button";
4
4
  import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
5
5
  import { WELLKNOWN_SERVICE_PATH_PREFIX } from "@arcblock/ux/lib/Util/constant";
6
6
  import { useMemoizedFn, useReactive } from "ahooks";
7
7
  import { translate } from "@arcblock/ux/lib/Locale/util";
8
8
  import { joinURL, withQuery } from "ufo";
9
+ import { SessionContext } from "@arcblock/did-connect/lib/Session";
10
+ import Toast from "@arcblock/ux/lib/Toast";
9
11
  import ResourceDialog from "./resource-dialog.js";
10
12
  import { translations } from "../libs/locales.js";
11
13
  export default function AddComponent({
12
14
  componentDid,
13
15
  resourceType,
14
16
  resourceDid,
17
+ grantedRoles = ["admin", "owner"],
15
18
  autoClose = true,
19
+ selectedMeta,
20
+ storeUrl,
16
21
  onComplete,
17
22
  onClose,
18
23
  render
19
24
  }) {
25
+ const { session } = useContext(SessionContext);
20
26
  const { locale } = useLocaleContext();
21
27
  const t = useMemoizedFn((key, data = {}) => {
22
28
  return translate(translations, key, locale, "en", data);
23
29
  });
30
+ const refIframe = useRef(null);
24
31
  const currentState = useReactive({
25
32
  loading: false,
26
33
  showDialog: false
@@ -43,11 +50,22 @@ export default function AddComponent({
43
50
  onComplete?.();
44
51
  });
45
52
  const handleOpen = useMemoizedFn(() => {
53
+ if (!session.user) {
54
+ Toast.warning(t("needLogin"));
55
+ return;
56
+ }
57
+ if (!grantedRoles.includes(session.user?.role)) {
58
+ Toast.warning(t("noPermission"));
59
+ return;
60
+ }
46
61
  currentState.loading = true;
47
62
  currentState.showDialog = true;
48
63
  });
49
64
  const handleLoad = useMemoizedFn(() => {
50
65
  currentState.loading = false;
66
+ if (refIframe.current) {
67
+ refIframe.current?.selectBlocklet(selectedMeta);
68
+ }
51
69
  });
52
70
  const handleIframeMessage = useMemoizedFn((event) => {
53
71
  if (event?.data?.event === "component.installed" && event.data.componentDid === componentDid) {
@@ -81,8 +99,11 @@ export default function AddComponent({
81
99
  currentState.showDialog && /* @__PURE__ */ jsx(
82
100
  ResourceDialog,
83
101
  {
102
+ ref: refIframe,
84
103
  src: importUrl,
104
+ storeUrl,
85
105
  open: true,
106
+ componentDid,
86
107
  loading: currentState.loading,
87
108
  onClose: handleClose,
88
109
  onComplete: handleComplete,
@@ -1,6 +1,7 @@
1
1
  import { ReactElement } from 'react';
2
- export default function PublishComponent({ componentDid, onClose, render, }: {
3
- componentDid: String;
2
+ export default function PublishComponent({ componentDid, grantedRoles, onClose, render, }: {
3
+ componentDid: string;
4
+ grantedRoles?: string[];
4
5
  onClose: () => void;
5
6
  render?: ({ onClick, loading }: {
6
7
  onClick: () => void;
@@ -1,16 +1,21 @@
1
1
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { useContext } from "react";
2
3
  import Button from "@arcblock/ux/lib/Button";
3
4
  import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
4
5
  import { WELLKNOWN_SERVICE_PATH_PREFIX } from "@arcblock/ux/lib/Util/constant";
5
6
  import { useMemoizedFn, useReactive } from "ahooks";
6
7
  import { translate } from "@arcblock/ux/lib/Locale/util";
8
+ import { SessionContext } from "@arcblock/did-connect/lib/Session";
9
+ import Toast from "@arcblock/ux/lib/Toast";
7
10
  import ResourceDialog from "./resource-dialog.js";
8
11
  import { translations } from "../libs/locales.js";
9
12
  export default function PublishComponent({
10
13
  componentDid,
14
+ grantedRoles,
11
15
  onClose,
12
16
  render
13
17
  }) {
18
+ const { session } = useContext(SessionContext);
14
19
  const { locale } = useLocaleContext();
15
20
  const t = useMemoizedFn((key, data = {}) => {
16
21
  return translate(translations, key, locale, "en", data);
@@ -28,6 +33,16 @@ export default function PublishComponent({
28
33
  currentState.loading = false;
29
34
  });
30
35
  const handleOpen = useMemoizedFn(() => {
36
+ if (!session.user) {
37
+ Toast.warning(t("needLogin"));
38
+ return;
39
+ }
40
+ if (grantedRoles && grantedRoles.length > 0) {
41
+ if (!grantedRoles?.includes(session.user?.role)) {
42
+ Toast.warning(t("noPermission"));
43
+ return;
44
+ }
45
+ }
31
46
  currentState.loading = true;
32
47
  currentState.showDialog = true;
33
48
  });
@@ -46,6 +61,16 @@ export default function PublishComponent({
46
61
  },
47
62
  "button"
48
63
  ),
49
- currentState.showDialog && /* @__PURE__ */ jsx(ResourceDialog, { src: exportUrl, open: true, onClose: handleClose, onLoad: handleLoad, loading: currentState.loading })
64
+ currentState.showDialog && /* @__PURE__ */ jsx(
65
+ ResourceDialog,
66
+ {
67
+ src: exportUrl,
68
+ open: true,
69
+ onClose: handleClose,
70
+ onLoad: handleLoad,
71
+ componentDid,
72
+ loading: currentState.loading
73
+ }
74
+ )
50
75
  ] });
51
76
  }
@@ -1,9 +1,14 @@
1
1
  /// <reference types="react" />
2
- export default function ResourceDialog({ src, open, loading, onClose, onComplete, onLoad, }: {
2
+ declare const _default: import("react").ForwardRefExoticComponent<{
3
3
  src: string;
4
- open?: boolean;
5
- loading?: boolean;
6
- onLoad?: () => void;
7
- onClose?: () => void;
8
- onComplete?: () => void;
9
- }): import("react").JSX.Element | null;
4
+ componentDid: string;
5
+ open?: boolean | undefined;
6
+ loading?: boolean | undefined;
7
+ storeUrl?: string | undefined;
8
+ onLoad?: (() => void) | undefined;
9
+ onClose?: (() => void) | undefined;
10
+ onComplete?: (() => void) | undefined;
11
+ } & import("react").RefAttributes<{
12
+ selectBlocklet: (blockletMeta: any) => void;
13
+ }>>;
14
+ export default _default;
@@ -1,17 +1,19 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { useMemoizedFn, useMount, useUnmount } from "ahooks";
3
- import { useRef } from "react";
4
- export default function ResourceDialog({
3
+ import { useRef, forwardRef, useImperativeHandle } from "react";
4
+ function ResourceDialog({
5
5
  src,
6
6
  open = false,
7
+ componentDid,
7
8
  loading = true,
9
+ storeUrl,
8
10
  onClose = () => {
9
11
  },
10
12
  onComplete = () => {
11
13
  },
12
14
  onLoad = () => {
13
15
  }
14
- }) {
16
+ }, ref) {
15
17
  const iframeRef = useRef(null);
16
18
  const listener = useMemoizedFn((event) => {
17
19
  if (open) {
@@ -32,6 +34,21 @@ export default function ResourceDialog({
32
34
  useUnmount(() => {
33
35
  window.removeEventListener("message", listener);
34
36
  });
37
+ useImperativeHandle(ref, () => ({
38
+ selectBlocklet: (blockletMeta) => {
39
+ if (iframeRef.current?.contentWindow) {
40
+ iframeRef.current.contentWindow.postMessage(
41
+ {
42
+ event: "resourceDialog.select",
43
+ data: blockletMeta,
44
+ componentDid,
45
+ storeUrl
46
+ },
47
+ "*"
48
+ );
49
+ }
50
+ }
51
+ }));
35
52
  if (!open) {
36
53
  return null;
37
54
  }
@@ -54,3 +71,4 @@ export default function ResourceDialog({
54
71
  }
55
72
  );
56
73
  }
74
+ export default forwardRef(ResourceDialog);
@@ -2,9 +2,13 @@ export declare const translations: {
2
2
  zh: {
3
3
  importResource: string;
4
4
  exportResource: string;
5
+ noPermission: string;
6
+ needLogin: string;
5
7
  };
6
8
  en: {
7
9
  importResource: string;
8
10
  exportResource: string;
11
+ noPermission: string;
12
+ needLogin: string;
9
13
  };
10
14
  };
@@ -1,10 +1,14 @@
1
1
  export const translations = {
2
2
  zh: {
3
3
  importResource: "\u5BFC\u5165",
4
- exportResource: "\u5BFC\u51FA"
4
+ exportResource: "\u5BFC\u51FA",
5
+ noPermission: "\u4F60\u6CA1\u6709\u6743\u9650\u6267\u884C\u8BE5\u64CD\u4F5C\uFF0C\u8BF7\u5207\u6362\u7BA1\u7406\u5458\u901A\u884C\u8BC1\u6216\u8054\u7CFB\u7BA1\u7406\u5458\u8FDB\u884C\u5B89\u88C5",
6
+ needLogin: "\u767B\u5F55\u540E\u624D\u53EF\u64CD\u4F5C"
5
7
  },
6
8
  en: {
7
9
  importResource: "Import",
8
- exportResource: "Export"
10
+ exportResource: "Export",
11
+ noPermission: "You do not have permission to perform this operation. Please switch your passport as admin or contact the administrator to install the blocklet",
12
+ needLogin: "Please login first"
9
13
  }
10
14
  };
@@ -1,9 +1,12 @@
1
1
  import { ReactElement } from 'react';
2
- export default function AddComponent({ componentDid, resourceType, resourceDid, autoClose, onComplete, onClose, render, }: {
2
+ export default function AddComponent({ componentDid, resourceType, resourceDid, grantedRoles, autoClose, selectedMeta, storeUrl, onComplete, onClose, render, }: {
3
3
  componentDid: string;
4
- resourceType: string;
5
- resourceDid: string;
4
+ resourceType?: string;
5
+ resourceDid?: string;
6
6
  autoClose: boolean;
7
+ selectedMeta?: any;
8
+ storeUrl?: string;
9
+ grantedRoles?: string[];
7
10
  onComplete: () => void;
8
11
  onClose: () => void;
9
12
  render?: ({ onClick, loading }: {
@@ -12,6 +12,8 @@ var _constant = require("@arcblock/ux/lib/Util/constant");
12
12
  var _ahooks = require("ahooks");
13
13
  var _util = require("@arcblock/ux/lib/Locale/util");
14
14
  var _ufo = require("ufo");
15
+ var _Session = require("@arcblock/did-connect/lib/Session");
16
+ var _Toast = _interopRequireDefault(require("@arcblock/ux/lib/Toast"));
15
17
  var _resourceDialog = _interopRequireDefault(require("./resource-dialog"));
16
18
  var _locales = require("../libs/locales");
17
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -19,17 +21,24 @@ function AddComponent({
19
21
  componentDid,
20
22
  resourceType,
21
23
  resourceDid,
24
+ grantedRoles = ["admin", "owner"],
22
25
  autoClose = true,
26
+ selectedMeta,
27
+ storeUrl,
23
28
  onComplete,
24
29
  onClose,
25
30
  render
26
31
  }) {
32
+ const {
33
+ session
34
+ } = (0, _react.useContext)(_Session.SessionContext);
27
35
  const {
28
36
  locale
29
37
  } = (0, _context.useLocaleContext)();
30
38
  const t = (0, _ahooks.useMemoizedFn)((key, data = {}) => {
31
39
  return (0, _util.translate)(_locales.translations, key, locale, "en", data);
32
40
  });
41
+ const refIframe = (0, _react.useRef)(null);
33
42
  const currentState = (0, _ahooks.useReactive)({
34
43
  loading: false,
35
44
  showDialog: false
@@ -52,11 +61,22 @@ function AddComponent({
52
61
  onComplete?.();
53
62
  });
54
63
  const handleOpen = (0, _ahooks.useMemoizedFn)(() => {
64
+ if (!session.user) {
65
+ _Toast.default.warning(t("needLogin"));
66
+ return;
67
+ }
68
+ if (!grantedRoles.includes(session.user?.role)) {
69
+ _Toast.default.warning(t("noPermission"));
70
+ return;
71
+ }
55
72
  currentState.loading = true;
56
73
  currentState.showDialog = true;
57
74
  });
58
75
  const handleLoad = (0, _ahooks.useMemoizedFn)(() => {
59
76
  currentState.loading = false;
77
+ if (refIframe.current) {
78
+ refIframe.current?.selectBlocklet(selectedMeta);
79
+ }
60
80
  });
61
81
  const handleIframeMessage = (0, _ahooks.useMemoizedFn)(event => {
62
82
  if (event?.data?.event === "component.installed" && event.data.componentDid === componentDid) {
@@ -89,8 +109,11 @@ function AddComponent({
89
109
  },
90
110
  children: t("importResource")
91
111
  }, "button"), currentState.showDialog && /* @__PURE__ */(0, _jsxRuntime.jsx)(_resourceDialog.default, {
112
+ ref: refIframe,
92
113
  src: importUrl,
114
+ storeUrl,
93
115
  open: true,
116
+ componentDid,
94
117
  loading: currentState.loading,
95
118
  onClose: handleClose,
96
119
  onComplete: handleComplete,
@@ -1,6 +1,7 @@
1
1
  import { ReactElement } from 'react';
2
- export default function PublishComponent({ componentDid, onClose, render, }: {
3
- componentDid: String;
2
+ export default function PublishComponent({ componentDid, grantedRoles, onClose, render, }: {
3
+ componentDid: string;
4
+ grantedRoles?: string[];
4
5
  onClose: () => void;
5
6
  render?: ({ onClick, loading }: {
6
7
  onClick: () => void;
@@ -5,19 +5,26 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  module.exports = PublishComponent;
7
7
  var _jsxRuntime = require("react/jsx-runtime");
8
+ var _react = require("react");
8
9
  var _Button = _interopRequireDefault(require("@arcblock/ux/lib/Button"));
9
10
  var _context = require("@arcblock/ux/lib/Locale/context");
10
11
  var _constant = require("@arcblock/ux/lib/Util/constant");
11
12
  var _ahooks = require("ahooks");
12
13
  var _util = require("@arcblock/ux/lib/Locale/util");
14
+ var _Session = require("@arcblock/did-connect/lib/Session");
15
+ var _Toast = _interopRequireDefault(require("@arcblock/ux/lib/Toast"));
13
16
  var _resourceDialog = _interopRequireDefault(require("./resource-dialog"));
14
17
  var _locales = require("../libs/locales");
15
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
19
  function PublishComponent({
17
20
  componentDid,
21
+ grantedRoles,
18
22
  onClose,
19
23
  render
20
24
  }) {
25
+ const {
26
+ session
27
+ } = (0, _react.useContext)(_Session.SessionContext);
21
28
  const {
22
29
  locale
23
30
  } = (0, _context.useLocaleContext)();
@@ -37,6 +44,16 @@ function PublishComponent({
37
44
  currentState.loading = false;
38
45
  });
39
46
  const handleOpen = (0, _ahooks.useMemoizedFn)(() => {
47
+ if (!session.user) {
48
+ _Toast.default.warning(t("needLogin"));
49
+ return;
50
+ }
51
+ if (grantedRoles && grantedRoles.length > 0) {
52
+ if (!grantedRoles?.includes(session.user?.role)) {
53
+ _Toast.default.warning(t("noPermission"));
54
+ return;
55
+ }
56
+ }
40
57
  currentState.loading = true;
41
58
  currentState.showDialog = true;
42
59
  });
@@ -61,6 +78,7 @@ function PublishComponent({
61
78
  open: true,
62
79
  onClose: handleClose,
63
80
  onLoad: handleLoad,
81
+ componentDid,
64
82
  loading: currentState.loading
65
83
  })]
66
84
  });
@@ -1,9 +1,14 @@
1
1
  /// <reference types="react" />
2
- export default function ResourceDialog({ src, open, loading, onClose, onComplete, onLoad, }: {
2
+ declare const _default: import("react").ForwardRefExoticComponent<{
3
3
  src: string;
4
- open?: boolean;
5
- loading?: boolean;
6
- onLoad?: () => void;
7
- onClose?: () => void;
8
- onComplete?: () => void;
9
- }): import("react").JSX.Element | null;
4
+ componentDid: string;
5
+ open?: boolean | undefined;
6
+ loading?: boolean | undefined;
7
+ storeUrl?: string | undefined;
8
+ onLoad?: (() => void) | undefined;
9
+ onClose?: (() => void) | undefined;
10
+ onComplete?: (() => void) | undefined;
11
+ } & import("react").RefAttributes<{
12
+ selectBlocklet: (blockletMeta: any) => void;
13
+ }>>;
14
+ export default _default;
@@ -3,18 +3,20 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- module.exports = ResourceDialog;
6
+
7
7
  var _jsxRuntime = require("react/jsx-runtime");
8
8
  var _ahooks = require("ahooks");
9
9
  var _react = require("react");
10
10
  function ResourceDialog({
11
11
  src,
12
12
  open = false,
13
+ componentDid,
13
14
  loading = true,
15
+ storeUrl,
14
16
  onClose = () => {},
15
17
  onComplete = () => {},
16
18
  onLoad = () => {}
17
- }) {
19
+ }, ref) {
18
20
  const iframeRef = (0, _react.useRef)(null);
19
21
  const listener = (0, _ahooks.useMemoizedFn)(event => {
20
22
  if (open) {
@@ -35,6 +37,18 @@ function ResourceDialog({
35
37
  (0, _ahooks.useUnmount)(() => {
36
38
  window.removeEventListener("message", listener);
37
39
  });
40
+ (0, _react.useImperativeHandle)(ref, () => ({
41
+ selectBlocklet: blockletMeta => {
42
+ if (iframeRef.current?.contentWindow) {
43
+ iframeRef.current.contentWindow.postMessage({
44
+ event: "resourceDialog.select",
45
+ data: blockletMeta,
46
+ componentDid,
47
+ storeUrl
48
+ }, "*");
49
+ }
50
+ }
51
+ }));
38
52
  if (!open) {
39
53
  return null;
40
54
  }
@@ -53,4 +67,5 @@ function ResourceDialog({
53
67
  border: "none"
54
68
  }
55
69
  });
56
- }
70
+ }
71
+ module.exports = (0, _react.forwardRef)(ResourceDialog);
@@ -2,9 +2,13 @@ export declare const translations: {
2
2
  zh: {
3
3
  importResource: string;
4
4
  exportResource: string;
5
+ noPermission: string;
6
+ needLogin: string;
5
7
  };
6
8
  en: {
7
9
  importResource: string;
8
10
  exportResource: string;
11
+ noPermission: string;
12
+ needLogin: string;
9
13
  };
10
14
  };
@@ -7,10 +7,14 @@ exports.translations = void 0;
7
7
  const translations = exports.translations = {
8
8
  zh: {
9
9
  importResource: "\u5BFC\u5165",
10
- exportResource: "\u5BFC\u51FA"
10
+ exportResource: "\u5BFC\u51FA",
11
+ noPermission: "\u4F60\u6CA1\u6709\u6743\u9650\u6267\u884C\u8BE5\u64CD\u4F5C\uFF0C\u8BF7\u5207\u6362\u7BA1\u7406\u5458\u901A\u884C\u8BC1\u6216\u8054\u7CFB\u7BA1\u7406\u5458\u8FDB\u884C\u5B89\u88C5",
12
+ needLogin: "\u767B\u5F55\u540E\u624D\u53EF\u64CD\u4F5C"
11
13
  },
12
14
  en: {
13
15
  importResource: "Import",
14
- exportResource: "Export"
16
+ exportResource: "Export",
17
+ noPermission: "You do not have permission to perform this operation. Please switch your passport as admin or contact the administrator to install the blocklet",
18
+ needLogin: "Please login first"
15
19
  }
16
20
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/ui-react",
3
- "version": "2.9.87",
3
+ "version": "2.9.88",
4
4
  "description": "Some useful front-end web components that can be used in Blocklets.",
5
5
  "keywords": [
6
6
  "react",
@@ -62,8 +62,8 @@
62
62
  }
63
63
  },
64
64
  "dependencies": {
65
- "@arcblock/bridge": "^2.9.87",
66
- "@arcblock/react-hooks": "^2.9.87",
65
+ "@arcblock/bridge": "^2.9.88",
66
+ "@arcblock/react-hooks": "^2.9.88",
67
67
  "@blocklet/js-sdk": "1.16.27-beta-c450492a",
68
68
  "@iconify-icons/logos": "^1.2.36",
69
69
  "@iconify-icons/material-symbols": "^1.2.58",
@@ -108,5 +108,5 @@
108
108
  "jest": "^28.1.3",
109
109
  "unbuild": "^2.0.0"
110
110
  },
111
- "gitHead": "2c766afe9708ba370cb35fcec6e0d5c767d48c51"
111
+ "gitHead": "4b25a950bb1e6486a5e955385e727a675b44a3a7"
112
112
  }
@@ -1,35 +1,46 @@
1
- import { useEffect, ReactElement } from 'react';
1
+ import { useEffect, ReactElement, useRef, useContext } from 'react';
2
2
  import Button from '@arcblock/ux/lib/Button';
3
3
  import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
4
4
  import { WELLKNOWN_SERVICE_PATH_PREFIX } from '@arcblock/ux/lib/Util/constant';
5
5
  import { useMemoizedFn, useReactive } from 'ahooks';
6
6
  import { translate } from '@arcblock/ux/lib/Locale/util';
7
7
  import { joinURL, withQuery } from 'ufo';
8
+ import { SessionContext } from '@arcblock/did-connect/lib/Session';
9
+ import Toast from '@arcblock/ux/lib/Toast';
8
10
 
9
11
  import ResourceDialog from './resource-dialog';
10
12
  import { translations } from '../libs/locales';
13
+ import type { SessionContext as TSessionContext } from '../../@types';
11
14
 
12
15
  export default function AddComponent({
13
16
  componentDid,
14
17
  resourceType,
15
18
  resourceDid,
19
+ grantedRoles = ['admin', 'owner'],
16
20
  autoClose = true,
21
+ selectedMeta,
22
+ storeUrl,
17
23
  onComplete,
18
24
  onClose,
19
25
  render,
20
26
  }: {
21
27
  componentDid: string;
22
- resourceType: string;
23
- resourceDid: string;
28
+ resourceType?: string;
29
+ resourceDid?: string;
24
30
  autoClose: boolean;
31
+ selectedMeta?: any;
32
+ storeUrl?: string;
33
+ grantedRoles?: string[];
25
34
  onComplete: () => void;
26
35
  onClose: () => void;
27
36
  render?: ({ onClick, loading }: { onClick: () => void; loading: boolean }) => ReactElement;
28
37
  }) {
38
+ const { session } = useContext<TSessionContext>(SessionContext);
29
39
  const { locale } = useLocaleContext();
30
40
  const t = useMemoizedFn((key, data = {}) => {
31
41
  return translate(translations, key, locale, 'en', data);
32
42
  });
43
+ const refIframe = useRef<{ selectBlocklet: (meta: any) => void }>(null);
33
44
 
34
45
  const currentState = useReactive({
35
46
  loading: false,
@@ -54,11 +65,22 @@ export default function AddComponent({
54
65
  onComplete?.();
55
66
  });
56
67
  const handleOpen = useMemoizedFn(() => {
68
+ if (!session.user) {
69
+ Toast.warning(t('needLogin'));
70
+ return;
71
+ }
72
+ if (!grantedRoles.includes(session.user?.role as string)) {
73
+ Toast.warning(t('noPermission'));
74
+ return;
75
+ }
57
76
  currentState.loading = true;
58
77
  currentState.showDialog = true;
59
78
  });
60
79
  const handleLoad = useMemoizedFn(() => {
61
80
  currentState.loading = false;
81
+ if (refIframe.current) {
82
+ refIframe.current?.selectBlocklet(selectedMeta);
83
+ }
62
84
  });
63
85
 
64
86
  const handleIframeMessage = useMemoizedFn((event: MessageEvent) => {
@@ -98,8 +120,11 @@ export default function AddComponent({
98
120
 
99
121
  {currentState.showDialog && (
100
122
  <ResourceDialog
123
+ ref={refIframe}
101
124
  src={importUrl}
125
+ storeUrl={storeUrl}
102
126
  open
127
+ componentDid={componentDid}
103
128
  loading={currentState.loading}
104
129
  onClose={handleClose}
105
130
  onComplete={handleComplete}
@@ -1,22 +1,28 @@
1
- import { ReactElement } from 'react';
1
+ import { ReactElement, useContext } from 'react';
2
2
  import Button from '@arcblock/ux/lib/Button';
3
3
  import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
4
4
  import { WELLKNOWN_SERVICE_PATH_PREFIX } from '@arcblock/ux/lib/Util/constant';
5
5
  import { useMemoizedFn, useReactive } from 'ahooks';
6
6
  import { translate } from '@arcblock/ux/lib/Locale/util';
7
+ import { SessionContext } from '@arcblock/did-connect/lib/Session';
8
+ import Toast from '@arcblock/ux/lib/Toast';
7
9
 
10
+ import type { SessionContext as TSessionContext } from '../../@types';
8
11
  import ResourceDialog from './resource-dialog';
9
12
  import { translations } from '../libs/locales';
10
13
 
11
14
  export default function PublishComponent({
12
15
  componentDid,
16
+ grantedRoles,
13
17
  onClose,
14
18
  render,
15
19
  }: {
16
- componentDid: String;
20
+ componentDid: string;
21
+ grantedRoles?: string[];
17
22
  onClose: () => void;
18
23
  render?: ({ onClick, loading }: { onClick: () => void; loading: boolean }) => ReactElement;
19
24
  }) {
25
+ const { session } = useContext<TSessionContext>(SessionContext);
20
26
  const { locale } = useLocaleContext();
21
27
  const t = useMemoizedFn((key, data = {}) => {
22
28
  return translate(translations, key, locale, 'en', data);
@@ -36,6 +42,17 @@ export default function PublishComponent({
36
42
  currentState.loading = false;
37
43
  });
38
44
  const handleOpen = useMemoizedFn(() => {
45
+ if (!session.user) {
46
+ Toast.warning(t('needLogin'));
47
+ return;
48
+ }
49
+
50
+ if (grantedRoles && grantedRoles.length > 0) {
51
+ if (!grantedRoles?.includes(session.user?.role)) {
52
+ Toast.warning(t('noPermission'));
53
+ return;
54
+ }
55
+ }
39
56
  currentState.loading = true;
40
57
  currentState.showDialog = true;
41
58
  });
@@ -59,7 +76,14 @@ export default function PublishComponent({
59
76
  )}
60
77
 
61
78
  {currentState.showDialog && (
62
- <ResourceDialog src={exportUrl} open onClose={handleClose} onLoad={handleLoad} loading={currentState.loading} />
79
+ <ResourceDialog
80
+ src={exportUrl}
81
+ open
82
+ onClose={handleClose}
83
+ onLoad={handleLoad}
84
+ componentDid={componentDid}
85
+ loading={currentState.loading}
86
+ />
63
87
  )}
64
88
  </>
65
89
  );
@@ -1,22 +1,31 @@
1
1
  import { useMemoizedFn, useMount, useUnmount } from 'ahooks';
2
- import { useRef } from 'react';
2
+ import { useRef, forwardRef, useImperativeHandle, Ref } from 'react';
3
3
 
4
- export default function ResourceDialog({
5
- src,
6
- open = false,
7
- loading = true,
8
- onClose = () => {},
9
- onComplete = () => {},
10
- onLoad = () => {},
11
- }: {
12
- src: string;
13
- open?: boolean;
14
- loading?: boolean;
15
- onLoad?: () => void;
16
- onClose?: () => void;
17
- onComplete?: () => void;
18
- }) {
19
- const iframeRef = useRef(null);
4
+ function ResourceDialog(
5
+ {
6
+ src,
7
+ open = false,
8
+ componentDid,
9
+ loading = true,
10
+ storeUrl,
11
+ onClose = () => {},
12
+ onComplete = () => {},
13
+ onLoad = () => {},
14
+ }: {
15
+ src: string;
16
+ componentDid: string;
17
+ open?: boolean;
18
+ loading?: boolean;
19
+ storeUrl?: string;
20
+ onLoad?: () => void;
21
+ onClose?: () => void;
22
+ onComplete?: () => void;
23
+ },
24
+ ref: Ref<{
25
+ selectBlocklet: (blockletMeta: any) => void;
26
+ }>
27
+ ) {
28
+ const iframeRef = useRef<HTMLIFrameElement>(null);
20
29
 
21
30
  const listener = useMemoizedFn((event: MessageEvent) => {
22
31
  if (open) {
@@ -40,6 +49,22 @@ export default function ResourceDialog({
40
49
  window.removeEventListener('message', listener);
41
50
  });
42
51
 
52
+ useImperativeHandle(ref, () => ({
53
+ selectBlocklet: (blockletMeta: any) => {
54
+ if (iframeRef.current?.contentWindow) {
55
+ iframeRef.current.contentWindow.postMessage(
56
+ {
57
+ event: 'resourceDialog.select',
58
+ data: blockletMeta,
59
+ componentDid,
60
+ storeUrl,
61
+ },
62
+ '*'
63
+ );
64
+ }
65
+ },
66
+ }));
67
+
43
68
  if (!open) {
44
69
  return null;
45
70
  }
@@ -62,3 +87,5 @@ export default function ResourceDialog({
62
87
  />
63
88
  );
64
89
  }
90
+
91
+ export default forwardRef(ResourceDialog);
@@ -2,9 +2,14 @@ export const translations = {
2
2
  zh: {
3
3
  importResource: '导入',
4
4
  exportResource: '导出',
5
+ noPermission: '你没有权限执行该操作,请切换管理员通行证或联系管理员进行安装',
6
+ needLogin: '登录后才可操作',
5
7
  },
6
8
  en: {
7
9
  importResource: 'Import',
8
10
  exportResource: 'Export',
11
+ noPermission:
12
+ 'You do not have permission to perform this operation. Please switch your passport as admin or contact the administrator to install the blocklet',
13
+ needLogin: 'Please login first',
9
14
  },
10
15
  };