add user page
This commit is contained in:
@@ -11,7 +11,9 @@ export function useEvent(id: string) {
|
||||
async () => {
|
||||
const result = await getNoteByID(id);
|
||||
if (result) {
|
||||
result["content"] = parser(result);
|
||||
if (result.kind === 1 || result.kind === 1063) {
|
||||
result["content"] = parser(result);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
const event = await ndk.fetchEvent(id);
|
||||
@@ -24,8 +26,10 @@ export function useEvent(id: string) {
|
||||
event.created_at,
|
||||
);
|
||||
event["event_id"] = event.id;
|
||||
// @ts-ignore
|
||||
event["content"] = parser(event);
|
||||
if (event.kind === 1 || event.kind === 1063) {
|
||||
// @ts-ignore
|
||||
event["content"] = parser(event);
|
||||
}
|
||||
return event;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import { useAccount } from "./useAccount";
|
||||
import { RelayContext } from "@shared/relayProvider";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { nip02ToArray } from "@utils/transform";
|
||||
import { useContext } from "react";
|
||||
|
||||
export function useFollows() {
|
||||
const ndk = useContext(RelayContext);
|
||||
const { account } = useAccount();
|
||||
const { status, data: follows } = useQuery(
|
||||
["follows", account.pubkey],
|
||||
async () => {
|
||||
const res = await ndk.fetchEvents({
|
||||
kinds: [3],
|
||||
authors: [account.pubkey],
|
||||
});
|
||||
const latest = [...res].slice(-1)[0];
|
||||
const list = nip02ToArray(latest.tags);
|
||||
return list;
|
||||
},
|
||||
{
|
||||
enabled: account ? true : false,
|
||||
},
|
||||
);
|
||||
|
||||
return { status, follows };
|
||||
}
|
||||
@@ -23,11 +23,13 @@ export function useProfile(id: string) {
|
||||
const current = Math.floor(Date.now() / 1000);
|
||||
const result = await getPleb(npub);
|
||||
|
||||
if (result && result.created_at + 86400 > current) {
|
||||
if (result && parseInt(result.created_at) + 86400 >= current) {
|
||||
console.log("cache", result);
|
||||
return result;
|
||||
} else {
|
||||
const user = ndk.getUser({ npub });
|
||||
await user.fetchProfile();
|
||||
console.log("new", user);
|
||||
await createPleb(id, user.profile);
|
||||
|
||||
return user.profile;
|
||||
|
||||
65
src/utils/hooks/useSocial.tsx
Normal file
65
src/utils/hooks/useSocial.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
import { useAccount } from "./useAccount";
|
||||
import { usePublish } from "@libs/ndk";
|
||||
import { RelayContext } from "@shared/relayProvider";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { nip02ToArray } from "@utils/transform";
|
||||
import { useContext } from "react";
|
||||
|
||||
export function useSocial() {
|
||||
const ndk = useContext(RelayContext);
|
||||
const queryClient = useQueryClient();
|
||||
const publish = usePublish();
|
||||
|
||||
const { account } = useAccount();
|
||||
const { status, data: userFollows } = useQuery(
|
||||
["userFollows", account.pubkey],
|
||||
async () => {
|
||||
const res = await ndk.fetchEvents({
|
||||
kinds: [3],
|
||||
authors: [account.pubkey],
|
||||
});
|
||||
const latest = [...res].slice(-1)[0];
|
||||
const list = nip02ToArray(latest.tags);
|
||||
return list;
|
||||
},
|
||||
{
|
||||
enabled: account ? true : false,
|
||||
},
|
||||
);
|
||||
|
||||
const unfollow = (pubkey: string) => {
|
||||
const followsAsSet = new Set(userFollows);
|
||||
followsAsSet.delete(pubkey);
|
||||
|
||||
const tags = [];
|
||||
followsAsSet.forEach((item) => {
|
||||
tags.push(["p", item]);
|
||||
});
|
||||
|
||||
// publish event
|
||||
publish({ content: "", kind: 3, tags: tags });
|
||||
// invalid cache
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["userFollows", account.pubkey],
|
||||
});
|
||||
};
|
||||
|
||||
const follow = (pubkey: string) => {
|
||||
const followsAsSet = new Set(userFollows);
|
||||
followsAsSet.add(pubkey);
|
||||
|
||||
const tags = [];
|
||||
followsAsSet.forEach((item) => {
|
||||
tags.push(["p", item]);
|
||||
});
|
||||
|
||||
// publish event
|
||||
publish({ content: "", kind: 3, tags: tags });
|
||||
// invalid cache
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["userFollows", account.pubkey],
|
||||
});
|
||||
};
|
||||
|
||||
return { status, userFollows, follow, unfollow };
|
||||
}
|
||||
Reference in New Issue
Block a user