Serializer: add interface for snapshot url retrieval
This commit is contained in:
@@ -173,7 +173,7 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
||||
private _prevScrollPos?: number;
|
||||
|
||||
// TODO: use feature toggle to allow v2 serializer
|
||||
private _serializer: DashboardSceneSerializerLike<Dashboard | DashboardV2Spec> = getDashboardSceneSerializer();
|
||||
private _serializer: DashboardSceneSerializerLike<Dashboard | DashboardV2Spec> = getDashboardSceneSerializer(true);
|
||||
|
||||
public constructor(state: Partial<DashboardSceneState>) {
|
||||
super({
|
||||
@@ -646,6 +646,10 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
|
||||
return this._serializer.getInitialVersion();
|
||||
};
|
||||
|
||||
public getSnapshotUrl = () => {
|
||||
return this._serializer.getSnapshotUrl();
|
||||
};
|
||||
|
||||
/** Hacky temp function until we refactor transformSaveModelToScene a bit */
|
||||
public setInitialSaveModel(version: number | undefined, saveModel?: Dashboard | DashboardV2Spec) {
|
||||
this._serializer.initialSaveModel = saveModel;
|
||||
|
||||
@@ -34,7 +34,7 @@ import { DashboardInteractions } from '../utils/interactions';
|
||||
import { DynamicDashNavButtonModel, dynamicDashNavActions } from '../utils/registerDynamicDashNavAction';
|
||||
import { isLibraryPanel } from '../utils/utils';
|
||||
|
||||
import { DashboardScene, isV2Dashboard } from './DashboardScene';
|
||||
import { DashboardScene } from './DashboardScene';
|
||||
import { GoToSnapshotOriginButton } from './GoToSnapshotOriginButton';
|
||||
|
||||
interface Props {
|
||||
@@ -140,17 +140,9 @@ export function ToolbarActions({ dashboard }: Props) {
|
||||
toolbarActions.push({
|
||||
group: 'icon-actions',
|
||||
condition: meta.isSnapshot && !meta.dashboardNotFound && !isEditing,
|
||||
render: () => {
|
||||
const saveModel = dashboard.getInitialSaveModel();
|
||||
|
||||
if (saveModel && isV2Dashboard(saveModel)) {
|
||||
throw new Error('v2 schema not implemented');
|
||||
}
|
||||
|
||||
return (
|
||||
<GoToSnapshotOriginButton key="go-to-snapshot-origin" originalURL={saveModel?.snapshot?.originalUrl ?? ''} />
|
||||
);
|
||||
},
|
||||
render: () => (
|
||||
<GoToSnapshotOriginButton key="go-to-snapshot-origin" originalURL={dashboard.getSnapshotUrl() ?? ''} />
|
||||
),
|
||||
});
|
||||
|
||||
if (!isEditingPanel && !isEditing) {
|
||||
|
||||
@@ -321,6 +321,33 @@ describe('DashboardSceneSerializer', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow retrieving snapshot url', () => {
|
||||
const initialSaveModel: Dashboard = {
|
||||
snapshot: {
|
||||
originalUrl: 'originalUrl/snapshot',
|
||||
created: '2023-01-01T00:00:00Z',
|
||||
expires: '2023-12-31T23:59:59Z',
|
||||
external: false,
|
||||
externalUrl: '',
|
||||
id: 1,
|
||||
key: 'snapshot-key',
|
||||
name: 'snapshot-name',
|
||||
orgId: 1,
|
||||
updated: '2023-01-01T00:00:00Z',
|
||||
userId: 1,
|
||||
},
|
||||
title: 'hello',
|
||||
uid: 'my-uid',
|
||||
schemaVersion: 30,
|
||||
version: 10,
|
||||
};
|
||||
|
||||
const serializer = new V1DashboardSerializer();
|
||||
serializer.initialSaveModel = initialSaveModel;
|
||||
|
||||
expect(serializer.getSnapshotUrl()).toBe('originalUrl/snapshot');
|
||||
});
|
||||
});
|
||||
|
||||
describe('v2 schema', () => {
|
||||
@@ -355,6 +382,11 @@ describe('DashboardSceneSerializer', () => {
|
||||
const serializer = new V2DashboardSerializer();
|
||||
expect(() => serializer.getTrackingInformation()).toThrow('Method not implemented.');
|
||||
});
|
||||
|
||||
it('should throw on getSnapshotUrl', () => {
|
||||
const serializer = new V2DashboardSerializer();
|
||||
expect(() => serializer.getSnapshotUrl()).toThrow('Method not implemented.');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ export interface DashboardSceneSerializerLike<T> {
|
||||
onSaveComplete(saveModel: T, result: SaveDashboardResponseDTO): void;
|
||||
getTrackingInformation: () => DashboardTrackingInfo | undefined;
|
||||
getInitialVersion: () => number | undefined;
|
||||
getSnapshotUrl: () => string | undefined;
|
||||
}
|
||||
|
||||
interface DashboardTrackingInfo {
|
||||
@@ -118,6 +119,10 @@ export class V1DashboardSerializer implements DashboardSceneSerializerLike<Dashb
|
||||
getInitialVersion() {
|
||||
return this.initialVersion;
|
||||
}
|
||||
|
||||
getSnapshotUrl() {
|
||||
return this.initialSaveModel?.snapshot?.originalUrl;
|
||||
}
|
||||
}
|
||||
|
||||
export class V2DashboardSerializer implements DashboardSceneSerializerLike<DashboardV2Spec> {
|
||||
@@ -152,6 +157,11 @@ export class V2DashboardSerializer implements DashboardSceneSerializerLike<Dashb
|
||||
getInitialVersion() {
|
||||
return this.initialVersion;
|
||||
}
|
||||
|
||||
getSnapshotUrl() {
|
||||
throw new Error('v2 schema: Method not implemented.');
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function getDashboardSceneSerializer(
|
||||
@@ -162,7 +172,7 @@ export function getDashboardSceneSerializer(
|
||||
return new V1DashboardSerializer();
|
||||
}
|
||||
|
||||
if (config.featureToggles.dashboardSchemaV2) {
|
||||
if (config.featureToggles.useV2DashboardsAPI) {
|
||||
return new V2DashboardSerializer();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user