mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Improve unread message indicator for floating chat
- Add hasUnread state to track whether user has seen new messages - Mark messages as read when chat is opened - Mark as unread when assistant responds while chat is closed - Change indicator from message count to simple dot for cleaner UX
This commit is contained in:
1 parent
15c8040e35
commit
477d055797
3 files changed
+23
-7
No files matched your search
@@ -5,7 +5,7 @@ import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
export function FloatingChatButton() {
|
||||
const { isOpen, setIsOpen, isPoppedOut, messages } = useGlobalChat();
|
||||
const { isOpen, setIsOpen, isPoppedOut, hasUnread } = useGlobalChat();
|
||||
const { config } = useAppContext();
|
||||
|
||||
// Don't render if disabled in settings or if popped out
|
||||
@@ -27,11 +27,9 @@ export function FloatingChatButton() {
|
||||
aria-label={isOpen ? 'Close chat' : 'Open chat'}
|
||||
>
|
||||
<MessageCircle className="h-6 w-6" />
|
||||
{/* Unread indicator */}
|
||||
{!isOpen && messages.length > 0 && (
|
||||
<span className="absolute -top-1 -right-1 h-4 w-4 rounded-full bg-destructive text-[10px] font-medium text-destructive-foreground flex items-center justify-center">
|
||||
{messages.length > 9 ? '9+' : messages.length}
|
||||
</span>
|
||||
{/* Unread indicator - small dot when there are unread messages */}
|
||||
{!isOpen && hasUnread && (
|
||||
<span className="absolute top-0 right-0 h-3 w-3 rounded-full bg-destructive ring-2 ring-background" />
|
||||
)}
|
||||
</Button>
|
||||
);
|
||||
|
||||
@@ -22,15 +22,26 @@ interface GlobalChatProviderProps {
|
||||
|
||||
export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
const [messages, setMessages] = useState<GlobalChatMessage[]>([]);
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [isOpen, setIsOpenInternal] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isPoppedOut, setIsPoppedOut] = useState(false);
|
||||
const [hasUnread, setHasUnread] = useState(false);
|
||||
const abortControllerRef = useRef<AbortController | null>(null);
|
||||
const isOpenRef = useRef(false);
|
||||
|
||||
const { settings } = useAISettings();
|
||||
const { config } = useAppContext();
|
||||
const { user } = useCurrentUser();
|
||||
|
||||
// When opening the chat, mark messages as read
|
||||
const setIsOpen = useCallback((open: boolean) => {
|
||||
isOpenRef.current = open;
|
||||
setIsOpenInternal(open);
|
||||
if (open) {
|
||||
setHasUnread(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Trim messages from the top when context gets too long
|
||||
const trimMessages = useCallback((msgs: GlobalChatMessage[]): GlobalChatMessage[] => {
|
||||
let totalChars = msgs.reduce((sum, msg) => sum + msg.content.length, 0);
|
||||
@@ -133,6 +144,11 @@ export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
// Final trim after response is complete
|
||||
setMessages(prev => trimMessages(prev));
|
||||
|
||||
// Mark as unread if chat is closed
|
||||
if (!isOpenRef.current) {
|
||||
setHasUnread(true);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
// User stopped generation, keep partial response
|
||||
@@ -179,6 +195,7 @@ export function GlobalChatProvider({ children }: GlobalChatProviderProps) {
|
||||
isOpen,
|
||||
isLoading,
|
||||
isPoppedOut,
|
||||
hasUnread,
|
||||
sendMessage,
|
||||
stopGeneration,
|
||||
clearMessages,
|
||||
|
||||
@@ -12,6 +12,7 @@ export interface GlobalChatContextType {
|
||||
isOpen: boolean;
|
||||
isLoading: boolean;
|
||||
isPoppedOut: boolean;
|
||||
hasUnread: boolean;
|
||||
sendMessage: (content: string, providerModel: string) => Promise<void>;
|
||||
stopGeneration: () => void;
|
||||
clearMessages: () => void;
|
||||
|
||||
Reference in new issue
Block a user