import { useQuery, useQueryClient } from "@tanstack/react-query"; import spinner from "@/assets/triangles.svg"; import { useEffect, useRef, useState } from "react"; import useLocalState from "@/state/state"; import type { FullNode, PostID } from "@/types/trill"; import type { Ship } from "@/types/urbit"; import type { AsyncRes } from "@/types/ui"; import { toFlat } from "@/logic/trill/helpers"; type Props = { host: Ship; id: PostID; nest?: number; // nested quotes rter?: Ship; rtat?: number; rtid?: PostID; className?: string; }; function PostData(props: Props) { const { api } = useLocalState((s) => ({ api: s.api, })); const { host, id, nest } = props; const [enest, setEnest] = useState(nest || 0); useEffect(() => { if (nest) setEnest(nest); }, [nest]); return function (Component: React.ElementType) { // const [showNested, setShowNested] = useState(nest <= 3); const handleShowNested = (e: React.MouseEvent) => { e.stopPropagation(); setEnest(enest! - 3); }; const [dead, setDead] = useState(false); const [denied, setDenied] = useState(false); const { isLoading, isError, data, refetch } = useQuery({ queryKey: ["trill-thread", host, id], queryFn: fetchNode, }); const queryClient = useQueryClient(); const dataRef = useRef(data); useEffect(() => { dataRef.current = data; }, [data]); async function fetchNode(): AsyncRes { let error = ""; const res = await api!.scryThread(host, id); console.log("scry res", res); if ("error" in res) error = res.error; if ("ok" in res) return res; else { const res2 = await api!.peekThread(host, id); return res2; } } async function peekTheNode() { // let timer; // peekNode({ ship: host, id }); // timer = setTimeout(() => { // const gotPost = dataRef.current && "fpost" in dataRef.current; // setDead(!gotPost); // // clearTimeout(timer); // }, 10_000); } // useEffect(() => { // const path = `${host}/${id}`; // if (path in peekedPosts) { // queryClient.setQueryData(["trill-thread", host, id], { // fpost: peekedPosts[path], // }); // } else if (path in deniedPosts) { // setDenied(true); // } // }, [peekedPosts]); // useEffect(() => { // const path = `${host}/${id}`; // if (path in deniedPosts) setDenied(true); // }, [deniedPosts]); // useEffect(() => { // const l = lastThread; // if (l && l.thread == id) { // queryClient.setQueryData(["trill-thread", host, id], { fpost: l }); // } // }, [lastThread]); function retryPeek(e: React.MouseEvent) { // e.stopPropagation(); // setDead(false); // peekTheNode(); } if (enest > 3) return (
); else return data ? ( dead ? (

{host} did not respond

) : denied ? (

{host} denied you access to this post

) : "error" in data ? (

Post not found

{data.error}

) : ( ) ) : // no data isLoading || isError ? (
) : (

...

); }; } export default PostData;