EditorViewer.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import React from 'react';
  2. import Editor from 'components/common/Editor/Editor';
  3. import { SchemaType } from 'generated-sources';
  4. import { parse, stringify } from 'lossless-json';
  5. import * as S from './EditorViewer.styled';
  6. export interface EditorViewerProps {
  7. data: string;
  8. schemaType?: string;
  9. maxLines?: number;
  10. }
  11. const getSchemaValue = (data: string, schemaType?: string) => {
  12. if (schemaType === SchemaType.JSON || schemaType === SchemaType.AVRO) {
  13. return stringify(parse(data), undefined, '\t');
  14. }
  15. return data;
  16. };
  17. const EditorViewer: React.FC<EditorViewerProps> = ({
  18. data,
  19. schemaType,
  20. maxLines,
  21. }) => {
  22. try {
  23. return (
  24. <S.Wrapper>
  25. <Editor
  26. isFixedHeight
  27. schemaType={schemaType}
  28. name="schema"
  29. value={getSchemaValue(data, schemaType)}
  30. setOptions={{
  31. showLineNumbers: false,
  32. maxLines,
  33. showGutter: false,
  34. }}
  35. readOnly
  36. />
  37. </S.Wrapper>
  38. );
  39. } catch (e) {
  40. return (
  41. <S.Wrapper>
  42. <p>{data}</p>
  43. </S.Wrapper>
  44. );
  45. }
  46. };
  47. export default EditorViewer;