@atlaskit/link-datasource 1.15.2 → 1.15.4

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/cjs/analytics/constants.js +1 -1
  3. package/dist/cjs/hooks/useAssetsClient.js +71 -27
  4. package/dist/cjs/services/cmdbService.js +128 -28
  5. package/dist/cjs/services/cmdbService.utils.js +64 -0
  6. package/dist/cjs/ui/assets-modal/modal/index.js +73 -9
  7. package/dist/cjs/ui/assets-modal/modal/render-assets-content/index.js +25 -12
  8. package/dist/cjs/ui/assets-modal/search-container/index.js +2 -1
  9. package/dist/cjs/ui/assets-modal/search-container/object-schema-select/index.js +21 -63
  10. package/dist/cjs/ui/issue-like-table/render-type/date-time/index.js +7 -1
  11. package/dist/es2019/analytics/constants.js +1 -1
  12. package/dist/es2019/hooks/useAssetsClient.js +39 -15
  13. package/dist/es2019/services/cmdbService.js +60 -14
  14. package/dist/es2019/services/cmdbService.utils.js +39 -0
  15. package/dist/es2019/ui/assets-modal/modal/index.js +70 -9
  16. package/dist/es2019/ui/assets-modal/modal/render-assets-content/index.js +24 -9
  17. package/dist/es2019/ui/assets-modal/search-container/index.js +2 -1
  18. package/dist/es2019/ui/assets-modal/search-container/object-schema-select/index.js +2 -25
  19. package/dist/es2019/ui/issue-like-table/render-type/date-time/index.js +7 -1
  20. package/dist/esm/analytics/constants.js +1 -1
  21. package/dist/esm/hooks/useAssetsClient.js +72 -28
  22. package/dist/esm/services/cmdbService.js +128 -28
  23. package/dist/esm/services/cmdbService.utils.js +57 -0
  24. package/dist/esm/ui/assets-modal/modal/index.js +73 -9
  25. package/dist/esm/ui/assets-modal/modal/render-assets-content/index.js +24 -9
  26. package/dist/esm/ui/assets-modal/search-container/index.js +2 -1
  27. package/dist/esm/ui/assets-modal/search-container/object-schema-select/index.js +18 -60
  28. package/dist/esm/ui/issue-like-table/render-type/date-time/index.js +7 -1
  29. package/dist/types/hooks/useAssetsClient.d.ts +5 -1
  30. package/dist/types/services/cmdbService.utils.d.ts +9 -0
  31. package/dist/types/ui/assets-modal/modal/render-assets-content/index.d.ts +3 -2
  32. package/dist/types/ui/assets-modal/search-container/index.d.ts +1 -0
  33. package/dist/types/ui/assets-modal/search-container/object-schema-select/index.d.ts +2 -1
  34. package/dist/types-ts4.5/hooks/useAssetsClient.d.ts +5 -1
  35. package/dist/types-ts4.5/services/cmdbService.utils.d.ts +9 -0
  36. package/dist/types-ts4.5/ui/assets-modal/modal/render-assets-content/index.d.ts +3 -2
  37. package/dist/types-ts4.5/ui/assets-modal/search-container/index.d.ts +1 -0
  38. package/dist/types-ts4.5/ui/assets-modal/search-container/object-schema-select/index.d.ts +2 -1
  39. package/package.json +1 -1
@@ -1,14 +1,12 @@
1
1
  import _extends from "@babel/runtime/helpers/extends";
2
2
  /** @jsx jsx */
3
3
 
4
- import { useEffect, useState } from 'react';
5
4
  import { jsx } from '@emotion/react';
6
5
  import debounce from 'debounce-promise';
7
6
  import { useIntl } from 'react-intl-next';
8
7
  import { Field } from '@atlaskit/form';
9
8
  import { AsyncSelect } from '@atlaskit/select';
10
9
  import { layers } from '@atlaskit/theme/constants';
11
- import { useDatasourceAnalyticsEvents } from '../../../../analytics';
12
10
  import { useObjectSchemas } from '../../../../hooks/useObjectSchemas';
13
11
  import { objectSchemaKey } from '../../../../types/assets/types';
14
12
  import { FieldContainer } from '../styled';
@@ -35,12 +33,9 @@ export const selectInAModalStyleFixProps = {
35
33
  export const AssetsObjectSchemaSelect = ({
36
34
  value,
37
35
  workspaceId,
36
+ initialObjectSchemas,
38
37
  classNamePrefix = 'assets-datasource-modal--object-schema-select'
39
38
  }) => {
40
- const [defaultOptions, setDefaultOptions] = useState(null);
41
- const {
42
- fireEvent
43
- } = useDatasourceAnalyticsEvents();
44
39
  const {
45
40
  formatMessage
46
41
  } = useIntl();
@@ -49,24 +44,6 @@ export const AssetsObjectSchemaSelect = ({
49
44
  objectSchemasLoading
50
45
  } = useObjectSchemas(workspaceId);
51
46
  const selectedObjectSchema = value ? objectSchemaToSelectOption(value) : undefined;
52
- useEffect(() => {
53
- const fetchInitialData = async () => {
54
- const {
55
- objectSchemas,
56
- totalObjectSchemas
57
- } = await fetchObjectSchemas('');
58
- // We only want to send modal ready event once after we've fetched the schema count
59
- fireEvent('ui.modal.ready.datasource', {
60
- schemasCount: totalObjectSchemas !== null && totalObjectSchemas !== void 0 ? totalObjectSchemas : 0,
61
- instancesCount: null
62
- });
63
- setDefaultOptions(mapObjectSchemasToOptions(objectSchemas));
64
- };
65
- if (defaultOptions === null) {
66
- fetchInitialData();
67
- }
68
- // eslint-disable-next-line react-hooks/exhaustive-deps
69
- }, []);
70
47
  const loadOptions = async inputValue => {
71
48
  const {
72
49
  objectSchemas
@@ -94,7 +71,7 @@ export const AssetsObjectSchemaSelect = ({
94
71
  autoFocus: true,
95
72
  classNamePrefix: classNamePrefix,
96
73
  isLoading: objectSchemasLoading,
97
- defaultOptions: defaultOptions !== null && defaultOptions !== void 0 ? defaultOptions : [],
74
+ defaultOptions: mapObjectSchemasToOptions(initialObjectSchemas),
98
75
  isSearchable: true,
99
76
  loadOptions: debouncedLoadOptions,
100
77
  placeholder: formatMessage(objectSchemaSelectMessages.placeholder),
@@ -21,8 +21,14 @@ const DateTimeRenderType = ({
21
21
  testId = DATETIME_TYPE_TEST_ID,
22
22
  display = 'datetime'
23
23
  }) => {
24
- const date = new Date(value);
25
24
  const intl = useIntl();
25
+ /* In some cases we get a value of `2023-12-20` which when parsed by JS assumes meantime timezone, causing the date
26
+ to be one day off in some timezones. We want it to display the date without converting timezones and a solution
27
+ is to replace the hyphens with slashes. So it should be 20th Dec regardless of the timezone in this case.
28
+ See https://stackoverflow.com/a/31732581
29
+ */
30
+ const dateValue = /^\d{4}-\d{2}-\d{2}$/.exec(value) ? value.replace(/-/g, '/') : value;
31
+ const date = new Date(dateValue);
26
32
  if (!value || isNaN(date.getTime())) {
27
33
  return /*#__PURE__*/React.createElement(React.Fragment, null);
28
34
  }
@@ -1,5 +1,5 @@
1
1
  export var EVENT_CHANNEL = 'media';
2
2
  export var packageMetaData = {
3
3
  packageName: "@atlaskit/link-datasource",
4
- packageVersion: "1.15.2"
4
+ packageVersion: "1.15.4"
5
5
  };
@@ -2,7 +2,14 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
3
  import _regeneratorRuntime from "@babel/runtime/regenerator";
4
4
  import { useEffect, useState } from 'react';
5
- import { fetchObjectSchema, getWorkspaceId } from '../services/cmdbService';
5
+ import { fetchObjectSchema, fetchObjectSchemas, getWorkspaceId } from '../services/cmdbService';
6
+ var handleAssetsClientErrors = function handleAssetsClientErrors(errorSetter, error) {
7
+ if (error instanceof Error) {
8
+ errorSetter(error);
9
+ } else {
10
+ errorSetter(new Error('Unexpected error occured'));
11
+ }
12
+ };
6
13
  export var useAssetsClient = function useAssetsClient(initialParameters) {
7
14
  var _useState = useState(false),
8
15
  _useState2 = _slicedToArray(_useState, 2),
@@ -14,29 +21,51 @@ export var useAssetsClient = function useAssetsClient(initialParameters) {
14
21
  setWorkspaceId = _useState4[1];
15
22
  var _useState5 = useState(),
16
23
  _useState6 = _slicedToArray(_useState5, 2),
17
- objectSchema = _useState6[0],
18
- setObjectSchema = _useState6[1];
24
+ workspaceError = _useState6[0],
25
+ setWorkspaceError = _useState6[1];
19
26
  var _useState7 = useState(),
20
27
  _useState8 = _slicedToArray(_useState7, 2),
21
- error = _useState8[0],
22
- setError = _useState8[1];
28
+ existingObjectSchema = _useState8[0],
29
+ setExistingObjectSchema = _useState8[1];
30
+ var _useState9 = useState(),
31
+ _useState10 = _slicedToArray(_useState9, 2),
32
+ existingObjectSchemaError = _useState10[0],
33
+ setExistingObjectSchemaError = _useState10[1];
34
+ var _useState11 = useState(),
35
+ _useState12 = _slicedToArray(_useState11, 2),
36
+ objectSchemas = _useState12[0],
37
+ setObjectSchemas = _useState12[1];
38
+ var _useState13 = useState(),
39
+ _useState14 = _slicedToArray(_useState13, 2),
40
+ totalObjectSchemas = _useState14[0],
41
+ setTotalObjectSchemas = _useState14[1];
42
+ var _useState15 = useState(),
43
+ _useState16 = _slicedToArray(_useState15, 2),
44
+ objectSchemasError = _useState16[0],
45
+ setObjectSchemasError = _useState16[1];
46
+
47
+ /*
48
+ * We wrap this in nested try/catch blocks because we want to handle
49
+ * workspaceError/existingObjectSchemaError/objectSchemasError differently
50
+ * if we need to implement more initial data fetching/errors we should look at a store
51
+ */
23
52
  useEffect(function () {
24
53
  _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
25
- var _workspaceId, fetchedObjectSchema;
54
+ var _workspaceId, fetchedObjectSchema, fetchedObjectSchemasResponse;
26
55
  return _regeneratorRuntime.wrap(function _callee$(_context) {
27
56
  while (1) switch (_context.prev = _context.next) {
28
57
  case 0:
29
58
  setLoading(true);
30
- setError(undefined);
59
+ setWorkspaceError(undefined);
31
60
  _context.prev = 2;
32
61
  _context.next = 5;
33
62
  return getWorkspaceId();
34
63
  case 5:
35
64
  _workspaceId = _context.sent;
36
65
  setWorkspaceId(_workspaceId);
37
- // Check schema from initial parameters still exists and fetch name for schema select
66
+ // Check schema from initial parameters still exists and fetch name/permissions for schema select
38
67
  if (!(initialParameters !== null && initialParameters !== void 0 && initialParameters.schemaId)) {
39
- _context.next = 17;
68
+ _context.next = 18;
40
69
  break;
41
70
  }
42
71
  _context.prev = 8;
@@ -44,38 +73,53 @@ export var useAssetsClient = function useAssetsClient(initialParameters) {
44
73
  return fetchObjectSchema(_workspaceId, initialParameters === null || initialParameters === void 0 ? void 0 : initialParameters.schemaId);
45
74
  case 11:
46
75
  fetchedObjectSchema = _context.sent;
47
- setObjectSchema(fetchedObjectSchema);
48
- _context.next = 17;
76
+ setExistingObjectSchema(fetchedObjectSchema);
77
+ _context.next = 18;
49
78
  break;
50
79
  case 15:
51
80
  _context.prev = 15;
52
81
  _context.t0 = _context["catch"](8);
53
- case 17:
54
- _context.next = 22;
82
+ handleAssetsClientErrors(setExistingObjectSchemaError, _context.t0);
83
+ case 18:
84
+ _context.prev = 18;
85
+ _context.next = 21;
86
+ return fetchObjectSchemas(_workspaceId);
87
+ case 21:
88
+ fetchedObjectSchemasResponse = _context.sent;
89
+ setObjectSchemas(fetchedObjectSchemasResponse.values);
90
+ setTotalObjectSchemas(fetchedObjectSchemasResponse.total);
91
+ _context.next = 29;
55
92
  break;
56
- case 19:
57
- _context.prev = 19;
58
- _context.t1 = _context["catch"](2);
59
- if (_context.t1 instanceof Error) {
60
- setError(_context.t1);
61
- } else {
62
- setError(new Error('Unexpected error occured'));
63
- }
64
- case 22:
65
- _context.prev = 22;
93
+ case 26:
94
+ _context.prev = 26;
95
+ _context.t1 = _context["catch"](18);
96
+ handleAssetsClientErrors(setObjectSchemasError, _context.t1);
97
+ case 29:
98
+ _context.next = 34;
99
+ break;
100
+ case 31:
101
+ _context.prev = 31;
102
+ _context.t2 = _context["catch"](2);
103
+ handleAssetsClientErrors(setWorkspaceError, _context.t2);
104
+ case 34:
105
+ _context.prev = 34;
66
106
  setLoading(false);
67
- return _context.finish(22);
68
- case 25:
107
+ return _context.finish(34);
108
+ case 37:
69
109
  case "end":
70
110
  return _context.stop();
71
111
  }
72
- }, _callee, null, [[2, 19, 22, 25], [8, 15]]);
112
+ }, _callee, null, [[2, 31, 34, 37], [8, 15], [18, 26]]);
73
113
  }))();
74
114
  }, [initialParameters]);
75
115
  return {
76
116
  workspaceId: workspaceId,
77
- workspaceError: error,
78
- objectSchema: objectSchema,
117
+ workspaceError: workspaceError,
118
+ existingObjectSchema: existingObjectSchema,
119
+ existingObjectSchemaError: existingObjectSchemaError,
120
+ objectSchemas: objectSchemas,
121
+ totalObjectSchemas: totalObjectSchemas,
122
+ objectSchemasError: objectSchemasError,
79
123
  assetsClientLoading: loading
80
124
  };
81
125
  };
@@ -1,51 +1,151 @@
1
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
3
  import { request } from '@atlaskit/linking-common';
4
+ import { FetchError, getStatusCodeGroup, mapFetchErrors, PermissionError } from './cmdbService.utils';
4
5
  export var getWorkspaceId = /*#__PURE__*/function () {
5
6
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
6
- var _workspaceDetailsResp;
7
- var url, workspaceDetailsResponse;
7
+ var url, _workspaceDetailsResp, workspaceDetailsResponse, error;
8
8
  return _regeneratorRuntime.wrap(function _callee$(_context) {
9
9
  while (1) switch (_context.prev = _context.next) {
10
10
  case 0:
11
11
  url = '/rest/servicedesk/cmdb/latest/workspace';
12
- _context.next = 3;
12
+ _context.prev = 1;
13
+ _context.next = 4;
13
14
  return request('get', url, undefined, undefined, [200, 201, 202, 203, 204]);
14
- case 3:
15
+ case 4:
15
16
  workspaceDetailsResponse = _context.sent;
16
17
  if ((_workspaceDetailsResp = workspaceDetailsResponse.results) !== null && _workspaceDetailsResp !== void 0 && _workspaceDetailsResp.length) {
17
- _context.next = 6;
18
+ _context.next = 7;
18
19
  break;
19
20
  }
20
- throw new Error('No workspace results found');
21
- case 6:
22
- return _context.abrupt("return", workspaceDetailsResponse.results[0].id);
21
+ throw new PermissionError('No workspace results found');
23
22
  case 7:
23
+ return _context.abrupt("return", workspaceDetailsResponse.results[0].id);
24
+ case 10:
25
+ _context.prev = 10;
26
+ _context.t0 = _context["catch"](1);
27
+ error = mapFetchErrors(_context.t0);
28
+ if (error instanceof FetchError) {
29
+ // TODO Fire error operational event for workspace here before remapping to PermissionError
30
+ // Only 429 and 5xx errors will be treated as FetchErrors otherwise PermissionError
31
+ if (getStatusCodeGroup(error) !== '5xx' && error.statusCode !== 429) {
32
+ error = new PermissionError('Failed to fetch workspace');
33
+ }
34
+ }
35
+ throw error;
36
+ case 15:
24
37
  case "end":
25
38
  return _context.stop();
26
39
  }
27
- }, _callee);
40
+ }, _callee, null, [[1, 10]]);
28
41
  }));
29
42
  return function getWorkspaceId() {
30
43
  return _ref.apply(this, arguments);
31
44
  };
32
45
  }();
33
- export var validateAql = function validateAql(workspaceId, data) {
34
- var url = "/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/aql/validate");
35
- return request('post', url, {
36
- qlQuery: data.qlQuery,
37
- context: 'SMART_LINKS'
38
- }, undefined, [200, 201, 202, 203, 204]);
39
- };
40
- export var fetchObjectSchema = function fetchObjectSchema(workspaceId, schemaId) {
41
- var url = "/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/objectschema/").concat(schemaId);
42
- return request('get', url, undefined, undefined, [200, 201, 202, 203, 204]);
43
- };
44
- export var fetchObjectSchemas = function fetchObjectSchemas(workspaceId, query) {
45
- var queryParams = new URLSearchParams();
46
- queryParams.set('maxResults', '20');
47
- queryParams.set('includeCounts', 'false');
48
- query && queryParams.set('query', query);
49
- var url = "/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/objectschema/list?").concat(queryParams);
50
- return request('get', url, undefined, undefined, [200, 201, 202, 203, 204]);
51
- };
46
+ export var validateAql = /*#__PURE__*/function () {
47
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(workspaceId, data) {
48
+ var url, error;
49
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
50
+ while (1) switch (_context2.prev = _context2.next) {
51
+ case 0:
52
+ url = "/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/aql/validate");
53
+ _context2.prev = 1;
54
+ _context2.next = 4;
55
+ return request('post', url, {
56
+ qlQuery: data.qlQuery,
57
+ context: 'SMART_LINKS'
58
+ }, undefined, [200, 201, 202, 203, 204]);
59
+ case 4:
60
+ return _context2.abrupt("return", _context2.sent);
61
+ case 7:
62
+ _context2.prev = 7;
63
+ _context2.t0 = _context2["catch"](1);
64
+ error = mapFetchErrors(_context2.t0);
65
+ if (error instanceof FetchError) {
66
+ // TODO Fire error operational event for aql here before remapping to PermissionError
67
+ if (error.statusCode === 401 || error.statusCode === 403) {
68
+ error = new PermissionError('Failed to fetch object schemas');
69
+ }
70
+ }
71
+ throw error;
72
+ case 12:
73
+ case "end":
74
+ return _context2.stop();
75
+ }
76
+ }, _callee2, null, [[1, 7]]);
77
+ }));
78
+ return function validateAql(_x, _x2) {
79
+ return _ref2.apply(this, arguments);
80
+ };
81
+ }();
82
+ export var fetchObjectSchema = /*#__PURE__*/function () {
83
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(workspaceId, schemaId) {
84
+ var url, error;
85
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
86
+ while (1) switch (_context3.prev = _context3.next) {
87
+ case 0:
88
+ url = "/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/objectschema/").concat(schemaId);
89
+ _context3.prev = 1;
90
+ _context3.next = 4;
91
+ return request('get', url, undefined, undefined, [200, 201, 202, 203, 204]);
92
+ case 4:
93
+ return _context3.abrupt("return", _context3.sent);
94
+ case 7:
95
+ _context3.prev = 7;
96
+ _context3.t0 = _context3["catch"](1);
97
+ error = mapFetchErrors(_context3.t0);
98
+ if (error instanceof FetchError) {
99
+ // TODO Fire error operational event for object schema here before remapping to PermissionError
100
+ if (error.statusCode === 401 || error.statusCode === 403) {
101
+ error = new PermissionError('Failed to fetch object schemas');
102
+ }
103
+ }
104
+ throw error;
105
+ case 12:
106
+ case "end":
107
+ return _context3.stop();
108
+ }
109
+ }, _callee3, null, [[1, 7]]);
110
+ }));
111
+ return function fetchObjectSchema(_x3, _x4) {
112
+ return _ref3.apply(this, arguments);
113
+ };
114
+ }();
115
+ export var fetchObjectSchemas = /*#__PURE__*/function () {
116
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(workspaceId, query) {
117
+ var queryParams, url, error;
118
+ return _regeneratorRuntime.wrap(function _callee4$(_context4) {
119
+ while (1) switch (_context4.prev = _context4.next) {
120
+ case 0:
121
+ queryParams = new URLSearchParams();
122
+ queryParams.set('maxResults', '20');
123
+ queryParams.set('includeCounts', 'false');
124
+ query && queryParams.set('query', query);
125
+ url = "/gateway/api/jsm/assets/workspace/".concat(workspaceId, "/v1/objectschema/list?").concat(queryParams);
126
+ _context4.prev = 5;
127
+ _context4.next = 8;
128
+ return request('get', url, undefined, undefined, [200, 201, 202, 203, 204]);
129
+ case 8:
130
+ return _context4.abrupt("return", _context4.sent);
131
+ case 11:
132
+ _context4.prev = 11;
133
+ _context4.t0 = _context4["catch"](5);
134
+ error = mapFetchErrors(_context4.t0);
135
+ if (error instanceof FetchError) {
136
+ // TODO Fire error operational event for object schemas here before remapping to PermissionError
137
+ if (error.statusCode === 401 || error.statusCode === 403) {
138
+ error = new PermissionError('Failed to fetch object schemas');
139
+ }
140
+ }
141
+ throw error;
142
+ case 16:
143
+ case "end":
144
+ return _context4.stop();
145
+ }
146
+ }, _callee4, null, [[5, 11]]);
147
+ }));
148
+ return function fetchObjectSchemas(_x5, _x6) {
149
+ return _ref4.apply(this, arguments);
150
+ };
151
+ }();
@@ -0,0 +1,57 @@
1
+ import _createClass from "@babel/runtime/helpers/createClass";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _inherits from "@babel/runtime/helpers/inherits";
4
+ import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
5
+ import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
6
+ import _wrapNativeSuper from "@babel/runtime/helpers/wrapNativeSuper";
7
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
8
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
9
+ export var mapFetchErrors = function mapFetchErrors(error) {
10
+ if (error instanceof Response && !error.ok) {
11
+ return new FetchError(error.status, "Error server response: ".concat(error.status));
12
+ }
13
+ return error;
14
+ };
15
+ export var getStatusCodeGroup = function getStatusCodeGroup(error) {
16
+ if (error instanceof FetchError) {
17
+ var statusCode = error.statusCode;
18
+ if (statusCode >= 100 && statusCode < 200) {
19
+ return '1xx';
20
+ }
21
+ if (statusCode >= 300 && statusCode < 400) {
22
+ return '3xx';
23
+ }
24
+ if (statusCode >= 400 && statusCode < 500) {
25
+ return '4xx';
26
+ }
27
+ if (statusCode >= 500 && statusCode < 600) {
28
+ return '5xx';
29
+ }
30
+ }
31
+ return 'unknown';
32
+ };
33
+ export var PermissionError = /*#__PURE__*/function (_Error) {
34
+ _inherits(PermissionError, _Error);
35
+ var _super = _createSuper(PermissionError);
36
+ function PermissionError(message) {
37
+ var _this;
38
+ _classCallCheck(this, PermissionError);
39
+ _this = _super.call(this, message);
40
+ _this.name = 'PermissionError';
41
+ return _this;
42
+ }
43
+ return _createClass(PermissionError);
44
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
45
+ export var FetchError = /*#__PURE__*/function (_Error2) {
46
+ _inherits(FetchError, _Error2);
47
+ var _super2 = _createSuper(FetchError);
48
+ function FetchError(statusCode, message) {
49
+ var _this2;
50
+ _classCallCheck(this, FetchError);
51
+ _this2 = _super2.call(this, message || "Fetch call failed with status code: ".concat(statusCode));
52
+ _this2.name = 'FetchError';
53
+ _this2.statusCode = statusCode;
54
+ return _this2;
55
+ }
56
+ return _createClass(FetchError);
57
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
@@ -24,6 +24,8 @@ import { fetchMessagesForLocale } from '../../../common/utils/locale/fetch-messa
24
24
  import { useAssetsClient } from '../../../hooks/useAssetsClient';
25
25
  import { useDatasourceTableState } from '../../../hooks/useDatasourceTableState';
26
26
  import i18nEN from '../../../i18n/en';
27
+ import { PermissionError } from '../../../services/cmdbService.utils';
28
+ import { AccessRequired } from '../../../ui/common/error-state/access-required';
27
29
  import { ModalLoadingError } from '../../common/error-state/modal-loading-error';
28
30
  import { AssetsSearchContainer } from '../search-container';
29
31
  import { AssetsSearchContainerLoading } from '../search-container/loading-state';
@@ -34,6 +36,9 @@ var modalBodyWrapperStyles = css({
34
36
  height: '420px',
35
37
  overflow: 'auto'
36
38
  });
39
+ var modalBodyErrorWrapperStyles = css({
40
+ alignItems: 'center'
41
+ });
37
42
  var AssetsModalTitle = jsx(ModalTitle, null, jsx(FormattedMessage, modalMessages.insertObjectsTitle));
38
43
  var PlainAssetsConfigModal = function PlainAssetsConfigModal(props) {
39
44
  var datasourceId = props.datasourceId,
@@ -57,17 +62,53 @@ var PlainAssetsConfigModal = function PlainAssetsConfigModal(props) {
57
62
  _useState8 = _slicedToArray(_useState7, 2),
58
63
  isNewSearch = _useState8[0],
59
64
  setIsNewSearch = _useState8[1];
65
+ var _useState9 = useState(),
66
+ _useState10 = _slicedToArray(_useState9, 2),
67
+ errorState = _useState10[0],
68
+ setErrorState = _useState10[1];
60
69
  var _useDatasourceAnalyti = useDatasourceAnalyticsEvents(),
61
70
  fireEvent = _useDatasourceAnalyti.fireEvent;
62
71
  var _useRef = useRef(uuidv4()),
63
72
  modalRenderInstanceId = _useRef.current;
64
-
65
- // If a workspaceError occurs this is a critical error
66
73
  var _useAssetsClient = useAssetsClient(initialParameters),
67
74
  workspaceId = _useAssetsClient.workspaceId,
68
75
  workspaceError = _useAssetsClient.workspaceError,
69
- objectSchema = _useAssetsClient.objectSchema,
76
+ existingObjectSchema = _useAssetsClient.existingObjectSchema,
77
+ existingObjectSchemaError = _useAssetsClient.existingObjectSchemaError,
78
+ objectSchemas = _useAssetsClient.objectSchemas,
79
+ objectSchemasError = _useAssetsClient.objectSchemasError,
80
+ totalObjectSchemas = _useAssetsClient.totalObjectSchemas,
70
81
  assetsClientLoading = _useAssetsClient.assetsClientLoading;
82
+
83
+ /* ------------------------------ PERMISSIONS ------------------------------ */
84
+ useEffect(function () {
85
+ if (workspaceError) {
86
+ // If a workspaceError occurs this is a critical error
87
+ if (workspaceError instanceof PermissionError) {
88
+ setErrorState('permission');
89
+ } else {
90
+ setErrorState('network');
91
+ }
92
+ }
93
+ }, [workspaceError]);
94
+ useEffect(function () {
95
+ if (objectSchemasError) {
96
+ // We only care about permission errors for objectSchemas fetching as the user can retry this action
97
+ if (objectSchemasError instanceof PermissionError) {
98
+ setErrorState('permission');
99
+ }
100
+ }
101
+ }, [objectSchemasError]);
102
+ useEffect(function () {
103
+ if (existingObjectSchemaError) {
104
+ // We only care about permission errors for existingObjectSchema fetching as the user can retry this action
105
+ if (existingObjectSchemaError instanceof PermissionError) {
106
+ setErrorState('permission');
107
+ }
108
+ }
109
+ }, [existingObjectSchemaError]);
110
+ /* ------------------------------ END PERMISSIONS ------------------------------ */
111
+
71
112
  var parameters = useMemo(function () {
72
113
  return {
73
114
  aql: aql || '',
@@ -105,6 +146,15 @@ var PlainAssetsConfigModal = function PlainAssetsConfigModal(props) {
105
146
  destinationObjectTypes: destinationObjectTypes
106
147
  };
107
148
  }, [destinationObjectTypes, extensionKey]);
149
+ useEffect(function () {
150
+ // We only want to send modal ready event once after we've fetched the schema count
151
+ if (totalObjectSchemas !== undefined) {
152
+ fireEvent('ui.modal.ready.datasource', {
153
+ schemasCount: totalObjectSchemas,
154
+ instancesCount: null
155
+ });
156
+ }
157
+ }, [fireEvent, totalObjectSchemas]);
108
158
  useEffect(function () {
109
159
  fireEvent('screen.datasourceModalDialog.viewed', {});
110
160
  }, [fireEvent]);
@@ -158,7 +208,7 @@ var PlainAssetsConfigModal = function PlainAssetsConfigModal(props) {
158
208
  setVisibleColumnKeys(defaultVisibleColumnKeys);
159
209
  }
160
210
  }, [defaultVisibleColumnKeys, isNewSearch]);
161
- var isDisabled = !!workspaceError || status !== 'resolved' || assetsClientLoading || !aql || !schemaId;
211
+ var isDisabled = !!errorState || status !== 'resolved' || assetsClientLoading || !aql || !schemaId;
162
212
  var retrieveUrlForSmartCardRender = useCallback(function () {
163
213
  var _data$key;
164
214
  var _responseItems = _slicedToArray(responseItems, 1),
@@ -260,8 +310,20 @@ var PlainAssetsConfigModal = function PlainAssetsConfigModal(props) {
260
310
  return _ref.apply(this, arguments);
261
311
  };
262
312
  }(), [aql, reset, schemaId, status]);
313
+ var renderErrorState = useCallback(function () {
314
+ if (errorState) {
315
+ switch (errorState) {
316
+ case 'permission':
317
+ return jsx(AccessRequired, null);
318
+ case 'network':
319
+ return jsx(ModalLoadingError, null);
320
+ default:
321
+ return jsx(ModalLoadingError, null);
322
+ }
323
+ }
324
+ }, [errorState]);
263
325
  var renderModalTitleContent = useCallback(function () {
264
- if (workspaceError) {
326
+ if (errorState) {
265
327
  return undefined;
266
328
  } else {
267
329
  if (!workspaceId || assetsClientLoading) {
@@ -273,14 +335,15 @@ var PlainAssetsConfigModal = function PlainAssetsConfigModal(props) {
273
335
  workspaceId: workspaceId,
274
336
  initialSearchData: {
275
337
  aql: aql,
276
- objectSchema: objectSchema
338
+ objectSchema: existingObjectSchema,
339
+ objectSchemas: objectSchemas
277
340
  },
278
341
  onSearch: handleOnSearch,
279
342
  modalTitle: AssetsModalTitle,
280
343
  isSearching: status === 'loading'
281
344
  });
282
345
  }
283
- }, [aql, assetsClientLoading, handleOnSearch, objectSchema, status, workspaceError, workspaceId]);
346
+ }, [errorState, workspaceId, assetsClientLoading, aql, existingObjectSchema, objectSchemas, handleOnSearch, status]);
284
347
  return jsx(IntlMessagesProvider, {
285
348
  defaultMessages: i18nEN,
286
349
  loaderFn: fetchMessagesForLocale
@@ -291,8 +354,9 @@ var PlainAssetsConfigModal = function PlainAssetsConfigModal(props) {
291
354
  shouldScrollInViewport: true,
292
355
  shouldCloseOnOverlayClick: false
293
356
  }, jsx(ModalHeader, null, renderModalTitleContent()), jsx(ModalBody, null, jsx("div", {
294
- css: modalBodyWrapperStyles
295
- }, workspaceError ? jsx(ModalLoadingError, null) : jsx(RenderAssetsContent, {
357
+ css: [modalBodyWrapperStyles, errorState && modalBodyErrorWrapperStyles]
358
+ }, errorState ? renderErrorState() : jsx(RenderAssetsContent, {
359
+ isFetchingInitialData: assetsClientLoading,
296
360
  status: status,
297
361
  responseItems: responseItems,
298
362
  visibleColumnKeys: visibleColumnKeys,