123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- import React, { PureComponent } from 'react';
- import { Spinner, HorizontalGroup } from '@grafana/ui';
- import { DashboardModel } from '../../state/DashboardModel';
- import {
- historySrv,
- RevisionsModel,
- VersionHistoryTable,
- VersionHistoryHeader,
- VersionsHistoryButtons,
- VersionHistoryComparison,
- } from '../VersionHistory';
- interface Props {
- dashboard: DashboardModel;
- }
- type State = {
- isLoading: boolean;
- isAppending: boolean;
- versions: DecoratedRevisionModel[];
- viewMode: 'list' | 'compare';
- diffData: { lhs: any; rhs: any };
- newInfo?: DecoratedRevisionModel;
- baseInfo?: DecoratedRevisionModel;
- isNewLatest: boolean;
- };
- export type DecoratedRevisionModel = RevisionsModel & {
- createdDateString: string;
- ageString: string;
- };
- export const VERSIONS_FETCH_LIMIT = 10;
- export class VersionsSettings extends PureComponent<Props, State> {
- limit: number;
- start: number;
- constructor(props: Props) {
- super(props);
- this.limit = VERSIONS_FETCH_LIMIT;
- this.start = 0;
- this.state = {
- isAppending: true,
- isLoading: true,
- versions: [],
- viewMode: 'list',
- isNewLatest: false,
- diffData: {
- lhs: {},
- rhs: {},
- },
- };
- }
- componentDidMount() {
- this.getVersions();
- }
- getVersions = (append = false) => {
- this.setState({ isAppending: append });
- historySrv
- .getHistoryList(this.props.dashboard, { limit: this.limit, start: this.start })
- .then((res) => {
- this.setState({
- isLoading: false,
- versions: [...this.state.versions, ...this.decorateVersions(res)],
- });
- this.start += this.limit;
- })
- .catch((err) => console.log(err))
- .finally(() => this.setState({ isAppending: false }));
- };
- getDiff = async () => {
- const selectedVersions = this.state.versions.filter((version) => version.checked);
- const [newInfo, baseInfo] = selectedVersions;
- const isNewLatest = newInfo.version === this.props.dashboard.version;
- this.setState({
- isLoading: true,
- });
- const lhs = await historySrv.getDashboardVersion(this.props.dashboard.id, baseInfo.version);
- const rhs = await historySrv.getDashboardVersion(this.props.dashboard.id, newInfo.version);
- this.setState({
- baseInfo,
- isLoading: false,
- isNewLatest,
- newInfo,
- viewMode: 'compare',
- diffData: {
- lhs: lhs.data,
- rhs: rhs.data,
- },
- });
- };
- decorateVersions = (versions: RevisionsModel[]) =>
- versions.map((version) => ({
- ...version,
- createdDateString: this.props.dashboard.formatDate(version.created),
- ageString: this.props.dashboard.getRelativeTime(version.created),
- checked: false,
- }));
- isLastPage() {
- return this.state.versions.find((rev) => rev.version === 1);
- }
- onCheck = (ev: React.FormEvent<HTMLInputElement>, versionId: number) => {
- this.setState({
- versions: this.state.versions.map((version) =>
- version.id === versionId ? { ...version, checked: ev.currentTarget.checked } : version
- ),
- });
- };
- reset = () => {
- this.setState({
- baseInfo: undefined,
- diffData: {
- lhs: {},
- rhs: {},
- },
- isNewLatest: false,
- newInfo: undefined,
- versions: this.state.versions.map((version) => ({ ...version, checked: false })),
- viewMode: 'list',
- });
- };
- render() {
- const { versions, viewMode, baseInfo, newInfo, isNewLatest, isLoading, diffData } = this.state;
- const canCompare = versions.filter((version) => version.checked).length === 2;
- const showButtons = versions.length > 1;
- const hasMore = versions.length >= this.limit;
- if (viewMode === 'compare') {
- return (
- <div>
- <VersionHistoryHeader
- isComparing
- onClick={this.reset}
- baseVersion={baseInfo?.version}
- newVersion={newInfo?.version}
- isNewLatest={isNewLatest}
- />
- {isLoading ? (
- <VersionsHistorySpinner msg="Fetching changes…" />
- ) : (
- <VersionHistoryComparison
- newInfo={newInfo!}
- baseInfo={baseInfo!}
- isNewLatest={isNewLatest}
- diffData={diffData}
- />
- )}
- </div>
- );
- }
- return (
- <div>
- <VersionHistoryHeader />
- {isLoading ? (
- <VersionsHistorySpinner msg="Fetching history list…" />
- ) : (
- <VersionHistoryTable versions={versions} onCheck={this.onCheck} canCompare={canCompare} />
- )}
- {this.state.isAppending && <VersionsHistorySpinner msg="Fetching more entries…" />}
- {showButtons && (
- <VersionsHistoryButtons
- hasMore={hasMore}
- canCompare={canCompare}
- getVersions={this.getVersions}
- getDiff={this.getDiff}
- isLastPage={!!this.isLastPage()}
- />
- )}
- </div>
- );
- }
- }
- const VersionsHistorySpinner = ({ msg }: { msg: string }) => (
- <HorizontalGroup>
- <Spinner />
- <em>{msg}</em>
- </HorizontalGroup>
- );
|