feat: refactor

This commit is contained in:
2024-01-12 20:32:45 +07:00
parent 67c6177291
commit 0487b8a801
63 changed files with 345 additions and 777 deletions

View File

@@ -0,0 +1,10 @@
import { useContext } from "react";
import { LumeContext } from "../context";
export const useArk = () => {
const context = useContext(LumeContext);
if (context === undefined) {
throw new Error("Please import Ark Provider to use useArk() hook");
}
return context;
};

View File

@@ -1,21 +1,24 @@
import { useQuery } from '@tanstack/react-query';
import { useArk } from '../provider';
import { useQuery } from "@tanstack/react-query";
import { useArk } from "./useArk";
export function useEvent(id: string) {
const ark = useArk();
const { status, isLoading, isError, data } = useQuery({
queryKey: ['event', id],
queryFn: async () => {
const event = await ark.getEventById({ id });
if (!event)
throw new Error(`Cannot get event with ${id}, will be retry after 10 seconds`);
return event;
},
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
retry: 2,
});
const ark = useArk();
const { status, isLoading, isError, data } = useQuery({
queryKey: ["event", id],
queryFn: async () => {
const event = await ark.getEventById({ id });
if (!event)
throw new Error(
`Cannot get event with ${id}, will be retry after 10 seconds`,
);
return event;
},
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
staleTime: Infinity,
retry: 2,
});
return { status, isLoading, isError, data };
return { status, isLoading, isError, data };
}

View File

@@ -1,5 +1,5 @@
import { useQuery } from "@tanstack/react-query";
import { useArk } from "../provider";
import { useArk } from "./useArk";
export function useProfile(pubkey: string) {
const ark = useArk();
@@ -20,6 +20,7 @@ export function useProfile(pubkey: string) {
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
staleTime: Infinity,
retry: 2,
});

View File

@@ -1,6 +1,7 @@
import { useStorage } from "@lume/storage";
import { NDKKind, NDKRelayUrl, NDKTag } from "@nostr-dev-kit/ndk";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useArk, useStorage } from "../provider";
import { useArk } from "./useArk";
export function useRelay() {
const ark = useArk();
@@ -14,13 +15,13 @@ export function useRelay() {
) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({
queryKey: ["relays", storage.account.pubkey],
queryKey: ["relays", ark.account.pubkey],
});
// Snapshot the previous value
const prevRelays: NDKTag[] = queryClient.getQueryData([
"relays",
storage.account.pubkey,
ark.account.pubkey,
]);
// create new relay list if not exist
@@ -42,7 +43,7 @@ export function useRelay() {
// Optimistically update to the new value
queryClient.setQueryData(
["relays", storage.account.pubkey],
["relays", ark.account.pubkey],
(prev: NDKTag[]) => [...prev, ["r", relay, purpose ?? ""]],
);
@@ -51,7 +52,7 @@ export function useRelay() {
},
onSettled: () => {
queryClient.invalidateQueries({
queryKey: ["relays", storage.account.pubkey],
queryKey: ["relays", ark.account.pubkey],
});
},
});
@@ -60,13 +61,13 @@ export function useRelay() {
mutationFn: async (relay: NDKRelayUrl) => {
// Cancel any outgoing refetches
await queryClient.cancelQueries({
queryKey: ["relays", storage.account.pubkey],
queryKey: ["relays", ark.account.pubkey],
});
// Snapshot the previous value
const prevRelays: NDKTag[] = queryClient.getQueryData([
"relays",
storage.account.pubkey,
ark.account.pubkey,
]);
if (!prevRelays) return;
@@ -80,14 +81,14 @@ export function useRelay() {
});
// Optimistically update to the new value
queryClient.setQueryData(["relays", storage.account.pubkey], prevRelays);
queryClient.setQueryData(["relays", ark.account.pubkey], prevRelays);
// Return a context object with the snapshotted value
return { prevRelays };
},
onSettled: () => {
queryClient.invalidateQueries({
queryKey: ["relays", storage.account.pubkey],
queryKey: ["relays", ark.account.pubkey],
});
},
});