Apps.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use client'
  2. import { useCallback, useEffect, useRef, useState } from 'react'
  3. import { useRouter } from 'next/navigation'
  4. import useSWRInfinite from 'swr/infinite'
  5. import { useTranslation } from 'react-i18next'
  6. import { useDebounceFn } from 'ahooks'
  7. import {
  8. RiApps2Line,
  9. RiExchange2Line,
  10. RiMessage3Line,
  11. RiRobot3Line,
  12. RiStackLine
  13. } from '@remixicon/react'
  14. import AppCard from './AppCard'
  15. import NewAppCard from './NewAppCard'
  16. import useAppsQueryState from './hooks/useAppsQueryState'
  17. import type { AppListResponse } from '@/models/app'
  18. import { fetchAppList } from '@/service/apps'
  19. import { useAppContext } from '@/context/app-context'
  20. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  21. import { CheckModal } from '@/hooks/use-pay'
  22. import TabSliderNew from '@/app/components/base/tab-slider-new'
  23. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  24. import Input from '@/app/components/base/input'
  25. import { useStore as useTagStore } from '@/app/components/base/tag-management/store'
  26. import TagManagementModal from '@/app/components/base/tag-management'
  27. import TagFilter from '@/app/components/base/tag-management/filter'
  28. const getKey = (
  29. pageIndex: number,
  30. previousPageData: AppListResponse,
  31. activeTab: string,
  32. tags: string[],
  33. keywords: string,
  34. ) => {
  35. if (!pageIndex || previousPageData.has_more) {
  36. const params: any = { url: 'apps', params: { page: pageIndex + 1, limit: 30, name: keywords } }
  37. if (activeTab !== 'all')
  38. params.params.mode = activeTab
  39. else
  40. delete params.params.mode
  41. if (tags.length)
  42. params.params.tag_ids = tags
  43. return params
  44. }
  45. return null
  46. }
  47. const Apps = () => {
  48. const { t } = useTranslation()
  49. const router = useRouter()
  50. const { isCurrentWorkspaceEditor, isCurrentWorkspaceDatasetOperator } = useAppContext()
  51. const showTagManagementModal = useTagStore(s => s.showTagManagementModal)
  52. const [activeTab, setActiveTab] = useTabSearchParams({
  53. defaultTab: 'all',
  54. })
  55. const { query: { tagIDs = [], keywords = '' }, setQuery } = useAppsQueryState()
  56. const [tagFilterValue, setTagFilterValue] = useState<string[]>(tagIDs)
  57. const [searchKeywords, setSearchKeywords] = useState(keywords)
  58. const setKeywords = useCallback((keywords: string) => {
  59. setQuery(prev => ({ ...prev, keywords }))
  60. }, [setQuery])
  61. const setTagIDs = useCallback((tagIDs: string[]) => {
  62. setQuery(prev => ({ ...prev, tagIDs }))
  63. }, [setQuery])
  64. const { data, isLoading, setSize, mutate } = useSWRInfinite(
  65. (pageIndex: number, previousPageData: AppListResponse) => getKey(pageIndex, previousPageData, activeTab, tagIDs, searchKeywords),
  66. fetchAppList,
  67. { revalidateFirstPage: true },
  68. )
  69. const anchorRef = useRef<HTMLDivElement>(null)
  70. const options = [
  71. { value: 'all', text: t('app.types.all')},
  72. // { value: 'chat', text: t('app.types.chatbot'), icon: <RiMessage3Line className='w-[14px] h-[14px] mr-1' /> },
  73. // { value: 'agent-chat', text: t('app.types.agent'), icon: <RiRobot3Line className='w-[14px] h-[14px] mr-1' /> },
  74. // { value: 'workflow', text: t('app.types.workflow'), icon: <RiExchange2Line className='w-[14px] h-[14px] mr-1' /> },
  75. ]
  76. useEffect(() => {
  77. document.title = `${t('common.menus.apps')} - 智脑云平台`
  78. if (localStorage.getItem(NEED_REFRESH_APP_LIST_KEY) === '1') {
  79. localStorage.removeItem(NEED_REFRESH_APP_LIST_KEY)
  80. mutate()
  81. }
  82. }, [mutate, t])
  83. useEffect(() => {
  84. if (isCurrentWorkspaceDatasetOperator)
  85. return router.replace('/datasets')
  86. }, [router, isCurrentWorkspaceDatasetOperator])
  87. useEffect(() => {
  88. const hasMore = data?.at(-1)?.has_more ?? true
  89. let observer: IntersectionObserver | undefined
  90. if (anchorRef.current) {
  91. observer = new IntersectionObserver((entries) => {
  92. if (entries[0].isIntersecting && !isLoading && hasMore)
  93. setSize((size: number) => size + 1)
  94. }, { rootMargin: '100px' })
  95. observer.observe(anchorRef.current)
  96. }
  97. return () => observer?.disconnect()
  98. }, [isLoading, setSize, anchorRef, mutate, data])
  99. const { run: handleSearch } = useDebounceFn(() => {
  100. setSearchKeywords(keywords)
  101. }, { wait: 500 })
  102. const handleKeywordsChange = (value: string) => {
  103. setKeywords(value)
  104. handleSearch()
  105. }
  106. const { run: handleTagsUpdate } = useDebounceFn(() => {
  107. setTagIDs(tagFilterValue)
  108. }, { wait: 500 })
  109. const handleTagsChange = (value: string[]) => {
  110. setTagFilterValue(value)
  111. handleTagsUpdate()
  112. }
  113. return (
  114. <>
  115. <div className='sticky top-0 flex justify-between items-center pt-4 px-12 pb-2 leading-[56px] z-10 flex-wrap gap-y-2'>
  116. <TabSliderNew
  117. value={activeTab}
  118. onChange={setActiveTab}
  119. options={options}
  120. />
  121. <div className='flex items-center gap-2'>
  122. <TagFilter type='app' value={tagFilterValue} onChange={handleTagsChange} />
  123. <Input
  124. showLeftIcon
  125. showClearIcon
  126. wrapperClassName='w-[200px]'
  127. value={keywords}
  128. onChange={e => handleKeywordsChange(e.target.value)}
  129. onClear={() => handleKeywordsChange('')}
  130. />
  131. </div>
  132. </div>
  133. <nav className='grid content-start grid-cols-1 gap-4 px-12 pt-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 grow shrink-0'>
  134. {isCurrentWorkspaceEditor
  135. && <NewAppCard onSuccess={mutate} />}
  136. {data?.map(({ data: apps }) => apps.map(app => (
  137. <AppCard key={app.id} app={app} onRefresh={mutate} />
  138. )))}
  139. <CheckModal />
  140. </nav>
  141. <div ref={anchorRef} className='h-0'> </div>
  142. {showTagManagementModal && (
  143. <TagManagementModal type='app' show={showTagManagementModal} />
  144. )}
  145. </>
  146. )
  147. }
  148. export default Apps