|
| 1 | +import { memo, useCallback } from "react"; |
| 2 | +import { useNavigate } from "@tanstack/react-router"; |
| 3 | +import { MessageCircleIcon } from "lucide-react"; |
| 4 | + |
| 5 | +import { useChatWidgetStore } from "../../chatWidgetStore"; |
| 6 | +import { useMobileConnectionState } from "../../hooks/useMobileConnectionState"; |
| 7 | +import { useChatWidgetStatus, type ChatWidgetTone } from "../../hooks/useChatWidgetStatus"; |
| 8 | + |
| 9 | +const TONE_DOT_CLASSES: Record<ChatWidgetTone, string> = { |
| 10 | + idle: "bg-emerald-500 dark:bg-emerald-400", |
| 11 | + running: "bg-blue-500 dark:bg-blue-400 animate-pulse", |
| 12 | + attention: "bg-amber-500 dark:bg-amber-400 animate-pulse", |
| 13 | + error: "bg-red-500 dark:bg-red-400", |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * The minimized floating pill/bubble for the chat widget. |
| 18 | + * Shows connection status, thread title, and activity status. |
| 19 | + * Tapping expands back to the full chat. |
| 20 | + */ |
| 21 | +export const ChatWidgetBubble = memo(function ChatWidgetBubble() { |
| 22 | + const expand = useChatWidgetStore((s) => s.expand); |
| 23 | + const lastThreadId = useChatWidgetStore((s) => s.lastThreadId); |
| 24 | + const navigate = useNavigate(); |
| 25 | + const connectionState = useMobileConnectionState(); |
| 26 | + const { label, tone, threadTitle } = useChatWidgetStatus(); |
| 27 | + |
| 28 | + const isDisconnected = connectionState === "disconnected" || connectionState === "reconnecting"; |
| 29 | + |
| 30 | + const handleClick = useCallback(() => { |
| 31 | + expand(); |
| 32 | + if (lastThreadId) { |
| 33 | + void navigate({ to: "/$threadId", params: { threadId: lastThreadId } }); |
| 34 | + } |
| 35 | + }, [expand, lastThreadId, navigate]); |
| 36 | + |
| 37 | + return ( |
| 38 | + <button |
| 39 | + type="button" |
| 40 | + onClick={handleClick} |
| 41 | + className="fixed bottom-[max(env(safe-area-inset-bottom,16px),16px)] left-1/2 z-[60] flex -translate-x-1/2 items-center gap-2.5 rounded-full border border-border/60 bg-card/90 px-4 py-2.5 shadow-2xl shadow-black/20 backdrop-blur-md transition-transform duration-200 active:scale-95 dark:border-border/40 dark:bg-card/80 dark:shadow-black/40" |
| 42 | + aria-label="Expand chat" |
| 43 | + > |
| 44 | + {/* Status dot */} |
| 45 | + <span className="relative flex size-2.5 shrink-0"> |
| 46 | + {isDisconnected ? ( |
| 47 | + <span className="size-2.5 rounded-full bg-red-500 dark:bg-red-400" /> |
| 48 | + ) : ( |
| 49 | + <span className={`size-2.5 rounded-full ${TONE_DOT_CLASSES[tone]}`} /> |
| 50 | + )} |
| 51 | + </span> |
| 52 | + |
| 53 | + {/* Thread title or app name */} |
| 54 | + <span className="max-w-[180px] truncate text-sm font-medium text-foreground"> |
| 55 | + {threadTitle ?? "OK Code"} |
| 56 | + </span> |
| 57 | + |
| 58 | + {/* Activity label */} |
| 59 | + <span className="shrink-0 text-xs text-muted-foreground"> |
| 60 | + {isDisconnected ? "Offline" : label} |
| 61 | + </span> |
| 62 | + |
| 63 | + {/* Chat icon */} |
| 64 | + <MessageCircleIcon className="size-4 shrink-0 text-muted-foreground" /> |
| 65 | + </button> |
| 66 | + ); |
| 67 | +}); |
0 commit comments