import useLocalState from "@/state/state"; import Icon from "@/components/Icon"; import spinner from "@/assets/triangles.svg"; import type { FC, FullFeed, FullNode } from "@/types/trill"; import Composer from "@/components/composer/Composer"; import type { UserProfile } from "@/types/nostrill"; import { useEffect, useState } from "react"; import toast from "react-hot-toast"; import { eventsToFF, eventToFn } from "@/logic/trill/helpers"; import { toFlat } from "../post/RP"; import type { NostrEvent } from "@/types/nostr"; import { createCache } from "@/logic/cache"; import Post from "../post/Post"; import Modal from "../modals/Modal"; type Props = { host: string; id: string; feed?: FC; profile?: UserProfile; }; const cache = createCache({ dbName: "nostrill", storeName: "nosted" }); export default function Thread(props: Props) { const { api, composerData, setComposerData, setModal, lastFact } = useLocalState((s) => ({ api: s.api, lastFact: s.lastFact, composerData: s.composerData, setComposerData: s.setComposerData, setModal: s.setModal, })); const { id, feed, profile } = props; const poast = feed?.feed[id]; const host = poast?.author || ""; const [error, setError] = useState(""); // const [data, setData] = useState<{fc: FC, head: Poast}>(() => getCachedData(id)); const [data, setData] = useState(); useEffect(() => { console.log({ composerData }); if (composerData) setModal( { setComposerData(null); }} > , ); }, [composerData]); // useTimeout(() => { // if (!data) setError("Request timed out"); // }, 10_000); useEffect(() => { if (!lastFact) return; if (!("nostr" in lastFact)) return; if (!("thread" in lastFact.nostr)) return; toast.success("thread fetched succesfully, rendering"); cache.set("evs", lastFact.nostr.thread); const nodes = lastFact.nostr.thread.map(eventToFn); const ff = eventsToFF(nodes); setData(ff); }, [lastFact]); useEffect(() => { if (!api) return; const init = async () => { const cached: NostrEvent[] | null = await cache.get("evs"); if (cached) { const nodes = cached.map(eventToFn); const ff = eventsToFF(nodes); setData(ff); } }; init(); }, [id]); async function tryAgain() { if (!api) return; setError(""); api.nostrThread(id); } return ( <>

Thread

~{host} #{id}
{data ? ( <> ) : error ? (

Error Loading Thread

{error}

) : ( <>

Loading Thread...

Loading
)}
); } function Head({ node, profile }: { node: FullNode; profile?: UserProfile }) { return ( <>
); } function Minithread({ ff }: { ff: FullFeed }) { const profiles = useLocalState((s) => s.profiles); const nodes = Object.values(ff); return (
{nodes.map((c) => { const profile = profiles.get(c.author); return (
); })}
); } function Grandchildren({ node }: { node: FullNode }) { return (
); }