#include "screens/feed/FeedPostView.hpp"
#include "screens/comments/CommentEditor.hpp"
#include "screens/comments/CommentsView.hpp"
#include "screens/comments/FadeInView.hpp"
#include "screens/comments/MentionParticipants.hpp"
#include "screens/comments/ReplyDraft.hpp"
#include "screens/components/AttachmentViews.hpp"
#include "screens/components/PollView.hpp"
#include "screens/feed/FeedPostActions.hpp"
#include "screens/post/DoubleTapView.hpp"
#include "screens/post/PostContentView.hpp"
#include "screens/post/PostFooterView.hpp"
#include "screens/post/PostHeaderView.hpp"
#include "spec/PostsSpec.hpp"
#include "store/AppState.hpp"
#include "store/SettingsStore.hpp"
#include "ui/Palette.hpp"
namespace dp {
using namespace ui;
using spec::PostsLayout;
namespace {
ViewPtr hLine() {
return RectangleShape(Palette::fg2())->frame(std::nullopt, PostsLayout::kHLineHeight)->opacity(PostsLayout::kHLineOpacity)->maxWidth();
}
void submitReply(CommentsSession* session, const Post& post, const std::string& content) {
std::vector<UploadResponse> attachments = session->takeAttachmentsAndClose();
FeedPostActions::createComment(TargetType::Post, post.data.id, std::nullopt, content, attachments, [](bool ok) {
if (ok) ReplyDraft::clear();
});
}
} // namespace
ViewPtr FeedPostView(const Post& post, const FeedPostContext& ctx) {
CommentsSession* session = ctx.session;
SettingsStore& settings = SettingsStore::instance();
const std::string postId = post.data.id;
const bool isReplyingToPost = session->isReplying(postId);
const bool own = AppState::instance().isCurrentUser(post.data.userId);
const std::vector<UserSearch::Result> participants = MentionParticipants::from(post.author, post.recentComments);
const std::optional<std::string> slug = post.data.slug;
auto onSelect = ctx.onSelect;
auto navigate = [onSelect, slug](const std::optional<std::string>& scrollTo) {
if (slug && onSelect) onSelect(*slug, scrollTo);
};
const Post postCopy = post; // small: captured by the gesture handlers
auto onEdit = ctx.onEdit;
// Gestures act only while no composer is open on the feed (FeedPostView.swift:143-144).
TapHandlers taps;
taps.single = [session, navigate] { if (!session->activeReply()) navigate(std::nullopt); };
taps.doubleTap = [session, postCopy, onEdit] {
if (session->activeReply()) return;
if (!FeedPostActions::doubleTapPost(postCopy) && onEdit) onEdit(postCopy);
};
PostEnvironment env = ctx.env;
env.taps = taps;
PostHeaderOptions headerOptions;
headerOptions.linksToProfile = false;
headerOptions.commentCount = post.commentCount;
std::vector<ViewPtr> top;
top.push_back(PostHeaderView(post.author, post.data.createdAt, headerOptions, env));
top.push_back(PostContentView(post.data.topic, post.data.title, post.data.content, env));
if (post.poll) top.push_back(PollView(*post.poll));
if (settings.showFeedAttachments() && !post.attachments.empty()) top.push_back(AttachmentViewer(post.attachments.front()));
PostFooterProps footer;
footer.targetId = postId;
footer.authorId = post.data.userId;
footer.starCount = post.data.stars;
footer.currentVote = post.myVote;
footer.reactions = post.reactions;
footer.onReact = [postCopy](const std::string& emoji) { FeedPostActions::reactPost(postCopy, emoji); };
footer.onReply = [session, postId] { session->beginReply(postId); };
footer.isReplying = isReplyingToPost;
if (own) {
footer.onEdit = [onEdit, postCopy] { if (onEdit) onEdit(postCopy); };
footer.onDelete = [postCopy] { FeedPostActions::deletePost(postCopy); };
}
top.push_back(PostFooterView(footer));
if (isReplyingToPost) {
CommentEditorProps props;
props.text = ReplyDraft::binding();
props.initialLineCount = PostsLayout::kEditorInitialLines;
props.focusOnAppear = true;
props.attachments = session->attachments();
props.onAttachmentsChange = [session](const std::vector<UploadResponse>& list) { session->setAttachments(list); };
props.mentionParticipants = participants;
props.anchorKey = ctx.composerAnchorKey;
props.onCancel = [session] { session->cancelReply(); };
props.onSubmit = [session, postCopy](const std::string& content) { submitReply(session, postCopy, content); };
top.push_back(FadeIn(session->editor("reply:" + postId).view(props), "reply-" + postId + "-" + std::to_string(session->epoch())));
}
std::vector<ViewPtr> card{hLine()->paddingBottom(PostsLayout::kCardSpacing),
VStack(top, PostsLayout::kCardSpacing, HAlign::Leading)->maxWidth(Alignment::leading())->paddingH(PostsLayout::kScreenPadding)};
if (settings.showFeedComments()) {
CommentsProps cp;
cp.comments = &post.recentComments;
cp.baseIndentationLevel = 1;
cp.linksToProfile = false;
cp.maxAttachments = settings.showFeedAttachments() ? 1 : 0;
cp.session = session;
cp.composerAnchorKey = ctx.composerAnchorKey;
cp.env = ctx.env;
cp.onSingleTap = [navigate](const CommentRef& c) { navigate(c.prefixedId); };
cp.onDoubleTap = [](const CommentRef& c) { FeedPostActions::doubleTapComment(c); };
cp.onReact = [](const CommentRef& c, const std::string& e) { FeedPostActions::reactComment(c, e); };
cp.onDelete = [](const CommentRef& c) { FeedPostActions::deleteComment(c); };
cp.onEdit = [](const CommentRef& c, const std::string& text) { FeedPostActions::editComment(c, text); };
cp.onReply = [session](const CommentRef& c) { session->beginReply(c.prefixedId); };
cp.replyingCommentId = session->activeReply();
cp.replyText = ReplyDraft::binding();
cp.onReplyAttachmentsChange = [session](const std::vector<UploadResponse>& list) { session->setAttachments(list); };
cp.onSubmitReply = [session](const CommentRef& c, const std::string& content) {
std::vector<UploadResponse> attachments = session->takeAttachmentsAndClose();
std::optional<TargetType> type = TargetType::fromRaw(c.targetType);
FeedPostActions::createComment(type.value_or(TargetType(TargetType::Post)), c.targetId, c.id, content, attachments,
[](bool ok) { if (ok) ReplyDraft::clear(); });
};
cp.onCancelReply = [session] { session->cancelReply(); };
cp.mentionParticipants = participants;
cp.showsDividers = true;
cp.showsLeadingDivider = true;
card.push_back(CommentsView(cp)->paddingTop(PostsLayout::kCardSpacing));
}
card.push_back(hLine()->paddingTop(PostsLayout::kCardSpacing));
ViewPtr body = VStack(card, 0, HAlign::Leading)->maxWidth(Alignment::leading())->foreground(Palette::fg1())->background(Palette::bg1());
return OnTaps(body, taps)->id(post.id());
}
} // namespace dp