You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app-picker.tsx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback, useEffect, useRef } from 'react'
  4. import {
  5. PortalToFollowElem,
  6. PortalToFollowElemContent,
  7. PortalToFollowElemTrigger,
  8. } from '@/app/components/base/portal-to-follow-elem'
  9. import type {
  10. OffsetOptions,
  11. Placement,
  12. } from '@floating-ui/react'
  13. import Input from '@/app/components/base/input'
  14. import AppIcon from '@/app/components/base/app-icon'
  15. import type { App } from '@/types/app'
  16. import { useTranslation } from 'react-i18next'
  17. type Props = {
  18. scope: string
  19. disabled: boolean
  20. trigger: React.ReactNode
  21. placement?: Placement
  22. offset?: OffsetOptions
  23. isShow: boolean
  24. onShowChange: (isShow: boolean) => void
  25. onSelect: (app: App) => void
  26. apps: App[]
  27. isLoading: boolean
  28. hasMore: boolean
  29. onLoadMore: () => void
  30. searchText: string
  31. onSearchChange: (text: string) => void
  32. }
  33. const AppPicker: FC<Props> = ({
  34. scope,
  35. disabled,
  36. trigger,
  37. placement = 'right-start',
  38. offset = 0,
  39. isShow,
  40. onShowChange,
  41. onSelect,
  42. apps,
  43. isLoading,
  44. hasMore,
  45. onLoadMore,
  46. searchText,
  47. onSearchChange,
  48. }) => {
  49. const { t } = useTranslation()
  50. const observerTarget = useRef<HTMLDivElement>(null)
  51. const observerRef = useRef<IntersectionObserver | null>(null)
  52. const loadingRef = useRef(false)
  53. const handleIntersection = useCallback((entries: IntersectionObserverEntry[]) => {
  54. const target = entries[0]
  55. if (!target.isIntersecting || loadingRef.current || !hasMore || isLoading) return
  56. loadingRef.current = true
  57. onLoadMore()
  58. // Reset loading state
  59. setTimeout(() => {
  60. loadingRef.current = false
  61. }, 500)
  62. }, [hasMore, isLoading, onLoadMore])
  63. useEffect(() => {
  64. if (!isShow) {
  65. if (observerRef.current) {
  66. observerRef.current.disconnect()
  67. observerRef.current = null
  68. }
  69. return
  70. }
  71. let mutationObserver: MutationObserver | null = null
  72. const setupIntersectionObserver = () => {
  73. if (!observerTarget.current) return
  74. // Create new observer
  75. observerRef.current = new IntersectionObserver(handleIntersection, {
  76. root: null,
  77. rootMargin: '100px',
  78. threshold: 0.1,
  79. })
  80. observerRef.current.observe(observerTarget.current)
  81. }
  82. // Set up MutationObserver to watch DOM changes
  83. mutationObserver = new MutationObserver((mutations) => {
  84. if (observerTarget.current) {
  85. setupIntersectionObserver()
  86. mutationObserver?.disconnect()
  87. }
  88. })
  89. // Watch body changes since Portal adds content to body
  90. mutationObserver.observe(document.body, {
  91. childList: true,
  92. subtree: true,
  93. })
  94. // If element exists, set up IntersectionObserver directly
  95. if (observerTarget.current)
  96. setupIntersectionObserver()
  97. return () => {
  98. if (observerRef.current) {
  99. observerRef.current.disconnect()
  100. observerRef.current = null
  101. }
  102. mutationObserver?.disconnect()
  103. }
  104. }, [isShow, handleIntersection])
  105. const getAppType = (app: App) => {
  106. switch (app.mode) {
  107. case 'advanced-chat':
  108. return 'chatflow'
  109. case 'agent-chat':
  110. return 'agent'
  111. case 'chat':
  112. return 'chat'
  113. case 'completion':
  114. return 'completion'
  115. case 'workflow':
  116. return 'workflow'
  117. }
  118. }
  119. const handleTriggerClick = () => {
  120. if (disabled) return
  121. onShowChange(true)
  122. }
  123. return (
  124. <PortalToFollowElem
  125. placement={placement}
  126. offset={offset}
  127. open={isShow}
  128. onOpenChange={onShowChange}
  129. >
  130. <PortalToFollowElemTrigger
  131. onClick={handleTriggerClick}
  132. >
  133. {trigger}
  134. </PortalToFollowElemTrigger>
  135. <PortalToFollowElemContent className='z-[1000]'>
  136. <div className="relative flex max-h-[400px] min-h-20 w-[356px] flex-col rounded-xl border-[0.5px] border-components-panel-border bg-components-panel-bg-blur shadow-lg backdrop-blur-sm">
  137. <div className='p-2 pb-1'>
  138. <Input
  139. showLeftIcon
  140. showClearIcon
  141. value={searchText}
  142. onChange={e => onSearchChange(e.target.value)}
  143. onClear={() => onSearchChange('')}
  144. />
  145. </div>
  146. <div className='min-h-0 flex-1 overflow-y-auto p-1'>
  147. {apps.map(app => (
  148. <div
  149. key={app.id}
  150. className='flex cursor-pointer items-center gap-3 rounded-lg py-1 pl-2 pr-3 hover:bg-state-base-hover'
  151. onClick={() => onSelect(app)}
  152. >
  153. <AppIcon
  154. className='shrink-0'
  155. size='xs'
  156. iconType={app.icon_type}
  157. icon={app.icon}
  158. background={app.icon_background}
  159. imageUrl={app.icon_url}
  160. />
  161. <div title={app.name} className='system-sm-medium grow text-components-input-text-filled'>{app.name}</div>
  162. <div className='system-2xs-medium-uppercase shrink-0 text-text-tertiary'>{getAppType(app)}</div>
  163. </div>
  164. ))}
  165. <div ref={observerTarget} className='h-4 w-full'>
  166. {isLoading && (
  167. <div className='flex justify-center py-2'>
  168. <div className='text-sm text-gray-500'>{t('common.loading')}</div>
  169. </div>
  170. )}
  171. </div>
  172. </div>
  173. </div>
  174. </PortalToFollowElemContent>
  175. </PortalToFollowElem>
  176. )
  177. }
  178. export default React.memo(AppPicker)