grafana/public/app/features/dashboard/components/DashboardSettings/VersionsSettings.tsx
Jack Westbrook c9eff1892e
DashboardSettings: Migrates history diff view from angular to react (#31997)
* chore(dashboardsettings): introduce deep-diff for diffing dashboard models

* feat(dashboardsettings): initial commit of diff comparision react migration

* feat(dashboardsettings): wip - use json-source-map and monaco editor

* chore(deps): add react-diff-viewer to package.json

* feat(dashboardsettings): take the simplistic road for diff view

* refactor(dashboardsettings): clean up Version Settings components

* chore: delete angular historyListCtrl code

* refactor(dashboardsettings): styling fixes

* Small color tweaks

* refactor(versionhistory): fix issues around summary text. write tests

* test(versionhistory): add test for jsonDiff

* refactor(versionhistory): cleanup utils and reduce dom elements

* test(versionsettings): add tests for comparision view

* test(versionsettings): finialise tests for version history comparison view

* test(versionsettings): remove debug

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2021-03-25 11:51:09 +01:00

193 lines
5.0 KiB
TypeScript

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&hellip;" />
) : (
<VersionHistoryComparison
newInfo={newInfo!}
baseInfo={baseInfo!}
isNewLatest={isNewLatest}
diffData={diffData}
/>
)}
</div>
);
}
return (
<div>
<VersionHistoryHeader />
{isLoading ? (
<VersionsHistorySpinner msg="Fetching history list&hellip;" />
) : (
<VersionHistoryTable versions={versions} onCheck={this.onCheck} />
)}
{this.state.isAppending && <VersionsHistorySpinner msg="Fetching more entries&hellip;" />}
{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>
);