UNPKG

@atlaskit/editor-plugin-extension

Version:

editor-plugin-extension plugin for @atlaskit/editor-core

136 lines 6.2 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _regeneratorRuntime from "@babel/runtime/regenerator"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React from 'react'; import { injectIntl } from 'react-intl'; import { withAnalyticsContext, withAnalyticsEvents } from '@atlaskit/analytics-next'; import { ACTION, ACTION_SUBJECT, editorAnalyticsChannel, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { configPanelMessages as messages } from '@atlaskit/editor-common/extensions'; import { Stack, Text } from '@atlaskit/primitives/compiled'; import SectionMessage from '@atlaskit/section-message'; // eslint-disable-next-line @repo/internal/react/no-class-components var FormErrorBoundaryInner = /*#__PURE__*/function (_React$Component) { function FormErrorBoundaryInner() { var _this; _classCallCheck(this, FormErrorBoundaryInner); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, FormErrorBoundaryInner, [].concat(args)); // eslint-disable-next-line @typescript-eslint/no-explicit-any _defineProperty(_this, "state", { error: undefined }); _defineProperty(_this, "getProductName", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var contextIdentifierProvider, context; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: contextIdentifierProvider = _this.props.contextIdentifierProvider; if (!contextIdentifierProvider) { _context.next = 7; break; } _context.next = 4; return contextIdentifierProvider; case 4: context = _context.sent; if (!context.product) { _context.next = 7; break; } return _context.abrupt("return", context.product); case 7: return _context.abrupt("return", 'atlaskit'); case 8: case "end": return _context.stop(); } }, _callee); }))); _defineProperty(_this, "fireAnalytics", function (analyticsErrorPayload) { var _this$props = _this.props, createAnalyticsEvent = _this$props.createAnalyticsEvent, extensionKey = _this$props.extensionKey, fields = _this$props.fields; _this.getProductName().then(function (product) { var _window; if (!createAnalyticsEvent) { // eslint-disable-next-line no-console console.error('ConfigPanel FormErrorBoundary: Missing `createAnalyticsEvent`', { channel: editorAnalyticsChannel, product: product, error: analyticsErrorPayload }); return; } var error = analyticsErrorPayload.error, errorInfo = analyticsErrorPayload.errorInfo, errorStack = analyticsErrorPayload.errorStack; var payload = { action: ACTION.ERRORED, actionSubject: ACTION_SUBJECT.CONFIG_PANEL, eventType: EVENT_TYPE.UI, attributes: { product: product, browserInfo: ((_window = window) === null || _window === void 0 || (_window = _window.navigator) === null || _window === void 0 ? void 0 : _window.userAgent) || 'unknown', extensionKey: extensionKey, fields: JSON.stringify(fields), error: error, errorInfo: errorInfo, errorStack: errorStack } }; createAnalyticsEvent(payload).fire(editorAnalyticsChannel); }).catch(function (e) { // eslint-disable-next-line no-console console.error('Failed to resolve product name from contextIdentifierProvider.', e); }); }); return _this; } _inherits(FormErrorBoundaryInner, _React$Component); return _createClass(FormErrorBoundaryInner, [{ key: "componentDidCatch", value: function componentDidCatch(error, errorInfo) { var _this2 = this; this.setState({ error: error }, function () { // Log the error _this2.fireAnalytics({ error: error.toString(), errorInfo: errorInfo, errorStack: error.stack }); }); } }, { key: "render", value: function render() { var intl = this.props.intl; var error = this.state.error; if (!error) { return this.props.children; } return /*#__PURE__*/React.createElement(SectionMessage, { title: intl.formatMessage(messages.errorBoundaryTitle), appearance: "error" }, /*#__PURE__*/React.createElement(Stack, null, /*#__PURE__*/React.createElement(Text, { as: "p" }, /*#__PURE__*/React.createElement("i", null, error.message)), /*#__PURE__*/React.createElement(Text, { as: "p" }, intl.formatMessage(messages.errorBoundaryNote)))); } }]); }(React.Component); // eslint-disable-next-line @typescript-eslint/ban-types export var FormErrorBoundaryImpl = injectIntl(FormErrorBoundaryInner); export var FormErrorBoundary = withAnalyticsContext()(withAnalyticsEvents()(FormErrorBoundaryImpl)); FormErrorBoundary.displayName = 'FormErrorBoundary';