add useBlock hook

This commit is contained in:
Ren Amamiya
2023-07-19 12:47:45 +07:00
parent 6307e8d1e4
commit 22c1eaa541
13 changed files with 131 additions and 221 deletions

View File

@@ -1,37 +1,23 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { createBlock } from '@libs/storage';
import { BLOCK_KINDS } from '@stores/constants';
import { useBlock } from '@utils/hooks/useBlock';
import { useProfile } from '@utils/hooks/useProfile';
import { shortenKey } from '@utils/shortenKey';
export function MentionUser({ pubkey }: { pubkey: string }) {
const { add } = useBlock();
const { user } = useProfile(pubkey);
const queryClient = useQueryClient();
const block = useMutation({
mutationFn: (data: { kind: number; title: string; content: string }) => {
return createBlock(data.kind, data.title, data.content);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['blocks'] });
},
});
const openBlock = () => {
block.mutate({
kind: BLOCK_KINDS.user,
title: user?.name || user?.displayNam,
content: pubkey,
});
};
return (
<button
type="button"
onClick={() => openBlock()}
onClick={() =>
add.mutate({
kind: BLOCK_KINDS.user,
title: user?.nip05 || user?.name || user?.displayNam,
content: pubkey,
})
}
className="break-words rounded bg-zinc-800 px-2 py-px text-sm font-normal text-blue-400 no-underline hover:bg-zinc-700 hover:text-blue-500"
>
{'@' + user?.name || user?.displayName || shortenKey(pubkey)}