-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Admin): dynamically load plugin's widget
- export widget using UMD and external dependencies. - import widget using SystemJS. - render as-is in Dashboard page for now. TODO: - provide a docker layout to organize/resize widgets. - improve development workflow. - test production workflow.
- Loading branch information
1 parent
ff22440
commit bf67ea3
Showing
13 changed files
with
235 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,97 @@ | ||
import Box from "@mui/material/Box"; | ||
import {createElement, Fragment, ReactElement, Suspense, useEffect, useState} from "react"; | ||
import {ErrorBoundary} from "react-error-boundary"; | ||
import {Alert} from "@mui/material"; | ||
import Module = System.Module; | ||
|
||
interface PluginDto { | ||
readonly Name: string; | ||
} | ||
|
||
interface WidgetData { | ||
readonly url: string; | ||
readonly element: ReactElement; | ||
} | ||
|
||
export default function Dashboard() { | ||
const [widgets, setWidgets] = useState<WidgetData[]>([]); | ||
|
||
useEffect(() => { | ||
async function getWidgets() { | ||
const plugins = await getPlugins(); | ||
const widgets: WidgetData[] = []; | ||
|
||
for (const plugin of plugins) { | ||
const url: string = `/api/v1/plugins/${plugin.Name}/assets/widget.umd.js`; | ||
|
||
try { | ||
const module: Module = await System.import(url); | ||
const element: ReactElement = createElement(module.default); | ||
|
||
widgets.push({ | ||
url: url, | ||
element: element, | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
if (lock) { | ||
return; | ||
} | ||
setWidgets(widgets); | ||
} | ||
|
||
let lock: boolean = false; | ||
|
||
getWidgets(); | ||
return () => { | ||
lock = true; | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
|
||
}, [widgets]); | ||
|
||
const getPlugins = async (): Promise<PluginDto[]> => { | ||
const response: Response = await fetch('/api/v1/plugins'); | ||
|
||
if (!response.ok) { | ||
return []; | ||
} | ||
return await response.json() as PluginDto[]; | ||
} | ||
|
||
return ( | ||
<Box className="page center"> | ||
<Alert severity="warning" sx={{m: 'auto'}}>Work in progress</Alert> | ||
<ErrorBoundary fallbackRender={ | ||
({error}) => { | ||
return ( | ||
<Alert severity="error" sx={{width: '500px', m: 'auto'}}> | ||
Error while rendering widgets: {error.message} | ||
</Alert> | ||
); | ||
} | ||
}> | ||
{widgets.map((widget, i) => { | ||
return ( | ||
<Fragment key={i}> | ||
<Box> | ||
<Suspense key={i} | ||
fallback={ | ||
<Alert severity="info" sx={{width: '500px', m: 'auto'}}> | ||
Loading widgets... | ||
</Alert> | ||
}> | ||
{widget.element} | ||
</Suspense> | ||
</Box> | ||
</Fragment> | ||
); | ||
})} | ||
</ErrorBoundary> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,34 @@ | ||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react' | ||
import {defineConfig} from 'vite'; | ||
import react from '@vitejs/plugin-react'; | ||
|
||
// https://vite.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
}) | ||
plugins: [ | ||
react() | ||
], | ||
server: { | ||
proxy: { | ||
'/api': 'http://localhost:11778' | ||
}, | ||
}, | ||
optimizeDeps: { | ||
include: [ | ||
'react', | ||
'@mui/material' | ||
] | ||
}, | ||
build: { | ||
rollupOptions: { | ||
external: [ | ||
'react', | ||
'@mui/material' | ||
], | ||
output: { | ||
globals: { | ||
'react': 'React', | ||
'@mui/material': 'MUI', | ||
} | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {Widget} from './src/widget'; | ||
|
||
export default Widget; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.