Apps.tsx 4.6 KB

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