Apps.tsx 5.7 KB

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