forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 16
feat(notices): notices button in the header #2668
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c8b875d
feat(notices): notices button in the header
camielvs 17fe17b
fix(notices): always-available inbox, capped badge, restore-banners t…
camielvs e208100
refactor(notices): drop the banner restore toggle from the inbox
camielvs f7b4ae4
feat(notices): unread state for the header inbox
camielvs 11d0e10
fix(notices): keep the inbox read for the session when storage fails
camielvs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| import { cleanup, fireEvent, render, screen } from "@testing-library/react"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { installSource } from "@/config/noticeTestSource"; | ||
| import { resetNoticeInboxForTests } from "@/hooks/useNoticeInbox"; | ||
| import { resetNoticeStateForTests } from "@/hooks/useNotices"; | ||
|
|
||
| import { NoticeInbox } from "./NoticeInbox"; | ||
|
|
||
| class ResizeObserverMock { | ||
| observe() {} | ||
| unobserve() {} | ||
| disconnect() {} | ||
| } | ||
|
|
||
| vi.stubGlobal("ResizeObserver", ResizeObserverMock); | ||
|
|
||
| describe("<NoticeInbox />", () => { | ||
| beforeEach(() => { | ||
| localStorage.clear(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| cleanup(); | ||
| delete window.__TANGLE_NOTICE_SOURCE__; | ||
| resetNoticeInboxForTests(); | ||
| resetNoticeStateForTests(); | ||
| }); | ||
|
|
||
| it("stays in the header with nothing to show", () => { | ||
| render(<NoticeInbox />); | ||
|
|
||
| const trigger = screen.getByTestId("notice-inbox-trigger"); | ||
| expect(trigger).toHaveAttribute("aria-label", "Notices, none"); | ||
| expect(screen.queryByTestId("notice-inbox-unread")).not.toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(trigger); | ||
|
|
||
| expect(screen.getByTestId("notice-inbox-empty")).toHaveTextContent( | ||
| "No notices", | ||
| ); | ||
| expect(screen.queryByTestId("notice-inbox-hide")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("counts the notices the reader has not opened yet", () => { | ||
| installSource([ | ||
| { id: "a", title: "One", body: "" }, | ||
| { id: "b", title: "Two", body: "" }, | ||
| ]); | ||
|
|
||
| render(<NoticeInbox />); | ||
|
|
||
| expect(screen.getByTestId("notice-inbox-unread")).toHaveTextContent("2"); | ||
| expect(screen.getByTestId("notice-inbox-trigger")).toHaveAttribute( | ||
| "aria-label", | ||
| "Notices, 2 unread", | ||
| ); | ||
| }); | ||
|
|
||
| it("lists every notice in full once opened", () => { | ||
| installSource([ | ||
| { id: "a", title: "One", body: "Body of **one**" }, | ||
| { id: "b", title: "Two", body: "Body of two" }, | ||
| ]); | ||
|
|
||
| render(<NoticeInbox />); | ||
| fireEvent.click(screen.getByTestId("notice-inbox-trigger")); | ||
|
|
||
| expect(screen.getByTestId("notice-inbox")).toBeInTheDocument(); | ||
| expect(screen.getAllByTestId("info-box-title")).toHaveLength(2); | ||
| expect(screen.getByText("one").tagName).toBe("STRONG"); | ||
| expect(screen.getByText(/Body of two/)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("clears the unread count once opened, and keeps it clear across a remount", () => { | ||
| installSource([{ id: "a", title: "One", body: "" }]); | ||
|
|
||
| render(<NoticeInbox />); | ||
| fireEvent.click(screen.getByTestId("notice-inbox-trigger")); | ||
|
|
||
| expect(screen.queryByTestId("notice-inbox-unread")).not.toBeInTheDocument(); | ||
|
|
||
| cleanup(); | ||
| render(<NoticeInbox />); | ||
|
|
||
| expect(screen.queryByTestId("notice-inbox-unread")).not.toBeInTheDocument(); | ||
| expect(screen.getByTestId("notice-inbox-trigger")).toHaveAttribute( | ||
| "aria-label", | ||
| "Notices, 1 active", | ||
| ); | ||
| }); | ||
|
|
||
| it("lets a dismissible notice be removed from the centre for good", () => { | ||
| installSource([ | ||
| { id: "a", title: "Optional", body: "", dismissible: true }, | ||
| { id: "b", title: "Mandatory", body: "" }, | ||
| ]); | ||
|
|
||
| render(<NoticeInbox />); | ||
| fireEvent.click(screen.getByTestId("notice-inbox-trigger")); | ||
|
|
||
| expect(screen.getAllByLabelText("Dismiss")).toHaveLength(1); | ||
| fireEvent.click(screen.getByLabelText("Dismiss")); | ||
|
|
||
| expect(screen.queryByText("Optional")).not.toBeInTheDocument(); | ||
| expect(screen.getByText("Mandatory")).toBeInTheDocument(); | ||
|
|
||
| cleanup(); | ||
| render(<NoticeInbox />); | ||
|
|
||
| expect(screen.getByTestId("notice-inbox-trigger")).toHaveAttribute( | ||
| "aria-label", | ||
| "Notices, 1 active", | ||
| ); | ||
| }); | ||
|
|
||
| it("removes a dismissed notice even when it cannot be persisted", () => { | ||
| installSource([ | ||
| { id: "a", title: "Optional", body: "", dismissible: true }, | ||
| ]); | ||
|
|
||
| render(<NoticeInbox />); | ||
| fireEvent.click(screen.getByTestId("notice-inbox-trigger")); | ||
|
|
||
| const setItem = vi | ||
| .spyOn(Storage.prototype, "setItem") | ||
| .mockImplementation(() => { | ||
| throw new Error("storage is unavailable"); | ||
| }); | ||
| fireEvent.click(screen.getByLabelText("Dismiss")); | ||
| setItem.mockRestore(); | ||
|
|
||
| expect(screen.queryByText("Optional")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("clears the unread count for the session even when it cannot be persisted", () => { | ||
| installSource([{ id: "a", title: "One", body: "" }]); | ||
|
|
||
| const setItem = vi | ||
| .spyOn(Storage.prototype, "setItem") | ||
| .mockImplementation(() => { | ||
| throw new Error("storage is unavailable"); | ||
| }); | ||
|
|
||
| render(<NoticeInbox />); | ||
| fireEvent.click(screen.getByTestId("notice-inbox-trigger")); | ||
|
|
||
| expect(screen.queryByTestId("notice-inbox-unread")).not.toBeInTheDocument(); | ||
|
|
||
| cleanup(); | ||
| render(<NoticeInbox />); | ||
| setItem.mockRestore(); | ||
|
|
||
| expect(screen.queryByTestId("notice-inbox-unread")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("keeps the centre open and reachable after the last notice goes", () => { | ||
| installSource([ | ||
| { id: "a", title: "Optional", body: "", dismissible: true }, | ||
| ]); | ||
|
|
||
| render(<NoticeInbox />); | ||
| const trigger = screen.getByTestId("notice-inbox-trigger"); | ||
| fireEvent.click(trigger); | ||
| fireEvent.click(screen.getByLabelText("Dismiss")); | ||
|
|
||
| expect(screen.getByTestId("notice-inbox-empty")).toHaveTextContent( | ||
| "No notices", | ||
| ); | ||
| expect(screen.queryByTestId("notice-inbox-hide")).not.toBeInTheDocument(); | ||
|
|
||
| fireEvent.keyDown(screen.getByTestId("notice-inbox"), { key: "Escape" }); | ||
| expect(trigger).toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(trigger); | ||
|
|
||
| expect(screen.getByTestId("notice-inbox-empty")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("caps the unread badge, keeping the exact count on the trigger", () => { | ||
| installSource( | ||
| Array.from({ length: 12 }, (_, index) => ({ | ||
| id: `notice-${index}`, | ||
| title: `Notice ${index}`, | ||
| body: "", | ||
| })), | ||
| ); | ||
|
|
||
| render(<NoticeInbox />); | ||
|
|
||
| expect(screen.getByTestId("notice-inbox-unread")).toHaveTextContent("9+"); | ||
| expect(screen.getByTestId("notice-inbox-trigger")).toHaveAttribute( | ||
| "aria-label", | ||
| "Notices, 12 unread", | ||
| ); | ||
| }); | ||
|
|
||
| it("orders the list by severity", () => { | ||
| installSource([ | ||
| { id: "a", title: "Info", body: "", variant: "info" }, | ||
| { id: "b", title: "Error", body: "", variant: "error" }, | ||
| { id: "c", title: "Warning", body: "", variant: "warning" }, | ||
| ]); | ||
|
|
||
| render(<NoticeInbox />); | ||
| fireEvent.click(screen.getByTestId("notice-inbox-trigger")); | ||
|
|
||
| expect( | ||
| screen.getAllByTestId("info-box-title").map((el) => el.textContent), | ||
| ).toEqual(["Error", "Warning", "Info"]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { useEffect } from "react"; | ||
|
|
||
| import TooltipButton from "@/components/shared/Buttons/TooltipButton"; | ||
| import { NoticeCard } from "@/components/shared/Notices/NoticeCard"; | ||
| import { Badge } from "@/components/ui/badge"; | ||
| import { Icon } from "@/components/ui/icon"; | ||
| import { BlockStack } from "@/components/ui/layout"; | ||
| import { | ||
| Popover, | ||
| PopoverContent, | ||
| PopoverTrigger, | ||
| } from "@/components/ui/popover"; | ||
| import { Text } from "@/components/ui/typography"; | ||
| import { closeNoticeInbox, useNoticeInbox } from "@/hooks/useNoticeInbox"; | ||
| import { tracking } from "@/utils/tracking"; | ||
|
|
||
| const VIEWPORT_GUTTER = 16; | ||
|
|
||
| function triggerLabel(unreadCount: number, total: number): string { | ||
| if (unreadCount > 0) return `Notices, ${unreadCount} unread`; | ||
| if (total > 0) return `Notices, ${total} active`; | ||
| return "Notices, none"; | ||
| } | ||
|
|
||
| export const NoticeInbox = () => { | ||
| const { notices, unreadCount, isOpen, setOpen, dismiss } = useNoticeInbox(); | ||
|
|
||
| useEffect(() => closeNoticeInbox, []); | ||
|
|
||
| const label = triggerLabel(unreadCount, notices.length); | ||
|
|
||
| return ( | ||
| <Popover open={isOpen} onOpenChange={setOpen}> | ||
| <PopoverTrigger asChild> | ||
| <TooltipButton | ||
| tooltip="Notices" | ||
| variant="header" | ||
| className="relative" | ||
| aria-label={label} | ||
| data-testid="notice-inbox-trigger" | ||
| {...tracking("header.notices")} | ||
| > | ||
| <Icon name="Megaphone" /> | ||
| {unreadCount > 0 && ( | ||
| <Badge | ||
| size="xs" | ||
| shape="rounded" | ||
| variant="destructive" | ||
| position="topright" | ||
| data-testid="notice-inbox-unread" | ||
| > | ||
| {unreadCount > 9 ? "9+" : unreadCount} | ||
| </Badge> | ||
| )} | ||
| </TooltipButton> | ||
| </PopoverTrigger> | ||
| <PopoverContent | ||
| align="end" | ||
| collisionPadding={VIEWPORT_GUTTER} | ||
| className="w-96" | ||
| data-testid="notice-inbox" | ||
| > | ||
| <BlockStack gap="3"> | ||
| <Text as="span" size="sm" weight="semibold"> | ||
| Notices | ||
| </Text> | ||
| {notices.length === 0 ? ( | ||
| <Text | ||
| as="span" | ||
| size="sm" | ||
| tone="subdued" | ||
| data-testid="notice-inbox-empty" | ||
| > | ||
| No notices | ||
| </Text> | ||
| ) : ( | ||
| notices.map((notice) => ( | ||
| <NoticeCard | ||
| key={notice.id} | ||
| notice={notice} | ||
| onDismiss={ | ||
| notice.dismissible ? () => dismiss(notice) : undefined | ||
| } | ||
| /> | ||
| )) | ||
| )} | ||
| </BlockStack> | ||
| </PopoverContent> | ||
| </Popover> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.