Apps.tsx 4.5 KB

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