wwf
3 天以前 a430284aa21e3ae1f0d5654e55b2ad2852519cc2
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
import React, { type FC, useCallback, useMemo, useState } from 'react'
import type { SchemaRoot } from '../../../types'
import { RiArrowLeftLine, RiCloseLine, RiSparklingLine } from '@remixicon/react'
import { useTranslation } from 'react-i18next'
import Button from '@/app/components/base/button'
import CodeEditor from '../code-editor'
import ErrorMessage from '../error-message'
import { getValidationErrorMessage, validateSchemaAgainstDraft7 } from '../../../utils'
import Loading from '@/app/components/base/loading'
 
type GeneratedResultProps = {
  schema: SchemaRoot
  isGenerating: boolean
  onBack: () => void
  onRegenerate: () => void
  onClose: () => void
  onApply: () => void
}
 
const GeneratedResult: FC<GeneratedResultProps> = ({
  schema,
  isGenerating,
  onBack,
  onRegenerate,
  onClose,
  onApply,
}) => {
  const { t } = useTranslation()
  const [parseError, setParseError] = useState<Error | null>(null)
  const [validationError, setValidationError] = useState<string>('')
 
  const formatJSON = (json: SchemaRoot) => {
    try {
      const schema = JSON.stringify(json, null, 2)
      setParseError(null)
      return schema
    }
    catch (e) {
      if (e instanceof Error)
        setParseError(e)
      else
        setParseError(new Error('Invalid JSON'))
      return ''
    }
  }
 
  const jsonSchema = useMemo(() => formatJSON(schema), [schema])
 
  const handleApply = useCallback(() => {
    const validationErrors = validateSchemaAgainstDraft7(schema)
    if (validationErrors.length > 0) {
      setValidationError(getValidationErrorMessage(validationErrors))
      return
    }
    onApply()
    setValidationError('')
  }, [schema, onApply])
 
  return (
    <div className='flex w-[480px] flex-col rounded-2xl border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-2xl shadow-shadow-shadow-9'>
      {
        isGenerating ? (
          <div className='flex h-[600px] flex-col items-center justify-center gap-y-3'>
            <Loading type='area' />
            <div className='system-xs-regular text-text-tertiary'>{t('workflow.nodes.llm.jsonSchema.generating')}</div>
          </div>
        ) : (
          <>
            <div className='absolute right-2.5 top-2.5 flex h-8 w-8 items-center justify-center' onClick={onClose}>
              <RiCloseLine className='h-4 w-4 text-text-tertiary' />
            </div>
            {/* Title */}
            <div className='flex flex-col gap-y-[0.5px] px-3 pb-1 pt-3.5'>
              <div className='system-xl-semibold flex pl-1 pr-8 text-text-primary'>
                {t('workflow.nodes.llm.jsonSchema.generatedResult')}
              </div>
              <div className='system-xs-regular flex px-1 text-text-tertiary'>
                {t('workflow.nodes.llm.jsonSchema.resultTip')}
              </div>
            </div>
            {/* Content */}
            <div className='px-4 py-2'>
              <CodeEditor
                className='rounded-lg'
                editorWrapperClassName='h-[424px]'
                value={jsonSchema}
                readOnly
                showFormatButton={false}
              />
              {parseError && <ErrorMessage message={parseError.message} />}
              {validationError && <ErrorMessage message={validationError} />}
            </div>
            {/* Footer */}
            <div className='flex items-center justify-between p-4 pt-2'>
              <Button variant='secondary' className='flex items-center gap-x-0.5' onClick={onBack}>
                <RiArrowLeftLine className='h-4 w-4' />
                <span>{t('workflow.nodes.llm.jsonSchema.back')}</span>
              </Button>
              <div className='flex items-center gap-x-2'>
                <Button
                  variant='secondary'
                  className='flex items-center gap-x-0.5'
                  onClick={onRegenerate}
                >
                  <RiSparklingLine className='h-4 w-4' />
                  <span>{t('workflow.nodes.llm.jsonSchema.regenerate')}</span>
                </Button>
                <Button variant='primary' onClick={handleApply}>
                  {t('workflow.nodes.llm.jsonSchema.apply')}
                </Button>
              </div>
            </div>
 
          </>
        )
      }
    </div>
  )
}
 
export default React.memo(GeneratedResult)