@@ -6,6 +6,7 @@ import { MRT_Row } from 'material-react-table';
66import { useEffect , useState } from 'react' ;
77import { useTranslation } from 'react-i18next' ;
88import { useDispatch } from 'react-redux' ;
9+ import semver from 'semver' ;
910import helpers from '../../../helpers' ;
1011import { useFilterFunc } from '../../../lib/util' ;
1112import { PluginInfo , reloadPage , setPluginSettings } from '../../../plugin/pluginsSlice' ;
@@ -285,9 +286,74 @@ export default function PluginSettings() {
285286
286287 const pluginSettings = useTypedSelector ( state => state . plugins . pluginSettings ) ;
287288
289+ const [ plugins , setPlugins ] = useState < PluginInfo [ ] > ( [ ] ) ;
290+
291+ useEffect ( ( ) => {
292+ // to do: this is reused code that needs to be refactored for this specific use, maybe in a different PR as a hook
293+ async function get ( ) {
294+ const pluginPaths = ( await fetch ( `${ helpers . getAppUrl ( ) } plugins` ) . then ( resp =>
295+ resp . json ( )
296+ ) ) as string [ ] ;
297+ const packageInfosPromise = await Promise . all < PluginInfo > (
298+ pluginPaths . map ( path =>
299+ fetch ( `${ helpers . getAppUrl ( ) } ${ path } /package.json` ) . then ( resp => {
300+ if ( ! resp . ok ) {
301+ if ( resp . status !== 404 ) {
302+ return Promise . reject ( resp ) ;
303+ }
304+ {
305+ console . warn (
306+ 'Missing package.json. ' +
307+ `Please upgrade the plugin ${ path } ` +
308+ ' by running "headlamp-plugin extract" again.' +
309+ ' Please use headlamp-plugin >= 0.8.0'
310+ ) ;
311+ return {
312+ name : path . split ( '/' ) . slice ( - 1 ) [ 0 ] ,
313+ version : '0.0.0' ,
314+ author : 'unknown' ,
315+ description : '' ,
316+ } ;
317+ }
318+ }
319+ return resp . json ( ) ;
320+ } )
321+ )
322+ ) ;
323+ const packageInfos = await packageInfosPromise ;
324+
325+ const pluginsWithIsEnabled = packageInfos . map ( plugin => {
326+ const matchedSetting = pluginSettings . find ( p => plugin . name === p . name ) ;
327+ if ( matchedSetting ) {
328+ // to do: compatible version is also reused code that needs to be refactored for this use
329+ const compatibleVersion = '>=0.8.0-alpha.3' ;
330+
331+ const isCompatible = semver . satisfies (
332+ semver . coerce ( plugin . devDependencies ?. [ '@kinvolk/headlamp-plugin' ] ) || '' ,
333+ compatibleVersion
334+ ) ;
335+
336+ return {
337+ ...plugin ,
338+ isEnabled : matchedSetting . isEnabled ,
339+ isCompatible : isCompatible ,
340+ } ;
341+ }
342+ return plugin ;
343+ } ) ;
344+
345+ setPlugins ( pluginsWithIsEnabled ) ;
346+ }
347+ get ( ) ;
348+ } , [ ] ) ;
349+
350+ if ( ! plugins . length ) {
351+ return null ;
352+ }
353+
288354 return (
289355 < PluginSettingsPure
290- plugins = { pluginSettings }
356+ plugins = { plugins }
291357 onSave = { plugins => {
292358 dispatch ( setPluginSettings ( plugins ) ) ;
293359 dispatch ( reloadPage ( ) ) ;
0 commit comments