grafana/public/app/features/datasources/NewDataSourcePage.tsx

245 lines
6.9 KiB
TypeScript
Raw Normal View History

import { css, cx } from '@emotion/css';
import React, { FC, PureComponent } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import { DataSourcePluginMeta, GrafanaTheme2, NavModel } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { Card, LinkButton, List, PluginSignatureBadge, FilterInput, useStyles2 } from '@grafana/ui';
import { Page } from 'app/core/components/Page/Page';
import { StoreState } from 'app/types';
import { PluginsErrorsInfo } from '../plugins/components/PluginsErrorsInfo';
import { addDataSource, loadDataSourcePlugins } from './state/actions';
import { setDataSourceTypeSearchQuery } from './state/reducers';
import { getDataSourcePlugins } from './state/selectors';
2018-10-02 09:18:42 -05:00
function mapStateToProps(state: StoreState) {
return {
navModel: getNavModel(),
plugins: getDataSourcePlugins(state.dataSources),
searchQuery: state.dataSources.dataSourceTypeSearchQuery,
categories: state.dataSources.categories,
isLoading: state.dataSources.isLoadingDataSources,
};
2018-10-02 09:18:42 -05:00
}
const mapDispatchToProps = {
addDataSource,
loadDataSourcePlugins,
setDataSourceTypeSearchQuery,
};
const connector = connect(mapStateToProps, mapDispatchToProps);
type Props = ConnectedProps<typeof connector>;
2018-10-03 09:04:30 -05:00
class NewDataSourcePage extends PureComponent<Props> {
2018-10-03 02:56:15 -05:00
componentDidMount() {
this.props.loadDataSourcePlugins();
2018-10-03 02:56:15 -05:00
}
onDataSourceTypeClicked = (plugin: DataSourcePluginMeta) => {
this.props.addDataSource(plugin);
2018-10-02 09:18:42 -05:00
};
onSearchQueryChange = (value: string) => {
this.props.setDataSourceTypeSearchQuery(value);
2018-10-04 04:42:17 -05:00
};
renderPlugins(plugins: DataSourcePluginMeta[], id?: string) {
if (!plugins || !plugins.length) {
return null;
}
return (
<List
items={plugins}
className={css`
> li {
margin-bottom: 2px;
}
`}
getItemKey={(item) => item.id.toString()}
renderItem={(item) => (
<DataSourceTypeCard
plugin={item}
onClick={() => this.onDataSourceTypeClicked(item)}
onLearnMoreClick={this.onLearnMoreClick}
/>
)}
aria-labelledby={id}
/>
);
}
onLearnMoreClick = (evt: React.SyntheticEvent<HTMLElement>) => {
evt.stopPropagation();
};
renderCategories() {
const { categories } = this.props;
return (
<>
{categories.map((category) => (
<div className="add-data-source-category" key={category.id}>
<div className="add-data-source-category__header" id={category.id}>
{category.title}
</div>
{this.renderPlugins(category.plugins, category.id)}
</div>
))}
<div className="add-data-source-more">
<LinkButton
variant="secondary"
href="https://grafana.com/plugins?type=datasource&utm_source=grafana_add_ds"
target="_blank"
rel="noopener"
>
Find more data source plugins on grafana.com
</LinkButton>
</div>
</>
);
}
2018-10-02 09:18:42 -05:00
render() {
const { navModel, isLoading, searchQuery, plugins } = this.props;
2018-10-02 09:18:42 -05:00
return (
<Page navModel={navModel}>
<Page.Contents isLoading={isLoading}>
<div className="page-action-bar">
<FilterInput value={searchQuery} onChange={this.onSearchQueryChange} placeholder="Filter by name or type" />
<div className="page-action-bar__spacer" />
<LinkButton href="datasources" fill="outline" variant="secondary" icon="arrow-left">
Cancel
</LinkButton>
</div>
{!searchQuery && <PluginsErrorsInfo />}
<div>
{searchQuery && this.renderPlugins(plugins)}
{!searchQuery && this.renderCategories()}
2018-10-03 09:04:30 -05:00
</div>
</Page.Contents>
</Page>
2018-10-02 09:18:42 -05:00
);
}
}
interface DataSourceTypeCardProps {
plugin: DataSourcePluginMeta;
onClick: () => void;
onLearnMoreClick: (evt: React.SyntheticEvent<HTMLElement>) => void;
}
const DataSourceTypeCard: FC<DataSourceTypeCardProps> = (props) => {
const { plugin, onLearnMoreClick } = props;
const isPhantom = plugin.module === 'phantom';
const onClick = !isPhantom && !plugin.unlicensed ? props.onClick : () => {};
// find first plugin info link
const learnMoreLink = plugin.info?.links?.length > 0 ? plugin.info.links[0] : null;
const styles = useStyles2(getStyles);
return (
<Card className={cx(styles.card, 'card-parent')} onClick={onClick}>
<Card.Heading
className={styles.heading}
aria-label={selectors.pages.AddDataSource.dataSourcePluginsV2(plugin.name)}
>
{plugin.name}
</Card.Heading>
<Card.Figure align="center" className={styles.figure}>
<img className={styles.logo} src={plugin.info.logos.small} alt="" />
</Card.Figure>
<Card.Description className={styles.description}>{plugin.info.description}</Card.Description>
{!isPhantom && (
<Card.Meta className={styles.meta}>
<PluginSignatureBadge status={plugin.signature} />
</Card.Meta>
)}
<Card.Actions className={styles.actions}>
{learnMoreLink && (
<LinkButton
variant="secondary"
href={`${learnMoreLink.url}?utm_source=grafana_add_ds`}
target="_blank"
rel="noopener"
onClick={onLearnMoreClick}
icon="external-link-alt"
aria-label={`${plugin.name}, learn more.`}
>
{learnMoreLink.name}
</LinkButton>
)}
</Card.Actions>
</Card>
);
};
function getStyles(theme: GrafanaTheme2) {
return {
heading: css({
fontSize: theme.v1.typography.heading.h5,
fontWeight: 'inherit',
}),
figure: css({
width: 'inherit',
marginRight: '0px',
'> img': {
width: theme.spacing(7),
},
}),
meta: css({
marginTop: '6px',
position: 'relative',
}),
description: css({
margin: '0px',
fontSize: theme.typography.size.sm,
}),
actions: css({
position: 'relative',
alignSelf: 'center',
marginTop: '0px',
opacity: 0,
'.card-parent:hover &, .card-parent:focus-within &': {
opacity: 1,
},
}),
card: css({
gridTemplateAreas: `
"Figure Heading Actions"
"Figure Description Actions"
"Figure Meta Actions"
"Figure - Actions"`,
}),
logo: css({
marginRight: theme.v1.spacing.lg,
marginLeft: theme.v1.spacing.sm,
width: theme.spacing(7),
maxHeight: theme.spacing(7),
}),
};
}
export function getNavModel(): NavModel {
const main = {
icon: 'database',
id: 'datasource-new',
text: 'Add data source',
href: 'datasources/new',
subTitle: 'Choose a data source type',
};
return {
main: main,
node: main,
};
}
Build: Upgrade Webpack 5 (#36444) * build(webpack): bump to v5 and successful yarn start compilation * build(webpack): update postcss dependencies * build(webpack): silence warnings about hash renamed to fullhash * build(webpack): enable persistent cache to store generated webpack modules / chunks * build(webpack): prefer eslintWebpackPlugin over tschecker so eslint doesn't block typechecking * chore(yarn): run yarn-deduplicate to clean up dependencies * chore(yarn): refresh lock file after clean install * build(webpack): prefer output.clean over CleanWebpackPlugin * build(webpack): prefer esbuild over babel-loader for dev config * build(babel): turn off cache compression to improve build performance * build(webpack): get production builds working * build(webpack): remove phantomJS (removed from grafana in v7) specific loader * build(webpack): put back babel for dev builds no performance gain in using esbuild in webpack * build(webpack): prefer terser and optimise css plugins for prod. slower but smaller bundles * build(webpack): clean up redundant code. inform postcss about node_modules * build(webpack): remove deprecation warnings flag * build(webpack): bump packages, dev performance optimisations, attempt to get hot working * chore(storybook): use webpack 5 for dev and production builds * build(storybook): speed up dev build * chore(yarn): refresh lock file * chore(webpack): bump webpack and related deps to latest * refactor(webpack): put back inline-source-map, move start scripts out of grafana toolkit * feat(webpack): prefer react-refresh over react-hot-loader * build(webpack): update webpack.hot to use react-refresh * chore: remove react-hot-loader from codebase * refactor(queryeditorrow): fix circular dependency causing react-fast-refresh errors * revert(webpack): remove stats.errorDetails from common config * build(webpack): bump to v5 and successful yarn start compilation * build(webpack): update postcss dependencies * build(webpack): silence warnings about hash renamed to fullhash * build(webpack): enable persistent cache to store generated webpack modules / chunks * build(webpack): prefer eslintWebpackPlugin over tschecker so eslint doesn't block typechecking * chore(yarn): run yarn-deduplicate to clean up dependencies * chore(yarn): refresh lock file after clean install * build(webpack): prefer output.clean over CleanWebpackPlugin * build(webpack): prefer esbuild over babel-loader for dev config * build(babel): turn off cache compression to improve build performance * build(webpack): get production builds working * build(webpack): remove phantomJS (removed from grafana in v7) specific loader * build(webpack): put back babel for dev builds no performance gain in using esbuild in webpack * build(webpack): prefer terser and optimise css plugins for prod. slower but smaller bundles * build(webpack): clean up redundant code. inform postcss about node_modules * build(webpack): remove deprecation warnings flag * build(webpack): bump packages, dev performance optimisations, attempt to get hot working * chore(storybook): use webpack 5 for dev and production builds * build(storybook): speed up dev build * chore(yarn): refresh lock file * chore(webpack): bump webpack and related deps to latest * refactor(webpack): put back inline-source-map, move start scripts out of grafana toolkit * feat(webpack): prefer react-refresh over react-hot-loader * build(webpack): update webpack.hot to use react-refresh * chore: remove react-hot-loader from codebase * refactor(queryeditorrow): fix circular dependency causing react-fast-refresh errors * revert(webpack): remove stats.errorDetails from common config * revert(webpack): remove include from babel-loader so symlinks (enterprise) work as before * refactor(webpack): fix deprecation warnings in prod builds * fix(storybook): fix failing builds due to replacing css-optimise webpack plugin * fix(storybook): use raw-loader for svg icons * build(webpack): fix dev script colors error * chore(webpack): bump css-loader and react-refresh-webpack-plugin to latest versions Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2021-08-31 05:55:05 -05:00
export default connector(NewDataSourcePage);