refactor: include relays in kind3

This commit is contained in:
2024-01-30 22:38:23 +00:00
parent ad8d0af976
commit 07510d92ca
6 changed files with 63 additions and 42 deletions

View File

@ -2,7 +2,7 @@ import { HexKey } from "@snort/system";
import { FormattedMessage } from "react-intl";
import AsyncButton from "@/Components/Button/AsyncButton";
import useEventPublisher from "@/Hooks/useEventPublisher";
import useFollowsControls from "@/Hooks/useFollowControls";
import useLogin from "@/Hooks/useLogin";
import { parseId } from "@/Utils";
@ -14,32 +14,18 @@ export interface FollowButtonProps {
}
export default function FollowButton(props: FollowButtonProps) {
const pubkey = parseId(props.pubkey);
const { publisher, system } = useEventPublisher();
const { follows, readonly } = useLogin(s => ({ follows: s.follows, readonly: s.readonly }));
const isFollowing = follows.item.includes(pubkey);
const readonly = useLogin(s => s.readonly);
const control = useFollowsControls();
const isFollowing = control.isFollowing(pubkey);
const baseClassname = props.className ? `${props.className} ` : "";
async function follow(pubkey: HexKey) {
if (publisher) {
const ev = await publisher.contactList([pubkey, ...follows.item].map(a => ["p", a]));
system.BroadcastEvent(ev);
}
}
async function unfollow(pubkey: HexKey) {
if (publisher) {
const ev = await publisher.contactList(follows.item.filter(a => a !== pubkey).map(a => ["p", a]));
system.BroadcastEvent(ev);
}
}
return (
<AsyncButton
className={isFollowing ? `${baseClassname} secondary` : `${baseClassname} primary`}
disabled={readonly}
onClick={async e => {
e.stopPropagation();
await (isFollowing ? unfollow(pubkey) : follow(pubkey));
await (isFollowing ? control.removeFollow([pubkey]) : control.addFollow([pubkey]));
}}>
{isFollowing ? <FormattedMessage {...messages.Unfollow} /> : <FormattedMessage {...messages.Follow} />}
</AsyncButton>

View File

@ -1,12 +1,10 @@
import { dedupe } from "@snort/shared";
import { HexKey } from "@snort/system";
import { ReactNode, useMemo } from "react";
import { FormattedMessage } from "react-intl";
import ProfilePreview from "@/Components/User/ProfilePreview";
import useEventPublisher from "@/Hooks/useEventPublisher";
import useFollowsControls from "@/Hooks/useFollowControls";
import useLogin from "@/Hooks/useLogin";
import { setFollows } from "@/Utils/Login";
import AsyncButton from "../Button/AsyncButton";
import messages from "../messages";
@ -30,19 +28,13 @@ export default function FollowListBase({
actions,
profileActions,
}: FollowListBaseProps) {
const { publisher, system } = useEventPublisher();
const { id, follows } = useLogin(s => ({ id: s.id, follows: s.follows }));
const control = useFollowsControls();
const login = useLogin();
const profilePreviewOptions = useMemo(() => ({ about: showAbout, profileCards: true }), [showAbout]);
async function followAll() {
if (publisher) {
const newFollows = dedupe([...pubkeys, ...follows.item]);
const ev = await publisher.contactList(newFollows.map(a => ["p", a]));
setFollows(id, newFollows, ev.created_at);
await system.BroadcastEvent(ev);
}
await control.addFollow(pubkeys);
}
return (