priority-selector.tsx 2.3 KB

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