Skip to content

Commit 93c701b

Browse files
committed
Adjust vhost based on whether wss/ws is disabled
1 parent 22609ab commit 93c701b

File tree

3 files changed

+149
-79
lines changed

3 files changed

+149
-79
lines changed

portals/devportal/src/main/webapp/source/src/app/components/Apis/Details/AsyncApiConsole/AsyncApiUI.jsx

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ const Root = styled('div')({
5454
padding: '2px 2px 2px 10px',
5555
borderRadius: '4px',
5656
color: '#3b4151',
57+
'&.Mui-disabled': {
58+
backgroundColor: '#f5f5f5',
59+
color: '#999',
60+
border: '2px solid #ddd',
61+
cursor: 'not-allowed',
62+
},
5763
}
5864
});
5965

@@ -69,11 +75,21 @@ export default function AsyncApiUI(props) {
6975
const { api } = useContext(ApiContext);
7076
const isAdvertised = api.advertiseInfo && api.advertiseInfo.advertised;
7177

78+
const getPreferredEndpoint = (URLsObj, apiType) => {
79+
if (!URLsObj) return '';
80+
if (apiType === CONSTANTS.API_TYPES.WS) {
81+
// prefer ws, but if ws does not exist fall back to wss
82+
if (URLsObj.ws) return URLsObj.ws;
83+
if (URLsObj.wss) return URLsObj.wss;
84+
return '';
85+
}
86+
// for non-WS APIs prefer http, but if http does not exist fall back to https
87+
if (URLsObj.http) return URLsObj.http;
88+
if (URLsObj.https) return URLsObj.https;
89+
return '';
90+
};
7291
let initialEndpoint;
73-
initialEndpoint = URLs && (URLs.http || URLs.https);
74-
if (api.type === CONSTANTS.API_TYPES.WS) {
75-
initialEndpoint = URLs && (URLs.ws || URLs.wss);
76-
}
92+
initialEndpoint = getPreferredEndpoint(URLs, api.type);
7793

7894
let expandable = true;
7995
if (!URLs || (!URLs.http && !URLs.https)) {
@@ -84,11 +100,8 @@ export default function AsyncApiUI(props) {
84100
const [endPoint, setEndpoint] = useState(initialEndpoint);
85101

86102
useEffect(() => {
87-
let newInitialEndpoint = URLs && URLs.http;
88-
if (api.type === CONSTANTS.API_TYPES.WS) {
89-
newInitialEndpoint = URLs && URLs.ws;
90-
}
91-
setEndpoint(newInitialEndpoint);
103+
const newInitialEndpoint = getPreferredEndpoint(URLs, api.type);
104+
setEndpoint(newInitialEndpoint || '');
92105
}, [URLs, api.type]);
93106

94107
useEffect(() => {
@@ -229,13 +242,21 @@ export default function AsyncApiUI(props) {
229242
id="api-endpoint-select"
230243
value={endPoint}
231244
displayEmpty
245+
disabled={Object.keys(URLs).length === 0 || !Object.values(URLs).some((url) => url)}
232246
onChange={handleServerChange}
233247
>
234-
{Object.entries(URLs).map(([key, value]) => {
235-
if (value) {
236-
return <MenuItem value={value} key={key}>{value}</MenuItem>;
237-
}
238-
})}
248+
{Object.keys(URLs).length === 0 || !Object.values(URLs).some((url) => url) ? (
249+
<MenuItem value='' disabled>
250+
<em>No servers available</em>
251+
</MenuItem>
252+
) : (
253+
Object.entries(URLs).map(([key, value]) => {
254+
if (value) {
255+
return <MenuItem value={value} key={key}>{value}</MenuItem>;
256+
}
257+
return null;
258+
})
259+
)}
239260
</Select>
240261
</FormControl>
241262
{api.type === CONSTANTS.API_TYPES.WEBSUB && allTopics.list.map((topic, index) => (

portals/devportal/src/main/webapp/source/src/app/components/Apis/Details/Environments.jsx

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ function Environments(props) {
118118

119119
const intl = useIntl();
120120

121+
const pickFirstEnabledUrl = (urls) => {
122+
if (!urls || typeof urls !== 'object') {
123+
return '';
124+
}
125+
// Get all truthy values and return the first one, or '' if none are found.
126+
const firstUrl = Object.values(urls).find(
127+
(val) => typeof val === 'string' && val.trim() !== '',
128+
);
129+
return firstUrl || '';
130+
};
131+
121132
const onCopy = () => {
122133
setUrlCopied(true);
123134
const caller = function () {
@@ -128,19 +139,16 @@ function Environments(props) {
128139

129140
const getDefaultVersionUrl = () => {
130141
const { defaultVersionURLs } = selectedEndpoint;
131-
if (defaultVersionURLs
132-
&& (defaultVersionURLs.https
133-
|| defaultVersionURLs.http
134-
|| defaultVersionURLs.ws
135-
|| defaultVersionURLs.wss)) {
142+
const firstEnabledDefaultUrl = pickFirstEnabledUrl(defaultVersionURLs);
143+
if (firstEnabledDefaultUrl) {
136144
return (
137145
<>
138146
{`
139147
${intl.formatMessage({
140148
id: 'Apis.Details.Environments.default.url',
141149
defaultMessage: '( Default Version ) ',
142150
})}
143-
${(defaultVersionURLs.https || defaultVersionURLs.http || defaultVersionURLs.ws || defaultVersionURLs.wss)}`}
151+
${(firstEnabledDefaultUrl)}`}
144152
<Tooltip
145153
title={
146154
urlCopied
@@ -161,10 +169,7 @@ function Environments(props) {
161169
aria-label='Copy the Default Version URL to clipboard'
162170
size='large'
163171
onClick={() => {
164-
navigator.clipboard.writeText(defaultVersionURLs.https
165-
|| defaultVersionURLs.http
166-
|| defaultVersionURLs.ws
167-
|| defaultVersionURLs.wss).then(onCopy('urlCopied'));
172+
navigator.clipboard.writeText(firstEnabledDefaultUrl).then(onCopy('urlCopied'));
168173
}}
169174
>
170175
<Icon color='secondary'>file_copy</Icon>
@@ -243,10 +248,7 @@ function Environments(props) {
243248
<InputBase
244249
className={classes.input}
245250
inputProps={{ 'aria-label': 'api url' }}
246-
value={selectedEndpoint.URLs.https
247-
|| selectedEndpoint.URLs.http
248-
|| selectedEndpoint.URLs.wss
249-
|| selectedEndpoint.URLs.ws}
251+
value={pickFirstEnabledUrl(selectedEndpoint.URLs)}
250252
data-testid='http-url'
251253
/>
252254
</Tooltip>
@@ -303,8 +305,7 @@ function Environments(props) {
303305
<InputBase
304306
className={classes.input}
305307
inputProps={{ 'aria-label': 'api url' }}
306-
value={selectedEndpoint.URLs.wss
307-
|| selectedEndpoint.URLs.ws}
308+
value={pickFirstEnabledUrl(selectedEndpoint.URLs)}
308309
data-testid='websocket-url'
309310
/>
310311
</Tooltip>

0 commit comments

Comments
 (0)