From 2f9fd25569c47c442bb1546bc8757c94877d744b Mon Sep 17 00:00:00 2001 From: ninaflaaten Date: Sun, 22 Nov 2026 14:04:54 +0100 Subject: [PATCH] Add new feature: notifications when a new match is made --- e2e/uiAccessibility.spec.ts | 7 +- e2e/uiMatchAndRemovePlant.spec.ts | 2 +- frontend/src/components/sidebar/Sidebar.css | 45 ++++++++ frontend/src/components/sidebar/Sidebar.tsx | 14 +++ frontend/src/hooks/useNewMatches.ts | 33 ++++++ frontend/src/i18n/locales/en/translation.json | 4 +- frontend/src/i18n/locales/no/translation.json | 4 +- .../src/pages/myMatches/MyMatchesPage.css | 40 +++++-- .../src/pages/myMatches/MyMatchesPage.tsx | 70 ++++++------ frontend/src/tests/SidebarNewMatches.test.tsx | 100 ++++++++++++++++++ 10 files changed, 277 insertions(+), 42 deletions(-) create mode 100644 frontend/src/hooks/useNewMatches.ts create mode 100644 frontend/src/tests/SidebarNewMatches.test.tsx diff --git a/e2e/uiAccessibility.spec.ts b/e2e/uiAccessibility.spec.ts index 774d724e..f17dbac6 100644 --- a/e2e/uiAccessibility.spec.ts +++ b/e2e/uiAccessibility.spec.ts @@ -298,7 +298,12 @@ const PAGES = [ path: "#/explorePlants", loaded: ".plant-grid > li", }, - { name: "MyMatches", path: "#/myMatches", loaded: ".matches-grid" }, + { + name: "MyMatches", + path: "#/myMatches", + // The status line stops being busy when the matches are loaded + loaded: '.my-matches-count[aria-busy="false"]', + }, { name: "Profile", path: "#/profile", loaded: "main.profile" }, { name: "PlantOwnerTest", diff --git a/e2e/uiMatchAndRemovePlant.spec.ts b/e2e/uiMatchAndRemovePlant.spec.ts index 09313dd6..49f1ed9f 100644 --- a/e2e/uiMatchAndRemovePlant.spec.ts +++ b/e2e/uiMatchAndRemovePlant.spec.ts @@ -31,7 +31,7 @@ test("user can like a plant in PlantMatch, find it in MyMatches and remove the m await matchCards.first().locator(".remove-match-button").click(); await expect(matchCards).toHaveCount(0); - await expect(page.locator(".matches-grid")).toContainText( + await expect(page.locator(".my-matches-count")).toContainText( /no matches|ingen matcher/i, ); }); diff --git a/frontend/src/components/sidebar/Sidebar.css b/frontend/src/components/sidebar/Sidebar.css index e41e4c86..c14362e8 100644 --- a/frontend/src/components/sidebar/Sidebar.css +++ b/frontend/src/components/sidebar/Sidebar.css @@ -54,6 +54,51 @@ border-color: transparent; } +/* The number of new matches, on My Matches */ +.sidebar__badge { + display: inline-flex; + align-items: center; + justify-content: center; + align-self: center; + min-width: 1.6rem; + height: 1.6rem; + margin-left: auto; + padding: 0 0.45rem; + box-sizing: border-box; + border-radius: 999px; + background: var(--dark-pink); + color: var(--text-light); + font-size: 0.9rem; + font-weight: 700; + line-height: 1; + animation: sidebar-badge-pop 0.3s ease; +} + +@keyframes sidebar-badge-pop { + from { + transform: scale(0.4); + opacity: 0; + } +} + +@media (prefers-reduced-motion: reduce) { + .sidebar__badge { + animation: none; + } +} + +.sidebar__visually-hidden { + position: absolute; + width: 1px; + height: 1px; + padding: 0; + margin: -1px; + overflow: hidden; + clip-path: inset(50%); + white-space: nowrap; + border: 0; +} + .sidebar__link:focus-visible { outline: 2px solid var(--dark-green); outline-offset: 2px; diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx index c4f42a22..18676c89 100644 --- a/frontend/src/components/sidebar/Sidebar.tsx +++ b/frontend/src/components/sidebar/Sidebar.tsx @@ -4,6 +4,7 @@ import { NavLink } from "react-router-dom"; import LanguageSwitcher from "../sidebarButtons/LanguageSwitcher"; import ThemeToggle from "../sidebarButtons/ThemeToggle"; import { ROUTES } from "../../AppRoutes"; +import { useNewMatches } from "../../hooks/useNewMatches"; import "./Sidebar.css"; type SidebarProps = { @@ -15,6 +16,7 @@ type SidebarProps = { export default function Sidebar({ isOpen, onCloseSidebar, ref }: SidebarProps) { const { t } = useTranslation(); const closeButtonRef = useRef(null); + const newMatches = useNewMatches(); // The title is "I <3 Plants". The heart is put in its own span so it can be pink. const [titleStart, titleEnd] = t("sidebar.title").split("<3"); @@ -87,6 +89,18 @@ export default function Sidebar({ isOpen, onCloseSidebar, ref }: SidebarProps) { onClick={onCloseSidebar} > {t("sidebar.myMatches")} + {newMatches > 0 && ( + <> + {/* The number is for sighted users, the hidden text is read + by screen readers as part of the link */} + + + {t("sidebar.newMatches", { count: newMatches })} + + + )} diff --git a/frontend/src/hooks/useNewMatches.ts b/frontend/src/hooks/useNewMatches.ts new file mode 100644 index 00000000..2f181b7e --- /dev/null +++ b/frontend/src/hooks/useNewMatches.ts @@ -0,0 +1,33 @@ +import { useState } from "react"; +import { useLocation } from "react-router-dom"; +import { useUser } from "../context/UserContext"; +import { ROUTES } from "../AppRoutes"; + +// The number of plants matched since My Matches was last opened, shown as a +// badge on My Matches in the sidebar. +// The matches that have been "seen" are only kept in memory, as the user id is +// the only user data kept in the browser (see UserContext). So the matches the +// user already has when the app opens count as seen, and only matches made +// while the app is open are counted as new. +export function useNewMatches() { + const { user } = useUser(); + const { pathname } = useLocation(); + const matchedPlantIds = user?.matchedPlantIds; + const onMyMatches = pathname === ROUTES.myMatches; + + const [seenIds, setSeenIds] = useState(null); + + // Everything counts as seen when the user is first loaded, and whenever + // My Matches is open. Updated during render (not in an effect), so the badge + // never shows for a moment before it is cleared. + if ( + matchedPlantIds && + seenIds !== matchedPlantIds && + (seenIds === null || onMyMatches) + ) { + setSeenIds(matchedPlantIds); + } + + if (!matchedPlantIds || !seenIds || onMyMatches) return 0; + return matchedPlantIds.filter((id) => !seenIds.includes(id)).length; +} diff --git a/frontend/src/i18n/locales/en/translation.json b/frontend/src/i18n/locales/en/translation.json index eab30686..ebf44047 100644 --- a/frontend/src/i18n/locales/en/translation.json +++ b/frontend/src/i18n/locales/en/translation.json @@ -13,7 +13,9 @@ "plantOwnerTest": "Plant Owner Test", "openSidebar": "Open sidebar", "closeMenu": "Close menu", - "navigationLabel": "Sidebar navigation" + "navigationLabel": "Sidebar navigation", + "newMatches_one": ", {{count}} new match", + "newMatches_other": ", {{count}} new matches" }, "plantMatchPage": { "skipPlant": "Skip plant", diff --git a/frontend/src/i18n/locales/no/translation.json b/frontend/src/i18n/locales/no/translation.json index fb265e30..a47baeca 100644 --- a/frontend/src/i18n/locales/no/translation.json +++ b/frontend/src/i18n/locales/no/translation.json @@ -13,7 +13,9 @@ "plantOwnerTest": "Planteeiertest", "openSidebar": "Åpne sidemeny", "closeMenu": "Lukk menyen", - "navigationLabel": "Navigasjon i sidemenyen" + "navigationLabel": "Navigasjon i sidemenyen", + "newMatches_one": ", {{count}} ny match", + "newMatches_other": ", {{count}} nye matcher" }, "plantMatchPage": { "skipPlant": "Hopp over planten", diff --git a/frontend/src/pages/myMatches/MyMatchesPage.css b/frontend/src/pages/myMatches/MyMatchesPage.css index 2aeefff1..0ba4190d 100644 --- a/frontend/src/pages/myMatches/MyMatchesPage.css +++ b/frontend/src/pages/myMatches/MyMatchesPage.css @@ -11,25 +11,38 @@ gap: 30px; } -/* The number of matches and the sort menu, on one line above the plants */ +/* The sort menu, in the top right corner of the box with the plants it sorts */ .my-matches-header { grid-column: 1 / -1; display: flex; - flex-wrap: wrap; - justify-content: space-between; - align-items: center; - gap: 1rem; + justify-content: flex-end; } .my-matches-header .sort-menu { align-self: center; } -/* Same style as the number of results on Explore Plants */ +/* The number of matches, centered under the page title as a summary of the + page. A pill with a pink border, like the preference chips on the cards. */ .my-matches-count { - margin: 0; - font-size: 1.25rem; + max-width: 40rem; + margin: -10px 0 0; + padding: 0.5rem 1.25rem; + border: 2px solid var(--dark-pink); + border-radius: 999px; + color: var(--pink-text); + font-size: 1.2rem; + font-weight: 600; + text-align: center; +} + +.my-matches-count--message { + margin-top: 20px; + padding: 0; + border: none; color: var(--text-dark); + font-weight: 400; + line-height: 1.5; } .matches-grid { @@ -67,6 +80,17 @@ gap: 20px; } + .my-matches-count { + margin-top: 0; + font-size: 1.05rem; + } + + /* On mobile the sort menu takes the whole width */ + .my-matches-header .sort-menu, + .my-matches-header .sort-button { + width: 100%; + } + .matches-grid { grid-template-columns: 1fr; gap: 20px; diff --git a/frontend/src/pages/myMatches/MyMatchesPage.tsx b/frontend/src/pages/myMatches/MyMatchesPage.tsx index b3490641..82cb4f17 100644 --- a/frontend/src/pages/myMatches/MyMatchesPage.tsx +++ b/frontend/src/pages/myMatches/MyMatchesPage.tsx @@ -63,21 +63,31 @@ function MyMatchesPage() { return (
-
-
- {/* One status line that is always on the page, with aria-live, so - screen readers hear every change: the number of matches when - one is removed, and also "no matches" when the last one is - removed. While loading it says so, so "no matches" does not - flash before the matches are fetched. */} -

- {loading - ? t("myMatchesPage.loading") - : totalCount > 0 - ? t("myMatchesPage.matchCount", { count: totalCount }) - : t("myMatchesPage.noMatches")} -

- {totalCount > 0 && ( + {/* Centered under the page title, as a summary of the page. + One status line that is always on the page, with aria-live, so + screen readers hear every change: the number of matches when + one is removed, and also "no matches" when the last one is + removed. While loading it says so, so "no matches" does not + flash before the matches are fetched. */} +

0 + ? "my-matches-count" + : "my-matches-count my-matches-count--message" + } + aria-live="polite" + aria-busy={loading} + > + {loading + ? t("myMatchesPage.loading") + : totalCount > 0 + ? t("myMatchesPage.matchCount", { count: totalCount }) + : t("myMatchesPage.noMatches")} +

+ {/* Only shown when there are matches, so there is no empty pink box */} + {totalCount > 0 && ( +
+
- )} -
- - {plants.map((plant) => ( -
- - {/* Every plant here is matched, so the button only removes */} - removeMatch(plant.id)} - className="remove-match-button" - />
- ))} -
+ + {plants.map((plant) => ( +
+ + {/* Every plant here is matched, so the button only removes */} + removeMatch(plant.id)} + className="remove-match-button" + /> +
+ ))} +
+ )} {totalCount > 0 && ( ({ + useTranslation: () => ({ + t: (key: string, options?: { count?: number }) => + options?.count === undefined ? key : `${key}:${options.count}`, + }), +})); + +vi.mock("../components/sidebarButtons/LanguageSwitcher", () => ({ + default: () => null, +})); + +vi.mock("../components/sidebarButtons/ThemeToggle", () => ({ + default: () => null, +})); + +// The matched plants of the user. Changed by the tests to act as if a match +// was added +let matchedPlantIds: string[] = []; +vi.mock("../context/UserContext", () => ({ + useUser: () => ({ + userId: "user-1", + user: { matchedPlantIds }, + loading: false, + }), +})); + +function renderSidebar(route: string = ROUTES.plantMatch) { + const user = userEvent.setup(); + const view = render( + + {}} /> + , + ); + const rerender = () => + view.rerender( + + {}} /> + , + ); + return { user, rerender }; +} + +const badge = () => document.querySelector(".sidebar__badge"); + +describe("Sidebar new matches badge", () => { + beforeEach(() => { + matchedPlantIds = ["a", "b"]; + }); + + it("does not count the matches the user already had", () => { + renderSidebar(); + expect(badge()).toBeNull(); + }); + + it("shows the number of new matches on My Matches", () => { + const { rerender } = renderSidebar(); + + matchedPlantIds = [...matchedPlantIds, "c"]; + rerender(); + expect(badge()).toHaveTextContent("1"); + + matchedPlantIds = [...matchedPlantIds, "d"]; + rerender(); + expect(badge()).toHaveTextContent("2"); + + // Screen readers get the number as part of the link name + expect( + screen.getByRole("link", { name: /sidebar\.newMatches:2/ }), + ).toBeInTheDocument(); + }); + + it("does not count a match that is removed again", () => { + const { rerender } = renderSidebar(); + + matchedPlantIds = [...matchedPlantIds, "c"]; + rerender(); + matchedPlantIds = matchedPlantIds.filter((id) => id !== "c"); + rerender(); + expect(badge()).toBeNull(); + }); + + it("is cleared when My Matches is opened", async () => { + const { user, rerender } = renderSidebar(); + + matchedPlantIds = [...matchedPlantIds, "c"]; + rerender(); + expect(badge()).toHaveTextContent("1"); + + await user.click(screen.getByRole("link", { name: /sidebar.myMatches/ })); + expect(badge()).toBeNull(); + }); +});