import type { PostProps } from "./Post";
import Icon from "@/components/Icon";
import { useState } from "react";
import useLocalState from "@/state/state";
import { useLocation } from "wouter";
import { displayCount } from "@/logic/utils";
import { TrillReactModal, stringToReact } from "./Reactions";
import toast from "react-hot-toast";
import NostrIcon from "./wrappers/NostrIcon";
import type { SPID } from "@/types/ui";
// TODO abstract this somehow
function Footer({ user, poast, refetch }: PostProps) {
const [_showMenu, setShowMenu] = useState(false);
const [location, navigate] = useLocation();
const [reposting, _setReposting] = useState(false);
const { api, setComposerData, setModal, addNotification } = useLocalState(
(s) => ({
api: s.api,
setComposerData: s.setComposerData,
setModal: s.setModal,
addNotification: s.addNotification,
}),
);
const our = api!.airlock.our!;
function getComposerData(): SPID {
return "urbit" in user
? { trill: poast }
: { nostr: { post: poast, pubkey: user.nostr, eventId: poast.hash } };
}
function doReply(e: React.MouseEvent) {
console.log("do reply");
e.stopPropagation();
e.preventDefault();
setComposerData({ type: "reply", post: getComposerData() });
// Scroll to top where composer is located
window.scrollTo({ top: 0, behavior: "smooth" });
// Focus will be handled by the composer component
}
function doQuote(e: React.MouseEvent) {
e.stopPropagation();
e.preventDefault();
setComposerData({
type: "quote",
post: getComposerData(),
});
// Scroll to top where composer is located
window.scrollTo({ top: 0, behavior: "smooth" });
}
const childrenCount = poast.children
? poast.children.length
? poast.children.length
: Object.keys(poast.children).length
: 0;
const myRP = poast.engagement.shared.find((r) => r.pid.ship === our);
async function cancelRP(e: React.MouseEvent) {
e.stopPropagation();
e.preventDefault();
const r = await api!.deletePost(user, poast.id);
if (r) toast.success("Repost deleted");
// refetch();
if (location.includes(poast.id)) navigate("/");
}
async function sendRP(e: React.MouseEvent) {
// TODO update backend because contents are only markdown now
e.stopPropagation();
e.preventDefault();
const id = "urbit" in user ? poast.id : poast.hash;
const r = await api!.addRP(user, id);
if (r) {
toast.success("Your repost was published");
}
}
function doReact(e: React.MouseEvent) {
e.stopPropagation();
e.preventDefault();
const modal =