Files
SCM-Manager/scm-ui/src/containers/PluginLoader.js

121 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-08-24 12:43:10 +02:00
// @flow
import * as React from "react";
import { apiClient, Loading } from "@scm-manager/ui-components";
import { getUiPluginsLink } from "../modules/indexResource";
import { connect } from "react-redux";
2018-08-24 12:43:10 +02:00
type Props = {
2018-11-10 14:54:20 +01:00
loaded: boolean,
children: React.Node,
2018-11-10 14:54:20 +01:00
link: string,
callback: () => void
2018-08-24 12:43:10 +02:00
};
type State = {
message: string
};
type Plugin = {
2018-08-30 11:40:53 +02:00
name: string,
2018-08-24 12:43:10 +02:00
bundles: string[]
};
class PluginLoader extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
message: "booting"
};
}
componentDidMount() {
2018-11-10 14:54:20 +01:00
const { loaded } = this.props;
if (!loaded) {
this.setState({
message: "loading plugin information"
});
2018-11-10 14:54:20 +01:00
this.getPlugins(this.props.link);
}
}
2018-10-11 11:40:04 +02:00
getPlugins = (link: string): Promise<any> => {
return apiClient
.get(link)
2018-08-24 12:43:10 +02:00
.then(response => response.text())
.then(JSON.parse)
.then(pluginCollection => pluginCollection._embedded.plugins)
2018-08-24 12:43:10 +02:00
.then(this.loadPlugins)
2018-11-10 14:54:20 +01:00
.then(this.props.callback);
};
2018-08-24 12:43:10 +02:00
loadPlugins = (plugins: Plugin[]) => {
this.setState({
message: "loading plugins"
});
const promises = [];
const sortedPlugins = plugins.sort(comparePluginsByName);
for (let plugin of sortedPlugins) {
2018-08-24 12:43:10 +02:00
promises.push(this.loadPlugin(plugin));
}
return promises.reduce((chain, current) => {
return chain.then(chainResults => {
return current.then(currentResult => [...chainResults, currentResult])
}
);
}, Promise.resolve([]));
2018-08-24 12:43:10 +02:00
};
2018-08-24 12:43:10 +02:00
loadPlugin = (plugin: Plugin) => {
this.setState({
message: `loading ${plugin.name}`
});
const promises = [];
for (let bundle of plugin.bundles) {
2018-08-27 15:47:02 +02:00
promises.push(this.loadBundle(bundle));
2018-08-24 12:43:10 +02:00
}
return Promise.all(promises);
};
loadBundle = (bundle: string) => {
return fetch(bundle)
.then(response => {
return response.text();
})
.then(script => {
// TODO is this safe???
2018-08-31 10:47:42 +02:00
// eslint-disable-next-line no-eval
2018-08-30 11:56:33 +02:00
eval(script); // NOSONAR
2018-08-24 12:43:10 +02:00
});
};
render() {
2018-11-10 14:54:20 +01:00
const { loaded } = this.props;
const { message } = this.state;
if (loaded) {
2018-08-24 12:43:10 +02:00
return <div>{this.props.children}</div>;
}
return <Loading message={message} />;
}
}
const comparePluginsByName = (a: Plugin, b: Plugin) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
};
const mapStateToProps = state => {
const link = getUiPluginsLink(state);
return {
link
};
};
export default connect(mapStateToProps)(PluginLoader);