snort/src/Element/NoteFooter.tsx

161 lines
5.7 KiB
TypeScript
Raw Normal View History

2023-01-08 00:29:59 +00:00
import { useMemo, useState } from "react";
import { useSelector } from "react-redux";
2023-01-08 17:33:54 +00:00
import { faHeart, faReply, faThumbsDown, faTrash, faBolt, faRepeat } from "@fortawesome/free-solid-svg-icons";
2023-01-08 00:29:59 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2023-01-20 11:11:50 +00:00
import { formatShort } from "Number";
import useEventPublisher from "Feed/EventPublisher";
import { getReactions, normalizeReaction, Reaction } from "Util";
import { NoteCreator } from "Element/NoteCreator";
import LNURLTip from "Element/LNURLTip";
import useProfile from "Feed/ProfileFeed";
import { default as NEvent } from "Nostr/Event";
import { RootState } from "State/Store";
import { HexKey, TaggedRawEvent } from "Nostr";
import EventKind from "Nostr/EventKind";
2023-01-08 00:29:59 +00:00
2023-01-16 17:48:25 +00:00
export interface NoteFooterProps {
2023-01-17 13:03:15 +00:00
related: TaggedRawEvent[],
2023-01-16 17:48:25 +00:00
ev: NEvent
}
export default function NoteFooter(props: NoteFooterProps) {
2023-01-17 13:03:15 +00:00
const { related, ev } = props;
2023-01-08 00:29:59 +00:00
2023-01-17 13:03:15 +00:00
const login = useSelector<RootState, HexKey | undefined>(s => s.login.publicKey);
2023-01-16 17:48:25 +00:00
const author = useProfile(ev.RootPubKey)?.get(ev.RootPubKey);
2023-01-08 00:29:59 +00:00
const publisher = useEventPublisher();
const [reply, setReply] = useState(false);
const [tip, setTip] = useState(false);
const isMine = ev.RootPubKey === login;
2023-01-17 13:03:15 +00:00
const reactions = useMemo(() => getReactions(related, ev.Id, EventKind.Reaction), [related]);
const reposts = useMemo(() => getReactions(related, ev.Id, EventKind.Repost), [related]);
2023-01-08 00:29:59 +00:00
const groupReactions = useMemo(() => {
2023-01-16 17:48:25 +00:00
return reactions?.reduce((acc, { content }) => {
2023-01-17 13:03:15 +00:00
let r = normalizeReaction(content);
2023-01-08 00:29:59 +00:00
const amount = acc[r] || 0
return { ...acc, [r]: amount + 1 }
}, {
[Reaction.Positive]: 0,
[Reaction.Negative]: 0
});
}, [reactions]);
2023-01-16 17:48:25 +00:00
function hasReacted(emoji: string) {
2023-01-17 13:03:15 +00:00
return reactions?.some(({ pubkey, content }) => normalizeReaction(content) === emoji && pubkey === login)
}
function hasReposted() {
return reposts.some(a => a.pubkey === login);
2023-01-08 00:29:59 +00:00
}
2023-01-16 17:48:25 +00:00
async function react(content: string) {
2023-01-18 23:31:34 +00:00
if (!hasReacted(content)) {
2023-01-08 00:29:59 +00:00
let evLike = await publisher.react(ev, content);
publisher.broadcast(evLike);
2023-01-18 23:31:34 +00:00
}
2023-01-08 00:29:59 +00:00
}
async function deleteEvent() {
if (window.confirm(`Are you sure you want to delete ${ev.Id.substring(0, 8)}?`)) {
let evDelete = await publisher.delete(ev.Id);
publisher.broadcast(evDelete);
}
}
2023-01-08 17:33:54 +00:00
async function repost() {
2023-01-17 13:04:07 +00:00
if (!hasReposted()) {
let evRepost = await publisher.repost(ev);
publisher.broadcast(evRepost);
}
2023-01-08 17:33:54 +00:00
}
2023-01-08 00:29:59 +00:00
function tipButton() {
let service = author?.lud16 || author?.lud06;
if (service) {
return (
<>
2023-01-18 23:31:34 +00:00
<div className="reaction-pill" onClick={(e) => setTip(true)}>
<div className="reaction-pill-icon">
<FontAwesomeIcon icon={faBolt} />
</div>
</div>
2023-01-08 00:29:59 +00:00
</>
)
}
return null;
}
2023-01-16 17:48:25 +00:00
function reactionIcon(content: string, reacted: boolean) {
2023-01-08 00:29:59 +00:00
switch (content) {
case Reaction.Positive: {
2023-01-18 23:31:34 +00:00
return <FontAwesomeIcon icon={faHeart} />;
2023-01-08 00:29:59 +00:00
}
case Reaction.Negative: {
2023-01-18 23:31:34 +00:00
return <FontAwesomeIcon icon={faThumbsDown} />;
2023-01-08 00:29:59 +00:00
}
}
return content;
}
function repostIcon() {
return (
<div className={`reaction-pill ${hasReposted() ? 'reacted' : ''}`} onClick={() => repost()}>
<div className="reaction-pill-icon">
<FontAwesomeIcon icon={faRepeat} />
</div>
{reposts.length > 0 && (
<div className="reaction-pill-number">
{formatShort(reposts.length)}
</div>
)}
</div>
)
}
2023-01-08 00:29:59 +00:00
return (
<>
<div className="footer">
2023-01-19 10:19:10 +00:00
<div className={`reaction-pill ${reply ? 'reacted' : ''}`} onClick={(e) => setReply(s => !s)}>
2023-01-18 23:31:34 +00:00
<div className="reaction-pill-icon">
2023-01-08 00:29:59 +00:00
<FontAwesomeIcon icon={faReply} />
2023-01-18 23:31:34 +00:00
</div>
</div>
<div className={`reaction-pill ${hasReacted('+') ? 'reacted' : ''} `} onClick={(e) => react("+")}>
<div className="reaction-pill-icon">
<FontAwesomeIcon icon={faHeart} />
</div>
<div className="reaction-pill-number">
{formatShort(groupReactions[Reaction.Positive])}
</div>
</div>
<div className={`reaction-pill ${hasReacted('-') ? 'reacted' : ''}`} onClick={(e) => react("-")}>
<div className="reaction-pill-icon">
<FontAwesomeIcon icon={faThumbsDown} />
</div>
<div className="reaction-pill-number">
{formatShort(groupReactions[Reaction.Negative])}
</div>
</div>
{repostIcon()}
2023-01-18 23:31:34 +00:00
{tipButton()}
{isMine && (
<div className="reaction-pill trash-icon">
<div className="reaction-pill-icon">
<FontAwesomeIcon icon={faTrash} onClick={(e) => deleteEvent()} />
</div>
</div>
)}
2023-01-08 00:29:59 +00:00
</div>
<NoteCreator
autoFocus={true}
replyTo={ev}
2023-01-16 17:48:25 +00:00
onSend={() => setReply(false)}
show={reply}
/>
2023-01-17 13:03:15 +00:00
<LNURLTip svc={author?.lud16 || author?.lud06} onClose={() => setTip(false)} show={tip} />
2023-01-08 00:29:59 +00:00
</>
)
}