File size: 13,738 Bytes
ec4551b | 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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | 'use client';
import React, { FC, useCallback, useMemo, useState } from 'react';
import useSWR from 'swr';
import copy from 'copy-to-clipboard';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { useUser } from '@gitroom/frontend/components/layout/user.context';
import { useToaster } from '@gitroom/react/toaster/toaster';
import { useModals } from '@gitroom/frontend/components/layout/new-modal';
import { Button } from '@gitroom/react/form/button';
import { LoadingComponent } from '@gitroom/frontend/components/layout/loading';
interface ErrorRow {
id: string;
message: string;
body: string;
platform: string;
postId: string;
createdAt: string;
organization: {
id: string;
name: string;
users: { user: { id: string; email: string; name: string | null } }[];
};
post: { id: string; content: string | null };
}
interface ErrorsResponse {
items: ErrorRow[];
total: number;
page: number;
limit: number;
hasMore: boolean;
}
const safeParse = (value: string) => {
try {
return JSON.parse(value);
} catch {
return value;
}
};
const ErrorDetailsModal: FC<{ row: ErrorRow }> = ({ row }) => {
const modal = useModals();
const toaster = useToaster();
const parsedMessage = useMemo(() => safeParse(row.message), [row.message]);
const parsedBody = useMemo(() => safeParse(row.body), [row.body]);
const copyAll = useCallback(() => {
copy(
JSON.stringify(
{ message: parsedMessage, body: parsedBody, meta: row },
null,
2
)
);
toaster.show('Debug code copied to clipboard', 'success');
}, [parsedMessage, parsedBody, row, toaster]);
return (
<div className="rounded-[4px] border border-newTableBorder bg-newBgColorInner px-[16px] pb-[16px] relative w-full max-h-[80vh] overflow-auto">
<div className="sticky top-0 bg-newBgColorInner py-[16px] flex items-center justify-between gap-[12px] z-10 border-b border-newTableBorder mb-[12px]">
<div className="text-[16px] font-[600]">Error Details</div>
<div className="flex gap-[8px] items-center">
<Button onClick={copyAll}>Copy Debug Code</Button>
<button
className="outline-none w-[28px] h-[28px] flex items-center justify-center hover:bg-tableBorder cursor-pointer rounded"
type="button"
onClick={() => modal.closeAll()}
>
<svg
viewBox="0 0 15 15"
fill="none"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
>
<path
d="M11.7816 4.03157C12.0062 3.80702 12.0062 3.44295 11.7816 3.2184C11.5571 2.99385 11.193 2.99385 10.9685 3.2184L7.50005 6.68682L4.03164 3.2184C3.80708 2.99385 3.44301 2.99385 3.21846 3.2184C2.99391 3.44295 2.99391 3.80702 3.21846 4.03157L6.68688 7.49999L3.21846 10.9684C2.99391 11.193 2.99391 11.557 3.21846 11.7816C3.44301 12.0061 3.80708 12.0061 4.03164 11.7816L7.50005 8.31316L10.9685 11.7816C11.193 12.0061 11.5571 12.0061 11.7816 11.7816C12.0062 11.557 12.0062 11.193 11.7816 10.9684L8.31322 7.49999L11.7816 4.03157Z"
fill="currentColor"
fillRule="evenodd"
clipRule="evenodd"
/>
</svg>
</button>
</div>
</div>
<div className="grid grid-cols-2 gap-[12px] text-[13px] mb-[12px]">
<div>
<div className="opacity-60">Platform</div>
<div>{row.platform}</div>
</div>
<div>
<div className="opacity-60">Created</div>
<div>{new Date(row.createdAt).toLocaleString()}</div>
</div>
<div>
<div className="opacity-60">Organization</div>
<div>
{row.organization?.name}{' '}
<span className="opacity-60">({row.organization?.id})</span>
</div>
</div>
<div>
<div className="opacity-60">Users</div>
<div className="break-all">
{row.organization?.users
?.map((u) => u.user?.email)
.filter(Boolean)
.join(', ') || '—'}
</div>
</div>
<div className="col-span-2">
<div className="opacity-60">Post ID</div>
<div>{row.postId}</div>
</div>
</div>
<div className="text-[13px] font-[600] mb-[6px]">message</div>
<pre className="text-[12px] bg-sixth p-[12px] rounded overflow-auto max-h-[40vh] whitespace-pre-wrap break-all">
{typeof parsedMessage === 'string'
? parsedMessage
: JSON.stringify(parsedMessage, null, 2)}
</pre>
<div className="text-[13px] font-[600] mb-[6px] mt-[12px]">body</div>
<pre className="text-[12px] bg-sixth p-[12px] rounded overflow-auto max-h-[40vh] whitespace-pre-wrap break-all">
{typeof parsedBody === 'string'
? parsedBody
: JSON.stringify(parsedBody, null, 2)}
</pre>
</div>
);
};
const usePlatformsList = () => {
const fetch = useFetch();
return useSWR<string[]>('/admin/errors/platforms', async (url: string) => {
const res = await fetch(url);
if (!res.ok) return [];
return res.json();
});
};
const useErrorsList = (params: {
page: number;
limit: number;
platform: string;
email: string;
unknownFirst: boolean;
}) => {
const fetch = useFetch();
const query = new URLSearchParams({
page: String(params.page),
limit: String(params.limit),
...(params.platform ? { platform: params.platform } : {}),
...(params.email ? { email: params.email } : {}),
unknownFirst: params.unknownFirst ? 'true' : 'false',
});
const key = `/admin/errors?${query.toString()}`;
return useSWR<ErrorsResponse>(key, async (url: string) => {
const res = await fetch(url);
if (!res.ok) {
throw new Error('Failed to load errors');
}
return res.json();
});
};
export const AdminErrorsComponent: FC = () => {
const user = useUser();
const modal = useModals();
const toaster = useToaster();
const [page, setPage] = useState(0);
const [limit, setLimit] = useState(20);
const [platform, setPlatform] = useState('');
const [email, setEmail] = useState('');
const [emailInput, setEmailInput] = useState('');
const [unknownFirst, setUnknownFirst] = useState(true);
const { data: platforms } = usePlatformsList();
const { data, isLoading, error } = useErrorsList({
page,
limit,
platform,
email,
unknownFirst,
});
const onApplyEmail = useCallback(() => {
setPage(0);
setEmail(emailInput.trim());
}, [emailInput]);
const onClear = useCallback(() => {
setPage(0);
setEmail('');
setEmailInput('');
setPlatform('');
}, []);
const openDetails = useCallback(
(row: ErrorRow) => {
modal.openModal({
closeOnClickOutside: true,
withCloseButton: false,
classNames: {
modal: 'w-[100%] max-w-[1100px] text-textColor',
},
children: <ErrorDetailsModal row={row} />,
});
},
[modal]
);
const copyRow = useCallback(
(row: ErrorRow) => {
copy(
JSON.stringify(
{ message: safeParse(row.message), body: safeParse(row.body), meta: row },
null,
2
)
);
toaster.show('Debug code copied to clipboard', 'success');
},
[toaster]
);
if (!user?.isSuperAdmin) {
return (
<div className="text-textColor p-[20px]">
You do not have access to this page.
</div>
);
}
const totalPages = data ? Math.max(1, Math.ceil(data.total / limit)) : 1;
return (
<div className="flex flex-col gap-[16px] text-textColor">
<div className="flex items-center justify-between">
<div className="text-[20px] font-[600]">Errors</div>
<div className="text-[13px] opacity-70">
{data ? `${data.total} total` : ''}
</div>
</div>
<div className="flex flex-wrap gap-[12px] items-end bg-newBgColorInner border border-newTableBorder rounded-[8px] p-[12px]">
<div className="flex flex-col gap-[6px]">
<div className="text-[12px] opacity-70">Platform</div>
<select
value={platform}
onChange={(e) => {
setPage(0);
setPlatform(e.target.value);
}}
className="bg-newBgColorInner h-[38px] border border-newTableBorder rounded-[8px] px-[10px] text-[14px] text-textColor min-w-[180px]"
>
<option value="">All platforms</option>
{(platforms || []).map((p) => (
<option key={p} value={p}>
{p}
</option>
))}
</select>
</div>
<div className="flex flex-col gap-[6px]">
<div className="text-[12px] opacity-70">Email contains</div>
<div className="flex gap-[8px]">
<input
value={emailInput}
onChange={(e) => setEmailInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') onApplyEmail();
}}
placeholder="user@example.com"
className="bg-newBgColorInner h-[38px] border border-newTableBorder rounded-[8px] px-[10px] text-[14px] text-textColor min-w-[240px]"
/>
<Button onClick={onApplyEmail}>Apply</Button>
</div>
</div>
<label className="flex items-center gap-[6px] text-[13px] cursor-pointer h-[38px]">
<input
type="checkbox"
checked={unknownFirst}
onChange={(e) => {
setPage(0);
setUnknownFirst(e.target.checked);
}}
/>
Unknown Error first
</label>
<div className="flex flex-col gap-[6px]">
<div className="text-[12px] opacity-70">Per page</div>
<select
value={limit}
onChange={(e) => {
setPage(0);
setLimit(parseInt(e.target.value, 10));
}}
className="bg-newBgColorInner h-[38px] border border-newTableBorder rounded-[8px] px-[10px] text-[14px] text-textColor"
>
{[10, 20, 50, 100].map((n) => (
<option key={n} value={n}>
{n}
</option>
))}
</select>
</div>
<Button secondary onClick={onClear}>
Clear filters
</Button>
</div>
{isLoading ? (
<LoadingComponent />
) : error ? (
<div className="text-red-400">Failed to load errors.</div>
) : !data || data.items.length === 0 ? (
<div className="opacity-70">No errors found.</div>
) : (
<div className="border border-newTableBorder rounded-[8px] overflow-hidden">
<div className="grid grid-cols-[170px_120px_220px_1fr_220px] gap-[12px] px-[12px] py-[10px] bg-newBgColorInner text-[12px] uppercase opacity-70 border-b border-newTableBorder">
<div>Created</div>
<div>Platform</div>
<div>User / Org</div>
<div>Message</div>
<div className="text-right">Actions</div>
</div>
{data.items.map((row) => {
const isUnknown = (row.message || '').includes('Unknown Error');
const emails =
row.organization?.users
?.map((u) => u.user?.email)
.filter(Boolean)
.join(', ') || '—';
const preview =
(row.message || '').length > 280
? row.message.slice(0, 280) + '…'
: row.message;
return (
<div
key={row.id}
className="grid grid-cols-[170px_120px_220px_1fr_220px] gap-[12px] px-[12px] py-[10px] text-[13px] border-b border-newTableBorder last:border-b-0 items-start"
>
<div className="opacity-90">
{new Date(row.createdAt).toLocaleString()}
</div>
<div>
<span
className={
isUnknown
? 'text-red-400 font-[600]'
: 'opacity-90'
}
>
{row.platform}
</span>
</div>
<div className="break-all">
<div>{emails}</div>
<div className="opacity-60 text-[12px]">
{row.organization?.name}
</div>
</div>
<div className="break-all whitespace-pre-wrap font-mono text-[12px] opacity-90">
{preview}
</div>
<div className="flex gap-[8px] justify-end">
<Button secondary onClick={() => openDetails(row)}>
View
</Button>
<Button onClick={() => copyRow(row)}>Copy</Button>
</div>
</div>
);
})}
</div>
)}
<div className="flex items-center justify-between">
<div className="text-[13px] opacity-70">
Page {page + 1} of {totalPages}
</div>
<div className="flex gap-[8px]">
<Button
secondary
disabled={page === 0}
onClick={() => setPage((p) => Math.max(0, p - 1))}
>
Previous
</Button>
<Button
disabled={!data?.hasMore}
onClick={() => setPage((p) => p + 1)}
>
Next
</Button>
</div>
</div>
</div>
);
};
|