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.

feat(notys): make new content more visible to tab re-focus (#3401)

Aidan Holloway-Bidwellbabfb086f672
diff --git a/js/app/packages/app/component/ReactiveFavicon.tsx b/js/app/packages/app/component/ReactiveFavicon.tsx
index 30fc057cac..82bf39932c 100644
--- a/js/app/packages/app/component/ReactiveFavicon.tsx
+++ b/js/app/packages/app/component/ReactiveFavicon.tsx
@@ -1,4 +1,13 @@
+import {
+ addBadgeNotification,
+ removeBadgeNotifications,
+} from '@app/signal/sidebarBadges';
+import { globalSplitManager } from '@app/signal/splitLayout';
import { updateFavicon } from '@app/util/favicon';
+import { notificationToSidebarId } from '@app/util/notification-sidebar-id';
+import { runRefocusPulses } from '@app/util/refocus-highlight';
+import { ENABLE_REFOCUS_HIGHLIGHT } from '@core/constant/featureFlags';
+import type { UnifiedNotification } from '@notifications';
import { createBroadcastChannel } from '@solid-primitives/broadcast-channel';
import { createEffect, createSignal, on, onCleanup, onMount } from 'solid-js';
import { useReactiveColorString } from '../../theme/signals/themeReactive';
@@ -9,6 +18,7 @@ type BadgeMessage = { hasBadge: boolean };
export function ReactiveFavicon() {
const [showNotificationBadge, setShowNotificationBadge] = createSignal(false);
const [isAppFocused, setIsAppFocused] = createSignal(!document.hidden);
+ const pendingNotifications: UnifiedNotification[] = [];
const globalNotifications = useGlobalNotificationSource();
@@ -38,18 +48,28 @@ export function ReactiveFavicon() {
if (!document.hidden) {
setShowNotificationBadge(false);
postBadgeMessage({ hasBadge: false });
+ const toProcess = pendingNotifications.splice(0);
+ if (ENABLE_REFOCUS_HIGHLIGHT) {
+ runRefocusPulses(toProcess, globalSplitManager());
+ }
}
};
onMount(() => {
document.addEventListener('visibilitychange', handleVisibilityChange);
- const unsubscribeNotifications = globalNotifications.subscribe(() => {
- if (!isAppFocused()) {
- setShowNotificationBadge(true);
- postBadgeMessage({ hasBadge: true });
+ const unsubscribeNotifications = globalNotifications.subscribe(
+ (newNotification) => {
+ const sidebarId = notificationToSidebarId(newNotification);
+ if (sidebarId) addBadgeNotification(sidebarId, newNotification.id);
+
+ if (!isAppFocused()) {
+ setShowNotificationBadge(true);
+ postBadgeMessage({ hasBadge: true });
+ pendingNotifications.push(newNotification);
+ }
}
- });
+ );
onCleanup(() => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
@@ -61,5 +81,13 @@ export function ReactiveFavicon() {
updateFavicon(accentColor(), badgeColor(), showNotificationBadge());
});
+ createEffect(() => {
+ const viewedOrDone = globalNotifications
+ .notifications()
+ .filter((n) => n.viewed_at || n.done)
+ .map((n) => n.id);
+ if (viewedOrDone.length > 0) removeBadgeNotifications(viewedOrDone);
+ });
+
return null;
}
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 e4f9c9900e..0c9f6a2227 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
@@ -148,11 +148,19 @@ function ChannelGroupItem(props: {
const canOpenInNewSplit = () =>
globalSplitManager()?.canAppendSplit() ?? false;
- const navigateToLatestNotification = (newSplit = false) => {
+ const navigateToLatestNotification = async (newSplit = false) => {
const manager = globalSplitManager();
if (!manager) return;
const notification = latestNotification();
- openNotification(notification, manager, newSplit);
+ try {
+ await openNotification(notification, manager, newSplit);
+ await notificationSource.markAsRead(notification);
+ } catch (error) {
+ console.error(
+ 'Failed to open unread notification from sidebar widget:',
+ error
+ );
+ }
};
const openInCurrentSplit = () => {
@@ -194,6 +202,7 @@ function ChannelGroupItem(props: {
draggable={false}
variant="ghost"
size="sm"
+ data-unread-entity-id={props.group.entityId}
classList={{
'opacity-0 -translate-y-2': !isVisible(),
'opacity-100 translate-y-0': isVisible(),
diff --git a/js/app/packages/app/component/app-sidebar/sidebar.tsx b/js/app/packages/app/component/app-sidebar/sidebar.tsx
index 6796c82ddd..73cb467121 100644
--- a/js/app/packages/app/component/app-sidebar/sidebar.tsx
+++ b/js/app/packages/app/component/app-sidebar/sidebar.tsx
@@ -15,6 +15,7 @@ import {
type ListView,
} from '@app/constants/list-views';
import { useHotkeyInterceptor } from '@app/signal/hotkeyRoot';
+import { clearSidebarBadge, hasSidebarBadge } from '@app/signal/sidebarBadges';
import { globalSplitManager } from '@app/signal/splitLayout';
import { InCallPanel } from '@channel/Call';
import { useCallContextOptional } from '@channel/Call/CallContext';
@@ -52,6 +53,7 @@ import { useLocation } from '@solidjs/router';
import { Button, cn, Hotkey } from '@ui';
import {
type Component,
+ createEffect,
createMemo,
createSignal,
For,
@@ -739,6 +741,7 @@ const SidebarLink = (props: SidebarLinkProps) => {
const analytics = useAnalytics();
const layout = useSplitLayout();
const layoutManager = globalSplitManager();
+ const hasUnread = () => hasSidebarBadge(props.id);
const location = useLocation();
@@ -755,6 +758,10 @@ const SidebarLink = (props: SidebarLinkProps) => {
return activeContent?.id === props.id;
};
+ createEffect(() => {
+ if (isActive()) clearSidebarBadge(props.id);
+ });
+
const content = () =>
({
type: 'component',
@@ -817,6 +824,7 @@ const SidebarLink = (props: SidebarLinkProps) => {
onMouseLeave={() => setIsHovering(false)}
onMouseDown={(e) => {
if (e.button !== 0) return;
+ clearSidebarBadge(props.id);
analytics.track('sidebar_click', {
view: props.id,
});
@@ -846,8 +854,11 @@ const SidebarLink = (props: SidebarLinkProps) => {
}}
>
<Show when={props.icon}>
- <div class="shrink-0 [&_svg]:size-4">
+ <div class="relative shrink-0 [&_svg]:size-4">
<Dynamic component={props.icon} triggerAnimation={isHovering()} />
+ <Show when={hasUnread() && !isActive()}>
+ <div class="absolute -top-0.5 -right-0.5 size-1.5 bg-accent rounded-full ring-surface ring-2" />
+ </Show>
</div>
</Show>
diff --git a/js/app/packages/app/component/split-layout/components/SplitPanel.tsx b/js/app/packages/app/component/split-layout/components/SplitPanel.tsx
index 0f17c67287..9db3f02206 100644
--- a/js/app/packages/app/component/split-layout/components/SplitPanel.tsx
+++ b/js/app/packages/app/component/split-layout/components/SplitPanel.tsx
@@ -179,6 +179,7 @@ export function SplitPanel(props: SplitPanelProps) {
depth={1}
>
<Panel.Header
+ data-split-header
class={cn(
'block min-h-10.25 touch:min-h-11.25 p-0 overflow-visible',
shouldHideSplitHeader() && 'hidden'
diff --git a/js/app/packages/app/signal/sidebarBadges.ts b/js/app/packages/app/signal/sidebarBadges.ts
new file mode 100644
index 0000000000..c5c42d744c
--- /dev/null
+++ b/js/app/packages/app/signal/sidebarBadges.ts
@@ -0,0 +1,59 @@
+import type { ListView } from '@app/constants/list-views';
+import { createSignal } from 'solid-js';
+
+const [badgeMap, setBadgeMap] = createSignal(new Map<ListView, Set<string>>());
+
+export function hasSidebarBadge(id: ListView): boolean {
+ return (badgeMap().get(id)?.size ?? 0) > 0;
+}
+
+export function addBadgeNotification(
+ id: ListView,
+ notificationId: string
+): void {
+ setBadgeMap((prev) => {
+ const existing = prev.get(id);
+ if (existing?.has(notificationId)) return prev;
+ const next = new Map(prev);
+ const set = new Set(existing ?? []);
+ set.add(notificationId);
+ next.set(id, set);
+ return next;
+ });
+}
+
+export function removeBadgeNotifications(
+ notificationIds: Iterable<string>
+): void {
+ const ids = new Set(notificationIds);
+ if (ids.size === 0) return;
+ setBadgeMap((prev) => {
+ let changed = false;
+ const next = new Map(prev);
+ for (const [listView, set] of prev) {
+ let dropped = false;
+ const updated = new Set<string>();
+ for (const nid of set) {
+ if (ids.has(nid)) {
+ dropped = true;
+ continue;
+ }
+ updated.add(nid);
+ }
+ if (!dropped) continue;
+ changed = true;
+ if (updated.size === 0) next.delete(listView);
+ else next.set(listView, updated);
+ }
+ return changed ? next : prev;
+ });
+}
+
+export function clearSidebarBadge(id: ListView): void {
+ setBadgeMap((prev) => {
+ if (!prev.has(id)) return prev;
+ const next = new Map(prev);
+ next.delete(id);
+ return next;
+ });
+}
diff --git a/js/app/packages/app/util/notification-sidebar-id.ts b/js/app/packages/app/util/notification-sidebar-id.ts
new file mode 100644
index 0000000000..2db13ff11d
--- /dev/null
+++ b/js/app/packages/app/util/notification-sidebar-id.ts
@@ -0,0 +1,32 @@
+import type { ListView } from '@app/constants/list-views';
+import type { NotificationType } from '@core/types';
+import type { UnifiedNotification } from '@notifications';
+
+export function notificationToSidebarId(
+ n: UnifiedNotification
+): ListView | null {
+ const tag = n.notification_metadata.tag as NotificationType;
+ switch (tag) {
+ case 'channel_mention':
+ case 'channel_message_send':
+ case 'channel_message_reply':
+ case 'channel_invite':
+ case 'call-started':
+ case 'document_mention':
+ return 'channels';
+ case 'new_email':
+ return 'mail';
+ case 'task_assigned':
+ return 'tasks';
+ case 'mentioned_in_document_comment':
+ case 'replied_to_document_comment_thread':
+ case 'commented_on_document':
+ return 'documents';
+ case 'ai_response':
+ return 'agents';
+ case 'invite_to_team':
+ return null;
+ default:
+ return null;
+ }
+}
diff --git a/js/app/packages/app/util/refocus-highlight.ts b/js/app/packages/app/util/refocus-highlight.ts
new file mode 100644
index 0000000000..285de834b8
--- /dev/null
+++ b/js/app/packages/app/util/refocus-highlight.ts
@@ -0,0 +1,218 @@
+import type { SplitManager } from '@app/component/split-layout/layoutManager';
+import type { ListView } from '@app/constants/list-views';
+import { globalSplitManager } from '@app/signal/splitLayout';
+import { notificationToSidebarId } from '@app/util/notification-sidebar-id';
+import { toast } from '@core/component/Toast/Toast';
+import { isElementVisibleInViewport } from '@core/util/isElementVisibleInViewport';
+import {
+ getChannelNotificationParams,
+ openNotification,
+ type UnifiedNotification,
+} from '@notifications';
+
+const MAX_PULSES = 5;
+const TOAST_DURATION = 6000;
+
+// For new_email, the split is opened with threadId from metadata, not entity_id.
+function getSplitEntityId(n: UnifiedNotification): string {
+ const meta = n.notification_metadata;
+ if (meta.tag === 'new_email') return meta.content.threadId;
+ return n.entity_id;
+}
+
+function pulseElement(el: Element): void {
+ const rect = el.getBoundingClientRect();
+ if (rect.width === 0 || rect.height === 0) return;
+
+ const isSlimSidebar = !!el.closest('[data-slim="true"]');
+ const glowExtension = isSlimSidebar ? 60 : 0;
+
+ const accentBg = 'oklch(var(--a0l) var(--a0c) var(--a0h) / 0.25)';
+ const background = isSlimSidebar
+ ? `linear-gradient(to right, ${accentBg} ${rect.width}px, transparent ${rect.width + glowExtension}px)`
+ : accentBg;
+
+ const overlay = document.createElement('div');
+ Object.assign(overlay.style, {
+ position: 'fixed',
+ top: `${rect.top}px`,
+ left: `${rect.left}px`,
+ width: `${rect.width + glowExtension}px`,
+ height: `${rect.height}px`,
+ background,
+ borderRadius: isSlimSidebar ? '4px 0 0 4px' : '4px',
+ pointerEvents: 'none',
+ zIndex: '2147483647',
+ });
+ document.body.appendChild(overlay);
+
+ const reducedMotion = window.matchMedia(
+ '(prefers-reduced-motion: reduce)'
+ ).matches;
+
+ const keyframes: Keyframe[] = reducedMotion
+ ? [{ opacity: '0.6' }, { opacity: '0' }]
+ : [
+ { opacity: '0.4', transform: 'scale(1.04)', offset: 0 },
+ { opacity: '0.8', transform: 'scale(1)', offset: 0.07 },
+ { opacity: '0', transform: 'scale(1)', offset: 1 },
+ ];
+
+ overlay
+ .animate(keyframes, {
+ duration: reducedMotion ? 400 : 3000,
+ easing: 'ease-out',
+ fill: 'forwards',
+ })
+ .addEventListener('finish', () => overlay.remove());
+}
+
+type ToastSection = ListView | 'unknown';
+
+const TOAST_LABEL: Partial<Record<ToastSection, string>> = {
+ channels: 'message',
+ mail: 'email',
+ tasks: 'task',
+ documents: 'document update',
+ agents: 'agent response',
+ unknown: 'notification',
+};
+
+function buildToastMessage(countsBySection: Map<ToastSection, number>): string {
+ const parts: string[] = [];
+ for (const [type, count] of countsBySection.entries()) {
+ const label = TOAST_LABEL[type] ?? 'notification';
+ parts.push(`${count} new ${label}${count > 1 ? 's' : ''}`);
+ }
+ return parts.join(', ');
+}
+
+// Returns true if the notification was handled (element pulsed, or already pulsed
+// by an earlier notification in this batch). Returns false only when the global
+// pulse cap was hit — in that case the caller should fall through to the toast bucket.
+function tryPulse(
+ el: Element,
+ pulsed: Set<Element>,
+ pulseCount: { value: number }
+): boolean {
+ if (pulsed.has(el)) return true;
+ if (pulseCount.value >= MAX_PULSES) return false;
+ pulsed.add(el);
+ pulseCount.value++;
+ pulseElement(el);
+ return true;
+}
+
+export function runRefocusPulses(
+ notifications: UnifiedNotification[],
+ splitManager: SplitManager | undefined
+): void {
+ if (notifications.length === 0) return;
+
+ const pulsed = new Set<Element>();
+ const pulseCount = { value: 0 };
+ const toastBucket: UnifiedNotification[] = [];
+
+ for (const notification of notifications) {
+ // Rule 1: channel message bubble
+ const { messageId } = getChannelNotificationParams(notification);
+ if (messageId) {
+ const msgEl = document.querySelector(
+ `[data-message-id="${CSS.escape(messageId)}"]`
+ );
+ if (msgEl instanceof HTMLElement && isElementVisibleInViewport(msgEl)) {
+ if (!tryPulse(msgEl, pulsed, pulseCount))
+ toastBucket.push(notification);
+ continue;
+ }
+ }
+
+ // Rule 2: a split is open for this entity → pulse the header
+ if (splitManager) {
+ const entityIdForSplit = getSplitEntityId(notification);
+ const matchingSplit = splitManager
+ .splits()
+ .find(
+ (s) =>
+ s.content.id === entityIdForSplit && s.content.type !== 'component'
+ );
+ if (matchingSplit) {
+ const containerEl = document.querySelector(
+ `[data-split-id="${CSS.escape(matchingSplit.id)}"] [data-split-header]`
+ );
+ if (containerEl) {
+ if (!tryPulse(containerEl, pulsed, pulseCount))
+ toastBucket.push(notification);
+ continue;
+ }
+ }
+ }
+
+ // Rule 3: generic entity row visible in the soup/list view
+ const entityEl = document.querySelector(
+ `[data-entity-id="${CSS.escape(notification.entity_id)}"]`
+ );
+ if (
+ entityEl instanceof HTMLElement &&
+ isElementVisibleInViewport(entityEl)
+ ) {
+ if (!tryPulse(entityEl, pulsed, pulseCount))
+ toastBucket.push(notification);
+ continue;
+ }
+
+ // Rule 4: unread sidebar item (e.g. channel/DM entry in the Unread widget)
+ const unreadEl = document.querySelector(
+ `[data-unread-entity-id="${CSS.escape(notification.entity_id)}"]`
+ );
+ if (unreadEl) {
+ if (!tryPulse(unreadEl, pulsed, pulseCount))
+ toastBucket.push(notification);
+ continue;
+ }
+
+ // Rule 5: type-specific sidebar nav entry (never inbox)
+ const sidebarId = notificationToSidebarId(notification);
+ if (sidebarId) {
+ const sidebarEl = document.querySelector(
+ `[data-sidebar-link="${sidebarId}"]`
+ );
+ if (sidebarEl) {
+ if (!tryPulse(sidebarEl, pulsed, pulseCount))
+ toastBucket.push(notification);
+ continue;
+ }
+ }
+
+ // Rule 6: no visible affordance — falls through to grouped toast
+ toastBucket.push(notification);
+ }
+
+ if (toastBucket.length === 0) return;
+
+ const countsBySection = new Map<ToastSection, number>();
+ let mostRecent = toastBucket[0]!;
+ for (const n of toastBucket) {
+ const key: ToastSection = notificationToSidebarId(n) ?? 'unknown';
+ countsBySection.set(key, (countsBySection.get(key) ?? 0) + 1);
+ if (n.created_at > mostRecent.created_at) mostRecent = n;
+ }
+
+ toast.custom(
+ {
+ title: buildToastMessage(countsBySection),
+ actions: [
+ {
+ label: 'View',
+ onClick: () => {
+ // Re-read the split manager at click time — the layout may have
+ // changed between toast creation and the user clicking "View".
+ const manager = globalSplitManager();
+ if (manager) openNotification(mostRecent, manager);
+ },
+ },
+ ],
+ },
+ { duration: TOAST_DURATION }
+ );
+}
diff --git a/js/app/packages/core/constant/featureFlags.ts b/js/app/packages/core/constant/featureFlags.ts
index 7ae63a6a5c..f8bc3ed7e7 100644
--- a/js/app/packages/core/constant/featureFlags.ts
+++ b/js/app/packages/core/constant/featureFlags.ts
@@ -353,3 +353,8 @@ export const ENABLE_MARKDOWN_SIDE_PANEL = resolveFeatureFlag(
'ENABLE_MARKDOWN_SIDE_PANEL',
true
);
+
+export const ENABLE_REFOCUS_HIGHLIGHT = resolveFeatureFlag(
+ 'ENABLE_REFOCUS_HIGHLIGHT',
+ true
+);