feat: add discover newsfeeds and interests

This commit is contained in:
2024-10-31 13:34:25 +07:00
parent 043cabfd4e
commit c201b5816c
42 changed files with 579 additions and 41 deletions

View File

@@ -2,55 +2,61 @@ import { commands } from "@/commands.gen";
import type { NostrEvent } from "@/types";
import { useQuery } from "@tanstack/react-query";
import { nip19 } from "nostr-tools";
import { useMemo } from "react";
import { LumeEvent } from "./event";
export function useEvent(id: string, repost?: string) {
const hex = useMemo(() => {
try {
const normalized = id.replace("nostr:", "").replace(/[^\w\s]/gi, "");
const decoded = nip19.decode(normalized);
switch (decoded.type) {
case "note":
return decoded.data;
case "nevent":
return decoded.data.id;
default:
return normalized;
}
} catch {
return id;
}
}, [id]);
const { isLoading, isError, error, data } = useQuery({
queryKey: ["ids", "event", id],
queryFn: async () => {
try {
if (repost?.length) {
const nostrEvent: NostrEvent = JSON.parse(repost);
const res = await commands.getMetaFromEvent(nostrEvent.content);
if (res.status === "ok") {
nostrEvent.meta = res.data;
}
return new LumeEvent(nostrEvent);
}
let normalizedId = id.replace("nostr:", "").replace(/[^\w\s]/gi, "");
if (normalizedId.startsWith("nevent")) {
const decoded = nip19.decode(normalizedId);
if (decoded.type === "nevent") {
normalizedId = decoded.data.id;
}
}
const res = await commands.getEvent(normalizedId);
if (repost?.length) {
const nostrEvent: NostrEvent = JSON.parse(repost);
const res = await commands.getMetaFromEvent(nostrEvent.content);
if (res.status === "ok") {
const data = res.data;
const raw: NostrEvent = JSON.parse(data.raw);
if (data.parsed) {
raw.meta = data.parsed;
}
return new LumeEvent(raw);
} else {
throw new Error(res.error);
nostrEvent.meta = res.data;
}
} catch (e) {
throw new Error(String(e));
return new LumeEvent(nostrEvent);
}
const res = await commands.getEvent(hex);
if (res.status === "ok") {
const data = res.data;
const raw: NostrEvent = JSON.parse(data.raw);
if (data.parsed) {
raw.meta = data.parsed;
}
return new LumeEvent(raw);
} else {
throw new Error(res.error);
}
},
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
enabled: !!hex,
});
return { isLoading, isError, error, data };