import React from 'react'
import {ifcat} from '../../libs/utils'
import ObjectInspector from 'react-object-inspector';
import _ from 'underscore'
export default class Entry extends React.Component {
constructor(...args) {
super(...args);
this.state = {visible: false, width: window.innerWidth};
this._onWindowResize = this._onWindowResize.bind(this);
}
componentWillUnmount() {
window.removeEventListener("resize", this._onWindowResize)
}
componentDidMount() {
window.addEventListener("resize", this._onWindowResize, false)
}
_onWindowResize() {
this.setState({width: window.innerWidth});
}
_inspectPre(persistent) {
return {`${persistent.app}-${persistent.id}`};
}
_inspectTags(tags, persistent) {
return (tags || []).map((t, i) => {
return {t};
});
}
_inspectExpose(expose) {
return {expose};
}
_handleClick() {
this.setState({visible: !this.state.visible});
}
_inspectMetrics(entry) {
return (
{_.map(entry.transient.metrics, (value, key)=> {
value = typeof value === 'number' ? value.toFixed(3) : value;
return (
{key}
{value}
);
})}
)
}
_inspectRaw(preSpace) {
if (!this.state.visible) {
return null;
}
const {entry} = this.props;
return (
);
}
_inspectArgs(entry) {
let args = entry.args;
if (entry.expose === 'express') {
const express = entry.args[0];
args = (
{express.method}
{express.originalUrl}
{express.status} - {express.contentLength}
{Number(entry.args[0].duration).toFixed(3)}ms
);
}
if (!Array.isArray(args)) return {args};
return args.map((data, i) => {
if (typeof data === 'object' || typeof data === 'function') {
return ;
}
return {data};
});
}
_inspectCallSite(entry) {
let site = entry.transient.callsite;
if (entry.expose === 'express') {
const express = entry.args[0];
site = `${express.ip}`
} else {
site = `${site.file.substr(site.file.lastIndexOf('/') + 1)}:${site.line}`;
}
return {site};
}
render() {
let {entry} = this.props;
const lineNumber = new Array(this.props.maxLineChars - String(this.props.line).length).join(' ') + this.props.line;
const preSpace = new Array(this.props.maxLineChars).join(' ');
if (this.state.width < 1280) {
return (
{this._inspectPre(entry.persistent)}
{this._inspectTags(entry.persistent.tags, true)}
{this._inspectTags(entry.transient.tags)}
{this._inspectMetrics(entry)}
{this._inspectExpose(entry.expose)}
{this._inspectCallSite(entry)}
◀
{this._inspectArgs(entry)}
{this._inspectRaw(preSpace) }
);
}
return (
{this._inspectPre(entry.persistent)}
{this._inspectTags(entry.persistent.tags, true)}
{this._inspectTags(entry.transient.tags)}
{this._inspectMetrics(entry)}
{this._inspectArgs(entry)}
{this._inspectExpose(entry.expose)}
{this._inspectCallSite(entry)}
◀
{this._inspectRaw(preSpace) }
);
}
}