@@ -22,21 +22,48 @@ import { Action } from 'redux'
22
22
import { trackNavigateToPreview } from 'shared/modules/preview/previewDuck'
23
23
import { connect } from 'react-redux'
24
24
import { withBus } from 'react-suber'
25
+ import { GlobalState } from 'shared/globalState'
26
+ import {
27
+ Connection ,
28
+ getActiveConnectionData
29
+ } from 'shared/modules/connections/connectionsDuck'
25
30
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 ( ) )
30
53
}
31
54
}
32
55
33
56
type PreviewFrameProps = {
57
+ connectionData : Connection | null
34
58
executeTrackNavigateToPreview : ( ) => void
35
59
}
36
- const PreviewFrame = ( { executeTrackNavigateToPreview } : PreviewFrameProps ) => {
60
+ const PreviewFrame = ( {
61
+ connectionData,
62
+ executeTrackNavigateToPreview
63
+ } : PreviewFrameProps ) => {
37
64
function trackAndNavigateToPreview ( ) {
38
65
executeTrackNavigateToPreview ( )
39
- navigateToPreview ( )
66
+ navigateToPreview ( connectionData ?. db , connectionData ?. host )
40
67
}
41
68
42
69
return (
@@ -108,4 +135,10 @@ const mapDispatchToProps = (dispatch: Dispatch<Action>) => {
108
135
}
109
136
}
110
137
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