SkateSpots/src/hooks/useUnmuteUser.ts

51 lines
1.9 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useNostr } from '@nostrify/react';
import { useNostrPublish } from '@/hooks/useNostrPublish';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useToast } from '@/hooks/useToast';
import { useTranslation } from '@/contexts/language';
import { onlyVerified } from '@/lib/verifyEvents';
import { removeLocalMute } from '@/lib/localMutes';
import { MUTE_LIST_KIND } from '@/hooks/useMuteList';
/** Remove a pubkey from the viewer's NIP-51 kind-10000 mute list. */
export function useUnmuteUser() {
const { nostr } = useNostr();
const { mutateAsync: publish } = useNostrPublish();
const { user } = useCurrentUser();
const { toast } = useToast();
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (target: string) => {
if (!user) throw new Error(t('walletUi.trust.loginToMute'));
const events = onlyVerified(await nostr.query(
[{ kinds: [MUTE_LIST_KIND], authors: [user.pubkey], limit: 1 }],
{ signal: AbortSignal.timeout(8000) },
));
const existing = (events[0]?.tags ?? [])
.filter(([name]) => name === 'p')
.map(([, value]) => value)
.filter((value) => value && value !== target);
await publish({
kind: MUTE_LIST_KIND,
content: '',
tags: existing.map((pk) => ['p', pk]),
});
},
onSuccess: (_data, target) => {
removeLocalMute(target);
queryClient.invalidateQueries({ queryKey: ['mute-list', user?.pubkey] });
queryClient.invalidateQueries();
toast({ title: t('walletUi.trust.unmuted'), description: t('walletUi.trust.unmutedDesc') });
},
onError: (error) => {
toast({
title: t('walletUi.trust.unmuteFailed'),
description: error instanceof Error ? error.message : undefined,
variant: 'destructive',
});
},
});
}