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

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

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

     "use client"

    import {
      Dialog,
      DialogContent,
      DialogDescription,
      DialogFooter,
      DialogHeader,
      DialogTitle,
    } from '@/components/ui/dialog'
    import { Button } from '@/components/ui/button'
    import { Snippet } from '@/lib/types'
    import { strings, appConfig } from '@/lib/config'
    import { useSnippetForm } from '@/hooks/useSnippetForm'
    import { SnippetFormFields } from '@/components/features/snippet-editor/SnippetFormFields'
    import { CodeEditorSection } from '@/components/features/snippet-editor/CodeEditorSection'
    import { InputParameterList } from '@/components/features/snippet-editor/InputParameterList'

    interface SnippetDialogProps {
      open: boolean
      onOpenChange: (open: boolean) => void
      onSave: (snippet: Omit<Snippet, 'id' | 'createdAt' | 'updatedAt'>) => void
      editingSnippet?: Snippet | null
    }

    export function SnippetDialog({ open, onOpenChange, onSave, editingSnippet }: SnippetDialogProps) {
      const {
        title,
        description,
        language,
        code,
        hasPreview,
        functionName,
        inputParameters,
        errors,
        setTitle,
        setDescription,
        setLanguage,
        setCode,
        setHasPreview,
        setFunctionName,
        handleAddParameter,
        handleRemoveParameter,
        handleUpdateParameter,
        validate,
        getFormData,
        resetForm,
      } = useSnippetForm(editingSnippet, open)

      const handleSave = () => {
        if (!validate()) {
          return
        }

        onSave(getFormData())
        resetForm()
        onOpenChange(false)
      }

      const isPreviewSupported = appConfig.previewEnabledLanguages.includes(language)

      return (
        <Dialog open={open} onOpenChange={onOpenChange}>
          <DialogContent
            className="sm:max-w-[900px] max-h-[90vh] overflow-hidden flex flex-col"
            data-testid="snippet-dialog"
          >
            <DialogHeader className="pr-8">
              <DialogTitle className="text-2xl">
                {editingSnippet?.id ? strings.snippetDialog.edit.title : strings.snippetDialog.create.title}
              </DialogTitle>
              <DialogDescription>
                {editingSnippet?.id
                  ? strings.snippetDialog.edit.description
                  : strings.snippetDialog.create.description}
              </DialogDescription>
            </DialogHeader>

            <div className="space-y-4 py-4 overflow-y-auto flex-1">
              <SnippetFormFields
                title={title}
                description={description}
                language={language}
                errors={errors}
                onTitleChange={setTitle}
                onDescriptionChange={setDescription}
                onLanguageChange={setLanguage}
              />

              <CodeEditorSection
                code={code}
                language={language}
                hasPreview={hasPreview}
                functionName={functionName}
                inputParameters={inputParameters}
                errors={errors}
                onCodeChange={setCode}
                onPreviewChange={setHasPreview}
              />

              {hasPreview && isPreviewSupported && (
                <InputParameterList
                  inputParameters={inputParameters}
                  functionName={functionName}
                  onFunctionNameChange={setFunctionName}
                  onAddParameter={handleAddParameter}
                  onRemoveParameter={handleRemoveParameter}
                  onUpdateParameter={handleUpdateParameter}
                />
              )}
            </div>

            <DialogFooter>
              <Button
                variant="outline"
                onClick={() => onOpenChange(false)}
                data-testid="snippet-dialog-cancel-btn"
              >
                {strings.snippetDialog.buttons.cancel}
              </Button>
              <Button
                onClick={handleSave}
                data-testid="snippet-dialog-save-btn"
              >
                {editingSnippet ? strings.snippetDialog.buttons.update : strings.snippetDialog.buttons.create} Snippet
              </Button>
            </DialogFooter>
          </DialogContent>
        </Dialog>
      )
    }