Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix: channel read status (#2492)

gbirman15de2fdb252f
diff --git a/js/app/packages/app/component/app-sidebar/channels-unread-widget.tsx b/js/app/packages/app/component/app-sidebar/channels-unread-widget.tsx
index d2ba046941..cd193ffd2d 100644
--- a/js/app/packages/app/component/app-sidebar/channels-unread-widget.tsx
+++ b/js/app/packages/app/component/app-sidebar/channels-unread-widget.tsx
@@ -12,42 +12,30 @@ import {
} from 'solid-js';
import ChannelIcon from '@macro-icons/wide/channel.svg?component-solid';
import { UserIcon } from '@core/component/UserIcon';
-import {
- isChannelNotification,
- useSenderName,
-} from '@app/component/app-sidebar/utils';
+import { useSenderName } from '@app/component/app-sidebar/utils';
import { Button } from '@app/component/next-soup/soup-view/filters-bar/button';
import { globalSplitManager } from '@app/signal/splitLayout';
import { compareDateDesc } from '@core/util/date';
import { ContextMenuContent, MenuItem } from '@core/component/Menu';
import { ContextMenu } from '@kobalte/core/context-menu';
import { getChannelNotificationParams } from '@notifications/notification-navigation';
+import { isChannelNotification } from '@notifications/notification-helpers';
function getChannelInfo(notification: UnifiedNotification): {
channelName: string | null;
channelType: string | null;
isDM: boolean;
} {
- const metadata = notification.notification_metadata;
-
- if (
- metadata.tag === 'channel_mention' ||
- metadata.tag === 'channel_message_send' ||
- metadata.tag === 'channel_message_reply'
- ) {
- const channelType = metadata.content.channelType;
- const isDM = channelType === 'directMessage';
- return {
- channelName:
- 'channelName' in metadata.content
- ? (metadata.content.channelName ?? null)
- : null,
- channelType,
- isDM,
- };
+ if (!isChannelNotification(notification)) {
+ return { channelName: null, channelType: null, isDM: false };
}
- return { channelName: null, channelType: null, isDM: false };
+ const meta = notification.notification_metadata;
+ const channelType = meta.content.channelType;
+ const isDM = channelType === 'directMessage';
+ const channelName =
+ 'channelName' in meta.content ? (meta.content.channelName ?? null) : null;
+ return { channelName, channelType, isDM };
}
interface ChannelGroup {
diff --git a/js/app/packages/app/component/app-sidebar/utils.ts b/js/app/packages/app/component/app-sidebar/utils.ts
index 4322c1a885..c1b70e509e 100644
--- a/js/app/packages/app/component/app-sidebar/utils.ts
+++ b/js/app/packages/app/component/app-sidebar/utils.ts
@@ -1,5 +1,4 @@
import { tryMacroId, useDisplayNameParts } from '@core/user';
-import type { UnifiedNotification } from '@notifications';
export function useSenderName(senderId: string | null | undefined) {
const nameParts = useDisplayNameParts(tryMacroId(senderId ?? ''));
@@ -19,21 +18,3 @@ export function useSenderName(senderId: string | null | undefined) {
return null;
};
}
-
-const CHANNEL_NOTIFICATION_TYPES = [
- 'channel_mention',
- 'channel_message_send',
- 'channel_message_reply',
-] as const;
-
-type ChannelNotificationType = (typeof CHANNEL_NOTIFICATION_TYPES)[number];
-
-export function isChannelNotification(
- notification: UnifiedNotification
-): notification is UnifiedNotification & {
- notification_event_type: ChannelNotificationType;
-} {
- return CHANNEL_NOTIFICATION_TYPES.includes(
- notification.notification_event_type as ChannelNotificationType
- );
-}
diff --git a/js/app/packages/entity/src/utils/filter.ts b/js/app/packages/entity/src/utils/filter.ts
index 303fc8fb86..840084250e 100644
--- a/js/app/packages/entity/src/utils/filter.ts
+++ b/js/app/packages/entity/src/utils/filter.ts
@@ -1,7 +1,8 @@
+import { notificationIsRead } from '@notifications';
import type { EntityData } from '../types/entity';
import type { WithNotification } from '../types/notification';
export function unreadFilterFn(entity: WithNotification<EntityData>) {
if (entity.type === 'email') return !entity.isRead;
- return entity.notifications?.()?.some(({ viewed_at }) => !viewed_at) ?? false;
+ return entity.notifications?.()?.some((n) => !notificationIsRead(n)) ?? false;
}
diff --git a/js/app/packages/entity/src/utils/notification.ts b/js/app/packages/entity/src/utils/notification.ts
index fc92808f14..d38c7ffeaf 100644
--- a/js/app/packages/entity/src/utils/notification.ts
+++ b/js/app/packages/entity/src/utils/notification.ts
@@ -1,6 +1,6 @@
import type { NotificationStack } from '@notifications/notification-stacking';
import type { Notification } from '../types/notification';
-import type { UnifiedNotification } from '@notifications';
+import { notificationIsRead, type UnifiedNotification } from '@notifications';
import { match } from 'ts-pattern';
/**
@@ -105,11 +105,7 @@ export function isNotificationUnread(
): boolean {
if ('notifications' in item && Array.isArray(item.notifications)) {
const stack = item as NotificationStack;
- return stack.notifications.some(
- (notification) => !notification.viewed_at && !notification.done
- );
+ return stack.notifications.some((n) => !notificationIsRead(n));
}
-
- const notification = item as Notification;
- return !notification.viewed_at && !notification.done;
+ return !notificationIsRead(item as Notification);
}
diff --git a/js/app/packages/notifications/components/MarkMessageNotifications.tsx b/js/app/packages/notifications/components/MarkMessageNotifications.tsx
index 3f64089744..a87f55cd7c 100644
--- a/js/app/packages/notifications/components/MarkMessageNotifications.tsx
+++ b/js/app/packages/notifications/components/MarkMessageNotifications.tsx
@@ -2,7 +2,10 @@ import { onCleanup, onMount } from 'solid-js';
import type { JSXElement } from 'solid-js';
import { useGlobalNotificationSource } from '@app/component/GlobalAppState';
import type { UnifiedNotification } from '@notifications/types';
-import { useNotificationsForEntity } from '@notifications/notification-helpers';
+import {
+ isChannelNotification,
+ useNotificationsForEntity,
+} from '@notifications/notification-helpers';
export function MarkMessageNotifications(props: {
messageId: string;
@@ -14,14 +17,9 @@ export function MarkMessageNotifications(props: {
type: 'channel',
id: props.channelId,
});
- const isMessageNotification = (n: UnifiedNotification) => {
- return (
- (n.notification_metadata.tag === 'channel_mention' ||
- n.notification_metadata.tag === 'channel_message_send' ||
- n.notification_metadata.tag === 'channel_message_reply') &&
- n.notification_metadata.content.messageId === props.messageId
- );
- };
+ const isMessageNotification = (n: UnifiedNotification) =>
+ isChannelNotification(n) &&
+ n.notification_metadata.content.messageId === props.messageId;
let unsubscribe: () => void = () => {};
onCleanup(() => {
diff --git a/js/app/packages/notifications/index.ts b/js/app/packages/notifications/index.ts
index 10978945b1..69e13bc183 100644
--- a/js/app/packages/notifications/index.ts
+++ b/js/app/packages/notifications/index.ts
@@ -41,6 +41,7 @@ export {
} from './notification-metadata';
export { openNotificationFromId } from './notification-navigation';
export { openNotification } from './notification-navigation';
+export { CHANNEL_EVENT_TYPES } from './notification-source';
export type {
PlatformNotificationData,
PlatformNotificationHandle,
diff --git a/js/app/packages/notifications/notification-helpers.ts b/js/app/packages/notifications/notification-helpers.ts
index 6e25681bb6..a5e2de587c 100644
--- a/js/app/packages/notifications/notification-helpers.ts
+++ b/js/app/packages/notifications/notification-helpers.ts
@@ -1,8 +1,14 @@
import type { Entity, EntityType } from '@core/types';
import { type Accessor, createEffect, createMemo, onCleanup } from 'solid-js';
+import { isMatching, P } from 'ts-pattern';
+import { CHANNEL_EVENT_TYPES } from './notification-source';
import type { NotificationSource } from './notification-source';
import { type UnifiedNotification, compositeEntity } from './types';
+export const isChannelNotification = isMatching({
+ notification_metadata: { tag: P.union(...CHANNEL_EVENT_TYPES) },
+});
+
/**
* Returns a reactive accessor to all notifications for a given entity
* @param notificationSource
@@ -48,7 +54,13 @@ export function notificationIsOfEntityType(
* @returns boolean
*/
export function notificationIsRead(notification: UnifiedNotification): boolean {
- return !!notification.viewed_at || notification.done;
+ if (notification.viewed_at || notification.done) return true;
+ if (
+ notification.entity_type === 'channel' &&
+ !isChannelNotification(notification)
+ )
+ return true;
+ return false;
}
/**
diff --git a/js/app/packages/notifications/notification-navigation.ts b/js/app/packages/notifications/notification-navigation.ts
index f0d5146605..90548cbbc0 100644
--- a/js/app/packages/notifications/notification-navigation.ts
+++ b/js/app/packages/notifications/notification-navigation.ts
@@ -8,12 +8,8 @@ import { errAsync, ResultAsync } from 'neverthrow';
import { match, P } from 'ts-pattern';
import type { NotificationSource } from './notification-source';
import { getChannelParams } from '@block-channel/utils/link';
-
-const CHANNEL_EVENT_TYPES = [
- 'channel_mention',
- 'channel_message_send',
- 'channel_message_reply',
-] as const;
+import { isChannelNotification } from './notification-helpers';
+import { CHANNEL_EVENT_TYPES } from './notification-source';
/**
* Opens a split if it is not already open.
@@ -42,19 +38,23 @@ function openSplitIfNotOpen(
export function getChannelNotificationParams(
notification: UnifiedNotification
): { messageId?: string; threadId?: string; params?: Record<string, string> } {
- let messageId: string | undefined;
- let threadId: string | undefined;
-
- const tag = notification.notification_metadata.tag;
- if (tag === 'channel_mention') {
- messageId = notification.notification_metadata.content.messageId;
- threadId = notification.notification_metadata.content.threadId ?? undefined;
- } else if (tag === 'channel_message_send') {
- messageId = notification.notification_metadata.content.messageId;
- } else if (tag === 'channel_message_reply') {
- messageId = notification.notification_metadata.content.messageId;
- threadId = notification.notification_metadata.content.threadId;
- }
+ if (!isChannelNotification(notification)) return {};
+
+ const meta = notification.notification_metadata;
+ const { messageId, threadId } = match(meta)
+ .with({ tag: 'channel_mention' }, (m) => ({
+ messageId: m.content.messageId,
+ threadId: m.content.threadId ?? undefined,
+ }))
+ .with({ tag: 'channel_message_send' }, (m) => ({
+ messageId: m.content.messageId,
+ threadId: undefined,
+ }))
+ .with({ tag: 'channel_message_reply' }, (m) => ({
+ messageId: m.content.messageId,
+ threadId: m.content.threadId,
+ }))
+ .exhaustive();
const params = messageId ? getChannelParams(messageId, threadId) : undefined;
return { messageId, threadId, params };
diff --git a/js/app/packages/notifications/notification-source.ts b/js/app/packages/notifications/notification-source.ts
index 7ad8233177..e63ed69a95 100644
--- a/js/app/packages/notifications/notification-source.ts
+++ b/js/app/packages/notifications/notification-source.ts
@@ -34,6 +34,12 @@ import {
} from './types';
import { fromZodError } from 'zod-validation-error';
+export const CHANNEL_EVENT_TYPES = [
+ 'channel_mention',
+ 'channel_message_send',
+ 'channel_message_reply',
+] as const;
+
type NotificationsByEntity = Record<CompositeEntity, UnifiedNotification[]>;
type UnsubscribeFn = () => void;
diff --git a/js/app/packages/notifications/notification-stacking.ts b/js/app/packages/notifications/notification-stacking.ts
index 9a78610efb..2f20b6761b 100644
--- a/js/app/packages/notifications/notification-stacking.ts
+++ b/js/app/packages/notifications/notification-stacking.ts
@@ -1,6 +1,8 @@
import { compareDateDesc } from '@core/util/date';
import type { UnifiedNotification } from './types';
import type { NotificationType } from '@core/types';
+import { isChannelNotification } from './notification-helpers';
+import { match } from 'ts-pattern';
export interface NotificationStack {
type: NotificationType;
@@ -30,10 +32,9 @@ export function getAllNotificationsFromGroup(
*/
export function getThreadId(group: NotificationStack): string {
const notification = group.notifications[0];
- if (notification.notification_metadata.tag === 'channel_message_reply') {
- return notification.notification_metadata.content.threadId ?? '';
- }
- return '';
+ return match(notification.notification_metadata)
+ .with({ tag: 'channel_message_reply' }, (m) => m.content.threadId ?? '')
+ .otherwise(() => '');
}
/**
@@ -42,55 +43,41 @@ export function getThreadId(group: NotificationStack): string {
export function stackNotifications(
notifications: UnifiedNotification[]
): NotificationStack[] {
- // Do no stack and document_mention type notifications
- const filteredNotifications = notifications.filter(
- (n) => n.notification_metadata.tag !== 'document_mention'
- );
+ const filteredNotifications = notifications.filter(isChannelNotification);
- const isChannelMention = (n: UnifiedNotification) =>
- n.notification_metadata.tag === 'channel_mention';
- const isChannelMessageSend = (n: UnifiedNotification) =>
- n.notification_metadata.tag === 'channel_message_send';
- const isChannelMessageReply = (n: UnifiedNotification) =>
- n.notification_metadata.tag === 'channel_message_reply';
+ const byTag = (tag: string) => (n: UnifiedNotification) =>
+ n.notification_metadata.tag === tag;
// Collect mention messageIds for shadowing
const mentionedMsgIds = new Set(
filteredNotifications
- .filter(isChannelMention)
- .flatMap((n) =>
- n.notification_metadata.tag === 'channel_mention'
- ? [n.notification_metadata.content.messageId]
- : []
+ .filter(byTag('channel_mention'))
+ .map((n) =>
+ match(n.notification_metadata)
+ .with({ tag: 'channel_mention' }, (m) => m.content.messageId)
+ .otherwise(() => undefined)
)
.filter(Boolean)
);
- const isShadowed = (n: UnifiedNotification) => {
- const tag = n.notification_metadata.tag;
- if (tag === 'channel_message_send' || tag === 'channel_message_reply') {
- const messageId = n.notification_metadata.content.messageId;
- return messageId && mentionedMsgIds.has(messageId);
- }
- return false;
- };
+ const isShadowed = (n: UnifiedNotification) =>
+ match(n.notification_metadata)
+ .with(
+ { tag: 'channel_message_send' },
+ { tag: 'channel_message_reply' },
+ (m) => !!m.content.messageId && mentionedMsgIds.has(m.content.messageId)
+ )
+ .otherwise(() => false);
- // Partition by type
- const mentions = filteredNotifications.filter(isChannelMention);
+ const mentions = filteredNotifications.filter(byTag('channel_mention'));
const newMsgs = filteredNotifications
- .filter(isChannelMessageSend)
+ .filter(byTag('channel_message_send'))
.filter((n) => !isShadowed(n));
const replies = filteredNotifications
- .filter(isChannelMessageReply)
+ .filter(byTag('channel_message_reply'))
.filter((n) => !isShadowed(n));
- const others = filteredNotifications.filter(
- (n) =>
- !isChannelMention(n) &&
- !isChannelMessageSend(n) &&
- !isChannelMessageReply(n)
- );
+ const others = notifications.filter((n) => !isChannelNotification(n));
- // Build groups
const groups: NotificationStack[] = [
...mentions.flatMap((n) => makeStack('channel_mention', [n])),
...makeStack('channel_message_send', newMsgs),
@@ -98,7 +85,6 @@ export function stackNotifications(
...others.flatMap((n) => makeStack(n.notification_metadata.tag, [n])),
];
- // Sort by newest notification in each stack
return groups.sort((a, b) =>
compareDateDesc(
a.notifications[0].created_at,
@@ -136,16 +122,15 @@ function makeStack(
}
function makeReplyStacks(replies: UnifiedNotification[]): NotificationStack[] {
- const byThread = groupBy(replies, (r) => {
- if (r.notification_metadata.tag === 'channel_message_reply') {
- return r.notification_metadata.content.threadId ?? '';
- }
- return '';
- });
+ const byThread = groupBy(replies, (r) =>
+ match(r.notification_metadata)
+ .with({ tag: 'channel_message_reply' }, (m) => m.content.threadId ?? '')
+ .otherwise(() => '')
+ );
return [...byThread.entries()]
.filter(([threadId]) => threadId !== '')
.map(([, group]) => ({
- type: 'channel_message_reply',
+ type: 'channel_message_reply' as const,
notifications: sortByRecency(group),
}));
}