2023-05-15 08:51:32 +08:00
|
|
|
|
'use client'
|
2023-06-06 10:42:32 +08:00
|
|
|
|
import type { FC } from 'react'
|
|
|
|
|
import React, { useEffect, useRef, useState } from 'react'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
import { usePathname, useRouter, useSelectedLayoutSegments } from 'next/navigation'
|
|
|
|
|
import useSWR, { SWRConfig } from 'swr'
|
2023-06-13 16:04:54 +08:00
|
|
|
|
import * as Sentry from '@sentry/react'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
import Header from '../components/header'
|
|
|
|
|
import { fetchAppList } from '@/service/apps'
|
|
|
|
|
import { fetchDatasets } from '@/service/datasets'
|
|
|
|
|
import { fetchLanggeniusVersion, fetchUserProfile, logout } from '@/service/common'
|
|
|
|
|
import Loading from '@/app/components/base/loading'
|
2023-05-20 21:55:47 +08:00
|
|
|
|
import { AppContextProvider } from '@/context/app-context'
|
2023-05-15 08:51:32 +08:00
|
|
|
|
import DatasetsContext from '@/context/datasets-context'
|
|
|
|
|
import type { LangGeniusVersionResponse, UserProfileResponse } from '@/models/common'
|
|
|
|
|
|
2023-06-13 16:04:54 +08:00
|
|
|
|
const isDevelopment = process.env.NODE_ENV === 'development'
|
|
|
|
|
|
2023-05-15 08:51:32 +08:00
|
|
|
|
export type ICommonLayoutProps = {
|
|
|
|
|
children: React.ReactNode
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CommonLayout: FC<ICommonLayoutProps> = ({ children }) => {
|
2023-06-13 16:04:54 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
const SENTRY_DSN = document?.body?.getAttribute('data-public-sentry-dsn')
|
|
|
|
|
if (!isDevelopment && SENTRY_DSN) {
|
|
|
|
|
Sentry.init({
|
|
|
|
|
dsn: SENTRY_DSN,
|
|
|
|
|
integrations: [
|
|
|
|
|
new Sentry.BrowserTracing({
|
|
|
|
|
}),
|
|
|
|
|
new Sentry.Replay(),
|
|
|
|
|
],
|
|
|
|
|
tracesSampleRate: 0.1,
|
|
|
|
|
replaysSessionSampleRate: 0.1,
|
|
|
|
|
replaysOnErrorSampleRate: 1.0,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
2023-05-15 08:51:32 +08:00
|
|
|
|
const router = useRouter()
|
|
|
|
|
const pathname = usePathname()
|
|
|
|
|
const segments = useSelectedLayoutSegments()
|
|
|
|
|
const pattern = pathname.replace(/.*\/app\//, '')
|
|
|
|
|
const [idOrMethod] = pattern.split('/')
|
|
|
|
|
const isNotDetailPage = idOrMethod === 'list'
|
2023-05-20 21:55:47 +08:00
|
|
|
|
const pageContainerRef = useRef<HTMLDivElement>(null)
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
const appId = isNotDetailPage ? '' : idOrMethod
|
|
|
|
|
|
|
|
|
|
const { data: appList, mutate: mutateApps } = useSWR({ url: '/apps', params: { page: 1 } }, fetchAppList)
|
|
|
|
|
const { data: datasetList, mutate: mutateDatasets } = useSWR(segments[0] === 'datasets' ? { url: '/datasets', params: { page: 1 } } : null, fetchDatasets)
|
|
|
|
|
const { data: userProfileResponse, mutate: mutateUserProfile } = useSWR({ url: '/account/profile', params: {} }, fetchUserProfile)
|
|
|
|
|
|
|
|
|
|
const [userProfile, setUserProfile] = useState<UserProfileResponse>()
|
|
|
|
|
const [langeniusVersionInfo, setLangeniusVersionInfo] = useState<LangGeniusVersionResponse>()
|
|
|
|
|
const updateUserProfileAndVersion = async () => {
|
|
|
|
|
if (userProfileResponse && !userProfileResponse.bodyUsed) {
|
|
|
|
|
const result = await userProfileResponse.json()
|
|
|
|
|
setUserProfile(result)
|
|
|
|
|
const current_version = userProfileResponse.headers.get('x-version')
|
|
|
|
|
const current_env = userProfileResponse.headers.get('x-env')
|
|
|
|
|
const versionData = await fetchLanggeniusVersion({ url: '/version', params: { current_version } })
|
|
|
|
|
setLangeniusVersionInfo({ ...versionData, current_version, latest_version: versionData.version, current_env })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
updateUserProfileAndVersion()
|
|
|
|
|
}, [userProfileResponse])
|
|
|
|
|
|
|
|
|
|
if (!appList || !userProfile || !langeniusVersionInfo)
|
|
|
|
|
return <Loading type='app' />
|
|
|
|
|
|
2023-06-06 10:42:32 +08:00
|
|
|
|
const curAppId = segments[0] === 'app' && segments[2]
|
2023-05-15 08:51:32 +08:00
|
|
|
|
const currentDatasetId = segments[0] === 'datasets' && segments[2]
|
|
|
|
|
const currentDataset = datasetList?.data?.find(opt => opt.id === currentDatasetId)
|
|
|
|
|
|
|
|
|
|
// if (!isNotDetailPage && !curApp) {
|
|
|
|
|
// alert('app not found') // TODO: use toast. Now can not get toast context here.
|
|
|
|
|
// // notify({ type: 'error', message: 'App not found' })
|
|
|
|
|
// router.push('/apps')
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const onLogout = async () => {
|
|
|
|
|
await logout({
|
|
|
|
|
url: '/logout',
|
|
|
|
|
params: {},
|
|
|
|
|
})
|
|
|
|
|
router.push('/signin')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<SWRConfig value={{
|
2023-06-06 10:42:32 +08:00
|
|
|
|
shouldRetryOnError: false,
|
2023-05-15 08:51:32 +08:00
|
|
|
|
}}>
|
2023-05-20 21:55:47 +08:00
|
|
|
|
<AppContextProvider value={{ apps: appList.data, mutateApps, userProfile, mutateUserProfile, pageContainerRef }}>
|
|
|
|
|
<DatasetsContext.Provider value={{ datasets: datasetList?.data || [], mutateDatasets, currentDataset }}>
|
|
|
|
|
<div ref={pageContainerRef} className='relative flex flex-col h-full overflow-auto bg-gray-100'>
|
2023-06-06 10:42:32 +08:00
|
|
|
|
<Header
|
|
|
|
|
isBordered={['/apps', '/datasets'].includes(pathname)}
|
|
|
|
|
curAppId={curAppId || ''}
|
|
|
|
|
userProfile={userProfile}
|
|
|
|
|
onLogout={onLogout}
|
|
|
|
|
langeniusVersionInfo={langeniusVersionInfo}
|
|
|
|
|
/>
|
2023-05-15 08:51:32 +08:00
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
</DatasetsContext.Provider>
|
2023-05-20 21:55:47 +08:00
|
|
|
|
</AppContextProvider>
|
2023-05-15 08:51:32 +08:00
|
|
|
|
</SWRConfig>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
export default React.memo(CommonLayout)
|