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
@@ -0,0 +1,87 @@
'use client';
import { useState } from 'react';
type Status = 'ACTIVE' | 'DISCONNECTED' | 'BANNED' | 'PAIRING';
interface BotSummary {
id: string;
jid: string | null;
displayName: string | null;
status: Status;
}
export function BotSettingsCard({ initial }: { initial: { bot: BotSummary | null; shared: boolean; sharedBotId?: string } }) {
const [bot, setBot] = useState<BotSummary | null>(initial.bot);
const [revealedJid, setRevealedJid] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
async function onReveal() {
setError(null);
const res = await fetch('/api/bot/reveal', {
method: 'POST',
credentials: 'include',
});
if (res.ok) {
const data = (await res.json()) as { jid: string };
setRevealedJid(data.jid);
} else {
setError('Reveal failed');
}
}
if (!bot) {
return (
<div className="rounded-lg border border-gray-200 p-6 bg-white">
<h2 className="text-lg font-medium mb-2">Bot</h2>
<p className="text-sm text-gray-600">
No bot assigned yet. Contact your platform administrator to get one assigned.
</p>
</div>
);
}
return (
<div className="rounded-lg border border-gray-200 p-6 bg-white">
<div className="flex items-center justify-between mb-3">
<h2 className="text-lg font-medium">Bot</h2>
<span
className={`text-xs px-2 py-1 rounded ${
bot.status === 'ACTIVE'
? 'bg-green-100 text-green-800'
: bot.status === 'PAIRING'
? 'bg-yellow-100 text-yellow-800'
: 'bg-red-100 text-red-800'
}`}
>
{bot.status}
</span>
</div>
<dl className="text-sm space-y-1 mb-4">
<div>
<dt className="inline font-medium">Number: </dt>
<dd className="inline">
{revealedJid ?? (bot.jid ? '••••••••••' : '—')}
{!revealedJid && bot.status === 'ACTIVE' && (
<button
type="button"
onClick={onReveal}
className="ml-2 text-xs text-blue-600 underline"
>
Reveal
</button>
)}
</dd>
</div>
<div>
<dt className="inline font-medium">Display name: </dt>
<dd className="inline">{bot.displayName ?? '—'}</dd>
</div>
</dl>
<p className="text-xs text-gray-400">
Bot is assigned by the platform administrator. Contact support to change or remove it.
</p>
{error && <p className="text-sm text-red-600 mt-2">{error}</p>}
</div>
);
}
+41
View File
@@ -0,0 +1,41 @@
import { BotSettingsCard } from './BotSettingsCard';
import { apiFetch } from '../../_lib/api';
interface BotSummary {
id: string;
platform: string;
jid: string | null;
displayName: string | null;
status: 'ACTIVE' | 'DISCONNECTED' | 'BANNED' | 'PAIRING';
isBot: boolean;
createdAt: string;
updatedAt: string;
}
interface BotState {
bot: BotSummary | null;
shared: boolean;
}
export default async function BotSettingsPage() {
let state: BotState = { bot: null, shared: false };
try {
const res = await apiFetch('/admin/bot');
if (res.ok) {
state = (await res.json()) as BotState;
}
} catch {
state = { bot: null, shared: false };
}
return (
<div className="max-w-2xl">
<h1 className="text-xl font-semibold mb-6">Bot Settings</h1>
<p className="text-sm text-gray-600 mb-4">
TOWER runs on a dedicated WhatsApp number. Adding the bot to a group makes it eligible for
claim by any tenant admin. The bot number is hidden from members and the public web.
</p>
<BotSettingsCard initial={state} />
</div>
);
}
+189
View File
@@ -0,0 +1,189 @@
'use client';
import { useState } from 'react';
interface RuleData {
id: string;
matchType: 'HASHTAG' | 'PREFIX' | 'REACTION_EMOJI';
matchValue: string;
action: 'FLAG' | 'AUTO_APPROVE' | 'SKIP' | 'REJECT';
priority: number;
isActive: boolean;
createdAt: string;
updatedAt: string;
}
const MATCH_TYPE_LABELS: Record<string, string> = {
HASHTAG: 'Hashtag',
PREFIX: 'Prefix',
REACTION_EMOJI: 'Reaction Emoji',
};
const ACTION_LABELS: Record<string, string> = {
FLAG: 'Flag (Pending)',
AUTO_APPROVE: 'Auto-approve',
SKIP: 'Skip (Silent Drop)',
REJECT: 'Reject (Visible)',
};
export function RuleManager({ initial }: { initial: RuleData[] }) {
const [rules, setRules] = useState<RuleData[]>(initial);
const [matchType, setMatchType] = useState<'HASHTAG' | 'PREFIX' | 'REACTION_EMOJI'>('HASHTAG');
const [matchValue, setMatchValue] = useState('');
const [action, setAction] = useState<'FLAG' | 'AUTO_APPROVE' | 'SKIP' | 'REJECT'>('FLAG');
const [priority, setPriority] = useState(0);
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
async function addRule() {
if (!matchValue.trim()) return;
setBusy(true);
setError('');
try {
const res = await fetch('/api/rules', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ matchType, matchValue: matchValue.trim(), action, priority }),
});
if (!res.ok) {
const err = await res.json().catch(() => ({ message: 'Failed to create rule' }));
setError(err.message ?? 'Failed to create rule');
return;
}
const created: RuleData = await res.json();
setRules((prev) => [...prev, created].sort((a, b) => a.priority - b.priority));
setMatchValue('');
setAction('FLAG');
setPriority(0);
} finally {
setBusy(false);
}
}
async function toggleRule(rule: RuleData) {
const res = await fetch(`/api/rules/${rule.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ isActive: !rule.isActive }),
});
if (res.ok) {
const updated: RuleData = await res.json();
setRules((prev) => prev.map((r) => (r.id === rule.id ? updated : r)));
}
}
async function deleteRule(id: string) {
const res = await fetch(`/api/rules/${id}`, { method: 'DELETE' });
if (res.ok) setRules((prev) => prev.filter((r) => r.id !== id));
}
return (
<div className="flex flex-col gap-6">
<section className="border border-gray-200 rounded-lg p-4">
<h2 className="text-base font-semibold mb-3">Add Rule</h2>
<div className="flex flex-wrap items-end gap-3">
<div className="flex flex-col gap-1">
<label className="text-xs text-gray-500">Type</label>
<select
value={matchType}
onChange={(e) => setMatchType(e.target.value as any)}
className="border border-gray-300 rounded px-3 py-2 text-sm"
>
<option value="HASHTAG">Hashtag</option>
<option value="PREFIX">Prefix</option>
<option value="REACTION_EMOJI">Reaction Emoji</option>
</select>
</div>
<div className="flex flex-col gap-1">
<label className="text-xs text-gray-500">Value</label>
<input
value={matchValue}
onChange={(e) => setMatchValue(e.target.value)}
placeholder={matchType === 'REACTION_EMOJI' ? '⭐' : '#important'}
className="border border-gray-300 rounded px-3 py-2 text-sm w-40"
/>
</div>
<div className="flex flex-col gap-1">
<label className="text-xs text-gray-500">Action</label>
<select
value={action}
onChange={(e) => setAction(e.target.value as any)}
className="border border-gray-300 rounded px-3 py-2 text-sm"
>
<option value="FLAG">Flag (Pending)</option>
<option value="AUTO_APPROVE">Auto-approve</option>
<option value="SKIP">Skip (Silent Drop)</option>
<option value="REJECT">Reject (Visible)</option>
</select>
</div>
<div className="flex flex-col gap-1">
<label className="text-xs text-gray-500">Priority</label>
<input
type="number"
value={priority}
onChange={(e) => setPriority(Number(e.target.value))}
className="border border-gray-300 rounded px-3 py-2 text-sm w-20"
/>
</div>
<button
type="button"
onClick={() => void addRule()}
disabled={busy || !matchValue.trim()}
className="bg-blue-600 text-white rounded px-4 py-2 text-sm hover:bg-blue-700 disabled:opacity-40"
>
{busy ? 'Adding...' : 'Add Rule'}
</button>
</div>
{error && <p className="text-red-600 text-sm mt-2">{error}</p>}
</section>
<section>
<h2 className="text-base font-semibold mb-3">Active Rules</h2>
{rules.length === 0 ? (
<p className="text-sm text-gray-400">No rules configured. Messages without matching rules are ignored.</p>
) : (
<table className="w-full text-sm">
<thead>
<tr className="border-b border-gray-200 text-left text-gray-500">
<th className="pb-2 pr-3">Type</th>
<th className="pb-2 pr-3">Value</th>
<th className="pb-2 pr-3">Action</th>
<th className="pb-2 pr-3">Priority</th>
<th className="pb-2 pr-3">Active</th>
<th className="pb-2" />
</tr>
</thead>
<tbody>
{rules.map((rule) => (
<tr key={rule.id} className="border-b border-gray-100">
<td className="py-2 pr-3">{MATCH_TYPE_LABELS[rule.matchType] ?? rule.matchType}</td>
<td className="py-2 pr-3 font-mono">{rule.matchValue}</td>
<td className="py-2 pr-3">{ACTION_LABELS[rule.action] ?? rule.action}</td>
<td className="py-2 pr-3">{rule.priority}</td>
<td className="py-2 pr-3">
<button
type="button"
onClick={() => void toggleRule(rule)}
className={`text-xs rounded px-2 py-0.5 ${rule.isActive ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-500'}`}
>
{rule.isActive ? 'ON' : 'OFF'}
</button>
</td>
<td className="py-2">
<button
type="button"
onClick={() => void deleteRule(rule.id)}
className="text-red-600 hover:text-red-800 text-xs"
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
)}
</section>
</div>
);
}
+36
View File
@@ -0,0 +1,36 @@
import { RuleManager } from './RuleManager';
import { apiFetch } from '../../_lib/api';
interface RuleData {
id: string;
matchType: 'HASHTAG' | 'PREFIX' | 'REACTION_EMOJI';
matchValue: string;
action: 'FLAG' | 'AUTO_APPROVE' | 'SKIP' | 'REJECT';
priority: number;
isActive: boolean;
createdAt: string;
updatedAt: string;
}
export default async function RulesSettingsPage() {
let rules: RuleData[] = [];
try {
const res = await apiFetch('/admin/rules');
if (res.ok) {
rules = (await res.json()) as RuleData[];
}
} catch {
rules = [];
}
return (
<div className="max-w-2xl">
<h1 className="text-xl font-semibold mb-6">Rules Engine</h1>
<p className="text-sm text-gray-600 mb-4">
Configure which hashtags, prefixes, and reaction emojis trigger message processing
and what action TOWER should take. Rules are matched in priority order.
</p>
<RuleManager initial={rules} />
</div>
);
}