Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

endpoint-modal.tsx 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { RiArrowRightUpLine, RiCloseLine } from '@remixicon/react'
  6. import ActionButton from '@/app/components/base/action-button'
  7. import Button from '@/app/components/base/button'
  8. import Drawer from '@/app/components/base/drawer'
  9. import Form from '@/app/components/header/account-setting/model-provider-page/model-modal/Form'
  10. import Toast from '@/app/components/base/toast'
  11. import { useRenderI18nObject } from '@/hooks/use-i18n'
  12. import cn from '@/utils/classnames'
  13. type Props = {
  14. formSchemas: any
  15. defaultValues?: any
  16. onCancel: () => void
  17. onSaved: (value: Record<string, any>) => void
  18. }
  19. const extractDefaultValues = (schemas: any[]) => {
  20. const result: Record<string, any> = {}
  21. for (const field of schemas) {
  22. if (field.default !== undefined)
  23. result[field.name] = field.default
  24. }
  25. return result
  26. }
  27. const EndpointModal: FC<Props> = ({
  28. formSchemas,
  29. defaultValues = {},
  30. onCancel,
  31. onSaved,
  32. }) => {
  33. const getValueFromI18nObject = useRenderI18nObject()
  34. const { t } = useTranslation()
  35. const initialValues = Object.keys(defaultValues).length > 0
  36. ? defaultValues
  37. : extractDefaultValues(formSchemas)
  38. const [tempCredential, setTempCredential] = React.useState<any>(initialValues)
  39. const handleSave = () => {
  40. for (const field of formSchemas) {
  41. if (field.required && !tempCredential[field.name]) {
  42. Toast.notify({ type: 'error', message: t('common.errorMsg.fieldRequired', { field: getValueFromI18nObject(field.label) }) })
  43. return
  44. }
  45. }
  46. onSaved(tempCredential)
  47. }
  48. return (
  49. <Drawer
  50. isOpen
  51. clickOutsideNotOpen={false}
  52. onClose={onCancel}
  53. footer={null}
  54. mask
  55. positionCenter={false}
  56. panelClassName={cn('mb-2 mr-2 mt-[64px] !w-[420px] !max-w-[420px] justify-start rounded-2xl border-[0.5px] border-components-panel-border !bg-components-panel-bg !p-0 shadow-xl')}
  57. >
  58. <>
  59. <div className='p-4 pb-2'>
  60. <div className='flex items-center justify-between'>
  61. <div className='system-xl-semibold text-text-primary'>{t('plugin.detailPanel.endpointModalTitle')}</div>
  62. <ActionButton onClick={onCancel}>
  63. <RiCloseLine className='h-4 w-4' />
  64. </ActionButton>
  65. </div>
  66. <div className='system-xs-regular mt-0.5 text-text-tertiary'>{t('plugin.detailPanel.endpointModalDesc')}</div>
  67. </div>
  68. <div className='grow overflow-y-auto'>
  69. <div className='px-4 py-2'>
  70. <Form
  71. value={tempCredential}
  72. onChange={(v) => {
  73. setTempCredential(v)
  74. }}
  75. formSchemas={formSchemas}
  76. isEditMode={true}
  77. showOnVariableMap={{}}
  78. validating={false}
  79. inputClassName='bg-components-input-bg-normal hover:bg-components-input-bg-hover'
  80. fieldMoreInfo={item => item.url
  81. ? (<a
  82. href={item.url}
  83. target='_blank' rel='noopener noreferrer'
  84. className='body-xs-regular inline-flex items-center text-text-accent-secondary'
  85. >
  86. {t('tools.howToGet')}
  87. <RiArrowRightUpLine className='ml-1 h-3 w-3' />
  88. </a>)
  89. : null}
  90. />
  91. </div>
  92. <div className={cn('flex justify-end p-4 pt-0')} >
  93. <div className='flex gap-2'>
  94. <Button onClick={onCancel}>{t('common.operation.cancel')}</Button>
  95. <Button variant='primary' onClick={handleSave}>{t('common.operation.save')}</Button>
  96. </div>
  97. </div>
  98. </div>
  99. </>
  100. </Drawer>
  101. )
  102. }
  103. export default React.memo(EndpointModal)