refactor: use UserState
This commit is contained in:
@ -1,54 +1,30 @@
|
||||
import { useContext, useMemo } from "react";
|
||||
import { FormattedMessage } from "react-intl";
|
||||
import { SnortContext } from "@snort/system-react";
|
||||
|
||||
import { useLogin } from "@/hooks/login";
|
||||
import { Login } from "@/login";
|
||||
import { MUTED } from "@/const";
|
||||
import { DefaultButton } from "./buttons";
|
||||
import { NostrLink } from "@snort/system";
|
||||
|
||||
export function useMute(pubkey: string) {
|
||||
const system = useContext(SnortContext);
|
||||
const login = useLogin();
|
||||
const { tags, content } = login?.muted ?? { tags: [] };
|
||||
const muted = useMemo(() => tags.filter(t => t.at(0) === "p"), [tags]);
|
||||
const isMuted = useMemo(() => muted.find(t => t.at(1) === pubkey), [pubkey, muted]);
|
||||
const link = NostrLink.publicKey(pubkey);
|
||||
|
||||
async function unmute() {
|
||||
const pub = login?.publisher();
|
||||
if (pub) {
|
||||
const newMuted = tags.filter(t => t.at(1) !== pubkey);
|
||||
const ev = await pub.generic(eb => {
|
||||
eb.kind(MUTED).content(content ?? "");
|
||||
for (const t of newMuted) {
|
||||
eb.tag(t);
|
||||
}
|
||||
return eb;
|
||||
});
|
||||
console.debug(ev);
|
||||
await system.BroadcastEvent(ev);
|
||||
Login.setMuted(newMuted, content ?? "", ev.created_at);
|
||||
}
|
||||
await login?.state?.unmute(link, true);
|
||||
}
|
||||
|
||||
async function mute() {
|
||||
const pub = login?.publisher();
|
||||
if (pub) {
|
||||
const newMuted = [...tags, ["p", pubkey]];
|
||||
const ev = await pub.generic(eb => {
|
||||
eb.kind(MUTED).content(content ?? "");
|
||||
for (const tag of newMuted) {
|
||||
eb.tag(tag);
|
||||
}
|
||||
return eb;
|
||||
});
|
||||
console.debug(ev);
|
||||
await system.BroadcastEvent(ev);
|
||||
Login.setMuted(newMuted, content ?? "", ev.created_at);
|
||||
try {
|
||||
await login?.state?.mute(link, true);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
return { isMuted, mute, unmute };
|
||||
return {
|
||||
isMuted: login?.state?.muted.some(a => a.equals(link)) ?? false,
|
||||
mute,
|
||||
unmute,
|
||||
};
|
||||
}
|
||||
|
||||
export function LoggedInMuteButton({ pubkey }: { pubkey: string }) {
|
||||
|
Reference in New Issue
Block a user