@atlaskit/renderer
Version:
Renderer component
95 lines • 2.78 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import { isSafeUrl } from '@atlaskit/adf-schema';
import Link from '@atlaskit/link';
import { LazyLoadedDatasourceRenderFailedAnalyticsWrapper } from '@atlaskit/link-datasource';
import React from 'react';
import { InlineCard } from './';
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components
export class CardErrorBoundary extends React.PureComponent {
constructor(...args) {
super(...args);
_defineProperty(this, "state", {
isError: false,
error: null
});
_defineProperty(this, "onClickFallback", e => {
const {
onClick,
url
} = this.props;
e.preventDefault();
if (onClick) {
onClick(e, url);
}
});
}
static getDerivedStateFromError(error) {
return {
isError: true,
error
};
}
render() {
if (this.state.isError) {
const {
url,
isDatasource,
unsupportedComponent: UnsupportedComponent,
datasourceId,
onSetLinkTarget
} = this.props;
if (url) {
let actualTarget;
if (onSetLinkTarget) {
try {
actualTarget = onSetLinkTarget(url);
} catch {
// If URL parsing fails, use the original target
}
}
const linkProps = {
href: url,
onClick: this.onClickFallback,
...(actualTarget === '_blank' && {
target: '_blank',
rel: 'noreferrer noopener'
})
};
const fallback = /*#__PURE__*/React.createElement(Link, {
href: linkProps.href,
onClick: linkProps.onClick,
target: linkProps.target,
rel: linkProps.rel
}, url);
if (isDatasource) {
if (isSafeUrl(url)) {
return /*#__PURE__*/React.createElement(LazyLoadedDatasourceRenderFailedAnalyticsWrapper, {
datasourceId: datasourceId,
error: this.state.error
}, /*#__PURE__*/React.createElement(InlineCard
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
, this.props));
} else {
return /*#__PURE__*/React.createElement(LazyLoadedDatasourceRenderFailedAnalyticsWrapper, {
datasourceId: datasourceId,
error: this.state.error
}, fallback);
}
} else {
return fallback;
}
} else {
return /*#__PURE__*/React.createElement(UnsupportedComponent, null);
}
}
return this.props.children;
}
componentDidCatch(_error) {
this.setState({
isError: true,
error: _error
});
}
}