Files
outline/app/stores/NotificationsStore.ts
codegen-sh[bot]andTom Moor 4fc6ac1f15 Add in-app reaction notifications (#9893)
* Add ReactionsCreate notification event type

- Add ReactionsCreate to NotificationEventType enum and defaults
- Add notification settings UI with SmileyIcon and proper labels
- Create ReactionsCreateNotificationsTask to handle comment reactions
- Update NotificationsProcessor to handle comments.add_reaction events
- Add eventText and path handling in client Notification model
- Notifications are enabled by default but never send emails

* Applied automatic fixes

* Show the actual emoji in the notification

* Cleanup notifications if reaction is removed

* PR feedback

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2025-08-11 18:54:43 -04:00

98 lines
2.5 KiB
TypeScript

import invariant from "invariant";
import orderBy from "lodash/orderBy";
import sortBy from "lodash/sortBy";
import { action, computed, runInAction } from "mobx";
import Notification from "~/models/Notification";
import { PaginationParams } from "~/types";
import { client } from "~/utils/ApiClient";
import RootStore from "./RootStore";
import Store, { RPCAction } from "./base/Store";
export default class NotificationsStore extends Store<Notification> {
actions = [RPCAction.List, RPCAction.Update];
constructor(rootStore: RootStore) {
super(rootStore, Notification);
}
@action
fetchPage = async (
options: ({ archived?: boolean } & PaginationParams) | undefined
): Promise<Notification[]> => {
this.isFetching = true;
try {
const res = await client.post("/notifications.list", options);
invariant(res?.data, "Document revisions not available");
let models: Notification[] = [];
runInAction("NotificationsStore#fetchPage", () => {
models = res.data.notifications.map(this.add);
this.isLoaded = true;
});
return models;
} finally {
this.isFetching = false;
}
};
/**
* Mark all notifications as read.
*/
@action
markAllAsRead = async () => {
await client.post("/notifications.update_all", {
viewedAt: new Date().toISOString(),
});
runInAction("NotificationsStore#markAllAsRead", () => {
const viewedAt = new Date();
this.data.forEach((notification) => {
notification.viewedAt = viewedAt;
});
});
};
/**
* Mark all notifications as archived.
*/
@action
markAllAsArchived = async () => {
await client.post("/notifications.update_all", {
archivedAt: new Date().toISOString(),
});
runInAction("NotificationsStore#markAllAsArchived", () => {
this.clear();
});
};
/**
* Returns the approximate number of unread notifications.
*/
@computed
get approximateUnreadCount(): number {
return this.active.filter((notification) => !notification.viewedAt).length;
}
/**
* Returns the notifications in order of created date.
*/
@computed
get orderedData(): Notification[] {
return sortBy(
orderBy(Array.from(this.data.values()), "createdAt", "desc"),
(item) => (item.viewedAt ? 1 : -1)
);
}
/**
* Returns only the active (non-archived) notifications.
*/
@computed
get active(): Notification[] {
return this.orderedData.filter((n) => !n.archivedAt);
}
}