All files / modules/10-common/components/GitDiffEditor GitDiffEditor.tsx

36.36% Statements 8/22
0% Branches 0/70
0% Functions 0/6
33.33% Lines 7/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130              274x 274x     274x 274x 274x   274x                               274x                                                                                                                                                                                                    
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useEffect, useRef, useState } from 'react'
import { MonacoDiffEditor } from 'react-monaco-editor'
import type { editor } from 'monaco-editor/esm/vs/editor/editor.api'
 
import { Button, Container, Icon, Layout, Text, useConfirmationDialog } from '@wings-software/uicore'
import { Intent } from '@harness/design-system'
import { useStrings } from 'framework/strings'
 
import css from './GitDiffEditor.module.scss'
 
interface GitInfo {
  branch: string
  content: string
}
 
interface GitDiffEditorInterface {
  remote: GitInfo
  local: GitInfo
  height?: React.CSSProperties['height']
  width?: React.CSSProperties['width']
  onSave: (updatedContent: string) => void
  onCancel: () => void
}
 
export const GitDiffEditor = ({
  remote = { branch: '', content: '' },
  local = { branch: '', content: '' },
  height,
  width,
  onSave,
  onCancel
}: GitDiffEditorInterface): JSX.Element => {
  const { getString } = useStrings()
  const editorRef = useRef<MonacoDiffEditor>(null)
  const [currentContent, setCurrentContent] = useState<string>(local.content)
  const [totalLinesInModifiedContent, setTotalLinesInModifiedContent] = useState<number>(0)
 
  useEffect(() => {
    setTotalLinesInModifiedContent(editorRef.current?.editor?.getModifiedEditor()?.getModel()?.getLineCount() || 0)
  }, [])
 
  const { openDialog } = useConfirmationDialog({
    contentText: getString('common.unsavedChanges'),
    titleText: getString('common.confirmText'),
    cancelButtonText: getString('cancel'),
    confirmButtonText: getString('confirm'),
    intent: Intent.WARNING,
    onCloseDialog: (isConfirmed: boolean) => {
      if (isConfirmed) {
        onCancel()
      }
    }
  })
 
  return (
    <Layout.Vertical>
      <Layout.Horizontal className={css.header} width={width}>
        <Layout.Horizontal className={css.panel} style={{ flex: 1 }}>
          {/** TODO @vardan uncomment when branch and version selector capabilities are added */}
          {/* <Container padding={{ right: 'large' }}>
              <BranchSelector branches={mockBranches} currentBranch={branch} isReadOnlyMode />
            </Container>
            <VersionSelector versions={mockVersions} isReadOnlyMode /> */}
          <Layout.Horizontal spacing="small">
            <Icon name="git-branch" size={18} />
            <Text>{remote.branch}</Text>
          </Layout.Horizontal>
        </Layout.Horizontal>
        <Layout.Horizontal className={css.panel} flex style={{ flex: 1 }}>
          {/* <Container padding={{ right: 'large' }}>
                <BranchSelector branches={mockBranches} currentBranch={branch} />
              </Container>
              <VersionSelector versions={mockVersions} isEditMode={isEditMode} /> */}
          <Layout.Horizontal spacing="small">
            <Icon name="git-branch" size={18} />
            <Text>{local.branch}</Text>
          </Layout.Horizontal>
          <Layout.Horizontal flex spacing="small">
            <Button minimal text={getString('cancel')} onClick={openDialog} />
            <Button intent="primary" text={getString('save')} onClick={() => onSave(currentContent)} />
          </Layout.Horizontal>
        </Layout.Horizontal>
      </Layout.Horizontal>
      <Container padding={{ top: 'xsmall' }}>
        <MonacoDiffEditor
          width={width ?? '100%'}
          height={height ?? 'calc(100% - 100px)'}
          language="javascript"
          original={remote.content}
          value={currentContent}
          options={{
            ignoreTrimWhitespace: true,
            minimap: { enabled: true },
            codeLens: true,
            renderSideBySide: true,
            lineNumbers: 'on',
            inDiffEditor: true,
            scrollBeyondLastLine: false,
            enableSplitViewResizing: false,
            fontFamily: "'Roboto Mono', monospace",
            fontSize: 13
          }}
          ref={editorRef}
          editorDidMount={(diffEditor?: editor.IStandaloneDiffEditor): void => {
            setTotalLinesInModifiedContent(diffEditor?.getModifiedEditor()?.getModel()?.getLineCount?.() || 0)
          }}
          onChange={(value: string, _event: editor.IModelContentChangedEvent) => {
            setCurrentContent(value)
            setTotalLinesInModifiedContent(
              editorRef.current?.editor?.getModifiedEditor()?.getModel()?.getLineCount?.() || 0
            )
          }}
        />
      </Container>
      <Container className={css.footer}>
        <Text padding={{ right: 'large' }} font={{ size: 'small', weight: 'bold' }}>
          {getString('common.totalLines')} {totalLinesInModifiedContent}
        </Text>
      </Container>
    </Layout.Vertical>
  )
}