|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + ThemeContext, |
| 4 | + THEME_DARK, |
| 5 | + THEME_LIGHT, |
| 6 | + applyThemeBehaviour, |
| 7 | + darkThemeMq, |
| 8 | +} from '@console/internal/components/ThemeProvider'; |
1 | 9 | import okdLogoImg from '../../imgs/okd-logo.svg'; |
2 | 10 | import openshiftLogoImg from '../../imgs/openshift-logo.svg'; |
3 | 11 | import onlineLogoImg from '../../imgs/openshift-online-logo.svg'; |
4 | 12 | import dedicatedLogoImg from '../../imgs/openshift-dedicated-logo.svg'; |
5 | 13 | import rosaLogoImg from '../../imgs/openshift-service-on-aws-logo.svg'; |
| 14 | +import { capitalize } from 'lodash'; |
| 15 | + |
| 16 | +type CUSTOM_LOGO = typeof FAVICON_TYPE | typeof MASTHEAD_TYPE; |
| 17 | +export const FAVICON_TYPE = 'Favicon'; |
| 18 | +export const MASTHEAD_TYPE = 'Masthead'; |
6 | 19 |
|
7 | 20 | export const getBrandingDetails = () => { |
8 | | - let logoImg, productName; |
| 21 | + let staticLogo, productName; |
9 | 22 | // Webpack won't bundle these images if we don't directly reference them, hence the switch |
10 | 23 | switch (window.SERVER_FLAGS.branding) { |
11 | 24 | case 'openshift': |
12 | | - logoImg = openshiftLogoImg; |
| 25 | + staticLogo = openshiftLogoImg; |
13 | 26 | productName = 'Red Hat OpenShift'; |
14 | 27 | break; |
15 | 28 | case 'ocp': |
16 | | - logoImg = openshiftLogoImg; |
| 29 | + staticLogo = openshiftLogoImg; |
17 | 30 | productName = 'Red Hat OpenShift'; |
18 | 31 | break; |
19 | 32 | case 'online': |
20 | | - logoImg = onlineLogoImg; |
| 33 | + staticLogo = onlineLogoImg; |
21 | 34 | productName = 'Red Hat OpenShift Online'; |
22 | 35 | break; |
23 | 36 | case 'dedicated': |
24 | | - logoImg = dedicatedLogoImg; |
| 37 | + staticLogo = dedicatedLogoImg; |
25 | 38 | productName = 'Red Hat OpenShift Dedicated'; |
26 | 39 | break; |
27 | 40 | case 'azure': |
28 | | - logoImg = openshiftLogoImg; |
| 41 | + staticLogo = openshiftLogoImg; |
29 | 42 | productName = 'Azure Red Hat OpenShift'; |
30 | 43 | break; |
31 | 44 | case 'rosa': |
32 | | - logoImg = rosaLogoImg; |
| 45 | + staticLogo = rosaLogoImg; |
33 | 46 | productName = 'Red Hat OpenShift Service on AWS'; |
34 | 47 | break; |
35 | 48 | default: |
36 | | - logoImg = okdLogoImg; |
| 49 | + staticLogo = okdLogoImg; |
37 | 50 | productName = 'OKD'; |
38 | 51 | } |
39 | | - if (window.SERVER_FLAGS.customLogoURL) { |
40 | | - logoImg = window.SERVER_FLAGS.customLogoURL; |
41 | | - } |
42 | 52 | if (window.SERVER_FLAGS.customProductName) { |
43 | 53 | productName = window.SERVER_FLAGS.customProductName; |
44 | 54 | } |
45 | | - return { logoImg, productName }; |
| 55 | + return { staticLogo, productName }; |
| 56 | +}; |
| 57 | + |
| 58 | +/** Fetches given custom logo and returns its blob URL. |
| 59 | + * Fetches the bridge's custom logo endpoint with query parameters that specify the type |
| 60 | + * and the theme of the requested custom logo. |
| 61 | + * The hook listens for theme changes to provide correct custom logo for given theme. |
| 62 | + * results into redux, adjusts the API polling frequency based on the poll success. |
| 63 | + * @param type - The type of the custom logo to query. |
| 64 | + * @returns Returns logoURL blob URL and the loading state of the API request. |
| 65 | + */ |
| 66 | +export const useCustomLogoURL = (type: CUSTOM_LOGO): { logoUrl: string; loading: Boolean } => { |
| 67 | + const [logoUrl, setLogoUrl] = React.useState(''); |
| 68 | + const [loading, setLoading] = React.useState(false); |
| 69 | + const theme = React.useContext(ThemeContext); |
| 70 | + |
| 71 | + React.useEffect(() => { |
| 72 | + // return when requested custom logo type is not configured |
| 73 | + if ( |
| 74 | + (type === MASTHEAD_TYPE && !window.SERVER_FLAGS.customLogosConfigured) || |
| 75 | + (type === FAVICON_TYPE && !window.SERVER_FLAGS.customFaviconsConfigured) |
| 76 | + ) { |
| 77 | + return; |
| 78 | + } |
| 79 | + setLoading(true); |
| 80 | + let reqTheme; |
| 81 | + const fetchData = async () => { |
| 82 | + if (type === FAVICON_TYPE) { |
| 83 | + if (!darkThemeMq.matches) { |
| 84 | + // Fetch Light theme favicon if the Dark preference is not set via the system preference |
| 85 | + reqTheme = THEME_LIGHT; |
| 86 | + } else { |
| 87 | + reqTheme = THEME_DARK; |
| 88 | + } |
| 89 | + } else { |
| 90 | + reqTheme = applyThemeBehaviour( |
| 91 | + theme, |
| 92 | + () => { |
| 93 | + return THEME_DARK; |
| 94 | + }, |
| 95 | + () => { |
| 96 | + return THEME_LIGHT; |
| 97 | + }, |
| 98 | + ); |
| 99 | + } |
| 100 | + const fetchURL = `${window.SERVER_FLAGS.basePath}custom-logo?type=${type}&theme=${capitalize( |
| 101 | + reqTheme, |
| 102 | + )}`; |
| 103 | + const response = await fetch(fetchURL); |
| 104 | + if (response.ok) { |
| 105 | + const blob = await response.blob(); |
| 106 | + setLogoUrl(URL.createObjectURL(blob)); |
| 107 | + setLoading(false); |
| 108 | + } else if (response.status === 404) { |
| 109 | + return; |
| 110 | + } else { |
| 111 | + throw new Error(`Failed to fetch ${fetchURL}: ${response.statusText}`); |
| 112 | + } |
| 113 | + }; |
| 114 | + fetchData().catch((err) => { |
| 115 | + // eslint-disable-next-line no-console |
| 116 | + console.warn(`Error while fetching ${type} logo: ${err}`); |
| 117 | + }); |
| 118 | + }, [theme, type]); |
| 119 | + |
| 120 | + return { logoUrl, loading }; |
46 | 121 | }; |
0 commit comments