@lyra/default-layout
Version:
The default layout components for Lyra
86 lines (67 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SchemaErrorReporter = undefined;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _schema = require('part:@lyra/base/schema');
var _schema2 = _interopRequireDefault(_schema);
var _SchemaErrors = require('./SchemaErrors');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function renderPath(path) {
return path.map(segment => {
if (segment.kind === 'type') {
return `${segment.name || '<unnamed>'}(${segment.type})`;
}
if (segment.kind === 'property') {
return segment.name;
}
if (segment.kind === 'type') {
return `${segment.type}(${segment.name || '<unnamed>'})`;
}
return null;
}).filter(Boolean).join(' > ');
}
function reportWarnings() {
if (!__DEV__) {
return;
}
/* eslint-disable no-console */
const problemGroups = _schema2.default._validation;
const groupsWithWarnings = problemGroups.filter(group => group.problems.some(problem => problem.severity === 'warning'));
if (groupsWithWarnings.length === 0) {
return;
}
console.groupCollapsed(`⚠️ Schema has ${groupsWithWarnings.length} warnings`);
groupsWithWarnings.forEach((group, i) => {
const path = renderPath(group.path);
console.group(`%cAt ${path}`, 'color: #FF7636');
group.problems.forEach((problem, j) => {
console.log(problem.message);
});
console.groupEnd(`At ${path}`);
});
console.groupEnd('Schema warnings');
/* eslint-enable no-console */
}
class SchemaErrorReporter extends _react2.default.Component {
constructor(...args) {
var _temp;
return _temp = super(...args), this.componentDidMount = reportWarnings, _temp;
}
render() {
const problemGroups = _schema2.default._validation;
const groupsWithErrors = problemGroups.filter(group => group.problems.some(problem => problem.severity === 'error'));
if (groupsWithErrors.length > 0) {
return _react2.default.createElement(_SchemaErrors.SchemaErrors, { problemGroups: groupsWithErrors });
}
return this.props.children();
}
}
exports.SchemaErrorReporter = SchemaErrorReporter;
SchemaErrorReporter.propTypes = {
children: _propTypes2.default.func
};