From a4be2500a984fd356d530137c368e176db0a9dac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cintia=20S=C3=A1nchez=20Garc=C3=ADa?= Date: Mon, 5 Dec 2022 14:44:15 +0100 Subject: [PATCH] Load CNCF banner in home page (#2573) Related to #2551 Signed-off-by: Cintia Sanchez Garcia --- web/src/context/AppCtx.tsx | 2 -- web/src/layout/home/HomeView.module.css | 6 +++++ web/src/layout/home/index.tsx | 26 ++++++++++++++++++- web/src/layout/package/Banner.module.css | 22 ---------------- web/src/layout/package/Banner.tsx | 25 +++++++++++++----- web/src/layout/package/PackageView.module.css | 12 +++++++-- web/src/layout/package/index.tsx | 21 +++++++++++++-- web/src/themes/dark.scss | 7 ++++- web/src/utils/bannerDispatcher.ts | 7 ++++- 9 files changed, 91 insertions(+), 37 deletions(-) diff --git a/web/src/context/AppCtx.tsx b/web/src/context/AppCtx.tsx index df70d328..6c4b4cc1 100644 --- a/web/src/context/AppCtx.tsx +++ b/web/src/context/AppCtx.tsx @@ -5,7 +5,6 @@ import { createContext, Dispatch, useContext, useEffect, useReducer, useState } import API from '../api'; import useSystemThemeMode from '../hooks/useSystemThemeMode'; import { Prefs, Profile, ThemePrefs, UserFullName } from '../types'; -import bannerDispatcher from '../utils/bannerDispatcher'; import cleanLoginUrlParams from '../utils/cleanLoginUrlParams'; import detectActiveThemeMode from '../utils/detectActiveThemeMode'; import browserHistory from '../utils/history'; @@ -302,7 +301,6 @@ function AppCtxProvider(props: Props) { ? detectActiveThemeMode() : activeProfilePrefs.theme.configured; themeBuilder.init(); - bannerDispatcher.init(); updateActiveStyleSheet(theme); setActiveInitialTheme(theme); refreshUserProfile(dispatch); diff --git a/web/src/layout/home/HomeView.module.css b/web/src/layout/home/HomeView.module.css index 015b9d38..382e5bc1 100644 --- a/web/src/layout/home/HomeView.module.css +++ b/web/src/layout/home/HomeView.module.css @@ -54,6 +54,12 @@ text-decoration: none; } +.banner { + max-height: 350px; + width: 350px; + background-color: var(--body-bg) !important; +} + @media only screen and (max-width: 767.98px) { .mainTitle { font-size: 2.5rem !important; diff --git a/web/src/layout/home/index.tsx b/web/src/layout/home/index.tsx index fb376f10..a5b6a81f 100644 --- a/web/src/layout/home/index.tsx +++ b/web/src/layout/home/index.tsx @@ -4,8 +4,9 @@ import { FaGithub, FaSlack, FaTwitter } from 'react-icons/fa'; import { Link, useHistory } from 'react-router-dom'; import API from '../../api'; -import { RepositoryKind, Stats } from '../../types'; +import { Banner as IBanner, RepositoryKind, Stats } from '../../types'; import alertDispatcher from '../../utils/alertDispatcher'; +import bannerDispatcher from '../../utils/bannerDispatcher'; import getSampleQueries from '../../utils/getSampleQueries'; import isWhiteLabel from '../../utils/isWhiteLabel'; import ExternalLink from '../common/ExternalLink'; @@ -14,6 +15,7 @@ import SampleQueries from '../common/SampleQueries'; import SearchBar from '../common/SearchBar'; import SearchTipsModal from '../common/SearchTipsModal'; import UserInvitation from '../controlPanel/members/UserInvitation'; +import Banner from '../package/Banner'; import AccountDeletion from './AccountDeletion'; import Counter from './Counter'; import styles from './HomeView.module.css'; @@ -37,6 +39,7 @@ const HomeView = (props: Props) => { const [isLoadingStats, setIsLoadingStats] = useState(false); const [stats, setStats] = useState(null); const [openTips, setOpenTips] = useState(false); + const [banner, setBanner] = useState(null); const whiteLabel = isWhiteLabel(); @@ -51,7 +54,18 @@ const HomeView = (props: Props) => { setIsLoadingStats(false); } } + + async function getBanner() { + try { + const CNCFBanner = await bannerDispatcher.getBanner(); + setBanner(CNCFBanner); + } catch { + setBanner(null); + } + } + fetchStats(); + getBanner(); }, []); useEffect(() => { @@ -138,6 +152,16 @@ const HomeView = (props: Props) => { + {!isNull(banner) && ( + setBanner(null)} + maxEqualRatio + /> + )} + {!whiteLabel && ( <>
diff --git a/web/src/layout/package/Banner.module.css b/web/src/layout/package/Banner.module.css index 54a8acad..0654063e 100644 --- a/web/src/layout/package/Banner.module.css +++ b/web/src/layout/package/Banner.module.css @@ -6,25 +6,3 @@ .loaded { max-height: 1500px; } - -.banner { - margin-top: 25px; - width: 230px; - background-color: var(--color-1-2) !important; -} - -.image { - width: auto; -} - -@media only screen and (min-width: 1400px) { - .banner { - width: 260px; - } -} - -@media only screen and (min-width: 1920px) { - .banner { - width: 310px; - } -} diff --git a/web/src/layout/package/Banner.tsx b/web/src/layout/package/Banner.tsx index b10e8142..c9d611f6 100644 --- a/web/src/layout/package/Banner.tsx +++ b/web/src/layout/package/Banner.tsx @@ -1,6 +1,6 @@ import classNames from 'classnames'; import { isNull } from 'lodash'; -import { useContext, useEffect, useState } from 'react'; +import { useContext, useEffect, useRef, useState } from 'react'; import { AppCtx } from '../../context/AppCtx'; import { Banner as IBanner } from '../../types'; @@ -8,13 +8,17 @@ import ExternalLink from '../common/ExternalLink'; import styles from './Banner.module.css'; interface Props { + wrapperClassName?: string; + className?: string; banner: IBanner; removeBanner: () => void; + maxEqualRatio: boolean; } const Banner = (props: Props) => { const { ctx } = useContext(AppCtx); const { effective } = ctx.prefs.theme; + const img = useRef(null); const [isLoaded, setIsLoaded] = useState(false); const [visibleBanner, setVisibleBanner] = useState(props.banner); const [bannerTimeout, setBannerTimeout] = useState(null); @@ -42,14 +46,19 @@ const Banner = (props: Props) => { if (isNull(visibleBanner)) return null; const getCardImage = () => ( -
+
{visibleBanner.name { - setIsLoaded(true); + if (props.maxEqualRatio && img && img.current && img.current.naturalHeight > img.current.naturalWidth) { + setIsLoaded(false); + } else { + setIsLoaded(true); + } }} />
@@ -58,11 +67,15 @@ const Banner = (props: Props) => { return (
{visibleBanner.link ? ( - + <>{getCardImage()} ) : ( - <>{getCardImage()} +
{getCardImage()}
)}
); diff --git a/web/src/layout/package/PackageView.module.css b/web/src/layout/package/PackageView.module.css index c291c7e0..eede67ad 100644 --- a/web/src/layout/package/PackageView.module.css +++ b/web/src/layout/package/PackageView.module.css @@ -141,6 +141,12 @@ width: 100%; } +.banner { + margin-top: 25px; + width: 230px; + background-color: var(--color-1-2) !important; +} + @media only screen and (max-width: 767.98px) { .imageWrapper { min-width: 50px; @@ -240,7 +246,8 @@ max-width: 260px; } - .info { + .info, + .banner { width: 260px; } @@ -264,7 +271,8 @@ max-width: 310px; } - .info { + .info, + .banner { width: 310px; } diff --git a/web/src/layout/package/index.tsx b/web/src/layout/package/index.tsx index 26a49f1b..163c5b73 100644 --- a/web/src/layout/package/index.tsx +++ b/web/src/layout/package/index.tsx @@ -180,8 +180,18 @@ const PackageView = (props: Props) => { setRelatedPackages([]); } } + + async function getBanner() { + try { + const CNCFBanner = await bannerDispatcher.getBanner(); + setBanner(CNCFBanner); + } catch { + setBanner(null); + } + } + if (!isNull(currentPkgId) && detail) { - setBanner(bannerDispatcher.getBanner()); + getBanner(); fetchRelatedPackages(detail); } }, [currentPkgId]); /* eslint-disable-line react-hooks/exhaustive-deps */ @@ -1187,7 +1197,14 @@ const PackageView = (props: Props) => {
)} - {!isNull(banner) && setBanner(null)} />} + {!isNull(banner) && ( + setBanner(null)} + maxEqualRatio={false} + /> + )}
diff --git a/web/src/themes/dark.scss b/web/src/themes/dark.scss index eb7a6718..e14cff59 100644 --- a/web/src/themes/dark.scss +++ b/web/src/themes/dark.scss @@ -306,7 +306,7 @@ background-color: var(--color-1-300); } - .card, + .card:not(.banner), .card.bg-white, .list-group-item { background-color: var(--color-1-500) !important; @@ -314,6 +314,11 @@ box-shadow: none !important; } + .banner { + border-color: var(--border-md) !important; + box-shadow: none !important; + } + .notificationCard { box-shadow: 0px 0px 25px 5px var(--color-black-25); } diff --git a/web/src/utils/bannerDispatcher.ts b/web/src/utils/bannerDispatcher.ts index 8687de07..17ae8e09 100644 --- a/web/src/utils/bannerDispatcher.ts +++ b/web/src/utils/bannerDispatcher.ts @@ -6,6 +6,7 @@ import { Banner } from '../types'; import getMetaTag from './getMetaTag'; class BannerDispatcher { + private isInitiated: boolean = false; private url: string | null = null; private banners: Banner[] = []; @@ -14,6 +15,7 @@ class BannerDispatcher { if (!isNull(this.url)) { this.banners = await this.getBannersInfo(this.url); } + this.isInitiated = true; } private async getBannersInfo(url: string): Promise { @@ -54,7 +56,10 @@ class BannerDispatcher { } } - public getBanner(): Banner | null { + public async getBanner(): Promise { + if (!this.isInitiated) { + await this.init(); + } if (this.banners.length > 0) { return this.banners[Math.floor(Math.random() * this.banners.length)]; } else {