Tom Butcher 89b6f476c1 Add ModelRoutes and ActionsContext for enhanced model management
- Introduced ModelRoutes to handle dynamic routing for model pages based on object models.
- Created ActionsContext to manage actions and modal states across components, improving action handling and navigation.
- Updated various components to utilize the new buildActionUrl utility for consistent URL generation.
- Refactored existing components to integrate the ActionsProvider, enhancing state management and user interaction.
2026-08-01 18:25:29 +01:00

57 lines
1.6 KiB
JavaScript

import { Suspense, useContext, useEffect } from 'react'
import { useLocation } from 'react-router-dom'
import { Flex, Spin } from 'antd'
import { LoadingOutlined } from '@ant-design/icons'
import PropTypes from 'prop-types'
import { getModelByName } from '../../../database/ObjectModels'
import { getObjectIdFromSearch } from '../../../utils/modelActions'
import { useActions } from '../context/ActionsContext'
import { AuthContext } from '../context/AuthContext'
const ModelPage = ({ modelName, pageName }) => {
const model = getModelByName(modelName)
const page = model.pages?.find((p) => p.name === pageName)
const location = useLocation()
const { setCurrentObject, setCurrentObjectType } = useActions()
const { userProfile } = useContext(AuthContext)
const objectId = getObjectIdFromSearch(modelName, location.search)
useEffect(() => {
setCurrentObjectType(modelName)
if (objectId) {
setCurrentObject({ _id: objectId, _user: userProfile })
}
return () => {
setCurrentObject(null)
setCurrentObjectType(null)
}
}, [modelName, objectId, setCurrentObject, setCurrentObjectType, userProfile])
if (!page?.content) {
return null
}
return (
<Suspense
fallback={
<Flex
justify='center'
align='center'
style={{ width: '100%', height: '100%' }}
>
<Spin indicator={<LoadingOutlined spin />} />
</Flex>
}
>
{page.content()}
</Suspense>
)
}
ModelPage.propTypes = {
modelName: PropTypes.string.isRequired,
pageName: PropTypes.string.isRequired
}
export default ModelPage