88import React from 'react' ;
99import _ from 'lodash' ;
1010import { finalize , switchMap , tap } from 'rxjs/operators' ;
11+ import { EuiSpacer } from '@elastic/eui' ;
1112import { i18n } from '@kbn/i18n' ;
12- import { AppLeaveAction , AppMountParameters } from 'kibana/public' ;
13+ import { AppLeaveAction , AppMountParameters , HttpStart } from 'kibana/public' ;
1314import { Adapters } from 'src/plugins/embeddable/public' ;
1415import { Subscription } from 'rxjs' ;
1516import {
@@ -54,13 +55,17 @@ import {
5455 unsavedChangesWarning ,
5556} from '../saved_map' ;
5657import { waitUntilTimeLayersLoad$ } from './wait_until_time_layers_load' ;
58+ import { SpacesPluginStart } from '../../../../../spaces/public' ;
59+ import { resolveSavedObject } from '../../../map_attribute_service' ;
5760
5861interface MapRefreshConfig {
5962 isPaused : boolean ;
6063 interval : number ;
6164}
6265
6366export interface Props {
67+ spacesApi ?: SpacesPluginStart ;
68+ http : HttpStart ;
6469 savedMap : SavedMap ;
6570 // saveCounter used to trigger MapApp render after SaveMap.save
6671 saveCounter : number ;
@@ -97,6 +102,7 @@ export interface State {
97102 savedQuery ?: SavedQuery ;
98103 isRefreshPaused : boolean ;
99104 refreshInterval : number ;
105+ savedObjectWarning : unknown ;
100106}
101107
102108export class MapApp extends React . Component < Props , State > {
@@ -115,6 +121,7 @@ export class MapApp extends React.Component<Props, State> {
115121 initialized : false ,
116122 isRefreshPaused : true ,
117123 refreshInterval : 0 ,
124+ savedObjectWarning : null ,
118125 } ;
119126 }
120127
@@ -363,6 +370,7 @@ export class MapApp extends React.Component<Props, State> {
363370 }
364371
365372 this . _initMapAndLayerSettings ( this . props . savedMap . getAttributes ( ) ) ;
373+ await this . getLegacyUrlConflictCallout ( savedObjectId ) ;
366374
367375 this . setState ( { initialized : true } ) ;
368376 }
@@ -434,6 +442,41 @@ export class MapApp extends React.Component<Props, State> {
434442 ) ;
435443 }
436444
445+ async getLegacyUrlConflictCallout ( savedObjectId : string | undefined ) {
446+ if ( ! savedObjectId ) {
447+ return ;
448+ }
449+ await this . props . savedMap . whenReady ( ) ;
450+ const { resolvedSavedObject } = await resolveSavedObject ( savedObjectId ) ;
451+ // This function returns a callout component *if* we have encountered a "legacy URL conflict" scenario
452+ if ( this . props . spacesApi && resolvedSavedObject ) {
453+ if ( resolvedSavedObject . outcome === 'conflict' ) {
454+ // We have resolved to one object, but another object has a legacy URL alias associated with this ID/page. We should display a
455+ // callout with a warning for the user, and provide a way for them to navigate to the other object.
456+ const currentObjectId = resolvedSavedObject . saved_object . id ;
457+ const otherObjectId = resolvedSavedObject . alias_target_id ! ; // This is always defined if outcome === 'conflict'
458+ // const otherObjectPath = this.props.http.basePath.prepend(
459+ // `${VIEW_NOTE_PATH}/${otherObjectId}${window.location.hash}`
460+ // );
461+ const otherObjectPath = window . location . href . replace ( currentObjectId , otherObjectId ) ;
462+ console . log ( this . props . http . basePath ) ;
463+ this . setState ( {
464+ savedObjectWarning : (
465+ < >
466+ { this . props . spacesApi . ui . components . getLegacyUrlConflict ( {
467+ objectNoun : 'Saved map' ,
468+ currentObjectId,
469+ otherObjectId,
470+ otherObjectPath,
471+ } ) }
472+ < EuiSpacer />
473+ </ >
474+ ) ,
475+ } ) ;
476+ }
477+ }
478+ }
479+
437480 _addFilter = async ( newFilters : Filter [ ] ) => {
438481 newFilters . forEach ( ( filter ) => {
439482 filter . $state = { store : esFilters . FilterStateStore . APP_STATE } ;
@@ -451,6 +494,7 @@ export class MapApp extends React.Component<Props, State> {
451494 { this . _renderTopNav ( ) }
452495 < h1 className = "euiScreenReaderOnly" > { `screenTitle placeholder` } </ h1 >
453496 < div id = "react-maps-root" >
497+ { this . state . savedObjectWarning }
454498 < MapContainer
455499 addFilters = { this . _addFilter }
456500 title = { this . props . savedMap . getAttributes ( ) . title }
0 commit comments