Skip to content

Commit

Permalink
refactor: merge all contexts into one
Browse files Browse the repository at this point in the history
  • Loading branch information
soofstad authored and pbullhove committed Mar 8, 2024
1 parent 4a06358 commit f602af8
Show file tree
Hide file tree
Showing 54 changed files with 352 additions and 454 deletions.
60 changes: 40 additions & 20 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {
ApplicationContext,
AuthContext,
DMApplicationProvider,
DmssAPI,
EntityView,
FSTreeProvider,
Loading,
RoleProvider,
ErrorResponse,
TApplication,
useDocument,
} from '@development-framework/dm-core'
import '@development-framework/dm-core/dist/main.css'
import { Button, Card, Icon, Typography } from '@equinor/eds-core-react'
import { refresh } from '@equinor/eds-icons'
import './main.css'

import { AxiosError } from 'axios'
import { useContext, useEffect, useState } from 'react'
import { RouterProvider, createBrowserRouter } from 'react-router-dom'
import ViewPage from './ViewPage'

import plugins from './plugins'
const appNotReadyPage = () => (
<div
style={{
Expand Down Expand Up @@ -56,18 +57,33 @@ function App() {
const idReference: string = `${import.meta.env.VITE_DATA_SOURCE}/$${
import.meta.env.VITE_APPLICATION_ID
}`
const {
document: application,
isLoading,
error,
} = useDocument<TApplication>(idReference)
const [application, setApplication] = useState<TApplication>()
const [error, setError] = useState<any>()
const [isLoading, setIsLoading] = useState<boolean>(true)
const { token } = useContext(AuthContext)

if (isLoading) return <Loading />
useEffect(() => {
setIsLoading(true)
const dmssAPI = new DmssAPI(token, import.meta.env.VITE_DMSS_URL)
dmssAPI
.documentGet({ address: idReference })
.then((response: any) => {
setApplication(response.data)
setError(null)
})
.catch((error: AxiosError<ErrorResponse>) => {
console.error(error)
setError(error.response?.data || { message: error.name, data: error })
})
.finally(() => setIsLoading(false))
}, [])

if (error || !application) {
if (isLoading) return <></>
if (error || !application || !application.type) {
console.error(error)
return appNotReadyPage()
}

const router = createBrowserRouter([
{
path: '/',
Expand Down Expand Up @@ -98,15 +114,19 @@ function App() {
),
},
])
const enableBlueprintCache =
import.meta.env.VITE_BLUEPRINT_CACHE_ENABLED === '1' || true

return (
<ApplicationContext.Provider value={application}>
<RoleProvider roles={application?.roles || []}>
<FSTreeProvider visibleDataSources={application?.dataSources || []}>
<RouterProvider router={router} />
</FSTreeProvider>
</RoleProvider>
</ApplicationContext.Provider>
<DMApplicationProvider
plugins={plugins}
application={application}
dmJobPath={import.meta.env.VITE_DM_JOB_URL}
dmssBasePath={import.meta.env.VITE_DMSS_URL}
enableBlueprintCache={enableBlueprintCache}
>
<RouterProvider router={router} />
</DMApplicationProvider>
)
}

Expand Down
32 changes: 2 additions & 30 deletions example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import {
DMJobProvider,
DMSSProvider,
UiPluginProvider,
} from '@development-framework/dm-core'
import React from 'react'
import { ToastContainer } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.min.css'

import ReactDOM from 'react-dom/client'
import { AuthProvider } from 'react-oauth2-code-pkce'
import App from './App'
import plugins from './plugins'

const fullCurrentURL = () =>
`${window.location.pathname}${window.location.search}${window.location.hash}`

const authEnabled = import.meta.env.VITE_AUTH_ENABLED === '1'
const authConfig = {
Expand All @@ -31,34 +21,16 @@ const authConfig = {
}/oauth2/logout`,
}

const Content = () => {
const enableBlueprintCache =
import.meta.env.VITE_BLUEPRINT_CACHE_ENABLED === '1' || true
return (
<DMSSProvider
dmssBasePath={import.meta.env.VITE_DMSS_URL}
enableBlueprintCache={enableBlueprintCache}
>
<DMJobProvider dmJobPath={import.meta.env.VITE_DM_JOB_URL}>
<UiPluginProvider pluginsToLoad={plugins}>
<App />
</UiPluginProvider>
<ToastContainer />
</DMJobProvider>
</DMSSProvider>
)
}

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)

root.render(
<React.StrictMode>
{authEnabled ? (
<AuthProvider authConfig={authConfig}>
<Content />
<App />
</AuthProvider>
) : (
<Content />
<App />
)}
</React.StrictMode>
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
DmssAPI,
IUIPlugin,
Loading,
useDMSS,
useApplication,
useDocument,
} from '@development-framework/dm-core'
import MermaidWrapper from './MermaidWrapper'
Expand Down Expand Up @@ -74,7 +74,7 @@ function useExplorer(dmssAPI: DmssAPI) {

export const BlueprintHierarchyPlugin = (props: IUIPlugin) => {
const { idReference } = props
const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()
const explorer = useExplorer(dmssAPI)

const [chart, setChart] = useState<string | undefined>(undefined)
Expand Down
4 changes: 2 additions & 2 deletions packages/dm-core-plugins/src/blueprint-hierarchy/loader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useDMSS } from '@development-framework/dm-core'
import { useApplication } from '@development-framework/dm-core'
import { IBlueprintType, TAttributeType } from './types'

export class Node {
Expand Down Expand Up @@ -58,7 +58,7 @@ const nonPrimitiveAttributes = (blueprint: IBlueprintType): TAttributeType[] =>
)

const search = async (query: any) => {
const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()

const response = await dmssAPI.search({
dataSources: ['WorkflowDS'],
Expand Down
4 changes: 2 additions & 2 deletions packages/dm-core-plugins/src/data-grid/DataGridPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
Stack,
TAttribute,
TGenericObject,
useApplication,
useBlueprint,
useDMSS,
useDocument,
} from '@development-framework/dm-core'
import { Button, Icon, Tooltip } from '@equinor/eds-core-react'
Expand All @@ -17,7 +17,7 @@ import { getFunctionalityVariables, reverseData } from './utils'
export function DataGridPlugin(props: IUIPlugin) {
const { idReference, config: userConfig, type, onChange } = props
const config: DataGridConfig = { ...defaultConfig, ...userConfig }
const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()
const [data, setData] = useState<any[]>()
const [initialData, setInitialData] = useState<any[]>()
const [loading, setLoading] = useState<boolean>(false)
Expand Down
11 changes: 3 additions & 8 deletions packages/dm-core-plugins/src/explorer/ExplorerPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import {
EBlueprint,
EntityView,
FSTreeContext,
Tree,
TreeNode,
TreeView,
useApplication,
} from '@development-framework/dm-core'
import { Progress } from '@equinor/eds-core-react'
import { useContext, useState } from 'react'
import { useState } from 'react'
import Sidebar from './components/Sidebar'
import NodeRightClickMenu from './components/context-menu/NodeRightClickMenu'

export default () => {
const { treeNodes, loading } = useContext<{
tree: null | Tree
treeNodes: TreeNode[]
loading: boolean
}>(FSTreeContext)
const { treeNodes, loading } = useApplication()
const [selectedType, setSelectedType] = useState<string>()
const [selectedEntity, setSelectedEntity] = useState<string>()
const [nodeDimensions, setNodeDimensions] = useState<string | undefined>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Dialog,
ErrorResponse,
TreeNode,
useDMSS,
useApplication,
} from '@development-framework/dm-core'
import { Button, Progress } from '@equinor/eds-core-react'
import { AxiosError } from 'axios'
Expand All @@ -22,7 +22,7 @@ type TProps = {
const DeleteDialog = (props: TProps) => {
const { setDialogId, node } = props
const [loading, setLoading] = useState<boolean>(false)
const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()

const handleDelete = () => {
setLoading(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import {
ApplicationContext,
BlueprintPicker,
Dialog,
ErrorResponse,
TAttribute,
TBlueprint,
TreeNode,
useDMSS,
useApplication,
} from '@development-framework/dm-core'
import { Button, Progress, TextField } from '@equinor/eds-core-react'
import { AxiosError } from 'axios'
import { useContext, useEffect, useState } from 'react'
import { useEffect, useState } from 'react'
import { toast } from 'react-toastify'
import { EDialog } from '../../types'
import {
Expand All @@ -30,9 +29,7 @@ const NewEntityDialog = (props: TProps) => {
const [blueprint, setBlueprint] = useState<TBlueprint>()
const [newName, setNewName] = useState<string>('')
const [loading, setLoading] = useState<boolean>(false)
const dmssAPI = useDMSS()

const { name } = useContext(ApplicationContext)
const { dmssAPI, name } = useApplication()

useEffect(() => {
if (!blueprintName) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ErrorResponse,
INPUT_FIELD_WIDTH,
TreeNode,
useDMSS,
useApplication,
} from '@development-framework/dm-core'
import { Button, Progress, TextField } from '@equinor/eds-core-react'
import { AxiosError } from 'axios'
Expand All @@ -26,7 +26,7 @@ const NewFolderDialog = (props: TProps) => {
const { setDialogId, node, setNodeOpen, isRoot } = props
const [folderName, setFolderName] = useState<string>('')
const [loading, setLoading] = useState<boolean>(false)
const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()

const handleCreate = () => {
const newFolder = {
Expand Down
4 changes: 2 additions & 2 deletions packages/dm-core-plugins/src/file/DownloadFileButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ErrorResponse,
Loading,
TFileEntity,
useDMSS,
useApplication,
} from '@development-framework/dm-core'
import { Button } from '@equinor/eds-core-react'
import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
Expand All @@ -20,7 +20,7 @@ export const DownloadFileButton = (props: DownloadButtonProps) => {
const [loading, setLoading] = useState<boolean>(true)
const [error, setError] = useState<string | null>(null)

const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()

useEffect(() => {
setError(null)
Expand Down
7 changes: 5 additions & 2 deletions packages/dm-core-plugins/src/file/UploadFileButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { TStorageReference, useDMSS } from '@development-framework/dm-core'
import {
TStorageReference,
useApplication,
} from '@development-framework/dm-core'
import { Button, Progress } from '@equinor/eds-core-react'
import { AxiosError } from 'axios'
import { ChangeEvent, useEffect, useRef, useState } from 'react'
Expand All @@ -14,7 +17,7 @@ export const UploadFileButton = (props: UploadButtonProps) => {
const textInput = useRef<HTMLInputElement>(null)
const [error, setError] = useState<string | undefined>()
const [loading, setLoading] = useState<boolean>(false)
const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()

useEffect(() => setError(undefined))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorResponse, useDMSS } from '@development-framework/dm-core'
import { ErrorResponse, useApplication } from '@development-framework/dm-core'
import { AxiosError, AxiosResponse } from 'axios'
import { useFormContext } from 'react-hook-form'
import { useRegistryContext } from '../context/RegistryContext'
Expand All @@ -12,7 +12,7 @@ const AddObject = (props: {
}) => {
const { type, namePath, defaultValue, onAdd } = props
const { setValue } = useFormContext()
const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()
const { idReference } = useRegistryContext()
const handleAdd = () => {
if (!defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
useApplication,
useDMSS,
useSearch,
} from '@development-framework/dm-core'
import { useApplication, useSearch } from '@development-framework/dm-core'
import { EdsProvider, Menu } from '@equinor/eds-core-react'
import { add } from '@equinor/eds-icons'
import { useState } from 'react'
Expand All @@ -19,17 +15,16 @@ const AddObjectBySearchButton = (props: {
}) => {
const { type, namePath, onAdd } = props
const { setValue } = useFormContext()
const dmssAPI = useDMSS()
const { dmssAPI, visibleDataSources } = useApplication()
const { idReference } = useRegistryContext()
const [isOpen, setIsOpen] = useState<boolean>(false)
const [anchorEl, setAnchorEl] = useState<HTMLButtonElement | null>(null)
const application = useApplication()
const [searchResult] = useSearch<any>(
{
type: 'dmss://system/SIMOS/Blueprint',
extends: [type],
},
application.dataSources
visibleDataSources
)

const openMenu = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { splitAddress, useDMSS } from '@development-framework/dm-core'
import { splitAddress, useApplication } from '@development-framework/dm-core'
import { AxiosResponse } from 'axios'
import { useFormContext } from 'react-hook-form'
import { useRegistryContext } from '../context/RegistryContext'
Expand All @@ -19,7 +19,7 @@ const AddStorageUncontained = (props: {
const { idReference } = useRegistryContext()
const { setValue } = useFormContext()

const dmssAPI = useDMSS()
const { dmssAPI } = useApplication()
const addDocument = () => {
const options = {
shouldDirty: true,
Expand Down
Loading

0 comments on commit f602af8

Please sign in to comment.