chore: clean up

This commit is contained in:
2024-04-03 07:29:46 +07:00
parent 89bb8d88f6
commit 763cb10e85
42 changed files with 104 additions and 532 deletions

View File

@@ -1,4 +0,0 @@
import { createContext } from "react";
import { type Ark } from "./ark";
export const ArkContext = createContext<Ark | null>(undefined);

View File

@@ -1,10 +0,0 @@
import { useContext } from "react";
import { ArkContext } from "../context";
export const useArk = () => {
const context = useContext(ArkContext);
if (context === undefined) {
throw new Error("useArk must be used within an ArkProvider");
}
return context;
};

View File

@@ -1,13 +1,18 @@
import { Event } from "@lume/types";
import { useQuery } from "@tanstack/react-query";
import { useArk } from "./useArk";
import { invoke } from "@tauri-apps/api/core";
export function useEvent(id: string) {
const ark = useArk();
const { isLoading, isError, data } = useQuery({
queryKey: ["event", id],
queryFn: async () => {
try {
const event = await ark.get_event(id);
const eventId: string = id
.replace("nostr:", "")
.split("'")[0]
.split(".")[0];
const cmd: string = await invoke("get_event", { id: eventId });
const event: Event = JSON.parse(cmd);
return event;
} catch (e) {
throw new Error(e);

View File

@@ -1,48 +0,0 @@
import { useInfiniteQuery } from "@tanstack/react-query";
import { useArk } from "./useArk";
const FETCH_LIMIT = 20;
const QUERY_KEY = "local";
const DEDUP = true;
export function useEvents(key: string, account?: string) {
const ark = useArk();
const {
data,
isError,
isLoading,
isRefetching,
isFetchingNextPage,
hasNextPage,
fetchNextPage,
} = useInfiniteQuery({
queryKey: [key, account],
initialPageParam: 0,
queryFn: async ({ pageParam }: { pageParam: number }) => {
const events = await ark.get_events(
QUERY_KEY,
FETCH_LIMIT,
pageParam,
DEDUP,
);
return events;
},
getNextPageParam: (lastPage) => {
const lastEvent = lastPage?.at(-1);
if (!lastEvent) return;
return lastEvent.created_at - 1;
},
select: (data) => data?.pages.flatMap((page) => page),
refetchOnWindowFocus: false,
});
return {
data,
isError,
isLoading,
isRefetching,
isFetchingNextPage,
hasNextPage,
fetchNextPage,
};
}

View File

@@ -1,8 +1,8 @@
import { useQuery } from "@tanstack/react-query";
import { useArk } from "./useArk";
import { Metadata } from "@lume/types";
import { invoke } from "@tauri-apps/api/core";
export function useProfile(pubkey: string) {
const ark = useArk();
const {
isLoading,
isError,
@@ -11,8 +11,14 @@ export function useProfile(pubkey: string) {
queryKey: ["user", pubkey],
queryFn: async () => {
try {
const profile = await ark.get_profile(pubkey);
return profile;
const id = pubkey
.replace("nostr:", "")
.split("'")[0]
.split(".")[0]
.split(",")[0]
.split("?")[0];
const cmd: Metadata = await invoke("get_profile", { id });
return cmd;
} catch (e) {
throw new Error(e);
}

View File

@@ -1,6 +1,4 @@
export * from "./ark";
export * from "./context";
export * from "./hooks/useArk";
export * from "./hooks/useEvent";
export * from "./hooks/useEvents";
export * from "./hooks/useProfile";