priority-selector.tsx 2.4 KB

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