-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathApp.tsx
More file actions
199 lines (189 loc) · 5.67 KB
/
App.tsx
File metadata and controls
199 lines (189 loc) · 5.67 KB
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
import { useQuery, useZero } from "@rocicorp/zero/react";
import Cookies from "js-cookie";
import { useState } from "react";
import { formatDate } from "./date.ts";
import { randInt } from "./rand.ts";
import { RepeatButton } from "./repeat-button.tsx";
import { mutators } from "./mutators.ts";
import { queries } from "./queries.ts";
import { randomMessage } from "./test-data.ts";
function App() {
const z = useZero();
const [users] = useQuery(queries.users.all());
const [mediums] = useQuery(queries.mediums.all());
const [filterUser, setFilterUser] = useState("");
const [filterText, setFilterText] = useState("");
const [allMessages] = useQuery(queries.messages.feed({}));
const [filteredMessages] = useQuery(
queries.messages.feed({
senderID: filterUser || undefined,
search: filterText || undefined,
})
);
const hasFilters = filterUser || filterText;
// If initial sync hasn't completed, these can be empty.
if (!users.length || !mediums.length) {
return null;
}
const viewer = users.find((user) => user.id === z.userID);
return (
<>
<div className="controls">
<div>
<RepeatButton
onTrigger={() => {
z.mutate(mutators.message.insert(randomMessage(users, mediums)));
}}
>
Add Messages
</RepeatButton>
<RepeatButton
onTrigger={(e) => {
if (!viewer && !e.shiftKey) {
alert(
"You must be logged in to delete. Hold shift to try anyway."
);
return false;
}
if (allMessages.length === 0) {
return false;
}
const index = randInt(allMessages.length);
z.mutate(mutators.message.delete({ id: allMessages[index].id }));
return true;
}}
>
Remove Messages
</RepeatButton>
<em>(hold down buttons to repeat)</em>
</div>
<div
style={{
justifyContent: "end",
}}
>
{viewer && `Logged in as ${viewer.name}`}
{viewer ? (
<button
onMouseDown={() => {
Cookies.remove("jwt");
location.reload();
}}
>
Logout
</button>
) : (
<button
onMouseDown={() => {
fetch("/api/login")
.then(() => {
location.reload();
})
.catch((error) => {
alert(`Failed to login: ${error.message}`);
});
}}
>
Login
</button>
)}
</div>
</div>
<div className="controls">
<div>
From:
<select
onChange={(e) => setFilterUser(e.target.value)}
style={{ flex: 1 }}
>
<option key={""} value="">
Sender
</option>
{users.map((user) => (
<option key={user.id} value={user.id}>
{user.name}
</option>
))}
</select>
</div>
<div>
Contains:
<input
type="text"
placeholder="message"
onChange={(e) => setFilterText(e.target.value)}
style={{ flex: 1 }}
/>
</div>
</div>
<div className="controls">
<em>
{!hasFilters ? (
<>Showing all {filteredMessages.length} messages</>
) : (
<>
Showing {filteredMessages.length} of {allMessages.length}{" "}
messages. Try opening{" "}
<a href="/" target="_blank">
another tab
</a>{" "}
to see them all!
</>
)}
</em>
</div>
{filteredMessages.length === 0 ? (
<h3>
<em>No posts found 😢</em>
</h3>
) : (
<table border={1} cellSpacing={0} cellPadding={6} width="100%">
<thead>
<tr>
<th>Sender</th>
<th>Medium</th>
<th>Message</th>
<th>Labels</th>
<th>Sent</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
{filteredMessages.map((message) => (
<tr key={message.id}>
<td align="left">{message.sender?.name}</td>
<td align="left">{message.medium?.name}</td>
<td align="left">{message.body}</td>
<td align="left">{message.labels.join(", ")}</td>
<td align="right">{formatDate(message.timestamp)}</td>
<td
onMouseDown={(e) => {
if (message.senderID !== z.userID && !e.shiftKey) {
alert(
"You aren't logged in as the sender of this message. Editing won't be permitted. Hold the shift key to try anyway."
);
return;
}
const body = prompt("Edit message", message.body);
if (body === null) {
return;
}
z.mutate(
mutators.message.update({
id: message.id,
body,
})
);
}}
>
✏️
</td>
</tr>
))}
</tbody>
</table>
)}
</>
);
}
export default App;