Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import {
enableGraphqlSoup,
isFeatureEnabled,
} from '@core/constant/featureFlags';
import type { Entity, EntityType } from '@core/types';
import { isMutedItem } from '@entity/utils/notification';
import { queryClient } from '@queries/client';
import {
toNotificationEntityRef,
updateNotificationsForEntities,
} from '@queries/notification/entity-mutations';
import { notificationKeys } from '@queries/notification/keys';
import {
bulkMarkNotificationsAsDone,
bulkMarkNotificationsAsUndone,
} from '@queries/notification/user-notifications';
import { type Accessor, createEffect, createMemo, onCleanup } from 'solid-js';
import { isMatching, P } from 'ts-pattern';
import { DOCUMENT_COMMENT_EVENT_TYPES } from './document-comment-notification';
import type { NotificationSource } from './notification-source';
import { CHANNEL_EVENT_TYPES, setDoneOverride } from './notification-source';
import { compositeEntity, type UnifiedNotification } from './types';
export const isChannelNotification = isMatching({
notification_metadata: { tag: P.union(...CHANNEL_EVENT_TYPES) },
});
export const isDocumentCommentNotification = isMatching({
notification_metadata: { tag: P.union(...DOCUMENT_COMMENT_EVENT_TYPES) },
});
/**
* Returns a reactive accessor to all notifications for a given entity
* @param notificationSource
* @param entity
* @returns Accessor<UnifiedNotification[]>
*/
export function useNotificationsForEntity(
notificationSource: NotificationSource,
entity: Entity
): Accessor<UnifiedNotification[]> {
return createMemo(
() =>
notificationSource.notificationsByEntity()[compositeEntity(entity)] ?? []
);
}
/**
* Checks if a notification is for a specific entity
* @param notification
* @param entity
* @returns boolean
*/
export function notificationIsOfEntity(
notification: UnifiedNotification,
entity: Entity
): boolean {
return (
notification.entity_type === entity.type &&
notification.entity_id === entity.id
);
}
export function notificationIsOfEntityType(
notification: UnifiedNotification,
entityType: string
): boolean {
return notification.entity_type === entityType;
}
/**
* Checks if a notification is seen
* @param notification
* @returns boolean
*/
export function notificationIsRead(notification: UnifiedNotification): boolean {
if (notification.state !== 'unseen') return true;
if (
notification.entity_type === 'channel' &&
!isChannelNotification(notification)
)
return true;
return false;
}
/**
* Checks if an entity has unread notifications
* @param notificationSource
* @param entity
* @returns boolean
*/
export function entityHasUnreadNotifications(
notificationSource: NotificationSource,
entity: Entity
): boolean {
const notifications =
notificationSource.notificationsByEntity()[compositeEntity(entity)] ?? [];
return notifications.some((notification) => {
return (
notificationIsOfEntity(notification, entity) &&
!notificationIsRead(notification)
);
});
}
export function useUnreadNotifications(notificationSource: NotificationSource) {
return createMemo(() =>
notificationSource.notifications().filter((n) => !notificationIsRead(n))
);
}
/**
* Returns reactive accessor if an item has notifications
* @param notificationSource
* @param entity
* @returns boolean
*/
export function useEntityHasUnreadNotifications(
notificationSource: NotificationSource,
entity: Entity
): Accessor<boolean> {
return createMemo(() =>
entityHasUnreadNotifications(notificationSource, entity)
);
}
/**
* Returns a reactive accessor to all notifications for an entity type
* @param notificationSource
* @param entityType
* @returns Accessor<UnifiedNotification[]>
*/
export function useEntityTypeNotifications(
notificationSource: NotificationSource,
entityType: EntityType
): Accessor<UnifiedNotification[]> {
return createMemo(() =>
notificationSource
.notifications()
.filter((n) => notificationIsOfEntityType(n, entityType))
);
}
/**
* Returns a reactive accessor to all unread notifications for an entity type
* @param notificationSource
* @param entityType
* @returns Accessor<UnifiedNotification[]>
*/
export function useUnreadEntityTypeNotifications(
notificationSource: NotificationSource,
entityType: EntityType
): Accessor<UnifiedNotification[]> {
return createMemo(() =>
notificationSource
.notifications()
.filter(
(n) =>
notificationIsOfEntityType(n, entityType) && !notificationIsRead(n)
)
);
}
/**
* Marks all notifications for an entity as done
* @param notificationSource
* @param entity
* @returns Promise<void>
*/
export async function markNotificationsForEntityAsDone(
notificationSource: NotificationSource,
entity: Entity
): Promise<void> {
await ensureNotificationSourceLoaded(notificationSource);
return notificationSource.bulkMarkAsDone(
notificationSource.notificationsByEntity()[compositeEntity(entity)] ?? []
);
}
export async function markNotificationForEntityIdAsRead(
notificationSource: NotificationSource,
id: string
): Promise<void> {
await ensureNotificationSourceLoaded(notificationSource);
return notificationSource.bulkMarkAsRead(
notificationSource
.notifications()
.filter((n) => n.entity_id === id && !notificationIsRead(n))
);
}
/** Cold imperative actions must load the full feed rather than act on an empty snapshot. */
export async function ensureNotificationSourceLoaded(
notificationSource: NotificationSource
): Promise<void> {
const query = notificationSource._notificationsQuery;
if (!query.isStarted || query.isLoading || query.data === undefined) {
await query.refetch();
}
}
/**
* The entity's notifications this client shows as unseen, from the row's own
* edges when it has them. A GraphQL feed that has not started shows nothing
* yet, and reading it would start the full notification feed.
*/
export function shownUnseenNotificationIds(
notificationSource: NotificationSource,
entity: Entity,
attached?: readonly UnifiedNotification[]
): string[] {
const shown =
attached ??
(notificationSource._notificationsQuery.isStarted
? notificationSource.notificationsByEntity()[compositeEntity(entity)]
: undefined) ??
[];
return shown.filter(({ state }) => state === 'unseen').map(({ id }) => id);
}
/**
* Marks all notifications for an entity as read
* @param notificationSource
* @param entity
* @returns Promise<void>
*/
export async function markNotificationsForEntityAsRead(
notificationSource: NotificationSource,
entity: Entity
): Promise<void> {
const entityRef = toNotificationEntityRef(entity);
if (isFeatureEnabled(enableGraphqlSoup) && entityRef) {
await updateNotificationsForEntities({
entities: [entityRef],
operation: 'MARK_SEEN',
expectedNotificationIds: shownUnseenNotificationIds(
notificationSource,
entity
),
});
return;
}
await notificationSource.bulkMarkAsRead(
notificationSource.notificationsByEntity()[compositeEntity(entity)] ?? []
);
}
/** Best-effort read marker for navigation/timer callers with no awaiting UI. */
export async function markNotificationsForEntityAsReadInBackground(
notificationSource: NotificationSource,
entity: Entity
): Promise<void> {
try {
await markNotificationsForEntityAsRead(notificationSource, entity);
} catch (error) {
console.error('Failed to mark entity notifications as read', error);
}
}
/**
* Returns a boolean indicating whether notifications for an entity are muted
* @param notificationSource
* @param entity
* @returns Accessor<boolean>
*/
export function useNotificationsMutedForEntity(
notificationSource: NotificationSource,
entity: Entity
): Accessor<boolean> {
return createMemo(() =>
isMutedItem(notificationSource.mutedEntities(), {
item_id: entity.id,
item_type: entity.type,
})
);
}
/**
* Optimistically flips the `done` override to `true` for these ids, fires the
* bulk-done API, and rolls the override back on failure. Used as a mutation's
* mutationFn / redoFn.
*/
export async function executeMarkNotificationsDone(
notificationIds: string[]
): Promise<void> {
const rollback = setDoneOverride(notificationIds, true);
try {
await bulkMarkNotificationsAsDone(notificationIds);
} catch (err) {
rollback();
throw err;
} finally {
await queryClient.invalidateQueries({
queryKey: notificationKeys.user._def,
refetchType: 'none',
});
}
}
/**
* Optimistically flips the override to `false` and fires the bulk-undone API.
* On failure the override is re-applied so the UI stays consistent with the
* server. Used as a mutation's undoFn.
*/
export async function executeMarkNotificationsUndone(
notificationIds: string[]
): Promise<void> {
const rollback = setDoneOverride(notificationIds, false);
try {
await bulkMarkNotificationsAsUndone(notificationIds);
} catch (err) {
rollback();
throw err;
} finally {
await queryClient.invalidateQueries({
queryKey: notificationKeys.user._def,
refetchType: 'none',
});
}
}
export function createEffectOnEntityTypeNotification(
notificationSource: NotificationSource,
type: EntityType,
callback: (n: UnifiedNotification) => void
) {
createEffect(() => {
let cleanup = notificationSource.subscribe((notification) => {
if (notificationIsOfEntityType(notification, type)) {
callback(notification);
}
});
onCleanup(() => {
if (cleanup) cleanup();
});
});
}