import { Button } from "../../../components/ui/button"; import { Icon } from "../../../components/ui/icon"; import { useEffect, useState } from "react"; import { usePaneAppConfig } from "../../../state/app/context"; import { colors } from "../../../theme/colors"; import { Box, Span, Text, TextAttributes, useUiCapabilities } from "../../../ui"; import { usePluginAppActions } from "../../runtime"; import { InlineAuthActions } from "../cloud/auth-actions"; import { getPreferredChatOpenChannelId, } from "./channels"; import { chatController, type ChatController } from "./controller"; interface ChatStatusWidgetProps { controller?: Pick; } type ChatStatusSnapshot = ReturnType; function getTotalUnreadCount(snapshot: ChatStatusSnapshot) { return snapshot.channelStates.reduce((total, state) => total + Math.max(0, state.unreadCount), 0); } function CloudStatusIcon() { const { nativePaneChrome } = useUiCapabilities(); if (!nativePaneChrome) { return ☁ ; } return ( ); } export function ChatStatusWidget({ controller = chatController }: ChatStatusWidgetProps) { const { createPaneFromTemplate } = usePluginAppActions(); const config = usePaneAppConfig(); const cloudPluginDisabled = config.disabledPlugins.includes("gloomberb-cloud"); const initialSnapshot = controller.getSnapshot(); const [username, setUsername] = useState(initialSnapshot.user?.username ?? null); const [hasSavedSession, setHasSavedSession] = useState(initialSnapshot.hasSavedSession); const [snapshot, setSnapshot] = useState(initialSnapshot); const unreadCount = getTotalUnreadCount(snapshot); const openChat = (event?: { preventDefault?: () => void; stopPropagation?: () => void }) => { event?.preventDefault?.(); event?.stopPropagation?.(); const channelId = getPreferredChatOpenChannelId(config, snapshot); createPaneFromTemplate("new-chat-pane", { arg: channelId }); }; useEffect(() => { const unsubscribe = controller.subscribe((nextSnapshot) => { setSnapshot(nextSnapshot); setUsername(nextSnapshot.user?.username ?? null); setHasSavedSession(nextSnapshot.hasSavedSession); }); void controller.refreshSession().catch(() => {}); return unsubscribe; }, [controller]); if (cloudPluginDisabled) return null; return ( {!username && !hasSavedSession ? ( <> ) : ( )} ); }