You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use client'
  2. import React, { useCallback, useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useContext } from 'use-context-selector'
  5. import useSWR from 'swr'
  6. import { useDebounceFn } from 'ahooks'
  7. import s from './style.module.css'
  8. import cn from '@/utils/classnames'
  9. import ExploreContext from '@/context/explore-context'
  10. import type { App } from '@/models/explore'
  11. import Category from '@/app/components/explore/category'
  12. import AppCard from '@/app/components/explore/app-card'
  13. import { fetchAppDetail, fetchAppList } from '@/service/explore'
  14. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  15. import CreateAppModal from '@/app/components/explore/create-app-modal'
  16. import type { CreateAppModalProps } from '@/app/components/explore/create-app-modal'
  17. import Loading from '@/app/components/base/loading'
  18. import Input from '@/app/components/base/input'
  19. import {
  20. DSLImportMode,
  21. } from '@/models/app'
  22. import { useImportDSL } from '@/hooks/use-import-dsl'
  23. import DSLConfirmModal from '@/app/components/app/create-from-dsl-modal/dsl-confirm-modal'
  24. type AppsProps = {
  25. onSuccess?: () => void
  26. }
  27. export enum PageType {
  28. EXPLORE = 'explore',
  29. CREATE = 'create',
  30. }
  31. const Apps = ({
  32. onSuccess,
  33. }: AppsProps) => {
  34. const { t } = useTranslation()
  35. const { hasEditPermission } = useContext(ExploreContext)
  36. const allCategoriesEn = t('explore.apps.allCategories', { lng: 'en' })
  37. const [keywords, setKeywords] = useState('')
  38. const [searchKeywords, setSearchKeywords] = useState('')
  39. const { run: handleSearch } = useDebounceFn(() => {
  40. setSearchKeywords(keywords)
  41. }, { wait: 500 })
  42. const handleKeywordsChange = (value: string) => {
  43. setKeywords(value)
  44. handleSearch()
  45. }
  46. const [currCategory, setCurrCategory] = useTabSearchParams({
  47. defaultTab: allCategoriesEn,
  48. disableSearchParams: false,
  49. })
  50. const {
  51. data: { categories, allList },
  52. } = useSWR(
  53. ['/explore/apps'],
  54. () =>
  55. fetchAppList().then(({ categories, recommended_apps }) => ({
  56. categories,
  57. allList: recommended_apps.sort((a, b) => a.position - b.position),
  58. })),
  59. {
  60. fallbackData: {
  61. categories: [],
  62. allList: [],
  63. },
  64. },
  65. )
  66. const filteredList = allList.filter(item => currCategory === allCategoriesEn || item.category === currCategory)
  67. const searchFilteredList = useMemo(() => {
  68. if (!searchKeywords || !filteredList || filteredList.length === 0)
  69. return filteredList
  70. const lowerCaseSearchKeywords = searchKeywords.toLowerCase()
  71. return filteredList.filter(item =>
  72. item.app && item.app.name && item.app.name.toLowerCase().includes(lowerCaseSearchKeywords),
  73. )
  74. }, [searchKeywords, filteredList])
  75. const [currApp, setCurrApp] = React.useState<App | null>(null)
  76. const [isShowCreateModal, setIsShowCreateModal] = React.useState(false)
  77. const {
  78. handleImportDSL,
  79. handleImportDSLConfirm,
  80. versions,
  81. isFetching,
  82. } = useImportDSL()
  83. const [showDSLConfirmModal, setShowDSLConfirmModal] = useState(false)
  84. const onCreate: CreateAppModalProps['onConfirm'] = async ({
  85. name,
  86. icon_type,
  87. icon,
  88. icon_background,
  89. description,
  90. }) => {
  91. const { export_data } = await fetchAppDetail(
  92. currApp?.app.id as string,
  93. )
  94. const payload = {
  95. mode: DSLImportMode.YAML_CONTENT,
  96. yaml_content: export_data,
  97. name,
  98. icon_type,
  99. icon,
  100. icon_background,
  101. description,
  102. }
  103. await handleImportDSL(payload, {
  104. onSuccess: () => {
  105. setIsShowCreateModal(false)
  106. },
  107. onPending: () => {
  108. setShowDSLConfirmModal(true)
  109. },
  110. })
  111. }
  112. const onConfirmDSL = useCallback(async () => {
  113. await handleImportDSLConfirm({
  114. onSuccess,
  115. })
  116. }, [handleImportDSLConfirm, onSuccess])
  117. if (!categories || categories.length === 0) {
  118. return (
  119. <div className="flex h-full items-center">
  120. <Loading type="area" />
  121. </div>
  122. )
  123. }
  124. return (
  125. <div className={cn(
  126. 'flex h-full flex-col border-l-[0.5px] border-divider-regular',
  127. )}>
  128. <div className='shrink-0 px-12 pt-6'>
  129. <div className={`mb-1 ${s.textGradient} text-xl font-semibold`}>{t('explore.apps.title')}</div>
  130. <div className='text-sm text-text-tertiary'>{t('explore.apps.description')}</div>
  131. </div>
  132. <div className={cn(
  133. 'mt-6 flex items-center justify-between px-12',
  134. )}>
  135. <>
  136. <Category
  137. list={categories}
  138. value={currCategory}
  139. onChange={setCurrCategory}
  140. allCategoriesEn={allCategoriesEn}
  141. />
  142. </>
  143. <Input
  144. showLeftIcon
  145. showClearIcon
  146. wrapperClassName='w-[200px]'
  147. value={keywords}
  148. onChange={e => handleKeywordsChange(e.target.value)}
  149. onClear={() => handleKeywordsChange('')}
  150. />
  151. </div>
  152. <div className={cn(
  153. 'relative mt-4 flex flex-1 shrink-0 grow flex-col overflow-auto pb-6',
  154. )}>
  155. <nav
  156. className={cn(
  157. s.appList,
  158. 'grid shrink-0 content-start gap-4 px-6 sm:px-12',
  159. )}>
  160. {searchFilteredList.map(app => (
  161. <AppCard
  162. key={app.app_id}
  163. isExplore
  164. app={app}
  165. canCreate={hasEditPermission}
  166. onCreate={() => {
  167. setCurrApp(app)
  168. setIsShowCreateModal(true)
  169. }}
  170. />
  171. ))}
  172. </nav>
  173. </div>
  174. {isShowCreateModal && (
  175. <CreateAppModal
  176. appIconType={currApp?.app.icon_type || 'emoji'}
  177. appIcon={currApp?.app.icon || ''}
  178. appIconBackground={currApp?.app.icon_background || ''}
  179. appIconUrl={currApp?.app.icon_url}
  180. appName={currApp?.app.name || ''}
  181. appDescription={currApp?.app.description || ''}
  182. show={isShowCreateModal}
  183. onConfirm={onCreate}
  184. confirmDisabled={isFetching}
  185. onHide={() => setIsShowCreateModal(false)}
  186. />
  187. )}
  188. {
  189. showDSLConfirmModal && (
  190. <DSLConfirmModal
  191. versions={versions}
  192. onCancel={() => setShowDSLConfirmModal(false)}
  193. onConfirm={onConfirmDSL}
  194. confirmDisabled={isFetching}
  195. />
  196. )
  197. }
  198. </div>
  199. )
  200. }
  201. export default React.memo(Apps)