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-18 23:31:34 +00:00
|
|
|
import { formatShort } from "../Number";
|
2023-01-08 00:29:59 +00:00
|
|
|
import useEventPublisher from "../feed/EventPublisher";
|
2023-01-17 13:03:15 +00:00
|
|
|
import { getReactions, normalizeReaction, Reaction } from "../Util";
|
2023-01-08 00:29:59 +00:00
|
|
|
import { NoteCreator } from "./NoteCreator";
|
|
|
|
import LNURLTip from "./LNURLTip";
|
2023-01-16 13:17:29 +00:00
|
|
|
import useProfile from "../feed/ProfileFeed";
|
2023-01-16 17:48:25 +00:00
|
|
|
import { default as NEvent } from "../nostr/Event";
|
|
|
|
import { RootState } from "../state/Store";
|
2023-01-17 13:03:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="footer">
|
2023-01-18 23:31:34 +00:00
|
|
|
{isMine && (
|
|
|
|
<div className="reaction-pill">
|
|
|
|
<div className="reaction-pill-icon">
|
|
|
|
<FontAwesomeIcon icon={faTrash} onClick={(e) => deleteEvent()} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div className="reaction-pill" onClick={(e) => setReply(s => !s)}>
|
|
|
|
<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 ${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>
|
|
|
|
<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>
|
|
|
|
{tipButton()}
|
2023-01-08 00:29:59 +00:00
|
|
|
</div>
|
2023-01-15 23:44:50 +00:00
|
|
|
<NoteCreator
|
2023-01-16 13:17:29 +00:00
|
|
|
autoFocus={true}
|
|
|
|
replyTo={ev}
|
2023-01-16 17:48:25 +00:00
|
|
|
onSend={() => setReply(false)}
|
2023-01-16 13:17:29 +00:00
|
|
|
show={reply}
|
2023-01-15 23:44:50 +00:00
|
|
|
/>
|
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
|
|
|
</>
|
|
|
|
)
|
2023-01-15 23:44:50 +00:00
|
|
|
}
|