summaryrefslogtreecommitdiff
path: root/front/src/components/post/Loader.tsx
blob: e45e01aa58857c2032f513a358e1e871524549a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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<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 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 (
        <div className={props.className}>
          <div className="lazy x-center not-found">
            <button className="x-center" onMouseUp={handleShowNested}>
              Load more
            </button>
          </div>
        </div>
      );
    else
      return data ? (
        dead ? (
          <div className={props.className}>
            <div className="no-response x-center not-found">
              <p>{host} did not respond</p>
              <button className="x-center" onMouseUp={retryPeek}>
                Try again
              </button>
            </div>
          </div>
        ) : denied ? (
          <div className={props.className}>
            <p className="x-center not-found">
              {host} denied you access to this post
            </p>
          </div>
        ) : "error" in data ? (
          <div className={props.className}>
            <p className="x-center not-found">Post not found</p>
            <p className="x-center not-found">{data.error}</p>
          </div>
        ) : (
          <Component
            data={toFlat(data.ok)}
            refetch={refetch}
            {...props}
            nest={enest}
          />
        )
      ) : // no data
      isLoading || isError ? (
        <div className={props.className}>
          <img className="x-center post-spinner" src={spinner} alt="" />
        </div>
      ) : (
        <div className={props.className}>
          <p>...</p>
        </div>
      );
  };
}
export default PostData;