import { useEffect } from 'react' import { useRouter } from 'next/router' import cx from 'classnames' import { XIcon } from '@primer/octicons-react' import { useLanguages } from '@/languages/components/LanguagesContext' import { useMainContext } from '@/frame/components/context/MainContext' import { useTranslation } from '@/languages/components/useTranslation' import { ExcludesNull } from '@/frame/components/lib/ExcludesNull' import { useVersion } from '@/versions/components/useVersion' import { useUserLanguage } from '@/languages/components/useUserLanguage' import styles from './HeaderNotifications.module.scss' import { useSharedUIContext } from '@/frame/components/context/SharedUIContext' enum NotificationType { RELEASE = 'RELEASE', TRANSLATION = 'TRANSLATION', EARLY_ACCESS = 'EARLY_ACCESS', } type Notif = { content: string type?: NotificationType onClose?: () => void } export const HeaderNotifications = () => { const router = useRouter() const { currentVersion } = useVersion() const mainContext = useMainContext() const { relativePath, allVersions, data, currentPathWithoutLanguage } = mainContext const page = mainContext.page! const { userLanguage, setUserLanguageCookie } = useUserLanguage() const { languages } = useLanguages() const { setHasOpenHeaderNotifications } = useSharedUIContext() const { t } = useTranslation('header') const translationNotices: Array = [] if (router.locale === 'en') { if (userLanguage && userLanguage !== 'en' && languages[userLanguage]) { let href = `/${userLanguage}` if (currentPathWithoutLanguage !== '/') { href += currentPathWithoutLanguage } translationNotices.push({ type: NotificationType.TRANSLATION, content: `This article is also available in ${languages[userLanguage]?.name}.`, onClose: () => { try { setUserLanguageCookie('en') } catch (err) { // You can never be too careful because setting a cookie // can fail. For example, some browser // extensions disallow all setting of cookies and attempts // at the `document.cookie` setter could throw. Just swallow // and move on. console.warn('Unable to set cookie', err) } }, }) } } const releaseNotices: Array = [] if (currentVersion === data.variables.release_candidate.version) { releaseNotices.push({ type: NotificationType.RELEASE, content: `${allVersions[currentVersion].versionTitle}${t('notices.release_candidate')}`, }) } const allNotifications: Array = [ ...translationNotices, ...releaseNotices, // ONEOFF EARLY ACCESS NOTICE (relativePath || '').includes('early-access/') && !page.noEarlyAccessBanner ? { type: NotificationType.EARLY_ACCESS, content: t('notices.early_access'), } : null, ].filter(ExcludesNull) useEffect(() => { setHasOpenHeaderNotifications(allNotifications.length > 0) }, [allNotifications, setHasOpenHeaderNotifications]) return (
{allNotifications.map(({ type, content, onClose }, i) => { const isLast = i === allNotifications.length - 1 return (
{onClose && ( )}

) })}
) }