snort/src/Element/FollowButton.tsx

34 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-01 19:57:27 +00:00
import { useSelector } from "react-redux";
2023-01-20 11:11:50 +00:00
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-20 11:11:50 +00:00
import { HexKey } from "Nostr";
import { RootState } from "State/Store";
2023-01-27 18:57:15 +00:00
import { parseId } from "Util";
2023-01-01 19:57:27 +00:00
2023-01-16 17:48:25 +00:00
export interface FollowButtonProps {
pubkey: HexKey,
2023-01-25 18:08:53 +00:00
className?: string
2023-01-16 17:48:25 +00:00
}
export default function FollowButton(props: FollowButtonProps) {
2023-01-27 18:57:15 +00:00
const pubkey = parseId(props.pubkey);
2023-01-01 19:57:27 +00:00
const publiser = useEventPublisher();
2023-01-16 17:48:25 +00:00
const isFollowing = useSelector<RootState, boolean>(s => s.login.follows?.includes(pubkey) ?? false);
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-25 18:08:53 +00:00
<button type="button" className={isFollowing ? `${props.className ?? ''} secondary` : props.className} onClick={() => isFollowing ? unfollow(pubkey) : follow(pubkey)}>
{isFollowing ? 'Unfollow' : 'Follow'}
</button>
2023-01-01 19:57:27 +00:00
)
2023-01-25 18:08:53 +00:00
}