Apps.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use client'
  2. import { useEffect, useRef, useState } from 'react'
  3. import useSWRInfinite from 'swr/infinite'
  4. import { useTranslation } from 'react-i18next'
  5. import { useDebounceFn } from 'ahooks'
  6. import AppCard from './AppCard'
  7. import NewAppCard from './NewAppCard'
  8. import type { AppListResponse } from '@/models/app'
  9. import { fetchAppList } from '@/service/apps'
  10. import { useAppContext } from '@/context/app-context'
  11. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  12. import { CheckModal } from '@/hooks/use-pay'
  13. import TabSliderNew from '@/app/components/base/tab-slider-new'
  14. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  15. import { DotsGrid, SearchLg } from '@/app/components/base/icons/src/vender/line/general'
  16. import { XCircle } from '@/app/components/base/icons/src/vender/solid/general'
  17. import {
  18. // AiText,
  19. ChatBot,
  20. CuteRobot,
  21. } from '@/app/components/base/icons/src/vender/line/communication'
  22. import { Route } from '@/app/components/base/icons/src/vender/line/mapsAndTravel'
  23. const getKey = (
  24. pageIndex: number,
  25. previousPageData: AppListResponse,
  26. activeTab: string,
  27. keywords: string,
  28. ) => {
  29. if (!pageIndex || previousPageData.has_more) {
  30. const params: any = { url: 'apps', params: { page: pageIndex + 1, limit: 30, name: keywords } }
  31. if (activeTab !== 'all')
  32. params.params.mode = activeTab
  33. else
  34. delete params.params.mode
  35. return params
  36. }
  37. return null
  38. }
  39. const Apps = () => {
  40. const { t } = useTranslation()
  41. const { isCurrentWorkspaceManager } = useAppContext()
  42. const [activeTab, setActiveTab] = useTabSearchParams({
  43. defaultTab: 'all',
  44. })
  45. const [keywords, setKeywords] = useState('')
  46. const [searchKeywords, setSearchKeywords] = useState('')
  47. const { data, isLoading, setSize, mutate } = useSWRInfinite(
  48. (pageIndex: number, previousPageData: AppListResponse) => getKey(pageIndex, previousPageData, activeTab, searchKeywords),
  49. fetchAppList,
  50. { revalidateFirstPage: true },
  51. )
  52. const anchorRef = useRef<HTMLDivElement>(null)
  53. const options = [
  54. { value: 'all', text: t('app.types.all'), icon: <DotsGrid className='w-[14px] h-[14px] mr-1'/> },
  55. { value: 'chat', text: t('app.types.chatbot'), icon: <ChatBot className='w-[14px] h-[14px] mr-1'/> },
  56. { value: 'agent-chat', text: t('app.types.agent'), icon: <CuteRobot className='w-[14px] h-[14px] mr-1'/> },
  57. // { value: 'completion', text: t('app.newApp.completeApp'), icon: <AiText className='w-[14px] h-[14px] mr-1'/> },
  58. { value: 'workflow', text: t('app.types.workflow'), icon: <Route className='w-[14px] h-[14px] mr-1'/> },
  59. ]
  60. useEffect(() => {
  61. document.title = `${t('common.menus.apps')} - Dify`
  62. if (localStorage.getItem(NEED_REFRESH_APP_LIST_KEY) === '1') {
  63. localStorage.removeItem(NEED_REFRESH_APP_LIST_KEY)
  64. mutate()
  65. }
  66. }, [])
  67. useEffect(() => {
  68. let observer: IntersectionObserver | undefined
  69. if (anchorRef.current) {
  70. observer = new IntersectionObserver((entries) => {
  71. if (entries[0].isIntersecting && !isLoading)
  72. setSize((size: number) => size + 1)
  73. }, { rootMargin: '100px' })
  74. observer.observe(anchorRef.current)
  75. }
  76. return () => observer?.disconnect()
  77. }, [isLoading, setSize, anchorRef, mutate])
  78. const { run: handleSearch } = useDebounceFn(() => {
  79. setSearchKeywords(keywords)
  80. }, { wait: 500 })
  81. const handleKeywordsChange = (value: string) => {
  82. setKeywords(value)
  83. handleSearch()
  84. }
  85. const handleClear = () => {
  86. handleKeywordsChange('')
  87. }
  88. return (
  89. <>
  90. <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'>
  91. <TabSliderNew
  92. value={activeTab}
  93. onChange={setActiveTab}
  94. options={options}
  95. />
  96. <div className="flex items-center px-2 w-[200px] h-8 rounded-lg bg-gray-200">
  97. <div className="pointer-events-none shrink-0 flex items-center mr-1.5 justify-center w-4 h-4">
  98. <SearchLg className="h-3.5 w-3.5 text-gray-500" aria-hidden="true" />
  99. </div>
  100. <input
  101. type="text"
  102. name="query"
  103. className="grow block h-[18px] bg-gray-200 rounded-md border-0 text-gray-600 text-[13px] placeholder:text-gray-500 appearance-none outline-none"
  104. placeholder={t('common.operation.search')!}
  105. value={keywords}
  106. onChange={(e) => {
  107. handleKeywordsChange(e.target.value)
  108. }}
  109. autoComplete="off"
  110. />
  111. {
  112. keywords && (
  113. <div
  114. className='shrink-0 flex items-center justify-center w-4 h-4 cursor-pointer'
  115. onClick={handleClear}
  116. >
  117. <XCircle className='w-3.5 h-3.5 text-gray-400' />
  118. </div>
  119. )
  120. }
  121. </div>
  122. </div>
  123. <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'>
  124. {isCurrentWorkspaceManager
  125. && <NewAppCard onSuccess={mutate} />}
  126. {data?.map(({ data: apps }: any) => apps.map((app: any) => (
  127. <AppCard key={app.id} app={app} onRefresh={mutate} />
  128. )))}
  129. <CheckModal />
  130. </nav>
  131. <div ref={anchorRef} className='h-0'> </div>
  132. </>
  133. )
  134. }
  135. export default Apps