summaryrefslogtreecommitdiff
path: root/front/src/components/post/Loader.tsx
blob: f3c47152d9fdb71541e975b6004f5af6174162c0 (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
149
150
151
152
153
154
155
156
157
158
159
160
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 { Ship } from "@/types/urbit";

function PostData(props: {
  host: Ship;
  id: PostID;
  rter?: Ship;
  rtat?: number;
  rtid?: PostID;
  nest?: number; // nested quotes
  className?: string;
}) {
  const { api } = useLocalState();
  const { host, id, nest } = props;
  const [enest, setEnest] = useState(nest);
  useEffect(() => {
    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(): Promise<any> {
      const res = await api!.scryPost(host, id, null, null);
      if ("fpost" 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;
        }
      }
    }
    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>
        ) : "no-node" in data || "bucun" 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>
          </div>
        ) : (
          <Component
            data={data.fpost}
            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;