import { useMemo, useState } from "react";
import { View } from "react-native";
import { useFeedbackBody } from "../../../shared/context/FeedbackBodyProvider.js";
import { useFeedbackUi } from "../../../shared/context/FeedbackProvider.js";
import { allowAuthenticatedAction } from "../../../shared/helpers.js";
import type { FeedbackScreenCommentBranchProps } from "../../../shared/types";
import { Button } from "./Button";
import { Comment, FeedbackForm } from "./primitives.js";
import { useNativeAction } from "../helpers.js";
import type { FeedbackScreenReplyListProps } from "../../../shared/types";
export function ReplyList({
entryId,
parentCommentId,
}: FeedbackScreenReplyListProps) {
const { hooks, commentSort, transformComments } = useFeedbackBody();
const { messages } = useFeedbackUi();
const replies = hooks.useComments({
entryId,
parentCommentId,
sort: commentSort,
});
const visible = useMemo(
() => transformComments?.(replies.results) ?? replies.results,
[replies.results, transformComments],
);
return (
{visible.map((reply) => (
))}
{(replies.status === "CanLoadMore" ||
replies.status === "LoadingMore") && (
);
}
export function CommentBranch({
comment,
entryId,
}: FeedbackScreenCommentBranchProps) {
const {
hooks,
maxCommentDepth,
renderActor,
isAuthenticated,
onUnauthenticated,
} = useFeedbackBody();
const { messages } = useFeedbackUi();
const [expanded, setExpanded] = useState(false);
const [replying, setReplying] = useState(false);
const [replyBody, setReplyBody] = useState("");
const setLike = hooks.useSetCommentLike();
const createComment = hooks.useCreateComment();
const likeAction = useNativeAction();
const replyAction = useNativeAction();
const toggleLike = (desiredState: boolean) => {
if (
!allowAuthenticatedAction(isAuthenticated, onUnauthenticated) ||
likeAction.pending
) {
return;
}
void likeAction.run(
() => setLike({ commentId: comment.id, desiredState }),
"Could not update like",
);
};
return (
{renderActor === undefined ? null : (
{renderActor(comment.actorId)}
)}
{comment.depth < maxCommentDepth ? (
{
if (
allowAuthenticatedAction(isAuthenticated, onUnauthenticated)
) {
setReplying((value) => !value);
}
}}
/>
) : null}
{replying ? (
{
if (replyBody.trim().length === 0) return;
if (
!allowAuthenticatedAction(isAuthenticated, onUnauthenticated) ||
replyAction.pending
) {
return;
}
void replyAction.run(async () => {
await createComment({
entryId,
parentCommentId: comment.id,
body: replyBody,
});
setReplyBody("");
setReplying(false);
setExpanded(true);
}, "Could not add reply");
}}
>
{messages.comments.reply}
) : null}
{expanded ? (
) : null}
);
}