|
| 1 | +import React, { useMemo, useState, useEffect } from 'react'; |
| 2 | +import Link from '@docusaurus/Link'; |
| 3 | +import styles from './styles.module.css'; |
| 4 | +import alertsData from '@site/src/data/homeAlerts.json'; |
| 5 | + |
| 6 | +const AUTO_ROTATE_MS = 8000; |
| 7 | + |
| 8 | +function parseStartDate(dateValue) { |
| 9 | + if (!dateValue) { |
| 10 | + return null; |
| 11 | + } |
| 12 | + |
| 13 | + return new Date(`${dateValue}T00:00:00`); |
| 14 | +} |
| 15 | + |
| 16 | +function parseEndDate(dateValue) { |
| 17 | + if (!dateValue) { |
| 18 | + return null; |
| 19 | + } |
| 20 | + |
| 21 | + return new Date(`${dateValue}T23:59:59`); |
| 22 | +} |
| 23 | + |
| 24 | +function isDateActive(alert, now) { |
| 25 | + const startDate = parseStartDate(alert.startDate); |
| 26 | + const endDate = parseEndDate(alert.endDate); |
| 27 | + |
| 28 | + if (startDate && now < startDate) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + if (endDate && now > endDate) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + return true; |
| 37 | +} |
| 38 | + |
| 39 | +function sortAlerts(a, b) { |
| 40 | + if (Boolean(a.pinned) !== Boolean(b.pinned)) { |
| 41 | + return a.pinned ? -1 : 1; |
| 42 | + } |
| 43 | + |
| 44 | + const aDate = a.publishedAt ? new Date(`${a.publishedAt}T00:00:00`).getTime() : 0; |
| 45 | + const bDate = b.publishedAt ? new Date(`${b.publishedAt}T00:00:00`).getTime() : 0; |
| 46 | + |
| 47 | + return bDate - aDate; |
| 48 | +} |
| 49 | + |
| 50 | +function severityLabel(severity) { |
| 51 | + if (severity === 'high') { |
| 52 | + return 'High'; |
| 53 | + } |
| 54 | + |
| 55 | + if (severity === 'medium') { |
| 56 | + return 'Medium'; |
| 57 | + } |
| 58 | + |
| 59 | + return 'Info'; |
| 60 | +} |
| 61 | + |
| 62 | +export default function HomeAlerts() { |
| 63 | + const alerts = useMemo(() => { |
| 64 | + const now = new Date(); |
| 65 | + |
| 66 | + return alertsData |
| 67 | + .filter((alert) => alert.enabled !== false) |
| 68 | + .filter((alert) => isDateActive(alert, now)) |
| 69 | + .sort(sortAlerts); |
| 70 | + }, []); |
| 71 | + |
| 72 | + const [activeIndex, setActiveIndex] = useState(0); |
| 73 | + const [isPaused, setIsPaused] = useState(false); |
| 74 | + |
| 75 | + useEffect(() => { |
| 76 | + if (alerts.length <= 1 || isPaused) { |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + |
| 80 | + const intervalId = setInterval(() => { |
| 81 | + setActiveIndex((prev) => (prev + 1) % alerts.length); |
| 82 | + }, AUTO_ROTATE_MS); |
| 83 | + |
| 84 | + return () => clearInterval(intervalId); |
| 85 | + }, [alerts.length, isPaused]); |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + if (activeIndex >= alerts.length) { |
| 89 | + setActiveIndex(0); |
| 90 | + } |
| 91 | + }, [activeIndex, alerts.length]); |
| 92 | + |
| 93 | + if (alerts.length === 0) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + const activeAlert = alerts[activeIndex]; |
| 98 | + const badgeClassName = `${styles.badge} ${styles[`badge${severityLabel(activeAlert.severity)}`]}`; |
| 99 | + |
| 100 | + const showControls = alerts.length > 1; |
| 101 | + |
| 102 | + const nextAlert = () => { |
| 103 | + setActiveIndex((prev) => (prev + 1) % alerts.length); |
| 104 | + }; |
| 105 | + |
| 106 | + const prevAlert = () => { |
| 107 | + setActiveIndex((prev) => (prev - 1 + alerts.length) % alerts.length); |
| 108 | + }; |
| 109 | + |
| 110 | + const pauseRotation = () => { |
| 111 | + setIsPaused(true); |
| 112 | + }; |
| 113 | + |
| 114 | + const resumeRotation = () => { |
| 115 | + setIsPaused(false); |
| 116 | + }; |
| 117 | + |
| 118 | + const handleBlurCapture = (event) => { |
| 119 | + if (!event.currentTarget.contains(event.relatedTarget)) { |
| 120 | + resumeRotation(); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <section |
| 126 | + className={styles.wrapper} |
| 127 | + aria-label="Home alerts" |
| 128 | + onMouseEnter={pauseRotation} |
| 129 | + onMouseLeave={resumeRotation} |
| 130 | + onFocusCapture={pauseRotation} |
| 131 | + onBlurCapture={handleBlurCapture} |
| 132 | + > |
| 133 | + <div className={`container ${styles.inner}`}> |
| 134 | + <div className={styles.content}> |
| 135 | + <span className={badgeClassName}>{severityLabel(activeAlert.severity)}</span> |
| 136 | + <div className={styles.copy}> |
| 137 | + {activeAlert.href ? ( |
| 138 | + <Link className={styles.titleLink} to={activeAlert.href}> |
| 139 | + <strong>{activeAlert.title}</strong> |
| 140 | + </Link> |
| 141 | + ) : ( |
| 142 | + <strong>{activeAlert.title}</strong> |
| 143 | + )} |
| 144 | + <p>{activeAlert.message}</p> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + |
| 148 | + {showControls && ( |
| 149 | + <div className={styles.controls}> |
| 150 | + <button |
| 151 | + type="button" |
| 152 | + className={styles.controlButton} |
| 153 | + onClick={prevAlert} |
| 154 | + aria-label="Previous alert" |
| 155 | + > |
| 156 | + Prev |
| 157 | + </button> |
| 158 | + <span className={styles.counter}>{activeIndex + 1} / {alerts.length}</span> |
| 159 | + <button |
| 160 | + type="button" |
| 161 | + className={styles.controlButton} |
| 162 | + onClick={nextAlert} |
| 163 | + aria-label="Next alert" |
| 164 | + > |
| 165 | + Next |
| 166 | + </button> |
| 167 | + </div> |
| 168 | + )} |
| 169 | + </div> |
| 170 | + </section> |
| 171 | + ); |
| 172 | +} |
0 commit comments