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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
import useLocalState from "@/state/state";
import Icon from "@/components/Icon";
import spinner from "@/assets/triangles.svg";
import Post from "@/components/post/Post";
import { extractThread, toFlat } from "@/logic/trill/helpers";
import type { FC, FullNode, Poast } from "@/types/trill";
import Composer from "@/components/composer/Composer";
import type { UserProfile } from "@/types/nostrill";
import type { Ship } from "@/types/urbit";
import { useEffect, useState } from "react";
export default function Thread({
host,
id,
feed,
profile,
}: {
host: Ship;
id: string;
feed?: FC;
profile?: UserProfile;
}) {
const poast = feed?.feed[id];
console.log({ poast });
return (
<>
<div className="thread-header">
<div className="thread-nav">
<button
className="back-btn"
onClick={() => window.history.back()}
title="Go back"
>
<Icon name="reply" size={16} />
<span>Back to Feed</span>
</button>
</div>
<h2>Thread</h2>
<div className="thread-info">
<span className="thread-host">~{host}</span>
<span className="thread-separator">•</span>
<span className="thread-id">#{id}</span>
</div>
</div>
<div id="feed-proper">
{poast && poast.children.length === 0 ? (
<Head poast={poast} profile={profile} />
) : (
<Loader poast={poast} host={host} id={id} profile={profile} />
)}
</div>
</>
);
}
function Loader({
host,
id,
profile,
poast,
}: {
host: Ship;
id: string;
poast?: Poast;
profile?: UserProfile;
}) {
const api = useLocalState((s) => s.api);
const [data, setData] = useState<FullNode>();
const [error, setError] = useState("");
console.log({ data });
async function fetchThread() {
const res = await api!.scryThread(host, id);
if ("error" in res) setError(res.error);
else setData(res.ok);
}
useEffect(() => {
fetchThread();
}, [host, id]);
if (data)
return (
<>
<Head poast={toFlat(data)} profile={profile} />
<div id="thread-children">
<ChildTree node={data} />
</div>
</>
);
if (poast)
return (
<>
<Head poast={poast} profile={profile} />
<div id="thread-children">
<h2>Loading Replies...</h2>
<div className="loading-container">
<img className="x-center" src={spinner} alt="Loading" />
</div>
</div>
</>
);
if (error)
return (
<div className="thread-header">
<h2>Error Loading Thread</h2>
<p className="error">{error}</p>
</div>
);
else
return (
<div id="feed-proper">
<h2>Loading Thread...</h2>
<div className="loading-container">
<img className="x-center" src={spinner} alt="Loading" />
</div>
</div>
);
}
function Head({ poast, profile }: { poast: Poast; profile?: UserProfile }) {
return (
<div id="thread-head">
<Post user={{ urbit: poast.host }} poast={poast} profile={profile} />
</div>
);
}
function ChildTree({ node }: { node: FullNode }) {
const profiles = useLocalState((s) => s.profiles);
const kids = Object.values(node.children || {});
kids.sort((a, b) => b.time - a.time);
return (
<>
{kids.map((k) => {
const profile = profiles.get(k.author);
return (
<div key={k.id} className="minithread">
<Post
user={{ urbit: k.author }}
profile={profile}
poast={toFlat(k)}
/>
<Grandchildren node={k} />
</div>
);
})}
</>
);
function Grandchildren({ node }: { node: FullNode }) {
return (
<div className="tail">
<ChildTree node={node} />
</div>
);
}
}
// function ChildTree({ node }: { node: FullNode }) {
// const { threadChildren, replies } = extractThread(node);
// return (
// <>
// <div id="tail">
// {threadChildren.map((n) => {
// return (
// <Post user={{ urbit: n.author }} key={n.id} poast={toFlat(n)} />
// );
// })}
// </div>
// <div id="replies">
// {replies.map((n) => (
// <ReplyThread key={n.id} node={n} />
// ))}
// </div>
// </>
// );
// }
// function ReplyThread({ node }: { node: FullNode }) {
// // const { threadChildren, replies } = extractThread(node);
// const { replies } = extractThread(node);
// return (
// <div className="trill-reply-thread">
// <div className="head">
// <Post user={{ urbit: node.author }} poast={toFlat(node)} />
// </div>
// <div className="tail">
// {replies.map((r) => (
// <Post key={r.id} user={{ urbit: r.author }} poast={toFlat(r)} />
// ))}
// </div>
// </div>
// );
// }
// function OwnData(props: Props) {
// const { api } = useLocalState((s) => ({
// api: s.api,
// }));
// const { host, id } = props;
// async function fetchThread() {
// return await api!.scryThread(host, id);
// }
// const { isLoading, isError, data, refetch } = useQuery({
// queryKey: ["trill-thread", host, id],
// queryFn: fetchThread,
// });
// return isLoading ? (
// <div className={props.className}>
// <div className="x-center not-found">
// <p className="x-center">Scrying Post, please wait...</p>
// <img src={spinner} className="x-center s-100" alt="" />
// </div>
// </div>
// ) : null;
// }
// function SomeoneElses(props: Props) {
// // const { api, following } = useLocalState((s) => ({
// // api: s.api,
// // following: s.following,
// // }));
// return <div>ho</div>;
// }
|