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.

call notification in browser (#2976)

Will Hutchinson5bac2702efea
diff --git a/js/app/packages/app/component/Root.tsx b/js/app/packages/app/component/Root.tsx
index 226b6c39a2..095c46b08e 100644
--- a/js/app/packages/app/component/Root.tsx
+++ b/js/app/packages/app/component/Root.tsx
@@ -92,6 +92,7 @@ import {
} from '@app/component/analytics-context';
import { PosthogProvider, usePosthog } from '@app/lib/analytics/posthog';
import { CallProvider } from '@channel/Call/CallContext';
+import { CallStartedNotifier } from '@channel/Call/CallStartedNotifier';
/** Syncs login cookie with auth state. Only updates on successful query (not errors/loading). */
function useSyncLoginCookie() {
@@ -446,6 +447,7 @@ export function Root() {
<MutationUndoProvider>
<ChannelsContextProvider>
<CallProvider>
+ <CallStartedNotifier />
<QuickAccessProvider>
<SearchProvider>
<ChatAttachmentsInit />
diff --git a/js/app/packages/channel/Call/CallStartedNotifier.tsx b/js/app/packages/channel/Call/CallStartedNotifier.tsx
new file mode 100644
index 0000000000..7df74bb32d
--- /dev/null
+++ b/js/app/packages/channel/Call/CallStartedNotifier.tsx
@@ -0,0 +1,160 @@
+import { ENABLE_CALLS } from '@core/constant/featureFlags';
+import { useChannelsContext } from '@core/context/channels';
+import { useUserId } from '@core/context/user';
+import { usePlatformNotificationState } from '@notifications';
+import { DefaultUserNameResolver } from '@notifications/notification-resolvers';
+import { createConnectionWebsocketEffect } from '@service-connection/websocket';
+import { useCallContext } from './CallContext';
+import { joinChannelCall } from './join-channel-call';
+
+type CallStartedPayload = {
+ channel_id?: string;
+ call_id?: string;
+ created_by?: string | null;
+};
+
+const RING_VOLUME = 0.15;
+const RING_PULSE_DURATION_S = 0.4;
+const RING_PULSE_GAP_S = 0.2;
+const RING_FADE_S = 0.02;
+// US dial-tone-style ring: superimposed 440Hz + 480Hz.
+const RING_FREQUENCIES_HZ = [440, 480];
+
+type WebkitWindow = Window & { webkitAudioContext?: typeof AudioContext };
+
+function playRingSound() {
+ const Ctx =
+ window.AudioContext ?? (window as WebkitWindow).webkitAudioContext;
+ if (!Ctx) return;
+
+ let ctx: AudioContext;
+ try {
+ ctx = new Ctx();
+ } catch (e) {
+ console.warn('Failed to create AudioContext for call ring', e);
+ return;
+ }
+
+ const playPulse = (start: number) => {
+ const gain = ctx.createGain();
+ gain.connect(ctx.destination);
+ gain.gain.setValueAtTime(0, start);
+ gain.gain.linearRampToValueAtTime(RING_VOLUME, start + RING_FADE_S);
+ gain.gain.linearRampToValueAtTime(0, start + RING_PULSE_DURATION_S);
+
+ for (const freq of RING_FREQUENCIES_HZ) {
+ const osc = ctx.createOscillator();
+ osc.frequency.value = freq;
+ osc.connect(gain);
+ osc.start(start);
+ osc.stop(start + RING_PULSE_DURATION_S + RING_FADE_S);
+ }
+ };
+
+ const t0 = ctx.currentTime;
+ playPulse(t0);
+ playPulse(t0 + RING_PULSE_DURATION_S + RING_PULSE_GAP_S);
+
+ const totalMs =
+ (RING_PULSE_DURATION_S * 2 + RING_PULSE_GAP_S + RING_FADE_S) * 1000 + 200;
+ setTimeout(() => {
+ void ctx.close().catch(() => {});
+ }, totalMs);
+}
+
+function safeJsonParse(s: string): unknown {
+ try {
+ return JSON.parse(s);
+ } catch {
+ return null;
+ }
+}
+
+function parsePayload(raw: unknown): CallStartedPayload | null {
+ const obj =
+ typeof raw === 'string'
+ ? safeJsonParse(raw)
+ : typeof raw === 'object'
+ ? raw
+ : null;
+ if (!obj || typeof obj !== 'object') return null;
+ return obj as CallStartedPayload;
+}
+
+/**
+ * Listens for `call_started` websocket events broadcast to channel members
+ * and surfaces a browser notification + ring tone for the recipients.
+ *
+ * Mount once near the app root, inside `<CallProvider>` and
+ * `<ChannelsContextProvider>`. The backend already excludes the caller from
+ * the broadcast (`call_service::send_call_event` filters on
+ * `triggered_by_user_id`), but we additionally skip when the user is already
+ * in the call as a defensive guard against same-user multi-device delivery.
+ */
+export function CallStartedNotifier() {
+ const callCtx = useCallContext();
+ const userId = useUserId();
+ const channelsCtx = useChannelsContext();
+ const notif = usePlatformNotificationState();
+
+ createConnectionWebsocketEffect((data) => {
+ if (data.type !== 'call_started') return;
+ if (!ENABLE_CALLS()) return;
+
+ const payload = parsePayload(data.data);
+ if (!payload) return;
+
+ const {
+ channel_id: channelId,
+ call_id: callId,
+ created_by: createdBy,
+ } = payload;
+ if (!channelId || !callId) return;
+
+ if (callCtx.activeCallId() === callId) return;
+ if (createdBy && createdBy === userId()) return;
+
+ void emitCallStartedNotification({
+ channelId,
+ createdBy: createdBy ?? null,
+ channelName: channelsCtx.channelsById()[channelId]?.name ?? undefined,
+ notif,
+ });
+ });
+
+ return null;
+}
+
+async function emitCallStartedNotification(args: {
+ channelId: string;
+ createdBy: string | null;
+ channelName: string | undefined;
+ notif: ReturnType<typeof usePlatformNotificationState>;
+}) {
+ const { channelId, createdBy, channelName, notif } = args;
+
+ // Play the sound regardless of notification permission so a user with
+ // browser notifications denied still gets an audio cue.
+ playRingSound();
+
+ if (notif === 'not-supported') return;
+
+ const callerName =
+ (createdBy ? await DefaultUserNameResolver(createdBy) : undefined) ??
+ 'Someone';
+ const target = channelName ? ` in #${channelName}` : '';
+
+ const handle = await notif.showNotification({
+ title: `Incoming call${target}`,
+ options: {
+ body: `${callerName} started a call`,
+ },
+ });
+
+ if (handle === 'not-granted' || handle === 'disabled-in-ui') return;
+ handle.onClick(() => {
+ window.focus();
+ void joinChannelCall(channelId);
+ handle.close();
+ });
+}
diff --git a/js/app/packages/channel/Call/index.ts b/js/app/packages/channel/Call/index.ts
index f92bb39975..2fa12e0e0f 100644
--- a/js/app/packages/channel/Call/index.ts
+++ b/js/app/packages/channel/Call/index.ts
@@ -20,6 +20,8 @@ export { ChannelCallButton } from './ChannelCallButton';
export { ChannelCallTab } from './ChannelCallTab';
export { ChannelCallAutoJoin } from './ChannelCallAutoJoin';
export { CallEventSync } from './CallEventSync';
+export { CallStartedNotifier } from './CallStartedNotifier';
+export { joinChannelCall } from './join-channel-call';
export { openChannelCallTab } from './open-channel-call-tab';
export {
InCallPanel,
diff --git a/js/app/packages/channel/Call/join-channel-call.ts b/js/app/packages/channel/Call/join-channel-call.ts
new file mode 100644
index 0000000000..d57bbb5677
--- /dev/null
+++ b/js/app/packages/channel/Call/join-channel-call.ts
@@ -0,0 +1,29 @@
+import { globalSplitManager } from '@app/signal/splitLayout';
+import { URL_PARAMS } from '@channel/Channel/link';
+
+/**
+ * Focuses the channel split (opens if needed) and pushes the
+ * `?join_call=true` deep-link param so `ChannelCallAutoJoin` drops the user
+ * straight into the call. Used for accept-call flows like clicking an
+ * incoming-call browser notification.
+ */
+export async function joinChannelCall(channelId: string): Promise<void> {
+ const manager = globalSplitManager();
+ if (!manager || !channelId) return;
+
+ const existing = manager.getSplitByContent('channel', channelId);
+ if (existing) {
+ existing.activate();
+ } else {
+ manager.openWithSplit(
+ { type: 'channel', id: channelId },
+ { activate: true, referredFrom: 'sidebar' }
+ );
+ }
+
+ const orchestrator = manager.getOrchestrator();
+ const handle = await orchestrator.getBlockHandle(channelId, 'channel');
+ await handle?.goToLocationFromParams({
+ [URL_PARAMS.joinCall]: 'true',
+ });
+}