index.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { useRouter } from 'next/navigation'
  5. import { useTranslation } from 'react-i18next'
  6. import ExploreContext from '@/context/explore-context'
  7. import Sidebar from '@/app/components/explore/sidebar'
  8. import { useAppContext } from '@/context/app-context'
  9. import { fetchMembers } from '@/service/common'
  10. import type { InstalledApp } from '@/models/explore'
  11. export type IExploreProps = {
  12. children: React.ReactNode
  13. }
  14. const Explore: FC<IExploreProps> = ({
  15. children,
  16. }) => {
  17. const { t } = useTranslation()
  18. const router = useRouter()
  19. const [controlUpdateInstalledApps, setControlUpdateInstalledApps] = useState(0)
  20. const { userProfile, isCurrentWorkspaceDatasetOperator } = useAppContext()
  21. const [hasEditPermission, setHasEditPermission] = useState(false)
  22. const [installedApps, setInstalledApps] = useState<InstalledApp[]>([])
  23. useEffect(() => {
  24. document.title = `${t('explore.title')} - Dify`;
  25. (async () => {
  26. const { accounts } = await fetchMembers({ url: '/workspaces/current/members', params: {} })
  27. if (!accounts)
  28. return
  29. const currUser = accounts.find(account => account.id === userProfile.id)
  30. setHasEditPermission(currUser?.role !== 'normal')
  31. })()
  32. }, [])
  33. useEffect(() => {
  34. if (isCurrentWorkspaceDatasetOperator)
  35. return router.replace('/datasets')
  36. }, [isCurrentWorkspaceDatasetOperator])
  37. return (
  38. <div className='flex h-full bg-gray-100 border-t border-gray-200 overflow-hidden'>
  39. <ExploreContext.Provider
  40. value={
  41. {
  42. controlUpdateInstalledApps,
  43. setControlUpdateInstalledApps,
  44. hasEditPermission,
  45. installedApps,
  46. setInstalledApps,
  47. }
  48. }
  49. >
  50. <Sidebar controlUpdateInstalledApps={controlUpdateInstalledApps} />
  51. <div className='grow w-0'>
  52. {children}
  53. </div>
  54. </ExploreContext.Provider>
  55. </div>
  56. )
  57. }
  58. export default React.memo(Explore)