1import type { NotificationResponse } from "expo-notifications";
2import * as Schema from "effect/Schema";
3
4export class NotificationNavigationError extends Schema.TaggedError<NotificationNavigationError>()(
5 "NotificationNavigationError",
6 {
7 operation: Schema.Literals(["read", "route", "clear"]),
8 notificationId: Schema.optional(Schema.String),
9 cause: Schema.Defect(),
10 },
11) {
12 override get message(): string {
13 return `Failed to ${this.operation} the last notification response.`;
14 }
15}
16
17export async function consumeLastAgentNotificationResponse(input: {
18 readonly getLastResponse: () => Promise<NotificationResponse | null>;
19 readonly clearLastResponse: () => Promise<void>;
20 readonly handleResponse: (response: NotificationResponse) => void;
21}): Promise<void> {
22 let response: NotificationResponse | null;
23 try {
24 response = await input.getLastResponse();
25 } catch (cause) {
26 console.error(new NotificationNavigationError({ operation: "read", cause }));
27 return;
28 }
29
30 if (!response) {
31 return;
32 }
33
34 try {
35 input.handleResponse(response);
36 } catch (cause) {
37 console.error(
38 new NotificationNavigationError({
39 operation: "route",
40 notificationId: response.notification.request.identifier,
41 cause,
42 }),
43 );
44 return;
45 }
46
47 try {
48 await input.clearLastResponse();
49 } catch (cause) {
50 console.error(
51 new NotificationNavigationError({
52 operation: "clear",
53 notificationId: response.notification.request.identifier,
54 cause,
55 }),
56 );
57 }
58}