All files / src/pages/admin/whatsapp inbox.tsx

92.59% Statements 25/27
92.85% Branches 26/28
100% Functions 6/6
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158                                19x 19x     19x 19x 19x   19x           19x     19x 19x 19x     19x   19x 19x       19x 19x 19x 1x       19x 1x 1x     19x         1x 1x           19x                                                             1x                                                                                                                  
import { useState, useEffect, useRef } from "react";
import { useSearch } from "@tanstack/react-router";
import { ArrowLeft, MessageCircle, User, Phone } from "lucide-react";
import { ConversationList } from "../../../components/whatsapp/ConversationList";
import { MessageThread } from "../../../components/whatsapp/MessageThread";
import { ReplyBox } from "../../../components/whatsapp/ReplyBox";
import { WhatsAppPageLayout } from "../../../components/whatsapp/WhatsAppPageLayout";
import {
	useWhatsAppConversations,
	useWhatsAppConversation,
	useWhatsAppTemplates,
	useReplyToConversation,
	useMarkConversationRead,
} from "../../../hooks/queries/useWhatsAppQueries";
 
export function WhatsAppInboxPage() {
	const searchParams = useSearch({ strict: false }) as { id?: number };
	const [selectedId, setSelectedId] = useState<number | null>(
		searchParams?.id ?? null,
	);
	const [search, setSearch] = useState("");
	const [contactTypeFilter, setContactTypeFilter] = useState("");
	const messageEndRef = useRef<HTMLDivElement>(null);
 
	const { data: conversations = [], isLoading: convsLoading } =
		useWhatsAppConversations({
			search: search || undefined,
			contactType: contactTypeFilter || undefined,
		});
 
	const { data: conversationData, isLoading: detailLoading } =
		useWhatsAppConversation(selectedId);
 
	const { data: templates = [] } = useWhatsAppTemplates();
	const replyMutation = useReplyToConversation();
	const { mutate: markAsRead } = useMarkConversationRead();
 
	// Auto-scroll to bottom when messages change
	const messageCount = conversationData?.messages?.length;
	// biome-ignore lint/correctness/useExhaustiveDependencies: messageCount is an intentional trigger to scroll on new messages
	useEffect(() => {
		messageEndRef.current?.scrollIntoView({ behavior: "smooth" });
	}, [messageCount]);
 
	// Mark conversation as read when selected and has unread messages
	const unreadCount = conversationData?.conversation?.unreadCount ?? 0;
	useEffect(() => {
		if (selectedId && unreadCount > 0) {
			markAsRead(selectedId);
		}
	}, [selectedId, unreadCount, markAsRead]);
 
	const handleSendText = (message: string) => {
		Iif (!selectedId) return;
		replyMutation.mutate({ id: selectedId, body: { message } });
	};
 
	const handleSendTemplate = (
		templateName: string,
		language: string,
		components?: unknown[],
	) => {
		Iif (!selectedId) return;
		replyMutation.mutate({
			id: selectedId,
			body: { templateName, language, components },
		});
	};
 
	return (
		<WhatsAppPageLayout fullHeight>
			<div className="flex flex-col h-full">
				{/* Two-panel layout */}
				<div className="flex flex-1 min-h-0">
					{/* Left Panel: Conversation List */}
					<div
						className={`w-80 flex-shrink-0 ${selectedId ? "hidden md:flex md:flex-col" : "flex flex-col w-full md:w-80"}`}
					>
						<ConversationList
							conversations={conversations}
							selectedId={selectedId}
							onSelect={setSelectedId}
							search={search}
							onSearchChange={setSearch}
							contactTypeFilter={contactTypeFilter}
							onContactTypeFilterChange={setContactTypeFilter}
							isLoading={convsLoading}
						/>
					</div>
 
					{/* Right Panel: Message Thread */}
					<div
						className={`flex-1 flex flex-col ${selectedId ? "flex" : "hidden md:flex"}`}
					>
						{selectedId && conversationData ? (
							<>
								{/* Contact Header */}
								<div className="flex items-center gap-3 px-4 py-3 border-b border-border-default bg-background-default">
									<button
										type="button"
										onClick={() => setSelectedId(null)}
										className="md:hidden p-1 text-foreground-muted hover:text-foreground-default"
									>
										<ArrowLeft className="h-5 w-5" />
									</button>
									<div className="h-10 w-10 rounded-full bg-green-100 flex items-center justify-center">
										<User className="h-5 w-5 text-green-700" />
									</div>
									<div className="min-w-0 flex-1">
										<p className="text-sm font-medium text-foreground-default truncate">
											{conversationData.conversation.contactName ||
												conversationData.conversation.phoneNumber}
										</p>
										<div className="flex items-center gap-2 text-xs text-foreground-muted">
											<Phone className="h-3 w-3" />
											<span>{conversationData.conversation.phoneNumber}</span>
											<span className="capitalize">
												&middot; {conversationData.conversation.contactType}
											</span>
										</div>
									</div>
								</div>
 
								{/* Messages */}
								<div className="flex-1 overflow-y-auto">
									<MessageThread
										messages={conversationData.messages}
										templates={templates}
										isLoading={detailLoading}
									/>
									<div ref={messageEndRef} />
								</div>
 
								{/* Reply Box */}
								<ReplyBox
									conversation={conversationData.conversation}
									templates={templates}
									onSendText={handleSendText}
									onSendTemplate={handleSendTemplate}
									isSending={replyMutation.isPending}
								/>
							</>
						) : (
							<div className="flex-1 flex flex-col items-center justify-center text-foreground-muted">
								<MessageCircle className="h-12 w-12 mb-3" />
								<p className="text-sm font-medium">Select a conversation</p>
								<p className="text-xs mt-1">
									Choose a conversation from the list to view messages
								</p>
							</div>
						)}
					</div>
				</div>
			</div>
		</WhatsAppPageLayout>
	);
}