good forst commit

This commit is contained in:
2026-06-09 02:02:40 +05:30
parent 801c1d7121
commit 249d759e6a
215 changed files with 15425 additions and 1240 deletions
+158
View File
@@ -0,0 +1,158 @@
import Link from 'next/link';
import { apiFetch } from '../../_lib/api';
interface MessageDetail {
id: string;
tenantId: string;
platform: string;
platformMsgId: string;
sourceGroupId: string;
sourceGroup: {
id: string;
name: string;
platformId: string;
claimStatus: string;
isActive: boolean;
createdAt: string;
} | null;
senderJid: string;
senderName: string | null;
senderTowerUser: {
id: string;
jid: string;
phoneHash: string;
displayName: string | null;
createdAt: string;
} | null;
content: string;
mediaUrl: string | null;
tags: string[];
status: string;
createdAt: string;
updatedAt: string;
approval: {
id: string;
adminId: string;
decision: string;
notes: string | null;
decidedAt: string;
} | null;
}
function Field({ label, children }: { label: string; children: React.ReactNode }) {
return (
<div className="grid grid-cols-[160px_1fr] gap-2 text-sm py-2 border-b border-gray-100">
<span className="text-gray-500 font-medium">{label}</span>
<span className="text-gray-900 break-words">{children ?? <span className="text-gray-300"></span>}</span>
</div>
);
}
export default async function MessageDetailPage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
let msg: MessageDetail | null = null;
let error: string | null = null;
try {
const res = await apiFetch(`/admin/messages/${id}`);
if (res.ok) {
msg = await res.json();
} else {
error = `API returned ${res.status}`;
}
} catch {
error = 'Failed to load message';
}
if (error || !msg) {
return (
<div className="max-w-2xl">
<Link href="/search" className="text-sm text-blue-600 hover:underline mb-4 inline-block">&larr; Back to search</Link>
<p className="text-red-600 text-sm">{error ?? 'Message not found'}</p>
</div>
);
}
return (
<div className="max-w-2xl">
<Link href="/search" className="text-sm text-blue-600 hover:underline mb-4 inline-block">&larr; Back to search</Link>
<h1 className="text-xl font-semibold mb-6">Message Detail</h1>
<div className="border border-gray-200 rounded-lg p-4 mb-6">
<p className="text-sm whitespace-pre-wrap bg-gray-50 rounded p-3 mb-2">{msg.content}</p>
<div className="flex flex-wrap gap-1">
{msg.tags.map((tag) => (
<span key={tag} className="rounded-full bg-blue-50 px-2 py-0.5 text-xs text-blue-600">{tag}</span>
))}
</div>
</div>
<div className="border border-gray-200 rounded-lg p-4">
<h2 className="text-sm font-semibold text-gray-700 mb-3">Metadata</h2>
<Field label="Status">
<span className={`font-medium ${msg.status === 'APPROVED' ? 'text-green-600' : msg.status === 'REJECTED' ? 'text-red-600' : 'text-amber-600'}`}>
{msg.status}
</span>
</Field>
<Field label="Message ID">{msg.id}</Field>
<Field label="Platform">{msg.platform}</Field>
<Field label="Platform Msg ID">{msg.platformMsgId}</Field>
<Field label="Created">{new Date(msg.createdAt).toLocaleString()}</Field>
<Field label="Updated">{new Date(msg.updatedAt).toLocaleString()}</Field>
<div className="mt-4 pt-2 border-t border-gray-200">
<h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Sender</h3>
<Field label="JID">{msg.senderJid}</Field>
<Field label="Name">{msg.senderName}</Field>
{msg.senderTowerUser && (
<>
<Field label="Display Name">{msg.senderTowerUser.displayName}</Field>
<Field label="Phone Hash">{msg.senderTowerUser.phoneHash}</Field>
<Field label="Tower User ID">{msg.senderTowerUser.id}</Field>
</>
)}
</div>
<div className="mt-4 pt-2 border-t border-gray-200">
<h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Source Group</h3>
{msg.sourceGroup ? (
<>
<Field label="Name">{msg.sourceGroup.name}</Field>
<Field label="Platform ID">{msg.sourceGroup.platformId}</Field>
<Field label="Claim Status">{msg.sourceGroup.claimStatus}</Field>
<Field label="Active">{msg.sourceGroup.isActive ? 'Yes' : 'No'}</Field>
</>
) : (
<Field label="Group">(deleted)</Field>
)}
</div>
{msg.approval && (
<div className="mt-4 pt-2 border-t border-gray-200">
<h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Approval</h3>
<Field label="Decision">
<span className={`font-medium ${msg.approval.decision === 'APPROVED' ? 'text-green-600' : 'text-red-600'}`}>
{msg.approval.decision}
</span>
</Field>
<Field label="Admin ID">{msg.approval.adminId}</Field>
<Field label="Decided At">{new Date(msg.approval.decidedAt).toLocaleString()}</Field>
<Field label="Notes">{msg.approval.notes}</Field>
</div>
)}
{msg.mediaUrl && (
<div className="mt-4 pt-2 border-t border-gray-200">
<h3 className="text-xs font-semibold text-gray-500 uppercase mb-2">Media</h3>
<Field label="Media URL">{msg.mediaUrl}</Field>
</div>
)}
</div>
</div>
);
}
+105
View File
@@ -0,0 +1,105 @@
import { apiFetch } from '../../_lib/api';
interface PendingMessage {
id: string;
content: string;
senderJid: string;
senderName: string | null;
tags: string[];
createdAt: string;
sourceGroupId: string;
sourceGroupName: string;
sourceGroupPlatformId: string;
}
type FetchResult<T> = { ok: true; data: T } | { ok: false; status: number; error: string };
async function fetchPending(): Promise<FetchResult<PendingMessage[]>> {
try {
const res = await apiFetch('/admin/messages/pending');
if (!res.ok) {
const body = await res.text().catch(() => '');
return { ok: false, status: res.status, error: body.slice(0, 200) || res.statusText };
}
return { ok: true, data: (await res.json()) as PendingMessage[] };
} catch (err) {
return { ok: false, status: 0, error: `API unreachable: ${(err as Error).message}` };
}
}
export default async function PendingMessagesPage() {
const result = await fetchPending();
const messages = result.ok ? result.data : [];
const error = !result.ok ? (result.status === 0 ? 'API unreachable' : `API ${result.status}: ${result.error}`) : null;
return (
<div className="max-w-3xl">
<h1 className="text-xl font-semibold mb-2">Pending messages</h1>
<p className="text-sm text-gray-500 mb-6">
Flagged messages waiting for an admin to approve. Approving forwards them to every active
route from the source group and indexes them in search.
</p>
{error && (
<div className="mb-4 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-800">
Failed to load: {error}
</div>
)}
{!error && messages.length === 0 && (
<div className="rounded-xl border border-gray-200 bg-white p-6 text-sm text-gray-500">
No pending messages right now. New flagged messages will appear here as they arrive.
</div>
)}
<ul className="space-y-3">
{messages.map((m) => (
<PendingMessageRow key={m.id} message={m} />
))}
</ul>
</div>
);
}
function PendingMessageRow({ message }: { message: PendingMessage }) {
return (
<li className="rounded-xl border border-gray-200 bg-white p-4 flex flex-col gap-2">
<div className="flex items-baseline justify-between gap-2">
<div className="text-sm font-medium text-gray-900">{message.sourceGroupName}</div>
<div className="text-xs text-gray-500">
{new Date(message.createdAt).toLocaleString()}
</div>
</div>
<div className="text-xs text-gray-500">
From <span className="font-mono">{message.senderName ?? message.senderJid}</span>
{message.tags.length > 0 && (
<span className="ml-2 inline-flex flex-wrap gap-1">
{message.tags.map((t) => (
<span
key={t}
className="rounded bg-blue-50 px-1.5 py-0.5 text-[10px] font-medium text-blue-700"
>
{t}
</span>
))}
</span>
)}
</div>
<div className="text-sm text-gray-800 whitespace-pre-wrap break-words">
{message.content}
</div>
<form
action={`/api/messages/${message.id}/approve`}
method="post"
className="flex justify-end pt-1"
>
<button
type="submit"
className="rounded bg-blue-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-blue-700"
>
Approve &amp; forward
</button>
</form>
</li>
);
}