# All files / src/components/features/snippet-editor SplitScreenEditor.tsx

0% Statements 0/137
0% Branches 0/1
0% Functions 0/1
0% Lines 0/137

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
    131
    132
    133
    134
    135
    136
    137
    138

     'use client'

    import { useState } from 'react'
    import { MonacoEditor } from '@/components/features/snippet-editor/MonacoEditor'
    import { ReactPreview } from '@/components/features/snippet-editor/ReactPreview'
    import { PythonOutput } from '@/components/features/python-runner/PythonOutput'
    import { Button } from '@/components/ui/button'
    import { Code, Eye, SplitHorizontal } from '@phosphor-icons/react'
    import { InputParameter } from '@/lib/types'

    interface SplitScreenEditorProps {
      value: string
      onChange: (value: string) => void
      language: string
      height?: string
      functionName?: string
      inputParameters?: InputParameter[]
    }

    type ViewMode = 'split' | 'code' | 'preview'

    export function SplitScreenEditor({
      value,
      onChange,
      language,
      height = '500px',
      functionName,
      inputParameters,
    }: SplitScreenEditorProps) {
      const [viewMode, setViewMode] = useState<ViewMode>('split')

      const isPreviewSupported = ['JSX', 'TSX', 'JavaScript', 'TypeScript', 'Python'].includes(language)
      const isPython = language === 'Python'

      if (!isPreviewSupported) {
        return (
          <MonacoEditor
            value={value}
            onChange={onChange}
            language={language}
            height={height}
          />
        )
      }

      return (
        <div className="flex flex-col gap-3">
          <div className="flex items-center justify-end">
            <div className="flex items-center gap-1 p-1 rounded-md" style={{ backgroundColor: 'var(--mat-sys-surface-variant)' }}>
              <Button
                variant={viewMode === 'code' ? 'filled' : 'text'}
                size="sm"
                onClick={() => setViewMode('code')}
                className="flex items-center gap-2 h-8"
              >
                <Code className="w-4 h-4" />
                <span className="hidden sm:inline">Code</span>
              </Button>
              <Button
                variant={viewMode === 'split' ? 'filled' : 'text'}
                size="sm"
                onClick={() => setViewMode('split')}
                className="flex items-center gap-2 h-8"
              >
                <SplitHorizontal className="w-4 h-4" />
                <span className="hidden sm:inline">Split</span>
              </Button>
              <Button
                variant={viewMode === 'preview' ? 'filled' : 'text'}
                size="sm"
                onClick={() => setViewMode('preview')}
                className="flex items-center gap-2 h-8"
              >
                <Eye className="w-4 h-4" />
                <span className="hidden sm:inline">{isPython ? 'Output' : 'Preview'}</span>
              </Button>
            </div>
          </div>

          <div
            className="rounded-md overflow-hidden border"
            style={{
              height,
              borderColor: 'var(--mat-sys-outline-variant)',
              backgroundColor: 'var(--mat-sys-surface)'
            }}
          >
            {viewMode === 'code' && (
              <MonacoEditor
                value={value}
                onChange={onChange}
                language={language}
                height={height}
              />
            )}

            {viewMode === 'preview' && (
              isPython ? (
                <PythonOutput code={value} />
              ) : (
                <ReactPreview
                  code={value}
                  language={language}
                  functionName={functionName}
                  inputParameters={inputParameters}
                />
              )
            )}

            {viewMode === 'split' && (
              <div className="grid grid-cols-2 h-full" style={{ gap: '1px', backgroundColor: 'var(--mat-sys-outline-variant)' }}>
                <div className="overflow-auto" style={{ backgroundColor: 'var(--mat-sys-surface)' }}>
                  <MonacoEditor
                    value={value}
                    onChange={onChange}
                    language={language}
                    height="100%"
                  />
                </div>
                <div className="overflow-auto" style={{ backgroundColor: 'var(--mat-sys-surface)' }}>
                  {isPython ? (
                    <PythonOutput code={value} />
                  ) : (
                    <ReactPreview
                      code={value}
                      language={language}
                      functionName={functionName}
                      inputParameters={inputParameters}
                    />
                  )}
                </div>
              </div>
            )}
          </div>
        </div>
      )
    }