add follow/unfollow function
This commit is contained in:
27
src/utils/hooks/useFollows.tsx
Normal file
27
src/utils/hooks/useFollows.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
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 };
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
// `usePageContext` allows us to access `pageContext` in any React component.
|
||||
// See https://vite-plugin-ssr.com/pageContext-anywhere
|
||||
import type { PageContext } from "@renderer/types";
|
||||
|
||||
import { createContext, useContext } from "react";
|
||||
|
||||
const Context = createContext<PageContext>(undefined as any);
|
||||
|
||||
export function PageContextProvider({
|
||||
pageContext,
|
||||
children,
|
||||
}: {
|
||||
pageContext: PageContext;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return <Context.Provider value={pageContext}>{children}</Context.Provider>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line react-refresh/only-export-components
|
||||
export function usePageContext() {
|
||||
const pageContext = useContext(Context);
|
||||
return pageContext;
|
||||
}
|
||||
Reference in New Issue
Block a user