config-credentials.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. import type { Credential } from '@/app/components/tools/types'
  7. import Drawer from '@/app/components/base/drawer-plus'
  8. import Button from '@/app/components/base/button'
  9. import Radio from '@/app/components/base/radio/ui'
  10. import { AuthType } from '@/app/components/tools/types'
  11. type Props = {
  12. credential: Credential
  13. onChange: (credential: Credential) => void
  14. onHide: () => void
  15. }
  16. const keyClassNames = 'py-2 leading-5 text-sm font-medium text-gray-900'
  17. type ItemProps = {
  18. text: string
  19. value: AuthType
  20. isChecked: boolean
  21. onClick: (value: AuthType) => void
  22. }
  23. const SelectItem: FC<ItemProps> = ({ text, value, isChecked, onClick }) => {
  24. return (
  25. <div
  26. className={cn(isChecked ? 'border-[2px] border-indigo-600 shadow-sm bg-white' : 'border border-gray-100', 'mb-2 flex items-center h-9 pl-3 w-[150px] rounded-xl bg-gray-25 hover:bg-gray-50 cursor-pointer space-x-2')}
  27. onClick={() => onClick(value)}
  28. >
  29. <Radio isChecked={isChecked} />
  30. <div className='text-sm font-normal text-gray-900'>{text}</div>
  31. </div>
  32. )
  33. }
  34. const ConfigCredential: FC<Props> = ({
  35. credential,
  36. onChange,
  37. onHide,
  38. }) => {
  39. const { t } = useTranslation()
  40. const [tempCredential, setTempCredential] = React.useState<Credential>(credential)
  41. return (
  42. <Drawer
  43. isShow
  44. onHide={onHide}
  45. title={t('tools.createTool.authMethod.title')!}
  46. panelClassName='mt-2 !w-[520px]'
  47. maxWidthClassName='!max-w-[520px]'
  48. height='calc(100vh - 16px)'
  49. headerClassName='!border-b-black/5'
  50. body={
  51. <div className='pt-2 px-6'>
  52. <div className='space-y-4'>
  53. <div>
  54. <div className={keyClassNames}>{t('tools.createTool.authMethod.type')}</div>
  55. <div className='flex space-x-3'>
  56. <SelectItem
  57. text={t('tools.createTool.authMethod.types.none')}
  58. value={AuthType.none}
  59. isChecked={tempCredential.auth_type === AuthType.none}
  60. onClick={value => setTempCredential({ ...tempCredential, auth_type: value })}
  61. />
  62. <SelectItem
  63. text={t('tools.createTool.authMethod.types.api_key')}
  64. value={AuthType.apiKey}
  65. isChecked={tempCredential.auth_type === AuthType.apiKey}
  66. onClick={value => setTempCredential({ ...tempCredential, auth_type: value })}
  67. />
  68. </div>
  69. </div>
  70. {tempCredential.auth_type === AuthType.apiKey && (
  71. <>
  72. <div>
  73. <div className={keyClassNames}>{t('tools.createTool.authMethod.key')}</div>
  74. <input
  75. value={tempCredential.api_key_header}
  76. onChange={e => setTempCredential({ ...tempCredential, api_key_header: e.target.value })}
  77. className='w-full h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' />
  78. </div>
  79. <div>
  80. <div className={keyClassNames}>{t('tools.createTool.authMethod.value')}</div>
  81. <input
  82. value={tempCredential.api_key_value}
  83. onChange={e => setTempCredential({ ...tempCredential, api_key_value: e.target.value })}
  84. className='w-full h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' />
  85. </div>
  86. </>)}
  87. </div>
  88. <div className='mt-4 shrink-0 flex justify-end space-x-2 py-4'>
  89. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium !text-gray-700' onClick={onHide}>{t('common.operation.cancel')}</Button>
  90. <Button className='flex items-center h-8 !px-3 !text-[13px] font-medium' type='primary' onClick={() => {
  91. onChange(tempCredential)
  92. onHide()
  93. }}>{t('common.operation.save')}</Button>
  94. </div>
  95. </div>
  96. }
  97. />
  98. )
  99. }
  100. export default React.memo(ConfigCredential)