From e2be71d458fc2dbf9966c8218fccfe7c10ce47e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Mon, 1 Apr 2019 21:00:51 -0700 Subject: [PATCH] feat(ui): collapse alert groups on mobile by default --- ui/src/Common/Device.js | 5 +++ .../Grid/AlertGrid/AlertGroup/index.js | 3 +- .../Grid/AlertGrid/AlertGroup/index.test.js | 33 +++++++++++++++++++ ui/src/Components/NavBar/index.js | 7 ++-- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 ui/src/Common/Device.js diff --git a/ui/src/Common/Device.js b/ui/src/Common/Device.js new file mode 100644 index 000000000..dba858c8a --- /dev/null +++ b/ui/src/Common/Device.js @@ -0,0 +1,5 @@ +function IsMobile() { + return window.innerWidth < 768; +} + +export { IsMobile }; diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.js index 7acccbcae..ed304967e 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.js @@ -13,6 +13,7 @@ import { faMinus } from "@fortawesome/free-solid-svg-icons/faMinus"; import { APIGroup } from "Models/API"; import { Settings } from "Stores/Settings"; import { SilenceFormStore } from "Stores/SilenceFormStore"; +import { IsMobile } from "Common/Device"; import { MountFade } from "Components/Animations/MountFade"; import { TooltipWrapper } from "Components/TooltipWrapper"; import { GroupHeader } from "./GroupHeader"; @@ -74,7 +75,7 @@ const AlertGroup = observer( // this observable needs to be passed down to it collapse = observable( { - value: false, + value: IsMobile(), toggle() { this.value = !this.value; } diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js index 0eac2cd75..dee848ab6 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/index.test.js @@ -28,13 +28,24 @@ const MockGroup = groupName => { return group; }; +let originalInnerWidth; + +beforeAll(() => { + originalInnerWidth = global.innerWidth; +}); + beforeEach(() => { + global.innerWidth = originalInnerWidth; alertStore = new AlertStore([]); settingsStore = new Settings(); silenceFormStore = new SilenceFormStore(); group = MockGroup("fakeGroup"); }); +afterEach(() => { + global.innerWidth = originalInnerWidth; +}); + const MockAlerts = alertCount => { for (let i = 1; i <= alertCount; i++) { let alert = MockAlert([], { instance: `instance${i}` }, "active"); @@ -97,6 +108,28 @@ describe("", () => { expect(tree.find("Alert")).toHaveLength(0); expect(tree.find("ul.list-group")).toHaveLength(0); }); + + it("is expanded by default on desktop", () => { + // set window.innerWidth to 2k to mock a desktop window + global.innerWidth = 2048; + + MockAlerts(3); + const tree = MountedAlertGroup(jest.fn(), false); + const alertGroup = tree.find("AlertGroup"); + expect(alertGroup.instance().collapse.value).toBe(false); + expect(tree.find("Alert")).toHaveLength(3); + }); + + it("is collapsed by default on mobile", () => { + // set window.innerWidth to <768 to mock a mobile window + global.innerWidth = 700; + + MockAlerts(3); + const tree = MountedAlertGroup(jest.fn(), false); + const alertGroup = tree.find("AlertGroup"); + expect(alertGroup.instance().collapse.value).toBe(true); + expect(tree.find("Alert")).toHaveLength(0); + }); }); const ValidateLoadButtonPresent = (totalAlerts, isPresent) => { diff --git a/ui/src/Components/NavBar/index.js b/ui/src/Components/NavBar/index.js index eafafdd4a..553a60249 100644 --- a/ui/src/Components/NavBar/index.js +++ b/ui/src/Components/NavBar/index.js @@ -11,6 +11,7 @@ import IdleTimer from "react-idle-timer"; import { AlertStore } from "Stores/AlertStore"; import { Settings } from "Stores/Settings"; import { SilenceFormStore } from "Stores/SilenceFormStore"; +import { IsMobile } from "Common/Device"; import { NavBarSlide } from "Components/Animations/NavBarSlide"; import { MainModal } from "Components/MainModal"; import { SilenceModal } from "Components/SilenceModal"; @@ -100,6 +101,8 @@ const NavBar = observer( ? "flex-column flex-sm-column flex-md-row flex-lg-row flex-xl-row" : "flex-row"; + const isMobile = IsMobile(); + return ( = 768 ? DesktopIdleTimeout : MobileIdleTimeout - } + timeout={isMobile ? MobileIdleTimeout : DesktopIdleTimeout} >