import React, { useEffect, useState } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import { HorizontalGroup, useTheme2 } from '@grafana/ui'; import PageHeader from 'app/core/components/PageHeader/PageHeader'; import { UpgradeBox, UpgradeContent, UpgradeContentProps } from 'app/core/components/Upgrade/UpgradeBox'; import { GrafanaRouteComponentProps } from 'app/core/navigation/types'; import { getNavModel } from 'app/core/selectors/navModel'; import { highlightTrial } from 'app/features/admin/utils'; import { loadDataSource, loadDataSourceMeta } from 'app/features/datasources/state/actions'; import { getDataSourceLoadingNav } from 'app/features/datasources/state/navModel'; import { EnterpriseStoreState } from '../types'; import { CacheCTA } from './CacheCTA'; import { CacheClean } from './CacheClean'; import { CacheSettingsForm } from './CacheSettingsForm'; import { loadDataSourceCache, enableDataSourceCache, disableDataSourceCache, updateDataSourceCache, cleanCache, } from './state/actions'; interface RouteProps extends GrafanaRouteComponentProps<{ uid: string }> {} function mapStateToProps(state: EnterpriseStoreState, props: RouteProps) { const dataSourceUid = props.match.params.uid; const dataSourceLoadingNav = getDataSourceLoadingNav('cache'); return { ...state.dataSourceCache, navModel: getNavModel(state.navIndex, `datasource-cache-${dataSourceUid}`, dataSourceLoadingNav), pageId: dataSourceUid, isDefault: state.dataSources.dataSource.isDefault, }; } const mapDispatchToProps = { disableDataSourceCache, updateDataSourceCache, enableDataSourceCache, loadDataSourceCache, loadDataSource, cleanCache, loadDataSourceMeta, }; export const connector = connect(mapStateToProps, mapDispatchToProps); export type Props = ConnectedProps; export const Caching = (props: Props) => { const [enabled, setEnabled] = useState(props.enabled); const [loading, setLoading] = useState(true); const [useDefaultTTL, setUseDefaultTTL] = useState(props.useDefaultTTL); const [ttlQueriesMs, setTtlQueriesMs] = useState(props.ttlQueriesMs); const [ttlResourcesMs, setTtlResourcesMs] = useState(props.ttlResourcesMs); const { navModel, pageId, loadDataSource, loadDataSourceCache, loadDataSourceMeta, enableDataSourceCache } = props; useEffect(() => { setEnabled(props.enabled); setUseDefaultTTL(props.useDefaultTTL); setTtlQueriesMs(props.ttlQueriesMs); setTtlResourcesMs(props.ttlResourcesMs); setLoading(false); }, [props.useDefaultTTL, props.ttlQueriesMs, props.ttlResourcesMs, props.enabled]); useEffect(() => { loadDataSource(pageId).then(loadDataSourceMeta); loadDataSourceCache(pageId); }, [loadDataSourceCache, loadDataSource, loadDataSourceMeta, pageId]); const content = enabled ? ( CacheSettingsForm({ ...props, loading, useDefaultTTL, setUseDefaultTTL, ttlQueriesMs, setTtlQueriesMs, ttlResourcesMs, setTtlResourcesMs, }) ) : highlightTrial() ? ( { enableDataSourceCache(pageId); }, }} /> ) : ( CacheCTA(props) ); return (
{highlightTrial() && ( )}
{enabled && !highlightTrial() &&

Caching

}
{enabled && }
{content}
); }; export interface DataSourceCacheUpgradeContentProps { action?: UpgradeContentProps['action']; } export const DataSourceCacheUpgradeContent = ({ action }: DataSourceCacheUpgradeContentProps) => { const theme = useTheme2(); return ( ); }; export default connector(Caching);