Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | 1x 1x 1x 47x 47x 47x 47x 47x 47x 91x 91x 47x 1x 47x 47x 1x 1x 47x 2x 1x 47x 2x 2x 47x 3x 47x 41x 34x 7x 3x 1x 7x 7x 7x 7x 47x 46x 3x 43x 47x 47x 47x 1x 23x 2x 2x 2x 1x | import React, { useEffect, useRef, useState } from "react";
import { Button, Spinner } from "react-bootstrap";
import { useParams, useNavigate } from "react-router";
import { useInfiniteQuery } from "react-query";
import axios from "axios";
import { toast } from "react-toastify";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import ChatMessageDisplay from "main/components/Chat/ChatMessageDisplay";
import ChatMessageCreate from "main/components/Chat/ChatMessageCreate";
import ChatCooldownModal from "main/components/Chat/ChatCooldownModal";
import { useBackendMutation } from "main/utils/useBackend";
import { useBackend } from "main/utils/useBackend";
const PAGE_SIZE = 25;
const REFRESH_RATE = 2000;
const ChatHistoryPage = ({ readOnly = false, isAdmin = false }) => {
const { gameId } = useParams();
const navigate = useNavigate();
const loadMoreRef = useRef(null);
const { data: farmerList } = useBackend(
[`/api/farmer/game/all?gameId=${gameId}`],
{
method: "GET",
url: "/api/farmer/game/all",
params: {
gameId: gameId,
},
},
[],
{ refetchInterval: REFRESH_RATE, enabled: !!gameId },
);
const hasValidFarmer = Array.isArray(farmerList);
const userIdToUsername = hasValidFarmer
? farmerList.reduce((acc, user) => {
acc[user.userId] = user.username || "";
return acc;
}, {})
: {};
const deleteMutation = useBackendMutation(
(id) => ({
url: "/api/chat/hide",
method: "PUT",
params: { chatMessageId: id },
}),
{
onSuccess: () => {
// React Query will refetch automatically if keys match
},
},
[`/api/chat/admin/get?gameId=${gameId}`], // IMPORTANT
);
const [cooldownTarget, setCooldownTarget] = useState(null);
const cooldownMutation = useBackendMutation(
({ userId, seconds }) => ({
url: "/api/admin/users/chatCooldown",
method: "POST",
params: { userId, seconds },
}),
{ onSuccess: (data) => toast.success(data.message) },
);
const handleDelete = (id) => {
if (window.confirm("Delete this message?")) {
deleteMutation.mutate(id);
}
};
const fetchChatPage = async ({ pageParam = 0 }) => {
const response = await axios.get(
isAdmin ? "/api/chat/admin/get" : "/api/chat/get",
{
params: {
gameId,
page: pageParam,
size: PAGE_SIZE,
},
},
);
return response.data;
};
const {
data,
status,
fetchNextPage,
hasNextPage,
isFetching,
isFetchingNextPage,
} = useInfiniteQuery(["chatHistory", gameId], fetchChatPage, {
getNextPageParam: (lastPage, pages) =>
lastPage?.last === false ? pages.length : undefined,
refetchInterval: REFRESH_RATE,
enabled: !!gameId,
});
useEffect(() => {
if (!hasNextPage) {
return;
}
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting && hasNextPage && !isFetchingNextPage) {
fetchNextPage();
}
},
{
threshold: 1.0,
},
);
const currentRef = loadMoreRef.current;
observer.observe(currentRef);
return () => {
observer.unobserve(currentRef);
};
}, [fetchNextPage, hasNextPage, isFetchingNextPage]);
const messages = Array.isArray(data?.pages)
? data.pages.flatMap((page) => {
if (!page || !Array.isArray(page.content)) {
return [];
}
return page.content.map((message) => ({
...message,
username: userIdToUsername[message.userId],
}));
})
: [];
const isInitialLoading = status === "loading";
const showError = status === "error";
return (
<BasicLayout>
<div className="pt-3" data-testid="ChatHistoryPage">
<Button
variant="secondary"
className="mb-3"
onClick={() => navigate(-1)}
data-testid="ChatHistoryPage-back"
>
Back
</Button>
<div className="d-flex flex-column mb-3">
<div className="d-flex justify-content-between align-items-center">
<span className="text-muted">
Game #{gameId} {readOnly && "• Admin Read Only"}
</span>
<span className="text-muted">Game #{gameId}</span>
</div>
{isAdmin && (
<div className="text-muted mt-1" style={{ fontSize: "0.9rem" }}>
<span style={{ color: "red", fontWeight: "bold" }}>🚫</span> =
Deleted Message
</div>
)}
</div>
{!readOnly && (
<div className="mb-4">
<ChatMessageCreate gameId={gameId} />
</div>
)}
<div
style={{
minHeight: "50vh",
maxHeight: "70vh",
overflowY: "auto",
border: "1px solid #dee2e6",
borderRadius: "0.5rem",
padding: "1rem",
backgroundColor: "white",
}}
data-testid="ChatHistoryPage-message-container"
>
{isInitialLoading && (
<div className="text-center my-3">
<Spinner animation="border" role="status" size="sm" /> Loading
messages...
</div>
)}
{showError && (
<div className="text-center text-danger my-3">
Unable to load chat messages.
</div>
)}
{!isInitialLoading && messages.length === 0 && (
<div className="text-center text-muted my-3">
No messages available for this game.
</div>
)}
{messages.map((message) => (
<div
key={message.id}
className="d-flex align-items-start mb-2 border rounded p-2"
style={
message.hidden
? {
backgroundColor: "#fff5f5",
borderLeft: "4px solid red",
}
: {}
}
>
<div className="flex-grow-1">
<div
style={
message.hidden ? { opacity: 0.6, fontStyle: "italic" } : {}
}
>
<ChatMessageDisplay message={message} />
</div>
{message.hidden && (
<div
className="mt-1"
style={{
color: "red",
fontSize: "0.8rem",
fontWeight: "bold",
}}
>
🚫 Message Deleted
</div>
)}
</div>
{isAdmin && (
<button
className="btn btn-warning btn-sm ms-2"
data-testid={`ChatHistoryPage-cooldown-${message.id}`}
onClick={() =>
setCooldownTarget({
userId: message.userId,
username: message.username,
})
}
>
Cooldown
</button>
)}
{isAdmin && !message.hidden && (
<button
className="btn btn-danger btn-sm ms-2"
data-testid={`ChatHistoryPage-delete-${message.id}`}
onClick={() => handleDelete(message.id)}
>
Delete
</button>
)}
</div>
))}
<div
ref={loadMoreRef}
className="text-center text-muted mt-3"
data-testid="ChatHistoryPage-status"
>
{isFetchingNextPage
? "Loading more messages..."
: hasNextPage
? "Scroll to load more messages"
: "[no more messages]"}
</div>
</div>
{isFetching && !isFetchingNextPage && (
<div className="text-center text-muted mt-2">
Updating conversation...
</div>
)}
{cooldownTarget && (
<ChatCooldownModal
username={cooldownTarget.username || "this user"}
onClose={() => setCooldownTarget(null)}
onConfirm={(seconds) =>
cooldownMutation.mutate({
userId: cooldownTarget.userId,
seconds,
})
}
/>
)}
</div>
</BasicLayout>
);
};
export default ChatHistoryPage;
|