priority-selector.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Fragment } from 'react'
  2. import type { FC } from 'react'
  3. import { Popover, Transition } from '@headlessui/react'
  4. import { useTranslation } from 'react-i18next'
  5. import { PreferredProviderTypeEnum } from '../declarations'
  6. import { Check, DotsHorizontal } from '@/app/components/base/icons/src/vender/line/general'
  7. import Button from '@/app/components/base/button'
  8. type SelectorProps = {
  9. value?: string
  10. onSelect: (key: PreferredProviderTypeEnum) => void
  11. }
  12. const Selector: FC<SelectorProps> = ({
  13. value,
  14. onSelect,
  15. }) => {
  16. const { t } = useTranslation()
  17. const options = [
  18. {
  19. key: PreferredProviderTypeEnum.custom,
  20. text: 'API',
  21. },
  22. {
  23. key: PreferredProviderTypeEnum.system,
  24. text: t('common.modelProvider.quota'),
  25. },
  26. ]
  27. return (
  28. <Popover className='relative'>
  29. <Popover.Button>
  30. {
  31. ({ open }) => (
  32. <Button className={`
  33. px-0 w-6 h-6 bg-white rounded-md
  34. ${open && '!bg-gray-100'}
  35. `}>
  36. <DotsHorizontal className='w-3 h-3 text-gray-700' />
  37. </Button>
  38. )
  39. }
  40. </Popover.Button>
  41. <Transition
  42. as={Fragment}
  43. leave='transition ease-in duration-100'
  44. leaveFrom='opacity-100'
  45. leaveTo='opacity-0'
  46. >
  47. <Popover.Panel className='absolute top-7 right-0 w-[144px] bg-white border-[0.5px] border-gray-200 rounded-lg shadow-lg z-10'>
  48. <div className='p-1'>
  49. <div className='px-3 pt-2 pb-1 text-sm font-medium text-gray-700'>{t('common.modelProvider.card.priorityUse')}</div>
  50. {
  51. options.map(option => (
  52. <Popover.Button as={Fragment} key={option.key}>
  53. <div
  54. className='flex items-center justify-between px-3 h-9 text-sm text-gray-700 rounded-lg cursor-pointer hover:bg-gray-50'
  55. onClick={() => onSelect(option.key)}
  56. >
  57. <div className='grow'>{option.text}</div>
  58. {value === option.key && <Check className='w-4 h-4 text-primary-600' />}
  59. </div>
  60. </Popover.Button>
  61. ))
  62. }
  63. </div>
  64. </Popover.Panel>
  65. </Transition>
  66. </Popover>
  67. )
  68. }
  69. export default Selector