-
Notifications
You must be signed in to change notification settings - Fork 763
feat: add OS notifications for completed/failed tasks #976
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
Open
yehyal
wants to merge
5
commits into
pingdotgg:main
Choose a base branch
from
yehyal:feat/notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+381
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
000bedd
Add OS Notifications for completed/failed tasks
yehya-allawand 7de3609
Merge branch 'main' into feat/notifications
yehyal dadf2b2
Merge branch 'main' into feat/notifications
yehyal e969db5
Moved Notification Logic to named hook
yehya-allawand 0a1e63b
Merge branch 'main' into feat/notifications
yehyal 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { useCallback, useEffect, useState } from "react"; | ||
|
|
||
| import { | ||
| getNotificationPermission, | ||
| requestNotificationPermission, | ||
| } from "../lib/nativeNotifications"; | ||
|
|
||
| export function useNotification() { | ||
| const [permission, setPermission] = useState(getNotificationPermission()); | ||
|
|
||
| const refresh = useCallback(() => { | ||
| setPermission(getNotificationPermission()); | ||
| }, []); | ||
|
|
||
| const requestPermission = useCallback(async () => { | ||
| const next = await requestNotificationPermission(); | ||
| setPermission(next); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| refresh(); | ||
| window.addEventListener("focus", refresh); | ||
|
|
||
| return () => { | ||
| window.removeEventListener("focus", refresh); | ||
| }; | ||
| }, [refresh]); | ||
|
|
||
| return { permission, requestPermission, refresh }; | ||
| } |
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,132 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { | ||
| canShowNativeNotification, | ||
| getNotificationPermission, | ||
| requestNotificationPermission, | ||
| showNativeNotification, | ||
| } from "./nativeNotifications"; | ||
|
|
||
| type TestWindow = Window & typeof globalThis & { desktopBridge?: unknown; nativeApi?: unknown }; | ||
|
|
||
| const getTestWindow = (): TestWindow => { | ||
| const testGlobal = globalThis as typeof globalThis & { window?: TestWindow }; | ||
| if (!testGlobal.window) { | ||
| testGlobal.window = {} as TestWindow; | ||
| } | ||
| return testGlobal.window; | ||
| }; | ||
|
|
||
| const createNotificationMock = () => { | ||
| const ctorSpy = vi.fn(); | ||
|
|
||
| class MockNotification { | ||
| static permission: NotificationPermission = "default"; | ||
| static requestPermission = vi.fn(async () => "default" as NotificationPermission); | ||
|
|
||
| constructor(title: string, options?: NotificationOptions) { | ||
| ctorSpy({ title, options }); | ||
| } | ||
| } | ||
|
|
||
| return { MockNotification, ctorSpy }; | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| const win = getTestWindow(); | ||
| delete win.desktopBridge; | ||
| delete win.nativeApi; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete (globalThis as { Notification?: unknown }).Notification; | ||
| }); | ||
|
|
||
| describe("nativeNotifications", () => { | ||
| it("returns unsupported permission when Notification is unavailable", () => { | ||
| delete (globalThis as { Notification?: unknown }).Notification; | ||
| expect(getNotificationPermission()).toBe("unsupported"); | ||
| }); | ||
|
|
||
| it("returns permission when Notification is available", () => { | ||
| const { MockNotification } = createNotificationMock(); | ||
| MockNotification.permission = "granted"; | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
|
|
||
| expect(getNotificationPermission()).toBe("granted"); | ||
| }); | ||
|
|
||
| it("requests permission when supported", async () => { | ||
| const { MockNotification } = createNotificationMock(); | ||
| MockNotification.requestPermission = vi.fn(async () => "granted"); | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
|
|
||
| await expect(requestNotificationPermission()).resolves.toBe("granted"); | ||
| expect(MockNotification.requestPermission).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("falls back to current permission when request throws", async () => { | ||
| const { MockNotification } = createNotificationMock(); | ||
| MockNotification.permission = "denied"; | ||
| MockNotification.requestPermission = vi.fn(async () => { | ||
| throw new Error("no"); | ||
| }); | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
|
|
||
| await expect(requestNotificationPermission()).resolves.toBe("denied"); | ||
| }); | ||
|
|
||
| it("canShowNativeNotification respects permission in web context", () => { | ||
| const { MockNotification } = createNotificationMock(); | ||
| MockNotification.permission = "denied"; | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
|
|
||
| expect(canShowNativeNotification()).toBe(false); | ||
| MockNotification.permission = "granted"; | ||
| expect(canShowNativeNotification()).toBe(true); | ||
| }); | ||
|
|
||
| it("canShowNativeNotification is allowed in desktop context when supported", () => { | ||
| const { MockNotification } = createNotificationMock(); | ||
| MockNotification.permission = "denied"; | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
| (getTestWindow() as unknown as Record<string, unknown>).desktopBridge = {}; | ||
|
|
||
| expect(canShowNativeNotification()).toBe(true); | ||
| }); | ||
|
|
||
| it("showNativeNotification returns false when permission is not granted", () => { | ||
| const { MockNotification, ctorSpy } = createNotificationMock(); | ||
| MockNotification.permission = "denied"; | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
|
|
||
| expect(showNativeNotification({ title: "Test" })).toBe(false); | ||
| expect(ctorSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("showNativeNotification sends a notification when allowed", () => { | ||
| const { MockNotification, ctorSpy } = createNotificationMock(); | ||
| MockNotification.permission = "granted"; | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
|
|
||
| expect( | ||
| showNativeNotification({ | ||
| title: "Test", | ||
| body: "Hello", | ||
| tag: "tag-1", | ||
| }), | ||
| ).toBe(true); | ||
| expect(ctorSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("showNativeNotification sends a notification in desktop mode", () => { | ||
| const { MockNotification, ctorSpy } = createNotificationMock(); | ||
| MockNotification.permission = "denied"; | ||
| (globalThis as { Notification?: unknown }).Notification = MockNotification; | ||
| (getTestWindow() as unknown as Record<string, unknown>).nativeApi = {}; | ||
|
|
||
| expect(showNativeNotification({ title: "Test" })).toBe(true); | ||
| expect(ctorSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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,57 @@ | ||
| export function isAppBackgrounded(): boolean { | ||
| if (typeof document === "undefined") return false; | ||
| if (document.visibilityState !== "visible") return true; | ||
| if (typeof document.hasFocus === "function") { | ||
| return !document.hasFocus(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| export function canShowNativeNotification(): boolean { | ||
| if (typeof Notification === "undefined") return false; | ||
| if ( | ||
| typeof window !== "undefined" && | ||
| (window.desktopBridge !== undefined || window.nativeApi !== undefined) | ||
| ) { | ||
| return true; | ||
| } | ||
| return Notification.permission === "granted"; | ||
| } | ||
|
|
||
| export function getNotificationPermission(): NotificationPermission | "unsupported" { | ||
| if (typeof Notification === "undefined") return "unsupported"; | ||
| return Notification.permission; | ||
| } | ||
|
|
||
| export async function requestNotificationPermission(): Promise< | ||
| NotificationPermission | "unsupported" | ||
| > { | ||
| if (typeof Notification === "undefined") return "unsupported"; | ||
| try { | ||
| return await Notification.requestPermission(); | ||
| } catch { | ||
| return Notification.permission; | ||
| } | ||
| } | ||
|
|
||
| export function showNativeNotification(input: { | ||
| title: string; | ||
| body?: string; | ||
| tag?: string; | ||
| }): boolean { | ||
| if (!canShowNativeNotification()) return false; | ||
| try { | ||
| const options: NotificationOptions = {}; | ||
| if (input.body !== undefined) { | ||
| options.body = input.body; | ||
| } | ||
| if (input.tag !== undefined) { | ||
| options.tag = input.tag; | ||
| } | ||
| const notification = new Notification(input.title, options); | ||
| void notification; | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also notify on input/approval requested?
also maybe have the setting be more configurable than a boolean flag so users can set more granular levels of when they wanna be notified
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to push out a mvp first for just task status notification, but I can definitely work on adding it for input/approval
can you give me a bit more info regarding fine tuning the notifications? some example scenarios / levels that you want to add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which style do you prefer
Codex App current style:
