Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide Notification Banner with Scroll Threshold and Device Normalisation Functions #4295

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const NOTIFICATION_COOKIE = "notification_cookie";
const cookie_age = 31536000;
const checkNotificationsUri = window._clientConfig.rootUri + "/notifications";
const checkNotificationsInterval = 60000; // in ms
const scrollThreshold = 10;

const tickIcon = () =>
<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
Expand Down Expand Up @@ -113,12 +114,31 @@ const getIcon = (notification: Notification): JSX.Element => {
}
};

const normaliseDeltaY = (event: WheelEvent): number => {
let deltaY = event.deltaY;
switch (event.deltaMode) {
case 1: // Lines
deltaY *= 16;
break;
case 2: // Pages
deltaY *= 800;
break;
}
return deltaY;
};

const NotificationsBanner: React.FC = () => {
const [notifications, setNotifications] = useState<Notification[]>([]);

const autoHideListener = (event: any) => {
if (event.type === "keydown" && event.key === "Escape") {
if (event.type === "keydown" && (event.key === "Escape" || event.key === "ArrowUp" || event.key === "ArrowDown")) {
setNotifications(prevNotifs => prevNotifs.filter(n => n.lifespan !== TRANSIENT));
} else if (event.type === "wheel") {
const wheelEvent = event as WheelEvent;
const normalisedDeltaY = normaliseDeltaY(wheelEvent);
if (Math.abs(normalisedDeltaY) >= scrollThreshold) {
setNotifications(prevNotifs => prevNotifs.filter(n => n.lifespan !== TRANSIENT));
}
} else if (event.type !== "keydown") {
if (event.target.className !== "notification-url") {
setNotifications(prevNotifs => prevNotifs.filter(n => n.lifespan !== TRANSIENT));
Expand Down Expand Up @@ -167,7 +187,7 @@ const NotificationsBanner: React.FC = () => {
const checkNotificationsRef:NodeJS.Timeout = setInterval(checkNotifications, checkNotificationsInterval);

document.addEventListener("mouseup", autoHideListener);
document.addEventListener("scroll", autoHideListener);
document.addEventListener("wheel", autoHideListener);
document.addEventListener("keydown", autoHideListener);

// clean up cookie
Expand All @@ -181,7 +201,7 @@ const NotificationsBanner: React.FC = () => {
// Clean up the event listener when the component unmounts
return () => {
document.removeEventListener("mouseup", autoHideListener);
document.removeEventListener("scroll", autoHideListener);
document.removeEventListener("wheel", autoHideListener);
document.removeEventListener("keydown", autoHideListener);
clearInterval(checkNotificationsRef);
};
Expand Down Expand Up @@ -251,3 +271,4 @@ const NotificationsBanner: React.FC = () => {

export const notificationsBanner = angular.module('gr.notificationsBanner', [])
.component('notificationsBanner', react2angular(NotificationsBanner));