import React, { useCallback, useMemo, useState } from "react";
import { useUser, useCreateReport, ReportReasonKey, reportReasons } from "@replyke/react-js";
const ReportPostModal = ({ reportedPost, onClose }) => {
const { user } = useUser();
const createEntityReport = useCreateReport({ type: "entity" });
const [submitting, setSubmitting] = useState(false);
const [reason, setReason] = useState<ReportReasonKey | null>(null);
const buttonActive = useMemo(() => !!reason && !!reportedPost, [reason, reportedPost]);
const handleSubmitReport = async () => {
try {
if (!reportedPost) throw new Error("No entity to report selected");
if (!reason) throw new Error("No reason to report selected");
if (!user) {
alert("Please sign in or create an account to continue.");
return;
}
setSubmitting(true);
await createEntityReport({ targetId: reportedPost.id, reason });
alert("Report submitted successfully.");
onClose();
} catch (err) {
console.error("Failed to submit entity report")
} finally {
setSubmitting(false);
}
};
return (
<div>
<h2>Submit a Report</h2>
<p>Thank you for looking out for our community. Let us know what is happening, and we'll review it.</p>
<div className="report-reasons">
{Object.entries(reportReasons).map(([key, value]) => (
<button
key={key}
onClick={() => setReason(key as ReportReasonKey)}
className={`reason-button ${key === reason ? "selected" : ""}`}
>
{value}
</button>
))}
</div>
<button
className="submit-button"
onClick={handleSubmitReport}
disabled={!buttonActive || submitting}
>
{submitting ? "Submitting..." : "Submit Report"}
</button>
<button onClick={onClose} className="close-button">Cancel</button>
</div>
);
};
export default ReportPostModal;