11import React , { useCallback , useEffect , useState } from 'react' ;
22import PropTypes from 'prop-types' ;
33import queryString from 'query-string' ;
4+ import keyBy from 'lodash.keyby' ;
5+ import isEmpty from 'lodash.isempty' ;
46import Button from '@leafygreen-ui/button' ;
57import { css , cx } from '@leafygreen-ui/emotion' ;
6- import { getSiteUrl } from '../utils/get-site-url' ;
78import { isBrowser } from '../utils/is-browser' ;
89import { theme } from '../theme/docsTheme' ;
10+ import { fetchDocuments } from '../utils/realm' ;
11+ import { useSiteMetadata } from '../hooks/use-site-metadata' ;
12+ import { BRANCHES_COLLECTION } from '../build-constants' ;
913import Select from './Select' ;
1014
1115const SELECT_WIDTH = '336px' ;
@@ -19,30 +23,6 @@ const selectStyle = css`
1923 }
2024` ;
2125
22- const PROPERTY_NAME_MAPPING = {
23- 'atlas-open-service-broker' : 'MongoDB Atlas Open Service Broker on Kubernetes' ,
24- 'atlas-cli' : 'MongoDB Atlas CLI' ,
25- 'bi-connector' : 'MongoDB Connector for BI' ,
26- charts : 'MongoDB Charts' ,
27- cloud : 'MongoDB Atlas' ,
28- compass : 'MongoDB Compass' ,
29- docs : 'MongoDB Server' ,
30- drivers : 'MongoDB Drivers' ,
31- 'kafka-connector' : 'MongoDB Kafka Connector' ,
32- 'kubernetes-operator' : 'MongoDB Enterprise Kubernetes Operator' ,
33- mongocli : 'MongoDB CLI' ,
34- mongoid : 'Mongoid' ,
35- mms : 'MongoDB Ops Manager' ,
36- 'ruby-driver' : 'MongoDB Ruby Driver' ,
37- 'spark-connector' : 'MongoDB Connector for Spark' ,
38- } ;
39-
40- const fullProductName = ( property ) => {
41- if ( ! property ) return null ;
42- // Display full product name on product dropdown
43- return PROPERTY_NAME_MAPPING [ property . replace ( '_' , '-' ) ] || property ;
44- } ;
45-
4626const isPrimaryBranch = ( version ) => {
4727 return version === 'main' || version === 'master' ;
4828} ;
@@ -64,16 +44,52 @@ const isVersioned = (versionOptions) => {
6444 return ! ( versionOptions . length === 1 && isPrimaryBranch ( versionOptions [ 0 ] ) ) ;
6545} ;
6646
47+ // Validation for necessary url fields to bypass errors
48+ const hasValidHostName = ( repoDocument ) => {
49+ if ( ! repoDocument ?. url ?. dotcomprd || ! repoDocument ?. prefix ?. dotcomprd ) return false ;
50+ return true ;
51+ } ;
52+
53+ // Add mms-docs to reposMap. It does not have a document in repos_branches collection.
54+ // TODO: Remove when mms-docs is added to repos_branches
55+ const addOldGenToReposMap = ( reposMap ) => {
56+ const oldGenRepos = {
57+ mms : {
58+ displayName : 'MongoDB Ops Manager' ,
59+ url : { dotcomprd : 'http://mongodb.com/' } ,
60+ prefix : { dotcomprd : 'docs/ops-manager' } ,
61+ } ,
62+ } ;
63+ return {
64+ ...oldGenRepos ,
65+ ...reposMap ,
66+ } ;
67+ } ;
68+
6769const DeprecatedVersionSelector = ( { metadata : { deprecated_versions : deprecatedVersions } } ) => {
70+ const { reposDatabase } = useSiteMetadata ( ) ;
6871 const [ product , setProduct ] = useState ( '' ) ;
6972 const [ version , setVersion ] = useState ( '' ) ;
73+ const [ reposMap , setReposMap ] = useState ( { } ) ;
74+
7075 const updateProduct = useCallback ( ( { value } ) => {
7176 setProduct ( value ) ;
7277 setVersion ( '' ) ;
7378 } , [ ] ) ;
7479 const updateVersion = useCallback ( ( { value } ) => setVersion ( value ) , [ ] ) ;
7580 const buttonDisabled = ! ( product && version ) ;
7681
82+ // Fetch repos_branches for `displayName` and url
83+ useEffect ( ( ) => {
84+ if ( reposDatabase ) {
85+ fetchDocuments ( reposDatabase , BRANCHES_COLLECTION ) . then ( ( resp ) => {
86+ const reposBranchesMap = keyBy ( resp , 'project' ) ;
87+ const reposBranchesMapWithOldGen = addOldGenToReposMap ( reposBranchesMap ) ;
88+ setReposMap ( reposBranchesMapWithOldGen ) ;
89+ } ) ;
90+ }
91+ } , [ reposDatabase ] ) ;
92+
7793 useEffect ( ( ) => {
7894 if ( isBrowser ) {
7995 // Extract the value of 'site' query string from the page url to pre-select product
@@ -87,23 +103,25 @@ const DeprecatedVersionSelector = ({ metadata: { deprecated_versions: deprecated
87103 const generateUrl = ( ) => {
88104 // Our current LG button version has a bug where a disabled button with an href allows the disabled
89105 // button to be clickable. This logic can be removed when LG button is version >= 12.0.4.
90- if ( buttonDisabled ) {
106+ if ( buttonDisabled || isEmpty ( reposMap ) || ! hasValidHostName ( reposMap [ product ] ) ) {
91107 return null ;
92108 }
93109
110+ // Utilizing hardcoded env because legacy sites are not available on dev/stage
111+ const hostName = reposMap [ product ] . url . dotcomprd + reposMap [ product ] . prefix . dotcomprd ;
94112 const versionOptions = deprecatedVersions [ product ] ;
95- const hostName = getSiteUrl ( product ) ;
96113 const versionName = isVersioned ( versionOptions ) ? version : '' ;
97- return [ 'docs' , 'mms' , 'cloud-docs' , 'atlas-cli' ] . includes ( product )
98- ? `${ hostName } /${ versionName } `
99- : `${ hostName } /${ product } /${ versionName } ` ;
114+ return `${ hostName } /${ versionName } ` ;
100115 } ;
101116
102117 const productChoices = deprecatedVersions
103- ? Object . keys ( deprecatedVersions ) . map ( ( product ) => ( {
104- text : fullProductName ( product ) ,
105- value : product ,
106- } ) )
118+ ? Object . keys ( deprecatedVersions )
119+ . map ( ( product ) => ( {
120+ text : reposMap [ product ] ?. displayName ,
121+ value : product ,
122+ } ) )
123+ // Ensure invalid entries do not break selector
124+ . filter ( ( { text } ) => ! ! text )
107125 : [ ] ;
108126
109127 const versionChoices = deprecatedVersions [ product ]
0 commit comments