All files / modules/72-templates-library/components/TemplateStudio/StageTemplateCanvas StageTemplateCanvas.tsx

96.15% Statements 25/26
67.74% Branches 21/31
75% Functions 3/4
96.15% Lines 25/26

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              13x 13x 13x   13x 13x 13x 13x 13x 13x   13x                           1x 1x 1x 1x     1x       1x 1x   1x 1x 1x               1x 1x 1x       1x                                         13x  
/*
 * 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 from 'react'
import SplitPane from 'react-split-pane'
import { debounce, isEmpty } from 'lodash-es'
import type { TemplateFormRef } from '@templates-library/components/TemplateStudio/TemplateStudio'
import { StageTemplateFormWithRef } from '@templates-library/components/TemplateStudio/StageTemplateCanvas/StageTemplateForm/StageTemplateForm'
import { StageTemplateDiagram } from '@templates-library/components/TemplateStudio/StageTemplateCanvas/StageTemplateDiagram/StageTemplateDiagram'
import { RightDrawer } from '@pipeline/components/PipelineStudio/RightDrawer/RightDrawer'
import { TemplateDrawer } from '@templates-library/components/TemplateDrawer/TemplateDrawer'
import { SplitViewTypes } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineActions'
import { usePipelineContext } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineContext'
 
const StageTemplateCanvas = (_props: unknown, formikRef: TemplateFormRef): JSX.Element => {
  const {
    state: {
      pipeline: { stages },
      pipelineView,
      pipelineView: {
        isSplitViewOpen,
        splitViewData: { type = SplitViewTypes.StageView }
      },
      selectionState: { selectedStageId }
    },
    getStageFromPipeline,
    updatePipelineView,
    setSelection
  } = usePipelineContext()
  const [splitPaneSize, setSplitPaneSize] = React.useState(200)
  const setSplitPaneSizeDeb = React.useRef(debounce(setSplitPaneSize, 200))
  const handleStageResize = (size: number): void => {
    setSplitPaneSizeDeb.current(size)
  }
  const resizerStyle = navigator.userAgent.match(/firefox/i)
    ? { display: 'flow-root list-item' }
    : { display: 'inline-table' }
 
  const selectedStage = getStageFromPipeline(selectedStageId || '')
  const openSplitView = isSplitViewOpen && !!selectedStage?.stage
 
  React.useEffect(() => {
    Eif (selectedStageId) {
      updatePipelineView({
        ...pipelineView,
        isSplitViewOpen: true,
        splitViewData: { type: SplitViewTypes.StageView }
      })
    }
  }, [selectedStageId])
 
  React.useEffect(() => {
    Eif (!isEmpty(stages)) {
      setSelection({ stageId: stages?.[0]?.stage?.identifier })
    }
  }, [stages])
 
  return (
    <>
      <SplitPane
        size={splitPaneSize}
        split="horizontal"
        minSize={160}
        maxSize={300}
        style={{ overflow: 'auto' }}
        pane2Style={{ overflow: 'initial', zIndex: 2 }}
        resizerStyle={resizerStyle}
        onChange={handleStageResize}
      >
        <StageTemplateDiagram />
        {openSplitView && type === SplitViewTypes.StageView ? <StageTemplateFormWithRef ref={formikRef} /> : null}
      </SplitPane>
      <RightDrawer />
      <TemplateDrawer />
    </>
  )
}
 
export const StageTemplateCanvasWithRef = React.forwardRef(StageTemplateCanvas)