import React, { useMemo } from "react"; import { View, Text, TouchableOpacity, ActivityIndicator, StyleSheet, ScrollView, Modal, Animated, Pressable, } from "react-native"; import { X } from "lucide-react-native"; import { mergeStyles } from "../utils/helpers"; import type { Conversation } from "../services/OperoChatHistoryService"; import type { OperoChatStyles } from "../types"; export interface OperoChatHistoryDrawerProps { isVisible: boolean; conversations: Conversation[]; isLoading: boolean; hasMore: boolean; onClose: () => void; onConversationSelect: (conversationId: string) => void; onLoadMore: () => void; onOpen?: () => void; customStyles?: OperoChatStyles; } interface GroupedConversations { today: Conversation[]; yesterday: Conversation[]; thisWeek: Conversation[]; older: Conversation[]; } export const OperoChatHistoryDrawer: React.FC = ({ isVisible, conversations, isLoading, hasMore, onClose, onConversationSelect, onLoadMore, onOpen, customStyles, }) => { const styles = createStyles(mergeStyles(customStyles)); const slideAnim = React.useRef(new Animated.Value(-100)).current; React.useEffect(() => { if (isVisible) { // Load conversations when drawer opens if (onOpen) { onOpen(); } Animated.timing(slideAnim, { toValue: 0, duration: 300, useNativeDriver: true, }).start(); } else { Animated.timing(slideAnim, { toValue: -100, duration: 250, useNativeDriver: true, }).start(); } }, [isVisible, slideAnim, onOpen]); const groupedConversations = useMemo(() => { const now = new Date(); const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); const yesterday = new Date(today); yesterday.setDate(yesterday.getDate() - 1); const weekAgo = new Date(today); weekAgo.setDate(weekAgo.getDate() - 7); const grouped: GroupedConversations = { today: [], yesterday: [], thisWeek: [], older: [], }; conversations.forEach((conv) => { const convDate = new Date(conv.updated_at); const convDateOnly = new Date( convDate.getFullYear(), convDate.getMonth(), convDate.getDate(), ); if (convDateOnly.getTime() === today.getTime()) { grouped.today.push(conv); } else if (convDateOnly.getTime() === yesterday.getTime()) { grouped.yesterday.push(conv); } else if (convDate >= weekAgo) { grouped.thisWeek.push(conv); } else { grouped.older.push(conv); } }); return grouped; }, [conversations]); const formatTime = (dateString: string): string => { const date = new Date(dateString); const hours = date.getHours(); const minutes = date.getMinutes(); const ampm = hours >= 12 ? "PM" : "AM"; const displayHours = hours % 12 || 12; const displayMinutes = minutes.toString().padStart(2, "0"); return `${displayHours}:${displayMinutes} ${ampm}`; }; const formatDate = (dateString: string): string => { const date = new Date(dateString); return date.toLocaleDateString(undefined, { month: "short", day: "numeric", year: "numeric", }); }; const formatRelativeTime = (dateString: string): string => { const now = new Date(); const date = new Date(dateString); const diffMs = now.getTime() - date.getTime(); const diffSeconds = Math.floor(diffMs / 1000); const diffMinutes = Math.floor(diffSeconds / 60); const diffHours = Math.floor(diffMinutes / 60); const diffDays = Math.floor(diffHours / 24); const diffWeeks = Math.floor(diffDays / 7); const diffMonths = Math.floor(diffDays / 30); const diffYears = Math.floor(diffDays / 365); if (diffSeconds < 60) { return "Just now"; } else if (diffMinutes < 60) { return `${diffMinutes} ${diffMinutes === 1 ? "minute" : "minutes"} ago`; } else if (diffHours < 24) { return `${diffHours} ${diffHours === 1 ? "hour" : "hours"} ago`; } else if (diffDays < 7) { return `${diffDays} ${diffDays === 1 ? "day" : "days"} ago`; } else if (diffWeeks < 4) { return `${diffWeeks} ${diffWeeks === 1 ? "week" : "weeks"} ago`; } else if (diffMonths < 12) { return `${diffMonths} ${diffMonths === 1 ? "month" : "months"} ago`; } else { return `${diffYears} ${diffYears === 1 ? "year" : "years"} ago`; } }; const handleConversationSelect = (conversationId: string) => { onConversationSelect(conversationId); onClose(); }; const renderConversationItem = ( conversation: Conversation, showTime: boolean, ) => { // Use last_message_at if available, otherwise fall back to updated_at const displayDate = conversation.last_message_at || conversation.updated_at; const useRelativeTime = !!conversation.last_message_at; return ( handleConversationSelect(conversation.id)} activeOpacity={0.7} > {conversation.title} {useRelativeTime ? formatRelativeTime(displayDate) : showTime ? formatTime(displayDate) : formatDate(displayDate)} ); }; const renderSection = ( title: string, conversationsList: Conversation[], showTime: boolean = false, ) => { if (conversationsList.length === 0) return null; return ( {title} {conversationsList.map((conv) => renderConversationItem(conv, showTime), )} ); }; const renderEmptyState = () => ( No conversations yet Start a conversation to see your history here ); if (!isVisible) return null; return ( {/* Backdrop */} {/* Drawer */} {/* Header */} Conversation History {/* Content */} {isLoading && conversations.length === 0 ? ( Loading conversations... ) : conversations.length === 0 && !isLoading ? ( renderEmptyState() ) : ( <> {renderSection("Today", groupedConversations.today, true)} {renderSection( "Yesterday", groupedConversations.yesterday, true, )} {renderSection("This Week", groupedConversations.thisWeek)} {renderSection("Older", groupedConversations.older)} {isLoading && ( Loading conversations... )} {hasMore && !isLoading && ( Load More )} )} ); }; const createStyles = (customStyles: OperoChatStyles) => StyleSheet.create({ modalContainer: { flex: 1, flexDirection: "row", }, backdrop: { position: "absolute", top: 0, left: 0, right: 0, bottom: 0, backgroundColor: "rgba(0, 0, 0, 0.5)", }, drawer: { flex: 1, backgroundColor: customStyles?.backgroundColor || "#ffffff", shadowColor: "#000", shadowOffset: { width: 2, height: 0 }, shadowOpacity: 0.25, shadowRadius: 8, elevation: 5, }, header: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 20, paddingVertical: 16, paddingTop: 44, backgroundColor: customStyles?.primaryColor || "#3b82f6", borderBottomWidth: 1, borderBottomColor: "rgba(255, 255, 255, 0.1)", }, headerTitle: { fontSize: 18, fontWeight: "600", color: "#ffffff", flex: 1, }, closeButton: { width: 40, height: 40, borderRadius: 20, backgroundColor: "rgba(255, 255, 255, 0.2)", alignItems: "center", justifyContent: "center", }, scrollView: { flex: 1, }, scrollContent: { paddingBottom: 20, }, section: { marginTop: 16, }, sectionTitle: { fontSize: 14, fontWeight: "600", color: customStyles?.textColor || "#6b7280", paddingHorizontal: 20, paddingVertical: 8, textTransform: "uppercase", letterSpacing: 0.5, }, conversationItem: { paddingHorizontal: 20, paddingVertical: 12, borderBottomWidth: 1, borderBottomColor: customStyles?.conversationItemBorderColor || "#f3f4f6", }, conversationContent: { flex: 1, }, conversationTitle: { fontSize: 15, fontWeight: "500", color: customStyles?.conversationTitleColor || "#1f2937", marginBottom: 4, lineHeight: 20, }, conversationDate: { fontSize: 12, color: customStyles?.conversationDateColor || "#9ca3af", }, loadingContainer: { flexDirection: "row", alignItems: "center", justifyContent: "center", paddingVertical: 24, }, loadingText: { fontSize: 14, color: customStyles?.loadingTextColor || "#6b7280", marginLeft: 8, }, loadMoreButton: { marginHorizontal: 20, marginTop: 16, backgroundColor: customStyles?.loadMoreButtonBackgroundColor || "#f3f4f6", paddingVertical: 12, paddingHorizontal: 16, borderRadius: 8, alignItems: "center", }, loadMoreButtonText: { fontSize: 14, fontWeight: "500", color: customStyles?.loadMoreButtonTextColor || "#374151", }, emptyState: { flex: 1, alignItems: "center", justifyContent: "center", paddingVertical: 60, paddingHorizontal: 40, }, emptyStateText: { fontSize: 18, fontWeight: "600", color: customStyles?.textColor || "#1f2937", marginBottom: 8, textAlign: "center", }, emptyStateSubtext: { fontSize: 14, color: customStyles?.textColor || "#6b7280", textAlign: "center", lineHeight: 20, }, initialLoadingContainer: { flex: 1, alignItems: "center", justifyContent: "center", paddingVertical: 80, }, initialLoadingText: { fontSize: 16, color: customStyles?.loadingTextColor || "#6b7280", marginTop: 16, }, });