|
| 1 | +import * as React from 'react'; |
| 2 | +import { ThemeProvider } from 'emotion-theming'; |
| 3 | +import { Props } from './definitions/component'; |
| 4 | +import Styles from './theme/default'; |
| 5 | + |
| 6 | +import { Root } from './elements'; |
| 7 | +import Message from './Message'; |
| 8 | + |
| 9 | +// https://stackoverflow.com/a/48254637/4089357 |
| 10 | +const customStringify = function (v) { |
| 11 | + const cache = new Set() |
| 12 | + return JSON.stringify(v, function (key, value) { |
| 13 | + if (typeof value === 'object' && value !== null) { |
| 14 | + if (cache.has(value)) { |
| 15 | + // Circular reference found, discard key |
| 16 | + return |
| 17 | + } |
| 18 | + // Store value in our set |
| 19 | + cache.add(value) |
| 20 | + } |
| 21 | + return value |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +const getTheme = (props: Props) => ({ |
| 26 | + variant: props.variant || 'light', |
| 27 | + styles: { |
| 28 | + ...Styles(props), |
| 29 | + ...props.styles, |
| 30 | + }, |
| 31 | +}) |
| 32 | + |
| 33 | +class Console extends React.PureComponent<Props, any> { |
| 34 | + state = { |
| 35 | + theme: getTheme(this.props), |
| 36 | + prevStyles: this.props.styles, |
| 37 | + prevVariant: this.props.variant, |
| 38 | + } |
| 39 | + |
| 40 | + static getDerivedStateFromProps(props, state) { |
| 41 | + if ( |
| 42 | + props.variant !== state.prevVariant || |
| 43 | + JSON.stringify(props.styles) !== JSON.stringify(props.prevStyles) |
| 44 | + ) { |
| 45 | + return { |
| 46 | + theme: getTheme(props), |
| 47 | + prevStyles: props.styles, |
| 48 | + prevVariant: props.variant, |
| 49 | + } |
| 50 | + } |
| 51 | + return null |
| 52 | + } |
| 53 | + |
| 54 | + render() { |
| 55 | + let { |
| 56 | + filter = [], |
| 57 | + logs = [], |
| 58 | + searchKeywords, |
| 59 | + logFilter, |
| 60 | + logGrouping = true, |
| 61 | + } = this.props |
| 62 | + |
| 63 | + if (searchKeywords) { |
| 64 | + const regex = new RegExp(searchKeywords) |
| 65 | + |
| 66 | + const filterFun = logFilter |
| 67 | + ? logFilter |
| 68 | + : (log) => { |
| 69 | + try { |
| 70 | + return regex.test(customStringify(log)) |
| 71 | + } catch (e) { |
| 72 | + return true |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // @ts-ignore |
| 77 | + logs = logs.filter(filterFun) |
| 78 | + } |
| 79 | + |
| 80 | + if (logGrouping) { |
| 81 | + // @ts-ignore |
| 82 | + logs = logs.reduce((acc, log) => { |
| 83 | + const prevLog = acc[acc.length - 1] |
| 84 | + |
| 85 | + if ( |
| 86 | + prevLog && |
| 87 | + prevLog.amount && |
| 88 | + prevLog.method === log.method && |
| 89 | + prevLog.data.length === log.data.length && |
| 90 | + prevLog.data.every((value, i) => log.data[i] === value) |
| 91 | + ) { |
| 92 | + prevLog.amount += 1 |
| 93 | + |
| 94 | + return acc |
| 95 | + } |
| 96 | + |
| 97 | + acc.push({ ...log, amount: 1 }) |
| 98 | + |
| 99 | + return acc |
| 100 | + }, []) |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <ThemeProvider theme={this.state.theme}> |
| 105 | + <Root> |
| 106 | + {logs.map((log, i) => { |
| 107 | + // If the filter is defined and doesn't include the method |
| 108 | + const filtered = |
| 109 | + filter.length !== 0 && |
| 110 | + log.method && |
| 111 | + filter.indexOf(log.method) === -1 |
| 112 | + |
| 113 | + return filtered ? null : ( |
| 114 | + <Message |
| 115 | + log={log} |
| 116 | + key={log.id || `${log.method}-${i}`} |
| 117 | + linkifyOptions={this.props.linkifyOptions} |
| 118 | + /> |
| 119 | + ) |
| 120 | + })} |
| 121 | + </Root> |
| 122 | + </ThemeProvider> |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +export default Console; |
0 commit comments