| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import { useState } from 'react' | 
					
						
							|  |  |  | import useSWR from 'swr' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							| 
									
										
										
										
											2024-06-20 11:05:08 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   RiAddLine, | 
					
						
							|  |  |  |   RiArrowDownSLine, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   PortalToFollowElem, | 
					
						
							|  |  |  |   PortalToFollowElemContent, | 
					
						
							|  |  |  |   PortalToFollowElemTrigger, | 
					
						
							|  |  |  | } from '@/app/components/base/portal-to-follow-elem' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   ArrowUpRight, | 
					
						
							|  |  |  | } from '@/app/components/base/icons/src/vender/line/arrows' | 
					
						
							|  |  |  | import { useModalContext } from '@/context/modal-context' | 
					
						
							|  |  |  | import { fetchApiBasedExtensionList } from '@/service/common' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ApiBasedExtensionSelectorProps = { | 
					
						
							|  |  |  |   value: string | 
					
						
							|  |  |  |   onChange: (value: string) => void | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ApiBasedExtensionSelector: FC<ApiBasedExtensionSelectorProps> = ({ | 
					
						
							|  |  |  |   value, | 
					
						
							|  |  |  |   onChange, | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  |   const [open, setOpen] = useState(false) | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     setShowAccountSettingModal, | 
					
						
							|  |  |  |     setShowApiBasedExtensionModal, | 
					
						
							|  |  |  |   } = useModalContext() | 
					
						
							| 
									
										
										
										
											2023-11-06 20:29:41 +08:00
										 |  |  |   const { data, mutate } = useSWR( | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |     '/api-based-extension', | 
					
						
							|  |  |  |     fetchApiBasedExtensionList, | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  |   const handleSelect = (id: string) => { | 
					
						
							|  |  |  |     onChange(id) | 
					
						
							|  |  |  |     setOpen(false) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const currentItem = data?.find(item => item.id === value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <PortalToFollowElem | 
					
						
							|  |  |  |       open={open} | 
					
						
							|  |  |  |       onOpenChange={setOpen} | 
					
						
							|  |  |  |       placement='bottom-start' | 
					
						
							|  |  |  |       offset={4} | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <PortalToFollowElemTrigger onClick={() => setOpen(v => !v)} className='w-full'> | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           currentItem | 
					
						
							|  |  |  |             ? ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <div className='flex h-9 cursor-pointer items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2.5'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 <div className='text-sm text-text-primary'>{currentItem.name}</div> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |                 <div className='flex items-center'> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <div className='mr-1.5 w-[270px] truncate text-right text-xs text-text-quaternary'> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |                     {currentItem.api_endpoint} | 
					
						
							|  |  |  |                   </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                   <RiArrowDownSLine className={`h-4 w-4 text-text-secondary ${!open && 'opacity-60'}`} /> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |                 </div> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             : ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <div className='flex h-9 cursor-pointer items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2.5 text-sm text-text-quaternary'> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |                 {t('common.apiBasedExtension.selector.placeholder')} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <RiArrowDownSLine className={`h-4 w-4 text-text-secondary ${!open && 'opacity-60'}`} /> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |               </div> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       </PortalToFollowElemTrigger> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <PortalToFollowElemContent className='z-[102] w-[calc(100%-32px)] max-w-[576px]'> | 
					
						
							|  |  |  |         <div className='z-10 w-full rounded-lg border-[0.5px] border-components-panel-border bg-components-panel-bg shadow-lg'> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |           <div className='p-1'> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |             <div className='flex items-center justify-between px-3 pb-1 pt-2'> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |               <div className='text-xs font-medium text-text-tertiary'> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |                 {t('common.apiBasedExtension.selector.title')} | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |               <div | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 className='flex cursor-pointer items-center text-xs text-text-accent' | 
					
						
							| 
									
										
										
										
											2024-10-16 17:40:17 +08:00
										 |  |  |                 onClick={() => { | 
					
						
							|  |  |  |                   setOpen(false) | 
					
						
							|  |  |  |                   setShowAccountSettingModal({ payload: 'api-based-extension' }) | 
					
						
							|  |  |  |                 }} | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |               > | 
					
						
							|  |  |  |                 {t('common.apiBasedExtension.selector.manage')} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <ArrowUpRight className='ml-0.5 h-3 w-3' /> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |               </div> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |             <div className='max-h-[250px] overflow-y-auto'> | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 data?.map(item => ( | 
					
						
							|  |  |  |                   <div | 
					
						
							|  |  |  |                     key={item.id} | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                     className='w-full cursor-pointer rounded-md px-3 py-1.5 text-left hover:stroke-state-base-hover' | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |                     onClick={() => handleSelect(item.id!)} | 
					
						
							|  |  |  |                   > | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                     <div className='text-sm text-text-primary'>{item.name}</div> | 
					
						
							|  |  |  |                     <div className='text-xs text-text-tertiary'>{item.api_endpoint}</div> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |                   </div> | 
					
						
							|  |  |  |                 )) | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |           <div className='h-[1px] bg-divider-regular' /> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |           <div className='p-1'> | 
					
						
							|  |  |  |             <div | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               className='flex h-8 cursor-pointer items-center px-3 text-sm text-text-accent' | 
					
						
							| 
									
										
										
										
											2024-10-16 17:40:17 +08:00
										 |  |  |               onClick={() => { | 
					
						
							|  |  |  |                 setOpen(false) | 
					
						
							|  |  |  |                 setShowApiBasedExtensionModal({ payload: {}, onSaveCallback: () => mutate() }) | 
					
						
							|  |  |  |               }} | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |             > | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <RiAddLine className='mr-2 h-4 w-4' /> | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |               {t('common.operation.add')} | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </PortalToFollowElemContent> | 
					
						
							|  |  |  |     </PortalToFollowElem> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default ApiBasedExtensionSelector |