|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import EventDataSection from 'app/components/events/eventDataSection'; |
| 4 | +import {t} from 'app/locale'; |
| 5 | +import plugins from 'app/plugins'; |
| 6 | +import {Group} from 'app/types'; |
| 7 | +import {Event} from 'app/types/event'; |
| 8 | +import {defined, toTitleCase} from 'app/utils'; |
| 9 | + |
| 10 | +import {getContextComponent, getSourcePlugin} from './utils'; |
| 11 | + |
| 12 | +type Props = { |
| 13 | + alias: string; |
| 14 | + event: Event; |
| 15 | + type: string; |
| 16 | + value?: Record<string, any>; |
| 17 | + group?: Group; |
| 18 | +}; |
| 19 | + |
| 20 | +type State = { |
| 21 | + isLoading: boolean; |
| 22 | + pluginLoading?: boolean; |
| 23 | +}; |
| 24 | + |
| 25 | +class Chunk extends React.Component<Props, State> { |
| 26 | + state: State = { |
| 27 | + isLoading: false, |
| 28 | + }; |
| 29 | + |
| 30 | + UNSAFE_componentWillMount() { |
| 31 | + this.syncPlugin(); |
| 32 | + } |
| 33 | + |
| 34 | + componentDidUpdate(prevProps: Props) { |
| 35 | + if ( |
| 36 | + prevProps.type !== this.props.type || |
| 37 | + prevProps.group?.id !== this.props.group?.id |
| 38 | + ) { |
| 39 | + this.syncPlugin(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + syncPlugin() { |
| 44 | + const {group, type, alias} = this.props; |
| 45 | + // If we don't have a grouped event we can't sync with plugins. |
| 46 | + if (!group) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // Search using `alias` first because old plugins rely on it and type is set to "default" |
| 51 | + // e.g. sessionstack |
| 52 | + const sourcePlugin = |
| 53 | + type === 'default' |
| 54 | + ? getSourcePlugin(group.pluginContexts, alias) || |
| 55 | + getSourcePlugin(group.pluginContexts, type) |
| 56 | + : getSourcePlugin(group.pluginContexts, type); |
| 57 | + |
| 58 | + if (!sourcePlugin) { |
| 59 | + this.setState({pluginLoading: false}); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + this.setState( |
| 64 | + { |
| 65 | + pluginLoading: true, |
| 66 | + }, |
| 67 | + () => { |
| 68 | + plugins.load(sourcePlugin, () => { |
| 69 | + this.setState({pluginLoading: false}); |
| 70 | + }); |
| 71 | + } |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + getTitle() { |
| 76 | + const {value = {}, alias, type} = this.props; |
| 77 | + |
| 78 | + if (defined(value.title) && typeof value.title !== 'object') { |
| 79 | + return value.title; |
| 80 | + } |
| 81 | + |
| 82 | + if (!defined(type)) { |
| 83 | + return toTitleCase(alias); |
| 84 | + } |
| 85 | + |
| 86 | + switch (type) { |
| 87 | + case 'app': |
| 88 | + return t('App'); |
| 89 | + case 'device': |
| 90 | + return t('Device'); |
| 91 | + case 'os': |
| 92 | + return t('Operating System'); |
| 93 | + case 'user': |
| 94 | + return t('User'); |
| 95 | + case 'gpu': |
| 96 | + return t('Graphics Processing Unit'); |
| 97 | + case 'runtime': |
| 98 | + return t('Runtime'); |
| 99 | + case 'trace': |
| 100 | + return t('Trace Details'); |
| 101 | + case 'default': |
| 102 | + if (alias === 'state') return t('Application State'); |
| 103 | + return toTitleCase(alias); |
| 104 | + default: |
| 105 | + return toTitleCase(type); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + render() { |
| 110 | + const {pluginLoading} = this.state; |
| 111 | + |
| 112 | + // if we are currently loading the plugin, just render nothing for now. |
| 113 | + if (pluginLoading) { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + const {type, alias, value = {}, event} = this.props; |
| 118 | + |
| 119 | + // we intentionally hide reprocessing context to not imply it was sent by the SDK. |
| 120 | + if (alias === 'reprocessing') { |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + const Component = |
| 125 | + type === 'default' |
| 126 | + ? getContextComponent(alias) || getContextComponent(type) |
| 127 | + : getContextComponent(type); |
| 128 | + |
| 129 | + const isObjectValueEmpty = Object.values(value).filter(v => defined(v)).length === 0; |
| 130 | + |
| 131 | + // this can happen if the component does not exist |
| 132 | + if (!Component || isObjectValueEmpty) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + return ( |
| 137 | + <EventDataSection |
| 138 | + key={`context-${alias}`} |
| 139 | + type={`context-${alias}`} |
| 140 | + title={ |
| 141 | + <React.Fragment> |
| 142 | + {this.getTitle()} |
| 143 | + {defined(type) && type !== 'default' && alias !== type && ( |
| 144 | + <small>({alias})</small> |
| 145 | + )} |
| 146 | + </React.Fragment> |
| 147 | + } |
| 148 | + > |
| 149 | + <Component alias={alias} event={event} data={value} /> |
| 150 | + </EventDataSection> |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +export default Chunk; |
0 commit comments