csp.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {Component} from 'react';
  2. import Button from 'app/components/button';
  3. import ButtonBar from 'app/components/buttonBar';
  4. import EventDataSection from 'app/components/events/eventDataSection';
  5. import CSPContent from 'app/components/events/interfaces/cspContent';
  6. import CSPHelp from 'app/components/events/interfaces/cspHelp';
  7. import {t} from 'app/locale';
  8. import {Event} from 'app/types/event';
  9. function getView(view, data) {
  10. switch (view) {
  11. case 'report':
  12. return <CSPContent data={data} />;
  13. case 'raw':
  14. return <pre>{JSON.stringify({'csp-report': data}, null, 2)}</pre>;
  15. case 'help':
  16. return <CSPHelp data={data} />;
  17. default:
  18. throw new TypeError(`Invalid view: ${view}`);
  19. }
  20. }
  21. type Props = {
  22. event: Event;
  23. data: Record<string, any>;
  24. };
  25. type State = {
  26. view: string;
  27. };
  28. export default class CspInterface extends Component<Props, State> {
  29. state: State = {view: 'report'};
  30. toggleView = value => {
  31. this.setState({
  32. view: value,
  33. });
  34. };
  35. render() {
  36. const {view} = this.state;
  37. const {data} = this.props;
  38. const cleanData =
  39. data.original_policy !== 'string'
  40. ? data
  41. : {
  42. ...data,
  43. // Hide the report-uri since this is redundant and silly
  44. original_policy: data.original_policy.replace(/(;\s+)?report-uri [^;]+/, ''),
  45. };
  46. const actions = (
  47. <ButtonBar merged active={view}>
  48. <Button
  49. barId="report"
  50. size="xsmall"
  51. onClick={this.toggleView.bind(this, 'report')}
  52. >
  53. {t('Report')}
  54. </Button>
  55. <Button barId="raw" size="xsmall" onClick={this.toggleView.bind(this, 'raw')}>
  56. {t('Raw')}
  57. </Button>
  58. <Button barId="help" size="xsmall" onClick={this.toggleView.bind(this, 'help')}>
  59. {t('Help')}
  60. </Button>
  61. </ButtonBar>
  62. );
  63. const children = getView(view, cleanData);
  64. return (
  65. <EventDataSection
  66. type="csp"
  67. title={<h3>{t('CSP Report')}</h3>}
  68. actions={actions}
  69. wrapTitle={false}
  70. >
  71. {children}
  72. </EventDataSection>
  73. );
  74. }
  75. }