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(channels): clean up call logic + call tab (#2410)

teo1d2376fa53b1
diff --git a/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx b/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
index 614ecb104d..5ffdecf17a 100644
--- a/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
+++ b/js/app/packages/block-channel/component/NewChannelBlockAdapter.tsx
@@ -4,16 +4,13 @@ import {
type ChannelProps,
} from '@channel/Channel/Channel';
import { ChannelTopBarLiveIndicators } from '@channel/Channel/ChannelTopBarLiveIndicators';
+import {
+ ChannelTabProvider,
+ useChannelTab,
+} from '@channel/Channel/ChannelTabContext';
import { useBlockId } from '@core/block';
import { EntityPermissionsGate } from '@core/component/EntityPermissionsGate';
-import {
- type Component,
- createSignal,
- Match,
- Show,
- Suspense,
- Switch,
-} from 'solid-js';
+import { createSignal, Match, Show, Suspense, Switch } from 'solid-js';
import { blockHandleSignal } from '@core/signal/load';
import { createMethodRegistration } from '@core/orchestrator';
import { URL_PARAMS } from '@block-channel/constants';
@@ -32,12 +29,14 @@ import { ChannelParticipantsTab } from '@channel/Participants/ChannelParticipant
import { ChannelDebouncedNotificationReadMarker } from '@notifications/components/DebouncedNotificationReadMarker';
import { useGlobalNotificationSource } from '@app/component/GlobalAppState';
import type { BlockChannelProps } from './Block';
-import { CallProvider, CallOverlay, useCall } from '@channel/Call';
+import {
+ CallProvider,
+ ChannelCallButton,
+ ChannelCallTab,
+ useCall,
+} from '@channel/Call';
import { ENABLE_CALLS } from '@core/constant/featureFlags';
import { SplitHeaderRight } from '@app/component/split-layout/components/SplitHeader';
-import { Button } from '@ui/components/Button';
-import PhoneIcon from '@icon/regular/phone.svg';
-import PhoneDisconnectIcon from '@icon/regular/phone-disconnect.svg';
type ChannelTargetMessageParams = {
[URL_PARAMS.message]?: string;
@@ -49,66 +48,33 @@ type ChannelPropsTargetMessage = Pick<
'targetMessageId' | 'targetMessageReplyId'
>;
-const CallIcon: Component<{ isInCall: () => boolean }> = (props) => (
- <Show when={props.isInCall()} fallback={<PhoneIcon />}>
- <PhoneDisconnectIcon />
- </Show>
-);
-
-function CallButton(props: {
- joinCall: () => Promise<void>;
- leaveCall: () => Promise<void>;
- isInCall: () => boolean;
- isPending: () => boolean;
-}) {
- const handleClick = async () => {
- if (props.isPending()) return;
- try {
- if (props.isInCall()) {
- await props.leaveCall();
- } else {
- await props.joinCall();
- }
- } catch (e) {
- console.error('Call action failed', e);
- }
- };
-
+function CallTabLabel() {
return (
- <Button
- onClick={handleClick}
- disabled={props.isPending()}
- tooltip={props.isInCall() ? 'Leave Call' : 'Call'}
- class={
- props.isInCall()
- ? 'px-1 bg-accent/20 hover:bg-accent/30 text-accent-ink'
- : 'px-1'
- }
- size="icon-sm"
- >
- <CallIcon isInCall={props.isInCall} />
- </Button>
+ <span class="flex items-center gap-1.5">
+ <span class="size-1.5 rounded-full bg-accent animate-pulse" />
+ Call
+ </span>
);
}
-function NewTop(props: {
- channelId: string;
- activeTab: ChannelTabId;
- onTabChange: (value: ChannelTabId) => void;
- joinCall: () => Promise<void>;
- leaveCall: () => Promise<void>;
- isInCall: () => boolean;
- isPending: () => boolean;
-}) {
+function NewTop(props: { channelId: string }) {
+ const { activeTab, setActiveTab } = useChannelTab();
const channelName = useChannelName(props.channelId);
const channelType = useChannelType(props.channelId);
const participantsQuery = useChannelParticipantsQuery(() => props.channelId);
+ const call = useCall(() => props.channelId);
const participants = () =>
participantsQuery.isLoading ? [] : participantsQuery.data;
- const tabs = () =>
- channelType() === ChannelTypeEnum.DirectMessage
- ? CHANNEL_TABS.filter((tab) => tab.value !== 'participants')
- : CHANNEL_TABS;
+ const tabs = () => {
+ let filtered = [...CHANNEL_TABS];
+ if (channelType() === ChannelTypeEnum.DirectMessage)
+ filtered = filtered.filter((tab) => tab.value !== 'participants');
+ if (!ENABLE_CALLS() || !call.isInThisChannel())
+ filtered = filtered.filter((tab) => tab.value !== 'call');
+ return filtered.map((tab) =>
+ tab.value === 'call' ? { ...tab, label: <CallTabLabel /> } : tab
+ );
+ };
return (
<Suspense>
@@ -118,17 +84,12 @@ function NewTop(props: {
participants={participants() ?? []}
channelName={channelName() ?? 'New Channel'}
tabs={tabs()}
- activeTab={props.activeTab}
- onTabChange={props.onTabChange}
+ activeTab={activeTab()}
+ onTabChange={setActiveTab}
/>
<Show when={ENABLE_CALLS()}>
<SplitHeaderRight>
- <CallButton
- joinCall={props.joinCall}
- leaveCall={props.leaveCall}
- isInCall={props.isInCall}
- isPending={props.isPending}
- />
+ <ChannelCallButton channelId={props.channelId} />
</SplitHeaderRight>
</Show>
<ChannelTopBarLiveIndicators />
@@ -140,30 +101,10 @@ export function NewChannelBlockAdapter(props: BlockChannelProps) {
useBlockEntityCommands();
const notificationSource = useGlobalNotificationSource();
-
const channelId = useBlockId();
-
- return (
- <EntityPermissionsGate entityType="channel" entityId={channelId}>
- <ChannelDebouncedNotificationReadMarker
- notificationSource={notificationSource}
- channelId={channelId}
- debounceTime={500}
- />
- <CallProvider>
- <NewChannelBlockAdapterInner channelId={channelId} {...props} />
- </CallProvider>
- </EntityPermissionsGate>
- );
-}
-
-function NewChannelBlockAdapterInner(
- props: { channelId: string } & BlockChannelProps
-) {
const blockHandle = blockHandleSignal.get;
const [activeTab, setActiveTab] =
createSignal<ChannelTabId>(DEFAULT_CHANNEL_TAB);
- const call = useCall(() => props.channelId);
const convertTargetMessage = (
params: ChannelTargetMessageParams
@@ -171,7 +112,7 @@ function NewChannelBlockAdapterInner(
const messageId = params[URL_PARAMS.message] as string | undefined;
const threadId = params[URL_PARAMS.thread] as string | undefined;
- // For compatibility the naming is a little strange here.
+ // For compatibility the naming is a little strange here.
// New channels index by top level message and then separately handle replies.
// If we have a threadId that is actually the top level message and the reply is the message id.
const topLevelMessageId = threadId ? threadId : messageId;
@@ -198,36 +139,37 @@ function NewChannelBlockAdapterInner(
};
return (
- <div class="relative h-full flex flex-col">
- <Switch>
- <Match when={activeTab() === 'messages'}>
- <NewChannel
- channelId={props.channelId}
- onHandleReady={onChannelReady}
- {...convertTargetMessage(props)}
- />
- </Match>
- <Match when={activeTab() === 'attachments'}>
- <ChannelAttachmentsTab channelId={props.channelId} />
- </Match>
- <Match when={activeTab() === 'participants'}>
- <ChannelParticipantsTab channelId={props.channelId} />
- </Match>
- </Switch>
- <Show when={call.isInThisChannel()}>
- <div class="absolute inset-0 z-50">
- <CallOverlay onLeave={call.leaveCall} />
- </div>
- </Show>
- <NewTop
- channelId={props.channelId}
- activeTab={activeTab()}
- onTabChange={setActiveTab}
- joinCall={call.joinCall}
- leaveCall={call.leaveCall}
- isInCall={call.isInThisChannel}
- isPending={() => call.isJoining() || call.isLeaving()}
+ <EntityPermissionsGate entityType="channel" entityId={channelId}>
+ <ChannelDebouncedNotificationReadMarker
+ notificationSource={notificationSource}
+ channelId={channelId}
+ debounceTime={500}
/>
- </div>
+ <CallProvider>
+ <ChannelTabProvider activeTab={activeTab} setActiveTab={setActiveTab}>
+ <div class="h-full flex flex-col">
+ <Switch>
+ <Match when={activeTab() === 'messages'}>
+ <NewChannel
+ channelId={channelId}
+ onHandleReady={onChannelReady}
+ {...convertTargetMessage(props)}
+ />
+ </Match>
+ <Match when={activeTab() === 'attachments'}>
+ <ChannelAttachmentsTab channelId={channelId} />
+ </Match>
+ <Match when={activeTab() === 'participants'}>
+ <ChannelParticipantsTab channelId={channelId} />
+ </Match>
+ <Match when={activeTab() === 'call'}>
+ <ChannelCallTab channelId={channelId} />
+ </Match>
+ </Switch>
+ <NewTop channelId={channelId} />
+ </div>
+ </ChannelTabProvider>
+ </CallProvider>
+ </EntityPermissionsGate>
);
}
diff --git a/js/app/packages/channel/Call/ChannelCallButton.tsx b/js/app/packages/channel/Call/ChannelCallButton.tsx
new file mode 100644
index 0000000000..094418207b
--- /dev/null
+++ b/js/app/packages/channel/Call/ChannelCallButton.tsx
@@ -0,0 +1,48 @@
+import { Show } from 'solid-js';
+import { useChannelTab } from '@channel/Channel/ChannelTabContext';
+import { DEFAULT_CHANNEL_TAB } from '@channel/Channel/channel-tabs';
+import { Button } from '@ui/components/Button';
+import PhoneIcon from '@icon/regular/phone.svg';
+import PhoneDisconnectIcon from '@icon/regular/phone-disconnect.svg';
+import { useCall } from './useCall';
+
+export function ChannelCallButton(props: { channelId: string }) {
+ const { setActiveTab } = useChannelTab();
+ const call = useCall(() => props.channelId, {
+ onJoin: () => setActiveTab('call'),
+ onLeave: () => setActiveTab(DEFAULT_CHANNEL_TAB),
+ });
+
+ const isPending = () => call.isJoining() || call.isLeaving();
+
+ const handleClick = async () => {
+ if (isPending()) return;
+ try {
+ if (call.isInThisChannel()) {
+ await call.leaveCall();
+ } else {
+ await call.joinCall();
+ }
+ } catch (e) {
+ console.error('Call action failed', e);
+ }
+ };
+
+ return (
+ <Button
+ onClick={handleClick}
+ disabled={isPending()}
+ tooltip={call.isInThisChannel() ? 'Leave Call' : 'Call'}
+ class={
+ call.isInThisChannel()
+ ? 'px-1 bg-accent/20 hover:bg-accent/30 text-accent-ink'
+ : 'px-1'
+ }
+ size="icon-sm"
+ >
+ <Show when={call.isInThisChannel()} fallback={<PhoneIcon />}>
+ <PhoneDisconnectIcon />
+ </Show>
+ </Button>
+ );
+}
diff --git a/js/app/packages/channel/Call/ChannelCallTab.tsx b/js/app/packages/channel/Call/ChannelCallTab.tsx
new file mode 100644
index 0000000000..b621eb16fa
--- /dev/null
+++ b/js/app/packages/channel/Call/ChannelCallTab.tsx
@@ -0,0 +1,13 @@
+import { Show } from 'solid-js';
+import { useCall } from './useCall';
+import { CallOverlay } from './CallOverlay';
+
+export function ChannelCallTab(props: { channelId: string }) {
+ const call = useCall(() => props.channelId);
+
+ return (
+ <Show when={call.isInThisChannel()}>
+ <CallOverlay onLeave={call.leaveCall} />
+ </Show>
+ );
+}
diff --git a/js/app/packages/channel/Call/index.ts b/js/app/packages/channel/Call/index.ts
index c70c2fa1ab..d40bbf807f 100644
--- a/js/app/packages/channel/Call/index.ts
+++ b/js/app/packages/channel/Call/index.ts
@@ -6,3 +6,5 @@ export {
export type { CallState } from './CallContext';
export { useCall } from './useCall';
export { CallOverlay } from './CallOverlay';
+export { ChannelCallButton } from './ChannelCallButton';
+export { ChannelCallTab } from './ChannelCallTab';
diff --git a/js/app/packages/channel/Call/useCall.ts b/js/app/packages/channel/Call/useCall.ts
index 32c9fb006d..f24a707125 100644
--- a/js/app/packages/channel/Call/useCall.ts
+++ b/js/app/packages/channel/Call/useCall.ts
@@ -1,15 +1,42 @@
import { useJoinCallMutation, useLeaveCallMutation } from '@queries/call/call';
+import { RoomEvent } from 'livekit-client';
+import { onCleanup } from 'solid-js';
import { useCallContext } from './CallContext';
+type UseCallOptions = {
+ /** Called after successfully joining a call. */
+ onJoin?: () => void;
+ /** Called when the call ends for any reason (user leave, disconnect, kicked, etc.). */
+ onLeave?: () => void;
+};
+
/**
* Hook that orchestrates joining/leaving calls by combining
* the API mutations with the LiveKit room connection.
*/
-export function useCall(channelId: () => string) {
+export function useCall(channelId: () => string, options?: UseCallOptions) {
const callCtx = useCallContext();
const joinMutation = useJoinCallMutation();
const leaveMutation = useLeaveCallMutation();
+ // Track the disconnect listener so we can swap it when the room changes.
+ let cleanupDisconnectListener: (() => void) | null = null;
+
+ function attachDisconnectListener() {
+ cleanupDisconnectListener?.();
+ cleanupDisconnectListener = null;
+
+ const room = callCtx.room();
+ if (!room) return;
+
+ const handleDisconnect = () => options?.onLeave?.();
+ room.on(RoomEvent.Disconnected, handleDisconnect);
+ cleanupDisconnectListener = () =>
+ room.off(RoomEvent.Disconnected, handleDisconnect);
+ }
+
+ onCleanup(() => cleanupDisconnectListener?.());
+
async function joinCall() {
const id = channelId();
const tokenResponse = await joinMutation.mutateAsync(id);
@@ -27,12 +54,19 @@ export function useCall(channelId: () => string) {
}
throw e;
}
+ attachDisconnectListener();
+ options?.onJoin?.();
}
async function leaveCall() {
const id = channelId();
+ // Detach before disconnect so the RoomEvent.Disconnected handler
+ // doesn't double-fire onLeave.
+ cleanupDisconnectListener?.();
+ cleanupDisconnectListener = null;
try {
await callCtx.disconnect();
+ options?.onLeave?.();
} finally {
await leaveMutation.mutateAsync(id);
}
diff --git a/js/app/packages/channel/Channel/ChannelTabContext.tsx b/js/app/packages/channel/Channel/ChannelTabContext.tsx
new file mode 100644
index 0000000000..2319f53091
--- /dev/null
+++ b/js/app/packages/channel/Channel/ChannelTabContext.tsx
@@ -0,0 +1,37 @@
+import {
+ createContext,
+ useContext,
+ type Accessor,
+ type ParentProps,
+} from 'solid-js';
+import type { ChannelTabId } from './channel-tabs';
+
+type ChannelTabContextValue = {
+ activeTab: Accessor<ChannelTabId>;
+ setActiveTab: (tab: ChannelTabId) => void;
+};
+
+const ChannelTabContext = createContext<ChannelTabContextValue>();
+
+export function ChannelTabProvider(
+ props: ParentProps<{
+ activeTab: Accessor<ChannelTabId>;
+ setActiveTab: (tab: ChannelTabId) => void;
+ }>
+) {
+ return (
+ <ChannelTabContext.Provider
+ value={{ activeTab: props.activeTab, setActiveTab: props.setActiveTab }}
+ >
+ {props.children}
+ </ChannelTabContext.Provider>
+ );
+}
+
+export function useChannelTab(): ChannelTabContextValue {
+ const ctx = useContext(ChannelTabContext);
+ if (!ctx) {
+ throw new Error('useChannelTab must be used within <ChannelTabProvider>');
+ }
+ return ctx;
+}
diff --git a/js/app/packages/channel/Channel/channel-tabs.ts b/js/app/packages/channel/Channel/channel-tabs.ts
index 077f4cfd28..dd835e67c0 100644
--- a/js/app/packages/channel/Channel/channel-tabs.ts
+++ b/js/app/packages/channel/Channel/channel-tabs.ts
@@ -2,6 +2,7 @@ export const CHANNEL_TABS = [
{ value: 'messages', label: 'Messages' },
{ value: 'attachments', label: 'Attachments' },
{ value: 'participants', label: 'Participants' },
+ { value: 'call', label: 'Call' },
] as const;
export type ChannelTabId = (typeof CHANNEL_TABS)[number]['value'];
diff --git a/js/app/packages/core/component/Tabs.tsx b/js/app/packages/core/component/Tabs.tsx
index d8f8f4bc58..ca06841afb 100644
--- a/js/app/packages/core/component/Tabs.tsx
+++ b/js/app/packages/core/component/Tabs.tsx
@@ -2,12 +2,18 @@ import {
SegmentedControl as KSegmentedControl,
type SegmentedControlRootProps,
} from '@kobalte/core/segmented-control';
-import { createEffect, createSignal, For, splitProps } from 'solid-js';
+import {
+ type JSX,
+ createEffect,
+ createSignal,
+ For,
+ splitProps,
+} from 'solid-js';
import { cn } from '@ui/utils/classname';
export type TabItem = {
value: string;
- label: string;
+ label: string | JSX.Element;
};
export const Tabs = (