From 7bac4927e8895719a91011da9a2b997579238145 Mon Sep 17 00:00:00 2001
From: polwex <polwex@sortug.com>
Date: Thu, 18 Sep 2025 08:26:30 +0700
Subject: damn my trill codebase was really something

---
 front/src/components/post/Loader.tsx | 124 ++++++++++++++++-------------------
 1 file changed, 56 insertions(+), 68 deletions(-)

(limited to 'front/src/components/post/Loader.tsx')

diff --git a/front/src/components/post/Loader.tsx b/front/src/components/post/Loader.tsx
index a23bea1..e45e01a 100644
--- a/front/src/components/post/Loader.tsx
+++ b/front/src/components/post/Loader.tsx
@@ -2,23 +2,30 @@ 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 { PostID } from "@/types/trill";
+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";
 
-function PostData(props: {
+type Props = {
   host: Ship;
   id: PostID;
+  nest?: number; // nested quotes
   rter?: Ship;
   rtat?: number;
   rtid?: PostID;
-  nest?: number; // nested quotes
   className?: string;
-}) {
-  const { api } = useLocalState((s) => ({ api: s.api }));
+};
+function PostData(props: Props) {
+  const { api } = useLocalState((s) => ({
+    api: s.api,
+  }));
+
   const { host, id, nest } = props;
-  const [enest, setEnest] = useState(nest);
+
+  const [enest, setEnest] = useState(nest || 0);
   useEffect(() => {
-    setEnest(nest);
+    if (nest) setEnest(nest);
   }, [nest]);
 
   return function (Component: React.ElementType) {
@@ -39,61 +46,52 @@ function PostData(props: {
       dataRef.current = data;
     }, [data]);
 
-    async function fetchNode(): Promise<any> {
-      const res = await api!.scryPost(host, id, null, null);
-      if ("fpost" in res) return res;
+    async function fetchNode(): AsyncRes<FullNode> {
+      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 existing = queryClient.getQueryData(["trill-thread", host, id]);
-        const existingData = existing || data;
-        if ("bugen" in res) {
-          // we peek for the actual node
-          peekTheNode();
-          // if we have a cache we don't invalidate it
-          if (existingData && "fpost" in existingData) return existingData;
-          // if we don't have a cache then we show the loading screen
-          else return res;
-        }
-        if ("no-node" in res) {
-          if (existingData && "fpost" in existingData) return existingData;
-          else return res;
-        }
+        const res2 = await api!.peekThread(host, id);
+        return res2;
       }
     }
-    function peekTheNode() {
-      let timer;
-      peekNode({ ship: host, id });
-      timer = setTimeout(() => {
-        const gotPost = dataRef.current && "fpost" in dataRef.current;
-        setDead(!gotPost);
-        // clearTimeout(timer);
-      }, 10_000);
+    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 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]);
+    // 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();
+      // e.stopPropagation();
+      // setDead(false);
+      // peekTheNode();
     }
     if (enest > 3)
       return (
@@ -122,24 +120,14 @@ function PostData(props: {
               {host} denied you access to this post
             </p>
           </div>
-        ) : "no-node" in data || "bucun" in data ? (
+        ) : "error" in data ? (
           <div className={props.className}>
             <p className="x-center not-found">Post not found</p>
-          </div>
-        ) : "bugen" in data ? (
-          <div className={props.className}>
-            <div className="x-center not-found">
-              <p className="x-center">Post not found, requesting...</p>
-              <img src={spinner} className="x-center s-100" alt="" />
-            </div>
-          </div>
-        ) : "fpost" in data && data.fpost.contents === null ? (
-          <div className={props.className}>
-            <p className="x-center not-found">Post deleted</p>
+            <p className="x-center not-found">{data.error}</p>
           </div>
         ) : (
           <Component
-            data={data.fpost}
+            data={toFlat(data.ok)}
             refetch={refetch}
             {...props}
             nest={enest}
-- 
cgit v1.2.3

