Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(community): float typing indicator + drop ghost on send

Genera90f0c263316
diff --git a/src/shared/src/constants/community.ts b/src/shared/src/constants/community.ts
index 982aeedae..bf01c63b6 100644
--- a/src/shared/src/constants/community.ts
+++ b/src/shared/src/constants/community.ts
@@ -68,7 +68,14 @@ export const PRESENCE_MEMBER_CAP = 500
export const BANNER_COLOR_REGEX = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
// Typing indicator
+// TIMEOUT: how long a received typing.start stays visible before auto-expire
+// if no follow-up arrives. Kept generous so a missed heartbeat doesn't blink
+// the indicator off mid-burst.
+// THROTTLE: how often the local client will re-emit typing.start for the
+// same target while the user is still typing. Shorter than TIMEOUT so the
+// indicator on other clients gets refreshed with room to spare.
export const TYPING_INDICATOR_TIMEOUT_MS = 8_000
+export const TYPING_INDICATOR_THROTTLE_MS = 3_000
// Message deduplication cache
export const MESSAGE_DEDUP_CACHE_MAX = 500
diff --git a/src/shared/src/index.ts b/src/shared/src/index.ts
index 6837fa128..40dc09845 100644
--- a/src/shared/src/index.ts
+++ b/src/shared/src/index.ts
@@ -149,6 +149,7 @@ export {
BANNER_COLOR_REGEX,
NOTIFICATION_LEVEL_VALUES,
TYPING_INDICATOR_TIMEOUT_MS,
+ TYPING_INDICATOR_THROTTLE_MS,
MESSAGE_DEDUP_CACHE_MAX,
MESSAGE_DEDUP_CACHE_TRIM,
NOTIF_LEVELS,
diff --git a/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx b/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
index a9789966c..1c8556c84 100644
--- a/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
+++ b/src/web/src/app/community/channels/[serverId]/[channelId]/page.tsx
@@ -50,6 +50,7 @@ import {
communityWsSubscribe,
communityWsUnsubscribe,
communityWsSendTyping,
+ communityWsResetTypingThrottle,
} from "@/hooks/community/use-community-ws"
/**
@@ -360,6 +361,7 @@ function ChannelView() {
mentionType,
attachments: uploadedAttachments.length > 0 ? uploadedAttachments : undefined,
})
+ communityWsResetTypingThrottle({ channelId })
setReplyTo(null)
}
diff --git a/src/web/src/app/community/me/[dmId]/page.tsx b/src/web/src/app/community/me/[dmId]/page.tsx
index b900c2e22..052014611 100644
--- a/src/web/src/app/community/me/[dmId]/page.tsx
+++ b/src/web/src/app/community/me/[dmId]/page.tsx
@@ -28,6 +28,7 @@ import {
communityWsSubscribe,
communityWsUnsubscribe,
communityWsSendTyping,
+ communityWsResetTypingThrottle,
} from "@/hooks/community/use-community-ws"
// Thin re-mount wrapper — same reason as the server-side channel view: the
@@ -157,6 +158,7 @@ function DmView() {
avatar: currentUser.avatar,
},
})
+ communityWsResetTypingThrottle({ dmConversationId: dmId })
setReplyTo(null)
}
diff --git a/src/web/src/components/community/message-list.tsx b/src/web/src/components/community/message-list.tsx
index bc24d1e70..c2d8f4475 100644
--- a/src/web/src/components/community/message-list.tsx
+++ b/src/web/src/components/community/message-list.tsx
@@ -182,6 +182,7 @@ export function MessageList({
count={belowCount}
onClick={scrollToBottom}
/>
+ <TypingIndicator names={typingUsers ?? []} />
<div ref={scrollRef} className="flex-1 overflow-y-auto thin-scrollbar">
<div className="flex min-h-full flex-col justify-end px-4 py-8">
<div className="mb-6">
@@ -229,7 +230,6 @@ export function MessageList({
</div>
))}
- {typingUsers && typingUsers.length > 0 && <TypingIndicator names={typingUsers} />}
</div>
</div>
</div>
diff --git a/src/web/src/components/community/typing-indicator.tsx b/src/web/src/components/community/typing-indicator.tsx
index bf1965cdb..4e9270ea2 100644
--- a/src/web/src/components/community/typing-indicator.tsx
+++ b/src/web/src/components/community/typing-indicator.tsx
@@ -1,25 +1,36 @@
// Typing indicator — soft opacity pulse + "{names} is/are typing…".
// Uses the `typing-dot` keyframe in globals.css so we stay on the house easing
// curve and inherit the `prefers-reduced-motion` rules already wired there.
+// Rendered as a floating pill (same visual family as `ScrollDownButton`) so
+// new activity doesn't shift the message list layout when someone starts
+// typing.
export function TypingIndicator({ names }: { names: string[] }) {
- if (!names.length) return null
- const label = names.length === 1
- ? <><span className="font-medium text-foreground">{names[0]}</span> is typing…</>
- : names.length <= 3
- ? <><span className="font-medium text-foreground">{names.slice(0, -1).join(", ")} and {names[names.length - 1]}</span> are typing…</>
- : <><span className="font-medium text-foreground">{names.length} people</span> are typing…</>
+ const visible = names.length > 0
+ const label = names.length === 0
+ ? null
+ : names.length === 1
+ ? <><span className="font-medium text-foreground">{names[0]}</span> is typing…</>
+ : names.length <= 3
+ ? <><span className="font-medium text-foreground">{names.slice(0, -1).join(", ")} and {names[names.length - 1]}</span> are typing…</>
+ : <><span className="font-medium text-foreground">{names.length} people</span> are typing…</>
return (
- <div className="flex items-center gap-2 px-1 text-sm text-muted-foreground">
- <span className="flex gap-1">
- {[0, 1, 2].map((i) => (
- <span
- key={i}
- className="size-1.5 rounded-full bg-muted-foreground"
- style={{ animation: "typing-dot 1.4s ease-in-out infinite", animationDelay: `${i * 160}ms` }}
- />
- ))}
- </span>
- <span>{label}</span>
+ <div
+ className={`pointer-events-none absolute bottom-3 left-3 z-10 transition-all duration-200 ease-out ${
+ visible ? "translate-y-0 opacity-100" : "translate-y-2 opacity-0"
+ }`}
+ >
+ <div className="flex h-8 items-center gap-2 rounded-full border border-border bg-background/90 px-3 text-xs text-muted-foreground shadow-(--e1) backdrop-blur-sm">
+ <span className="flex gap-1">
+ {[0, 1, 2].map((i) => (
+ <span
+ key={i}
+ className="size-1.5 rounded-full bg-muted-foreground"
+ style={{ animation: "typing-dot 1.4s ease-in-out infinite", animationDelay: `${i * 160}ms` }}
+ />
+ ))}
+ </span>
+ <span>{label}</span>
+ </div>
</div>
)
}
diff --git a/src/web/src/hooks/community/use-community-ws.ts b/src/web/src/hooks/community/use-community-ws.ts
index 9312b1ff2..2d84baf62 100644
--- a/src/web/src/hooks/community/use-community-ws.ts
+++ b/src/web/src/hooks/community/use-community-ws.ts
@@ -51,7 +51,7 @@ import type {
CommunityMachineUpdated,
CommunityMachineRemoved,
} from "@alook/shared"
-import { isCommunityEvent, TYPING_INDICATOR_TIMEOUT_MS } from "@alook/shared"
+import { isCommunityEvent, TYPING_INDICATOR_TIMEOUT_MS, TYPING_INDICATOR_THROTTLE_MS } from "@alook/shared"
import type { Msg, Attachment } from "@/components/community/_types"
import { avatarInitial } from "@/lib/community/avatar"
import type { MachinesResponse } from "@/hooks/community/use-machines"
@@ -293,12 +293,27 @@ export function communityWsSendTyping(target: {
const now = Date.now()
const map = useCommunityStore.getState().lastTypingSent
const lastSent = map.get(key) || 0
- if (now - lastSent < TYPING_INDICATOR_TIMEOUT_MS) return
+ if (now - lastSent < TYPING_INDICATOR_THROTTLE_MS) return
map.set(key, now)
send({ type: "community:typing.start", ...target })
}
+/**
+ * Reset the outbound typing.start throttle for a target. Sending a message
+ * ends the current typing burst; the very next keystroke should re-emit
+ * typing.start immediately, not wait out the 8s dedup window.
+ */
+export function communityWsResetTypingThrottle(target: {
+ channelId?: string
+ dmConversationId?: string
+ threadId?: string
+}) {
+ const key = target.channelId || target.dmConversationId || target.threadId || ""
+ if (!key) return
+ useCommunityStore.getState().lastTypingSent.delete(key)
+}
+
export function useCommunityWs(options?: UseCommunityWsOptions) {
const queryClient = useQueryClient()
const viewerUserIdRef = useRef<string | null>(options?.viewerUserId ?? null)
@@ -348,6 +363,11 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
if (wsStore.hasSeenMessage(event.message.id)) return
wsStore.markSeenMessage(event.message.id)
+ // Sending a message is an implicit typing.stop for its author —
+ // clear immediately so the indicator doesn't linger under the
+ // freshly-arrived message until the 8s timeout expires.
+ clearTypingIndicator(event.message.authorId)
+
// 1) Patch the focused channel/dm page cache if the event matches.
if (event.channelId && event.channelId === sub.channelId) {
queryClient.setQueryData<PageCache>(
@@ -811,7 +831,7 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
}, [])
/**
- * Send a typing indicator. Client-side debounced at 8s per channelId /
+ * Send a typing indicator. Client-side throttled per channelId /
* dmConversationId. The DO also applies server-side dedup.
*/
const sendTyping = useCallback(
@@ -822,7 +842,7 @@ export function useCommunityWs(options?: UseCommunityWsOptions) {
const now = Date.now()
const map = useCommunityStore.getState().lastTypingSent
const lastSent = map.get(key) || 0
- if (now - lastSent < TYPING_INDICATOR_TIMEOUT_MS) return
+ if (now - lastSent < TYPING_INDICATOR_THROTTLE_MS) return
// Mutate the map in place — no equality change, no re-render (nothing
// subscribes to it). Keeping it in the store keeps the lifetime tied
@@ -882,3 +902,23 @@ function applyTypingIndicator(userId: string) {
})
}
+/**
+ * Immediately remove userId from `typingUsers` and cancel its pending timer.
+ * Called when the user sends a message — sending is an implicit typing.stop,
+ * and waiting for the 8s timeout leaves a ghost indicator hanging under the
+ * message that just arrived.
+ */
+function clearTypingIndicator(userId: string) {
+ useCommunityStore.setState((state) => {
+ const existing = state.typingTimers.get(userId)
+ if (!existing && !state.typingUsers.includes(userId)) return {}
+ if (existing) clearTimeout(existing)
+ const nextTimers = new Map(state.typingTimers)
+ nextTimers.delete(userId)
+ return {
+ typingUsers: state.typingUsers.filter((id) => id !== userId),
+ typingTimers: nextTimers,
+ }
+ })
+}
+