@@ -22,21 +22,48 @@ import { Action } from 'redux'
2222import { trackNavigateToPreview } from 'shared/modules/preview/previewDuck'
2323import { connect } from 'react-redux'
2424import { withBus } from 'react-suber'
25+ import { GlobalState } from 'shared/globalState'
26+ import {
27+ Connection ,
28+ getActiveConnectionData
29+ } from 'shared/modules/connections/connectionsDuck'
2530
26- export const navigateToPreview = ( ) : void => {
27- const path = window . location . pathname
28- if ( ! path . endsWith ( '/preview/' ) ) {
29- window . location . pathname = `${ path } ${ path . endsWith ( '/' ) ? '' : '/' } preview/`
31+ export const navigateToPreview = (
32+ db ?: string | null ,
33+ dbms ?: string | null
34+ ) : void => {
35+ const url = new URL ( window . location . href )
36+
37+ if (
38+ dbms &&
39+ ! url . searchParams . has ( 'dbms' ) &&
40+ ! url . searchParams . get ( 'connectURL' )
41+ ) {
42+ url . searchParams . set ( 'dbms' , dbms )
43+ }
44+
45+ if ( db && ! url . searchParams . has ( 'db' ) ) {
46+ url . searchParams . set ( 'db' , db )
47+ }
48+
49+ const previewPath = '/preview/'
50+ if ( ! url . pathname . endsWith ( previewPath ) ) {
51+ url . pathname = url . pathname . replace ( / \/ ? $ / , previewPath )
52+ window . location . href = decodeURIComponent ( url . toString ( ) )
3053 }
3154}
3255
3356type PreviewFrameProps = {
57+ connectionData : Connection | null
3458 executeTrackNavigateToPreview : ( ) => void
3559}
36- const PreviewFrame = ( { executeTrackNavigateToPreview } : PreviewFrameProps ) => {
60+ const PreviewFrame = ( {
61+ connectionData,
62+ executeTrackNavigateToPreview
63+ } : PreviewFrameProps ) => {
3764 function trackAndNavigateToPreview ( ) {
3865 executeTrackNavigateToPreview ( )
39- navigateToPreview ( )
66+ navigateToPreview ( connectionData ?. db , connectionData ?. host )
4067 }
4168
4269 return (
@@ -108,4 +135,10 @@ const mapDispatchToProps = (dispatch: Dispatch<Action>) => {
108135 }
109136}
110137
111- export default withBus ( connect ( null , mapDispatchToProps ) ( PreviewFrame ) )
138+ const mapStateToProps = ( state : GlobalState ) => ( {
139+ connectionData : getActiveConnectionData ( state )
140+ } )
141+
142+ export default withBus (
143+ connect ( mapStateToProps , mapDispatchToProps ) ( PreviewFrame )
144+ )
0 commit comments