Skip to content
Open
Show file tree
Hide file tree
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
@@ -0,0 +1,37 @@
import type { Notification } from "expo-notifications";
import { describe, expect, it } from "vite-plus/test";

import { foregroundNotificationBehavior } from "./foregroundNotificationBehavior";

function notificationFor(deepLink: string): Notification {
return {
date: 0,
request: { identifier: "notification-1", content: { data: { deepLink } }, trigger: null },
} as unknown as Notification;
}

describe("foregroundNotificationBehavior", () => {
const onScreen = "/threads/env-1/thread-1";

it("stays silent for the thread on screen once its feed is live", () => {
const behavior = foregroundNotificationBehavior(
notificationFor(onScreen),
onScreen,
() => true,
);
expect(behavior.shouldShowBanner).toBe(false);
expect(behavior.shouldPlaySound).toBe(false);
});

it("shows the alert while the thread on screen is cached or reconnecting", () => {
const checked: Array<string> = [];
const behavior = foregroundNotificationBehavior(notificationFor(onScreen), onScreen, (link) => {
checked.push(link);
return false;
});
expect(behavior.shouldShowBanner).toBe(true);
expect(behavior.shouldShowList).toBe(true);
expect(behavior.shouldPlaySound).toBe(true);
expect(checked).toEqual([onScreen]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ const SUPPRESS: NotificationBehavior = {
shouldSetBadge: false,
};

/**
* A notification for the thread on screen stays silent once its feed is live. While that thread
* is cached, reconnecting or still catching up, the screen may not have the event yet.
*/
export function foregroundNotificationBehavior(
notification: Notification,
deepLinkOnScreen: string | null,
hasLiveFeed: (deepLink: string) => boolean,
): NotificationBehavior {
const target = extractAgentNotificationDeepLink({ notification });
if (target !== null && target === deepLinkOnScreen) {
if (target !== null && target === deepLinkOnScreen && hasLiveFeed(target)) {
return SUPPRESS;
}
return SHOW;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
import * as Notifications from "expo-notifications";
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import * as Option from "effect/Option";
import { AsyncResult } from "effect/reactivity";

import { appAtomRegistry } from "../../state/atom-registry";
import { environmentPresentations } from "../../state/presentation";
import { environmentThreads } from "../../state/threads";
import { setAndroidThreadOnScreen } from "./androidNotifications";
import { foregroundNotificationBehavior } from "./foregroundNotificationBehavior";
import { routeAgentNotificationResponseOnce, threadDeepLinkOnScreen } from "./notificationPayload";
import { consumeLastAgentNotificationResponse } from "./notificationResponseConsumer";

let deepLinkOnScreen: string | null = null;

/** Whether the open thread's screen is connected and showing its live feed, not a cached copy. */
function threadHasLiveFeed(deepLink: string): boolean {
const [, , environment, thread] = deepLink.split("/");
if (!environment || !thread) return false;
const environmentId = EnvironmentId.make(decodeURIComponent(environment));
const presentation = appAtomRegistry.get(
environmentPresentations.presentationAtom(environmentId),
);
if (presentation?.connection.phase !== "connected") return false;
const threadAtom = environmentThreads.stateAtom(
environmentId,
ThreadId.make(decodeURIComponent(thread)),
);
// The thread screen holds its detail; reading one nobody holds would start loading it.
if (!appAtomRegistry.getNodes().has(threadAtom)) return false;
return Option.exists(
AsyncResult.value(appAtomRegistry.get(threadAtom)),
({ status }) => status === "live",
);
}

/** Called with the workspace pathname on every root navigation state change. */
export function setAgentNotificationPathname(pathname: string): void {
deepLinkOnScreen = threadDeepLinkOnScreen(pathname);
Expand All @@ -22,7 +49,9 @@ export function installAgentNotificationNavigation(navigate: (href: string) => v
const handledResponseIds = new Set<string>();
Notifications.setNotificationHandler({
handleNotification: (notification) =>
Promise.resolve(foregroundNotificationBehavior(notification, deepLinkOnScreen)),
Promise.resolve(
foregroundNotificationBehavior(notification, deepLinkOnScreen, threadHasLiveFeed),
),
});

const handleResponse = (response: Notifications.NotificationResponse): void => {
Expand Down
Loading