Repository navigation
Expand file tree
/
Copy pathReportModal.tsx
More file actions
365 lines (324 loc) · 10 KB
/
Copy pathReportModal.tsx
File metadata and controls
365 lines (324 loc) · 10 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"use client";
import React, { useRef, useState, useCallback } from "react";
import { XMarkIcon, FlagIcon } from "@heroicons/react/20/solid";
import { toast } from "sonner";
import { signIn, useSession } from "next-auth/react";
import { api } from "@/server/trpc/react";
import { useSearchParams, usePathname, useRouter } from "next/navigation";
import {
Description,
Dialog,
DialogPanel,
DialogTitle,
} from "@headlessui/react";
// Report types for different content
export type ReportType = "post" | "comment" | "discussion" | "article";
type ReportItem = {
type: ReportType;
id: string | number;
title?: string;
comment?: string;
};
// URL-aware hook for report modal state
export function useReportModal() {
const searchParams = useSearchParams();
const pathname = usePathname();
const router = useRouter();
const reportParam = searchParams?.get("report");
// Parse report param format: type_id (e.g., "post_abc123", "article_42")
const parseReportParam = useCallback(
(
param: string | null,
): { type: ReportType; id: string | number } | null => {
if (!param) return null;
const [type, ...idParts] = param.split("_");
const id = idParts.join("_");
if (!type || !id) return null;
const validTypes: ReportType[] = [
"post",
"comment",
"discussion",
"article",
];
if (!validTypes.includes(type as ReportType)) return null;
// Articles use numeric IDs, posts use string IDs
const parsedId =
type === "article" || type === "comment" || type === "discussion"
? parseInt(id, 10)
: id;
if (typeof parsedId === "number" && isNaN(parsedId)) return null;
return { type: type as ReportType, id: parsedId };
},
[],
);
const reportData = parseReportParam(reportParam);
const openReport = useCallback(
(type: ReportType, id: string | number) => {
const params = new URLSearchParams(searchParams?.toString() || "");
params.set("report", `${type}_${id}`);
router.push(`${pathname}?${params.toString()}`, { scroll: false });
},
[searchParams, pathname, router],
);
const closeReport = useCallback(() => {
const params = new URLSearchParams(searchParams?.toString() || "");
params.delete("report");
const newUrl = params.toString()
? `${pathname}?${params.toString()}`
: pathname;
router.push(newUrl, { scroll: false });
}, [searchParams, pathname, router]);
return {
isOpen: !!reportData,
reportData,
openReport,
closeReport,
};
}
// Global modal component that reads from URL
export function ReportModalProvider() {
const { data: session } = useSession();
const { isOpen, reportData, closeReport: closeReportUrl } = useReportModal();
const [reportBody, setReportBody] = useState("");
const [loading, setLoading] = useState(false);
const textAreaRef = useRef<HTMLTextAreaElement>(null);
// Wrap closeReport to also reset form state
const closeReport = useCallback(() => {
closeReportUrl();
setReportBody("");
setLoading(false);
}, [closeReportUrl]);
const { mutate: sendReport } = api.report.send.useMutation({
onSuccess: () => {
toast.success("Report submitted successfully");
closeReport();
},
onError: () => {
toast.error("Failed to submit report. Please try again.");
},
onSettled: () => {
setLoading(false);
},
});
// New mutation for storing reports in database (shows in admin dashboard)
const { mutate: createReport } = api.report.create.useMutation({
onSuccess: () => {
toast.success("Report submitted successfully");
closeReport();
},
onError: (error) => {
if (error.message === "You have already reported this item") {
toast.error("You have already reported this item");
} else {
toast.error("Failed to submit report. Please try again.");
}
},
onSettled: () => {
setLoading(false);
},
});
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (loading || !reportData) return;
if (!session) {
signIn();
return;
}
setLoading(true);
const { type, id } = reportData;
if (type === "post") {
// Store post flags in the DB queue (shows in admin dashboard) instead of
// the legacy email-only path. Never auto-hides — a human acts on it.
createReport({
postId: id as string,
reason: "OTHER",
details: reportBody || undefined,
});
} else if (type === "discussion") {
// Use new create mutation for discussions - stores in DB and shows in admin dashboard
createReport({
discussionId: id as number,
reason: "OTHER",
details: reportBody || undefined,
});
} else if (type === "comment") {
// Legacy comments still use send (email)
sendReport({
type: "comment",
body: reportBody,
id: id as number,
});
} else if (type === "article") {
sendReport({
type: "article",
body: reportBody,
id: id as number,
});
}
};
if (!isOpen || !reportData) return null;
const { type } = reportData;
const contentLabel =
type === "post"
? "article"
: type === "article"
? "feed article"
: "comment";
return (
<Dialog
open={isOpen}
onClose={closeReport}
initialFocus={textAreaRef}
className="relative z-50"
>
<div className="fixed inset-0 bg-canvas/70" aria-hidden="true" />
<div className="fixed inset-0 flex items-center justify-center p-4">
<DialogPanel className="relative w-full max-w-md rounded-lg border border-hairline bg-elevated p-6 shadow-xl">
<button
onClick={closeReport}
aria-label="Close modal"
className="absolute right-4 top-4 rounded-full p-1 text-faint hover:bg-hover hover:text-fg"
>
<XMarkIcon className="h-6 w-6" />
</button>
<DialogTitle className="font-display text-xl font-bold text-fg">
Report {contentLabel}
</DialogTitle>
<Description as="div" className="mt-4">
<p className="text-sm text-muted">
Is something inappropriate? Help us keep the community safe by
reporting content that violates our guidelines.
</p>
</Description>
<form onSubmit={handleSubmit} className="mt-6">
<label
htmlFor="report-comment"
className="block text-sm font-medium text-fg"
>
What's the issue? (optional)
</label>
<textarea
maxLength={500}
id="report-comment"
rows={4}
placeholder="Describe the issue..."
onChange={(e) => setReportBody(e.target.value)}
value={reportBody}
className="mt-2 block w-full rounded-md border border-hairline bg-inset px-3 py-2 text-fg placeholder:text-faint focus:border-accent focus:outline-none focus:ring-1 focus:ring-accent"
ref={textAreaRef}
/>
<div className="mt-6 flex justify-end gap-3">
<button
type="button"
onClick={closeReport}
className="rounded-md px-4 py-2 text-sm font-medium text-muted hover:bg-hover"
>
Cancel
</button>
<button
disabled={loading}
className="rounded-md bg-accent px-4 py-2 text-sm font-medium text-on-accent hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
type="submit"
>
{loading ? "Submitting..." : "Submit Report"}
</button>
</div>
</form>
</DialogPanel>
</div>
</Dialog>
);
}
// Button component to trigger report
type ReportButtonProps = {
type: ReportType;
id: string | number;
variant?: "icon" | "text" | "menu";
className?: string;
};
export function ReportButton({
type,
id,
variant = "icon",
className = "",
}: ReportButtonProps) {
const { data: session } = useSession();
const { openReport } = useReportModal();
const handleClick = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (!session) {
signIn();
return;
}
openReport(type, id);
};
if (variant === "icon") {
return (
<button
aria-label="Report content"
onClick={handleClick}
className={`rounded-full p-1.5 text-faint hover:bg-hover hover:text-fg ${className}`}
>
<FlagIcon className="h-5 w-5" />
</button>
);
}
if (variant === "menu") {
return (
<button
onClick={handleClick}
className={`flex w-full items-center gap-2 px-4 py-2 text-left text-sm text-fg hover:bg-hover ${className}`}
>
<FlagIcon className="h-4 w-4" />
Report
</button>
);
}
return (
<button
onClick={handleClick}
className={`flex items-center gap-2 text-sm text-muted hover:text-fg ${className}`}
>
<FlagIcon className="h-4 w-4" />
Report
</button>
);
}
// Legacy export for backwards compatibility
export const ReportModal = ({
type,
id,
title,
comment,
}: ReportItem & { title?: string; comment?: string }) => {
const { data: session } = useSession();
const { openReport } = useReportModal();
const isCommentLike = type === "comment" || type === "discussion";
const handleClick = () => {
if (!session) {
signIn();
return;
}
openReport(type, id);
};
if (isCommentLike) {
return (
<button
aria-label="Flag comment"
onClick={handleClick}
className="mr-4 flex rounded-full p-1.5 hover:bg-hover"
>
<FlagIcon className="h-5 fill-faint" />
</button>
);
}
return (
<button
onClick={handleClick}
className="w-full rounded px-2 py-1 text-left text-fg hover:bg-hover"
>
Report Article
</button>
);
};