1+ import { gte } from 'semver'
2+ import { stripScheme } from 'services/boltscheme.utils'
13import { GlobalState } from 'shared/globalState'
24import { inDesktop } from 'shared/modules/app/appDuck'
35import {
6+ getConnectedHost ,
47 getUseDb ,
58 isConnected ,
6- isConnectedAuraHost ,
7- useDb
9+ isConnectedAuraHost
810} from 'shared/modules/connections/connectionsDuck'
911import {
1012 Database ,
1113 findDatabaseByNameOrAlias ,
1214 getAllowOutgoingConnections ,
1315 getClientsAllowTelemetry ,
16+ getDatabases ,
17+ getSemanticVersion ,
1418 isServerConfigDone ,
19+ NAME ,
1520 shouldAllowOutgoingConnections ,
16- SYSTEM_DB
21+ SYSTEM_DB ,
22+ VERSION_FOR_CLUSTER_ROLE_IN_SHOW_DB
1723} from 'shared/modules/dbMeta/dbMetaDuck'
1824import {
1925 getAllowCrashReports ,
@@ -38,6 +44,30 @@ export function isSystemOrCompositeDb(db: Database): boolean {
3844 return db ?. name === SYSTEM_DB || db ?. type === 'composite'
3945}
4046
47+ export const getClusterRoleForCurrentDb = ( state : GlobalState ) => {
48+ const version = getSemanticVersion ( state )
49+ if ( ! version ) return null
50+
51+ if ( gte ( version , VERSION_FOR_CLUSTER_ROLE_IN_SHOW_DB ) ) {
52+ // In a cluster setup, there are many databases with the same name, often one per member
53+ // So our "cluster role" is the role we have on the database that lives
54+ // at the adress we're connected to
55+ const dbName = getUseDb ( state )
56+ const host = getConnectedHost ( state )
57+ if ( dbName && host ) {
58+ const databases = getDatabases ( state )
59+ const currentDb = databases
60+ . filter ( db => db . address === stripScheme ( host ) )
61+ . find ( database => database . name === dbName )
62+ return currentDb ?. role
63+ } else {
64+ return null
65+ }
66+ } else {
67+ return state [ NAME ] . role
68+ }
69+ }
70+
4171export type TelemetrySettingSource =
4272 | 'AURA'
4373 | 'BROWSER_SETTING'
0 commit comments