generic.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {Component} from 'react';
  2. import Button from 'sentry/components/button';
  3. import ButtonBar from 'sentry/components/buttonBar';
  4. import EventDataSection from 'sentry/components/events/eventDataSection';
  5. import KeyValueList from 'sentry/components/events/interfaces/keyValueList';
  6. import {getMeta} from 'sentry/components/events/meta/metaProxy';
  7. import {t} from 'sentry/locale';
  8. function getView(view: View, data: State['data']) {
  9. switch (view) {
  10. case 'report':
  11. return (
  12. <KeyValueList
  13. data={Object.entries(data).map(([key, value]) => ({
  14. key,
  15. value,
  16. subject: key,
  17. meta: getMeta(data, key),
  18. }))}
  19. isContextData
  20. />
  21. );
  22. case 'raw':
  23. return <pre>{JSON.stringify({'csp-report': data}, null, 2)}</pre>;
  24. default:
  25. throw new TypeError(`Invalid view: ${view}`);
  26. }
  27. }
  28. type Props = {
  29. data: Record<string, any>;
  30. type: string;
  31. };
  32. type View = 'report' | 'raw';
  33. type State = {
  34. view: View;
  35. } & Pick<Props, 'data'>;
  36. export default class GenericInterface extends Component<Props, State> {
  37. state: State = {
  38. view: 'report',
  39. data: this.props.data,
  40. };
  41. toggleView = (value: View) => {
  42. this.setState({
  43. view: value,
  44. });
  45. };
  46. render() {
  47. const {view, data} = this.state;
  48. const {type} = this.props;
  49. const title = (
  50. <div>
  51. <ButtonBar merged active={view}>
  52. <Button barId="report" size="xs" onClick={this.toggleView.bind(this, 'report')}>
  53. {t('Report')}
  54. </Button>
  55. <Button barId="raw" size="xs" onClick={this.toggleView.bind(this, 'raw')}>
  56. {t('Raw')}
  57. </Button>
  58. </ButtonBar>
  59. <h3>{t('Report')}</h3>
  60. </div>
  61. );
  62. const children = getView(view, data);
  63. return (
  64. <EventDataSection type={type} title={title} wrapTitle={false}>
  65. {children}
  66. </EventDataSection>
  67. );
  68. }
  69. }