snort/src/element/FollowButton.tsx

34 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-01-01 19:57:27 +00:00
import { useSelector } from "react-redux";
import useEventPublisher from "../feed/EventPublisher";
2023-01-10 12:41:55 +00:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faUserMinus, faUserPlus } from "@fortawesome/free-solid-svg-icons";
2023-01-16 17:48:25 +00:00
import { HexKey } from "../nostr";
import { RootState } from "../state/Store";
2023-01-01 19:57:27 +00:00
2023-01-16 17:48:25 +00:00
export interface FollowButtonProps {
pubkey: HexKey,
className?: string,
}
export default function FollowButton(props: FollowButtonProps) {
2023-01-01 19:57:27 +00:00
const pubkey = props.pubkey;
const publiser = useEventPublisher();
2023-01-16 17:48:25 +00:00
const isFollowing = useSelector<RootState, boolean>(s => s.login.follows?.includes(pubkey) ?? false);
const baseClassName = isFollowing ? `btn btn-warn follow-button` : `btn btn-success follow-button`
const className = props.className ? `${baseClassName} ${props.className}` : `${baseClassName}`;
2023-01-16 17:48:25 +00:00
async function follow(pubkey: HexKey) {
2023-01-01 19:57:27 +00:00
let ev = await publiser.addFollow(pubkey);
publiser.broadcast(ev);
}
2023-01-16 17:48:25 +00:00
async function unfollow(pubkey: HexKey) {
2023-01-01 20:31:09 +00:00
let ev = await publiser.removeFollow(pubkey);
publiser.broadcast(ev);
}
2023-01-01 19:57:27 +00:00
return (
2023-01-01 20:31:09 +00:00
<div className={className} onClick={() => isFollowing ? unfollow(pubkey) : follow(pubkey)}>
2023-01-16 17:48:25 +00:00
<FontAwesomeIcon icon={isFollowing ? faUserMinus : faUserPlus} size="lg" />
2023-01-01 19:57:27 +00:00
</div>
)
}