import { Link } from "react-router-dom";
import { MessageSquare, Trash2, Reply } from "lucide-react";
function formatDate(dateString) {
if (!dateString) return "";
const date = new Date(dateString);
const now = new Date();
const diff = now - date;
const minutes = Math.floor(diff / 60000);
const hours = Math.floor(diff / 3600000);
const days = Math.floor(diff / 86400000);
if (minutes < 1) return "just now";
if (minutes < 60) return `${minutes}m`;
if (hours < 24) return `${hours}h`;
if (days < 7) return `${days}d`;
return date.toLocaleDateString();
}
function ReplyItem({ reply, depth = 0, user, onReply, onDelete, isInline }) {
const author = reply.creator || reply.author || {};
const isReplyOwner = user?.did && author.did === user.did;
const containerStyle = isInline
? {
display: "flex",
gap: "10px",
padding: depth > 0 ? "10px 12px 10px 16px" : "12px 16px",
marginLeft: depth * 20,
borderLeft: depth > 0 ? "2px solid var(--accent-subtle)" : "none",
background: depth > 0 ? "rgba(168, 85, 247, 0.03)" : "transparent",
}
: {
marginLeft: depth * 24,
borderLeft: depth > 0 ? "2px solid var(--accent-subtle)" : "none",
paddingLeft: depth > 0 ? "16px" : "0",
background: depth > 0 ? "rgba(168, 85, 247, 0.02)" : "transparent",
marginBottom: "12px",
};
const avatarSize = isInline ? (depth > 0 ? 28 : 32) : depth > 0 ? 28 : 36;
return (
{isInline ? (
<>
{author.avatar ? (

) : (
0 ? "0.65rem" : "0.75rem",
fontWeight: 600,
color: "white",
}}
>
{(author.displayName ||
author.handle ||
"?")[0].toUpperCase()}
)}
0 ? "0.8rem" : "0.85rem",
color: "var(--text-primary)",
}}
>
{author.displayName || author.handle}
0 ? "0.75rem" : "0.8rem",
textDecoration: "none",
}}
>
@{author.handle}
·
{formatDate(reply.created || reply.createdAt)}
{isReplyOwner && (
)}
0 ? "0.85rem" : "0.9rem",
lineHeight: 1.5,
color: "var(--text-primary)",
}}
>
{reply.text || reply.body?.value}
>
) : (
<>
{author.avatar ? (

) : (
{(author.displayName ||
author.handle ||
"?")[0].toUpperCase()}
)}
{author.displayName || author.handle}
{author.handle && (
@{author.handle}
)}
·
{formatDate(reply.created || reply.createdAt)}
{isReplyOwner && (
)}
{reply.text || reply.body?.value}
>
)}
{reply.children &&
reply.children.map((child) => (
))}
);
}
export default function ReplyList({
replies,
rootUri,
user,
onReply,
onDelete,
isInline = false,
}) {
if (!replies || replies.length === 0) {
if (isInline) {
return (
No replies yet
);
}
return (
No replies yet. Be the first to reply!
);
}
const buildReplyTree = () => {
const replyMap = {};
const rootReplies = [];
replies.forEach((r) => {
replyMap[r.id || r.uri] = { ...r, children: [] };
});
replies.forEach((r) => {
const parentUri = r.inReplyTo || r.parentUri;
if (parentUri === rootUri) {
rootReplies.push(replyMap[r.id || r.uri]);
} else if (replyMap[parentUri]) {
replyMap[parentUri].children.push(replyMap[r.id || r.uri]);
} else {
rootReplies.push(replyMap[r.id || r.uri]);
}
});
return rootReplies;
};
const replyTree = buildReplyTree();
return (
{replyTree.map((reply) => (
))}
);
}