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(new-channels): typing indicators (#2245)

teobd269067bb57
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 4237fd6821..12eaaca10c 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -68,6 +68,7 @@ import {
import { resetKeyboardModality } from './util';
import { DebugSuspense } from '@channel/DebugSuspense';
import { useChannelParticipants } from '@channel/use-channel-participants';
+import { usePostTypingUpdateMutation } from '@queries/channel/typing';
type ChannelProps = {
channelId: string;
@@ -86,6 +87,7 @@ export function Channel(props: ChannelProps) {
const sendMessageMutation = useSendMessageMutation();
const patchMessageMutation = usePatchMessageMutation();
const deleteMessageMutation = useDeleteMessageMutation();
+ const typingMutation = usePostTypingUpdateMutation();
const addReactionMutation = useAddReactionMutation();
const removeReactionMutation = useRemoveReactionMutation();
const [threadListNavigation, setThreadListNavigation] =
@@ -284,12 +286,15 @@ export function Channel(props: ChannelProps) {
{(item) => {
const message = () => messageById().get(item.id);
const state = threadManager.getOrCreateThreadState(item.id);
+ const isNewestThread = () =>
+ item.id === messageIndex().keys.at(-1);
return (
<Show when={message()}>
{(m) => (
<ChannelThread
data={m}
channelId={() => props.channelId}
+ isNewestThread={isNewestThread()}
getMessageActions={getMessageActions}
targetReplyId={targetMessageController.pendingTargetReplyId()}
highlightedReplyId={targetMessageController.activeTargetMessageReplyId()}
@@ -350,6 +355,18 @@ export function Channel(props: ChannelProps) {
}}
onChange={(snapshot) => void setChannelInputSnapshot(snapshot)}
onSend={onSend}
+ onStartTyping={() =>
+ typingMutation.mutate({
+ channelId: props.channelId,
+ action: 'start',
+ })
+ }
+ onStopTyping={() =>
+ typingMutation.mutate({
+ channelId: props.channelId,
+ action: 'stop',
+ })
+ }
/>
</div>
</DebugSuspense>
diff --git a/js/app/packages/channel/Input/ChannelInput.tsx b/js/app/packages/channel/Input/ChannelInput.tsx
index 2b9cef9ee1..1334e8e7c2 100644
--- a/js/app/packages/channel/Input/ChannelInput.tsx
+++ b/js/app/packages/channel/Input/ChannelInput.tsx
@@ -6,6 +6,7 @@ import { createConfiguredChannelMarkdownEditor } from './configured-markdown-edi
import { createInputAttachmentTracker } from './attachment-tracker';
import { createInputState } from './create-input-state';
import { createMentionsTracker } from './mentions-tracker';
+import { createTypingTracker } from './create-typing-tracker';
import {
chatRuleset,
handleFileFolderDrop,
@@ -60,6 +61,11 @@ export function ChannelInput(props: ChannelInputProps) {
});
let clearComposer = () => {};
+ const typingTracker = createTypingTracker({
+ onStartTyping: () => props.onStartTyping?.(),
+ onStopTyping: () => props.onStopTyping?.(),
+ });
+
const inputState = createInputState({
initialInput: props.input,
mentions: mentionsTracker.mentions,
@@ -79,9 +85,15 @@ export function ChannelInput(props: ChannelInputProps) {
clearInput: () => markdownEditor.controls.clear(),
callbacks: {
onChange: props.onChange,
- onSend: props.onSend,
+ onSend: (snapshot) => {
+ typingTracker.stop();
+ return props.onSend?.(snapshot);
+ },
onToggleFormatRibbon: props.onToggleFormatRibbon,
- onClose: props.onClose,
+ onClose: (snapshot) => {
+ typingTracker.stop();
+ return props.onClose?.(snapshot);
+ },
onRemoveAttachment: props.onRemoveAttachment,
},
persistenceKey: props.persistenceKey,
@@ -99,9 +111,11 @@ export function ChannelInput(props: ChannelInputProps) {
},
onChange: (markdown) => {
inputState.setValue(markdown);
+ typingTracker.keystroke();
},
onEnter: () => {
if (isMobile()) return false;
+ typingTracker.stop();
inputState.commands.send();
return true;
},
diff --git a/js/app/packages/channel/Input/create-typing-tracker.ts b/js/app/packages/channel/Input/create-typing-tracker.ts
new file mode 100644
index 0000000000..abd1774bc5
--- /dev/null
+++ b/js/app/packages/channel/Input/create-typing-tracker.ts
@@ -0,0 +1,51 @@
+import { onCleanup } from 'solid-js';
+
+/** How long after the last keystroke before we tell the server the user stopped typing. */
+const INACTIVITY_TIMEOUT_MS = 2000;
+
+type TypingTrackerCallbacks = {
+ onStartTyping: () => void;
+ onStopTyping: () => void;
+};
+
+/**
+ * Tracks local typing activity and fires start/stop callbacks.
+ *
+ * - `onStartTyping` fires on the first keystroke and is not called again
+ * until after the user goes idle (stop fires first).
+ * - `onStopTyping` fires after `INACTIVITY_TIMEOUT_MS` of inactivity,
+ * or immediately when `stop()` is called (e.g. on send / close).
+ */
+export function createTypingTracker(callbacks: TypingTrackerCallbacks) {
+ let isTyping = false;
+ let inactivityTimer: ReturnType<typeof setTimeout> | undefined;
+
+ function clearTimer() {
+ if (inactivityTimer !== undefined) {
+ clearTimeout(inactivityTimer);
+ inactivityTimer = undefined;
+ }
+ }
+
+ function stopTyping() {
+ clearTimer();
+ if (isTyping) {
+ isTyping = false;
+ callbacks.onStopTyping();
+ }
+ }
+
+ function keystroke() {
+ if (!isTyping) {
+ isTyping = true;
+ callbacks.onStartTyping();
+ }
+ // Reset (or start) the inactivity timer on every keystroke.
+ clearTimer();
+ inactivityTimer = setTimeout(stopTyping, INACTIVITY_TIMEOUT_MS);
+ }
+
+ onCleanup(stopTyping);
+
+ return { keystroke, stop: stopTyping };
+}
diff --git a/js/app/packages/channel/Input/types.ts b/js/app/packages/channel/Input/types.ts
index 64c4acb279..3643507d7d 100644
--- a/js/app/packages/channel/Input/types.ts
+++ b/js/app/packages/channel/Input/types.ts
@@ -55,6 +55,8 @@ export type InputCallbacks = {
attachment: InputAttachmentData,
snapshot: InputSnapshot
) => void | Promise<void>;
+ onStartTyping?: () => void;
+ onStopTyping?: () => void;
};
export type InputCommands = {
diff --git a/js/app/packages/channel/Thread/ChannelThread.tsx b/js/app/packages/channel/Thread/ChannelThread.tsx
index b9597bc30d..cd594463af 100644
--- a/js/app/packages/channel/Thread/ChannelThread.tsx
+++ b/js/app/packages/channel/Thread/ChannelThread.tsx
@@ -8,6 +8,7 @@ import { tryMacroId, useDisplayName } from '@core/user';
import { Thread } from './Thread';
import type { ThreadProps } from './types';
import type { ApiThreadReply } from '@service-comms/client';
+import { ThreadTypingIndicator } from './ThreadTypingIndicator';
import type { ThreadReplyListHandle } from './ThreadReplyList';
import {
DEFAULT_VISIBLE_REPLY_COUNT,
@@ -241,6 +242,12 @@ export function ChannelThread(props: ThreadProps) {
</DebugSuspense>
</div>
</Show>
+ <Show when={props.isNewestThread}>
+ <ThreadTypingIndicator
+ channelId={props.channelId()}
+ threadId={null}
+ />
+ </Show>
</div>
</Thread.Row>
</DebugSuspense>
diff --git a/js/app/packages/channel/Thread/ThreadReplyInput.tsx b/js/app/packages/channel/Thread/ThreadReplyInput.tsx
index ab5716ae41..f53d2eff56 100644
--- a/js/app/packages/channel/Thread/ThreadReplyInput.tsx
+++ b/js/app/packages/channel/Thread/ThreadReplyInput.tsx
@@ -1,5 +1,6 @@
import { useUserId } from '@core/context/user';
import { useSendMessageMutation } from '@queries/channel/message';
+import { usePostTypingUpdateMutation } from '@queries/channel/typing';
import type { Accessor, Setter } from 'solid-js';
import { ChannelInput, createInputAttachmentTracker } from '../Input';
import type { InputSnapshot } from '../Input';
@@ -24,6 +25,7 @@ type ThreadReplyInputProps = {
export function ThreadReplyInput(props: ThreadReplyInputProps) {
const userId = useUserId();
const sendMessageMutation = useSendMessageMutation();
+ const typingMutation = usePostTypingUpdateMutation();
const participants = useChannelParticipants(() => props.channelId);
@@ -65,6 +67,20 @@ export function ThreadReplyInput(props: ThreadReplyInputProps) {
})}
markdownNamespace={`thread-reply-input-${props.messageId}-markdown`}
onChange={(snapshot) => void props.setReplyInputState(snapshot)}
+ onStartTyping={() =>
+ typingMutation.mutate({
+ channelId: props.channelId,
+ action: 'start',
+ threadId: props.messageId,
+ })
+ }
+ onStopTyping={() =>
+ typingMutation.mutate({
+ channelId: props.channelId,
+ action: 'stop',
+ threadId: props.messageId,
+ })
+ }
onClose={() => {
props.setReplyInputState(undefined);
props.setIsReplying(false);
diff --git a/js/app/packages/channel/Thread/ThreadTypingIndicator.tsx b/js/app/packages/channel/Thread/ThreadTypingIndicator.tsx
new file mode 100644
index 0000000000..58aa61c57e
--- /dev/null
+++ b/js/app/packages/channel/Thread/ThreadTypingIndicator.tsx
@@ -0,0 +1,82 @@
+import { getTypingUsersForChannel } from '@queries/channel/typing';
+import { idToDisplayName } from '@core/user';
+import { createMemo, Show } from 'solid-js';
+import { match } from 'ts-pattern';
+
+type ThreadTypingIndicatorProps = {
+ channelId: string;
+ threadId: string | null;
+};
+
+export function ThreadTypingIndicator(props: ThreadTypingIndicatorProps) {
+ const typingUsers = createMemo(() => {
+ const users = getTypingUsersForChannel(props.channelId, props.threadId);
+ return Array.from(users);
+ });
+
+ const typingText = createMemo(() => {
+ return getThreadTypingIndicatorText(typingUsers());
+ });
+
+ const isActive = () => typingUsers().length > 0;
+
+ return (
+ <Show when={isActive()}>
+ <ThreadTypingIndicatorContent text={typingText()} />
+ </Show>
+ );
+}
+
+type ThreadTypingIndicatorContentProps = {
+ text: string;
+};
+
+function ThreadTypingIndicatorContent(
+ props: ThreadTypingIndicatorContentProps
+) {
+ return (
+ <div class="flex flex-row items-stretch justify-start ml-[var(--left-of-connector)] min-h-7">
+ <ThreadTypingIndicatorConnector />
+ <div class="text-xs text-panel uppercase font-mono px-1 py-0.5 my-1 bg-edge flex items-center gap-1">
+ <ThreadTypingIndicatorDots />
+ <span>{props.text}</span>
+ </div>
+ </div>
+ );
+}
+
+function ThreadTypingIndicatorConnector() {
+ return (
+ <>
+ <div class="flex flex-col items-center justify-center">
+ <div class="border-l border-edge-muted min-h-1/2" />
+ <div class="border-l border-edge-muted min-h-1/2" />
+ </div>
+ <div class="flex flex-col items-center justify-center">
+ <div class="w-7 border-b border-edge-muted" />
+ </div>
+ </>
+ );
+}
+
+function ThreadTypingIndicatorDots() {
+ return (
+ <span class="flex">
+ <span class="animate-typing-dot [animation-delay:0ms]">.</span>
+ <span class="animate-typing-dot [animation-delay:200ms]">.</span>
+ <span class="animate-typing-dot [animation-delay:400ms]">.</span>
+ </span>
+ );
+}
+
+function getThreadTypingIndicatorText(userIds: string[]): string {
+ return match(userIds.length)
+ .with(0, () => '')
+ .with(1, () => `${idToDisplayName(userIds[0])} is typing`)
+ .with(
+ 2,
+ () =>
+ `${idToDisplayName(userIds[0])} and ${idToDisplayName(userIds[1])} are typing`
+ )
+ .otherwise(() => 'Multiple people are typing');
+}
diff --git a/js/app/packages/channel/Thread/index.ts b/js/app/packages/channel/Thread/index.ts
index e8104fab33..f5bb1bb8d0 100644
--- a/js/app/packages/channel/Thread/index.ts
+++ b/js/app/packages/channel/Thread/index.ts
@@ -1,3 +1,4 @@
export { Thread } from './Thread';
export { ChannelThread } from './ChannelThread';
+export { ThreadTypingIndicator } from './ThreadTypingIndicator';
export type { ThreadProps, ThreadState, ThreadActions } from './types';
diff --git a/js/app/packages/channel/Thread/types.ts b/js/app/packages/channel/Thread/types.ts
index e4384da742..a80bcc4b20 100644
--- a/js/app/packages/channel/Thread/types.ts
+++ b/js/app/packages/channel/Thread/types.ts
@@ -41,4 +41,5 @@ export type ThreadProps = {
highlighted?: boolean;
selectedMessageId?: Accessor<string | undefined>;
messageListScopeId?: string;
+ isNewestThread?: boolean;
} & ThreadState;