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.

chg(sidebar): using websocket only state for incoming calls widget (#3875)

Seamusd13b660d3873
diff --git a/js/app/packages/app/component/app-sidebar/active-call-widget.tsx b/js/app/packages/app/component/app-sidebar/active-call-widget.tsx
index f44797f652..aae9fbbbc3 100644
--- a/js/app/packages/app/component/app-sidebar/active-call-widget.tsx
+++ b/js/app/packages/app/component/app-sidebar/active-call-widget.tsx
@@ -1,15 +1,49 @@
-import { openChannelCallTab, useCallContextOptional } from '@channel/Call';
+import {
+ joinChannelCall,
+ openChannelCallTab,
+ useCallContextOptional,
+} from '@channel/Call';
+import { ContextMenuContent, MenuItem } from '@core/component/ContextMenu';
+import { ENABLE_CALLS } from '@core/constant/featureFlags';
import { useChannelsContext } from '@core/context/channels';
import { useUserId } from '@core/context/user';
import PhoneIcon from '@icon/wide-call.svg';
-import { useActiveCallsForChannelsQuery } from '@queries/call/call';
+import { ContextMenu } from '@kobalte/core/context-menu';
+import { createConnectionWebsocketEffect } from '@service-connection/websocket';
import type { ApiChannelWithLatest } from '@service-storage/channel-list-types';
import { ChannelTypeEnum } from '@service-storage/client';
import { Avatar, Button, cn, Tooltip } from '@ui';
-import { createMemo, createSignal, For, onCleanup, Show } from 'solid-js';
+import {
+ createEffect,
+ createMemo,
+ createSignal,
+ type FlowComponent,
+ For,
+ onCleanup,
+ Show,
+} from 'solid-js';
import type { SidebarState } from './sidebar';
const SLIM_MAX = 4;
+const MAX_RING_DURATION_MS = 30_000;
+
+type IncomingCall = {
+ channelId: string;
+ callId: string;
+ createdAt: string;
+ createdBy: string | null;
+};
+
+type CallStartedPayload = {
+ channel_id?: string;
+ call_id?: string;
+ created_by?: string | null;
+};
+
+type CallEndedPayload = {
+ channel_id?: string;
+ call_id?: string;
+};
function displayName(channel: ApiChannelWithLatest | undefined) {
if (!channel) return 'Channel';
@@ -97,20 +131,145 @@ function formatDuration(startedAt: string | undefined, nowMs: number) {
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
+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;
+}
+
+type IncomingCallContextMenuProps = {
+ callId: string;
+ channelId: string;
+ onDismiss: () => void;
+};
+
+const IncomingCallContextMenu: FlowComponent<IncomingCallContextMenuProps> = (
+ props
+) => {
+ return (
+ <ContextMenu>
+ <ContextMenu.Trigger class="size-full group/cm-trigger">
+ {props.children}
+ </ContextMenu.Trigger>
+
+ <ContextMenu.Portal>
+ <ContextMenuContent class="text-xs text-ink-muted">
+ <MenuItem
+ text="Join call"
+ onClick={() => void joinChannelCall(props.channelId)}
+ />
+ <MenuItem text="Dismiss" onClick={props.onDismiss} />
+ </ContextMenuContent>
+ </ContextMenu.Portal>
+ </ContextMenu>
+ );
+};
+
export function SidebarActiveCallWidget(props: { sidebarState: SidebarState }) {
const channelsCtx = useChannelsContext();
const callCtx = useCallContextOptional();
const userId = useUserId();
+ const [incomingCalls, setIncomingCalls] = createSignal<IncomingCall[]>([]);
+ const incomingCallTimeouts = new Map<string, number>();
const [nowMs, setNowMs] = createSignal(Date.now());
const durationTimer = globalThis.setInterval(
() => setNowMs(Date.now()),
1000
);
- onCleanup(() => globalThis.clearInterval(durationTimer));
+ onCleanup(() => {
+ globalThis.clearInterval(durationTimer);
+ for (const timeoutId of incomingCallTimeouts.values()) {
+ window.clearTimeout(timeoutId);
+ }
+ incomingCallTimeouts.clear();
+ });
- const activeCallsQuery = useActiveCallsForChannelsQuery(() =>
- channelsCtx.channels().map((channel) => channel.id)
- );
+ const dismissIncomingCall = (callId: string) => {
+ const timeoutId = incomingCallTimeouts.get(callId);
+ if (timeoutId !== undefined) {
+ window.clearTimeout(timeoutId);
+ incomingCallTimeouts.delete(callId);
+ }
+ setIncomingCalls((calls) => calls.filter((call) => call.callId !== callId));
+ };
+
+ const addIncomingCall = (call: IncomingCall) => {
+ const existingTimeoutId = incomingCallTimeouts.get(call.callId);
+ if (existingTimeoutId !== undefined) {
+ window.clearTimeout(existingTimeoutId);
+ }
+ incomingCallTimeouts.set(
+ call.callId,
+ window.setTimeout(
+ () => dismissIncomingCall(call.callId),
+ MAX_RING_DURATION_MS
+ )
+ );
+
+ setIncomingCalls((calls) => {
+ const withoutDuplicate = calls.filter(
+ (candidate) =>
+ candidate.callId !== call.callId &&
+ candidate.channelId !== call.channelId
+ );
+ return [call, ...withoutDuplicate].sort(
+ (a, b) =>
+ new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
+ );
+ });
+ };
+
+ createEffect(() => {
+ const activeCallId = callCtx?.activeCallId();
+ if (activeCallId) dismissIncomingCall(activeCallId);
+ });
+
+ createConnectionWebsocketEffect((data) => {
+ if (!ENABLE_CALLS()) return;
+
+ const payload = parsePayload(data.data);
+ if (!payload) return;
+
+ if (data.type === 'call_ended') {
+ const { channel_id: channelId, call_id: callId } =
+ payload as CallEndedPayload;
+ if (!channelId || !callId) return;
+
+ dismissIncomingCall(callId);
+ return;
+ }
+
+ if (data.type !== 'call_started') 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;
+
+ addIncomingCall({
+ channelId,
+ callId,
+ createdAt: new Date().toISOString(),
+ createdBy: createdBy ?? null,
+ });
+ });
const activeCalls = createMemo(() => {
const channelsById = channelsCtx.channelsById();
@@ -119,14 +278,17 @@ export function SidebarActiveCallWidget(props: { sidebarState: SidebarState }) {
: null;
const joinedCallId = callCtx?.isInCall() ? callCtx.activeCallId() : null;
- return (
- activeCallsQuery.data?.filter((call) => {
+ return incomingCalls()
+ .filter((call) => {
if (!channelsById[call.channelId]) return false;
return (
call.channelId !== joinedChannelId && call.callId !== joinedCallId
);
- }) ?? []
- );
+ })
+ .sort(
+ (a, b) =>
+ new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
+ );
});
const activeCallChannels = createMemo(() =>
@@ -162,25 +324,35 @@ export function SidebarActiveCallWidget(props: { sidebarState: SidebarState }) {
: `${displayName(channel())} call`;
};
return (
- <Tooltip label={label()} placement="right">
- <Button
- class="relative flex items-center cursor-default rounded-md text-ink-extra-muted not-disabled:hover:bg-ink/3 justify-center size-8"
- draggable={false}
- variant="ghost"
- size="sm"
- onMouseDown={(e) => {
- if (e.button !== 0) return;
- e.preventDefault();
- void openChannelCallTab(call.channelId);
- }}
+ <div class="size-8">
+ <IncomingCallContextMenu
+ callId={call.callId}
+ channelId={call.channelId}
+ onDismiss={() => dismissIncomingCall(call.callId)}
>
- <ChannelCallBadge
- channel={channel()}
- letters={channelLetters().get(call.channelId) ?? '?'}
- slim
- />
- </Button>
- </Tooltip>
+ <Tooltip label={label()} placement="right">
+ <Button
+ class="relative flex items-center cursor-default rounded-md text-ink-extra-muted not-disabled:hover:bg-ink/3 justify-center size-8"
+ draggable={false}
+ variant="ghost"
+ size="sm"
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ e.preventDefault();
+ void openChannelCallTab(call.channelId);
+ }}
+ >
+ <ChannelCallBadge
+ channel={channel()}
+ letters={
+ channelLetters().get(call.channelId) ?? '?'
+ }
+ slim
+ />
+ </Button>
+ </Tooltip>
+ </IncomingCallContextMenu>
+ </div>
);
}}
</For>
@@ -194,10 +366,10 @@ export function SidebarActiveCallWidget(props: { sidebarState: SidebarState }) {
>
<section class="size-full flex flex-col justify-center px-2 py-1.5">
<header class="text-xs font-medium text-ink-muted ml-2 mb-1 whitespace-nowrap">
- <h1>Active calls</h1>
+ <h1>Incoming calls</h1>
</header>
- <div class="flex-1">
+ <div class="flex-1 w-full">
<For each={activeCalls()}>
{(call) => {
const channel = () =>
@@ -210,33 +382,44 @@ export function SidebarActiveCallWidget(props: { sidebarState: SidebarState }) {
: `${displayName(channel())} call`;
};
return (
- <Button
- class={cn(
- 'flex items-center cursor-default rounded-md text-ink-extra-muted not-disabled:hover:bg-ink/3',
- 'justify-start gap-2 w-full h-8 py-1'
- )}
- draggable={false}
- variant="ghost"
- size="sm"
- title={label()}
- onMouseDown={(e) => {
- if (e.button !== 0) return;
- e.preventDefault();
- void openChannelCallTab(call.channelId);
- }}
- >
- <ChannelCallBadge
- channel={channel()}
- letters={channelLetters().get(call.channelId) ?? '?'}
- slim={false}
- />
- <span class="text-sm font-medium truncate">
- {displayName(channel())}
- </span>
- <span class="shrink-0 size-5 flex items-center justify-center text-xs font-medium bg-success/15 text-success rounded-md ml-auto">
- <PhoneIcon class="size-3" />
- </span>
- </Button>
+ <div class="w-full h-8">
+ <IncomingCallContextMenu
+ callId={call.callId}
+ channelId={call.channelId}
+ onDismiss={() => dismissIncomingCall(call.callId)}
+ >
+ <Tooltip class="w-full" label={label()} placement="right">
+ <Button
+ class={cn(
+ 'flex items-center cursor-default rounded-md text-ink-extra-muted not-disabled:hover:bg-ink/3',
+ 'justify-start gap-2 w-full h-8 py-1'
+ )}
+ draggable={false}
+ variant="ghost"
+ size="sm"
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ e.preventDefault();
+ void openChannelCallTab(call.channelId);
+ }}
+ >
+ <ChannelCallBadge
+ channel={channel()}
+ letters={
+ channelLetters().get(call.channelId) ?? '?'
+ }
+ slim={false}
+ />
+ <span class="text-sm font-medium truncate">
+ {displayName(channel())}
+ </span>
+ <span class="shrink-0 size-5 flex items-center justify-center text-xs font-medium bg-success/15 text-success rounded-md ml-auto">
+ <PhoneIcon class="size-3" />
+ </span>
+ </Button>
+ </Tooltip>
+ </IncomingCallContextMenu>
+ </div>
);
}}
</For>
diff --git a/js/app/packages/app/component/app-sidebar/sidebar.tsx b/js/app/packages/app/component/app-sidebar/sidebar.tsx
index 4de83297a0..f08f2262cb 100644
--- a/js/app/packages/app/component/app-sidebar/sidebar.tsx
+++ b/js/app/packages/app/component/app-sidebar/sidebar.tsx
@@ -39,7 +39,6 @@ import {
ENABLE_CALLS,
ENABLE_HOME_OVERRIDE,
ENABLE_NEW_PRICING_OVERRIDE,
- ENABLE_SIDEBAR_ACTIVE_CALLS,
ENABLE_TEAMS_OVERRIDE,
} from '@core/constant/featureFlags';
import {
@@ -1022,7 +1021,7 @@ export const AppSidebar = (props: AppSidebarProps) => {
</div>
<div class="mt-auto">
- <Show when={ENABLE_CALLS() && ENABLE_SIDEBAR_ACTIVE_CALLS()}>
+ <Show when={ENABLE_CALLS()}>
<div class="block max-h-[clamp(10%,60%,20rem)]">
<SidebarActiveCallWidget
sidebarState={props.sidebarState ?? 'expanded'}
diff --git a/js/app/packages/channel/Call/CallStartedNotifier.tsx b/js/app/packages/channel/Call/CallStartedNotifier.tsx
index f681bb7eb7..282b46ddd8 100644
--- a/js/app/packages/channel/Call/CallStartedNotifier.tsx
+++ b/js/app/packages/channel/Call/CallStartedNotifier.tsx
@@ -159,10 +159,11 @@ export function CallStartedNotifier() {
} = payload;
if (!channelId || !callId) return;
+ const createdAt = new Date().toISOString();
setActiveCallStartedCache({
channelId,
callId,
- createdAt: new Date().toISOString(),
+ createdAt,
createdBy: createdBy ?? '',
});
void invalidateActiveCallQueries();
diff --git a/js/app/packages/core/constant/featureFlags.ts b/js/app/packages/core/constant/featureFlags.ts
index 34561284a3..50a777355c 100644
--- a/js/app/packages/core/constant/featureFlags.ts
+++ b/js/app/packages/core/constant/featureFlags.ts
@@ -343,14 +343,6 @@ export function ENABLE_CALLS(): boolean {
return analytics.posthog.isFeatureEnabled('enable-calls') ?? false;
}
-// The sidebar active-calls widget fans out to one GET /call/{channelId}/active
-// request per channel every 15s. Flagged off until it's batched / socket-driven.
-export function ENABLE_SIDEBAR_ACTIVE_CALLS(): boolean {
- return (
- analytics.posthog.isFeatureEnabled('enable-sidebar-active-calls') ?? false
- );
-}
-
export const ENABLE_NEW_ONBOARDING_OVERRIDE = DEV_MODE_ENV ? true : undefined;
export const ENABLE_NEW_LOGIN_OVERRIDE = DEV_MODE_ENV ? true : undefined;
diff --git a/js/app/packages/ui/components/Tooltip.tsx b/js/app/packages/ui/components/Tooltip.tsx
index 58ec86a75d..b900415af3 100644
--- a/js/app/packages/ui/components/Tooltip.tsx
+++ b/js/app/packages/ui/components/Tooltip.tsx
@@ -5,6 +5,7 @@ import { Surface } from '@ui';
import type { ParentProps } from 'solid-js';
import { For, Show } from 'solid-js';
import { Hotkey } from '../../ui/components/Hotkey';
+import { cn } from '../utils/classname';
type TooltipProps = ParentProps<{
hotkey?: HotkeyToken | HotkeyToken[];
@@ -15,6 +16,7 @@ type TooltipProps = ParentProps<{
shortcut?: string | string[];
placement?: Placement;
as?: 'div' | 'span';
+ class?: string;
label: string;
}>;
@@ -57,7 +59,7 @@ export function Tooltip(props: TooltipProps) {
gutter={4}
>
<KobalteTooltip.Trigger
- class="inline-flex items-center"
+ class={cn('inline-flex items-center', props.class)}
as={props.as ?? 'div'}
>
{props.children}