import { View, Text, Pressable } from "react-native";
import { Link } from "expo-router";
import { useTheme } from "./theme";
import { Avatar } from "./ui/avatar";
interface ChatListItemProps {
chat: {
_id: string;
otherUser: {
_id: string;
name: string;
avatarId: string | null;
} | null;
lastMessage: {
content: string;
createdAt: number;
senderId: string;
} | null;
lastMessageAt: number;
};
currentUserId: string;
}
function formatRelativeTime(timestamp: number): string {
const now = Date.now();
const diff = now - timestamp;
const minutes = Math.floor(diff / 60000);
const hours = Math.floor(diff / 3600000);
const days = Math.floor(diff / 86400000);
if (minutes < 1) return "Сега";
if (minutes < 60) return `пред ${minutes} мин`;
if (hours < 24) return `пред ${hours} ч`;
if (days < 7) return `пред ${days} д`;
return new Date(timestamp).toLocaleDateString("mk-MK");
}
export function ChatListItem({ chat, currentUserId }: ChatListItemProps) {
const theme = useTheme();
const isSelfMessage = chat.lastMessage?.senderId === currentUserId;
const previewText = chat.lastMessage
? (isSelfMessage ? "Вие: " : "") +
(chat.lastMessage.content.length > 40
? chat.lastMessage.content.slice(0, 40) + "\u2026"
: chat.lastMessage.content)
: "Нема пораки";
return (
{chat.otherUser?.name ?? "Непознат"}
{chat.lastMessage && (
{formatRelativeTime(chat.lastMessage.createdAt)}
)}
{previewText}
);
}
export { formatRelativeTime };