Merge remote-tracking branch 'origin/task/BIT-26126-admin-ui' into task/BIT-26121-backend
This commit is contained in:
commit
8e02d4123b
21 files changed
+3834
-9
No files matched your search
+15
-9
@@ -1,13 +1,19 @@
|
||||
# Node
|
||||
admin-ui/node_modules/
|
||||
admin-ui/dist/
|
||||
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*.env
|
||||
*.pyc
|
||||
*.pyo
|
||||
.env
|
||||
*.log
|
||||
*.sqlite3
|
||||
*.egg-info/
|
||||
.venv/
|
||||
venv/
|
||||
dist/
|
||||
build/
|
||||
*.egg-info/
|
||||
node_modules/
|
||||
.DS_Store
|
||||
|
||||
# Docker
|
||||
.docker/
|
||||
|
||||
# Editor
|
||||
.vscode/
|
||||
.idea/
|
||||
@@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="pt">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>BizDocs Forwarder — Admin</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+2771
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "forwarder-admin-ui",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc && vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"react-router-dom": "^6.26.2",
|
||||
"lucide-react": "^0.446.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.3.11",
|
||||
"@types/react-dom": "^18.3.1",
|
||||
"@vitejs/plugin-react": "^4.3.2",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"postcss": "^8.4.47",
|
||||
"tailwindcss": "^3.4.14",
|
||||
"typescript": "^5.6.3",
|
||||
"vite": "^5.4.10"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
plugins: {
|
||||
tailwindcss: {},
|
||||
autoprefixer: {},
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { BrowserRouter, Routes, Route } from 'react-router-dom'
|
||||
import Layout from './components/Layout'
|
||||
import Dashboard from './pages/Dashboard'
|
||||
import Accounts from './pages/Accounts'
|
||||
import Emails from './pages/Emails'
|
||||
import ConnectionTest from './pages/ConnectionTest'
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route element={<Layout />}>
|
||||
<Route index element={<Dashboard />} />
|
||||
<Route path="/accounts" element={<Accounts />} />
|
||||
<Route path="/emails" element={<Emails />} />
|
||||
<Route path="/connection-test" element={<ConnectionTest />} />
|
||||
</Route>
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { NavLink, Outlet } from 'react-router-dom'
|
||||
import { LayoutDashboard, Users, Mail, Wifi } from 'lucide-react'
|
||||
|
||||
const nav = [
|
||||
{ to: '/', label: 'Dashboard', icon: LayoutDashboard },
|
||||
{ to: '/accounts', label: 'Contas', icon: Users },
|
||||
{ to: '/emails', label: 'Emails', icon: Mail },
|
||||
{ to: '/connection-test', label: 'Teste Ligação', icon: Wifi },
|
||||
]
|
||||
|
||||
export default function Layout() {
|
||||
return (
|
||||
<div className="flex h-screen">
|
||||
<aside className="w-56 bg-gray-900 text-white flex flex-col">
|
||||
<div className="px-5 py-4 border-b border-gray-700">
|
||||
<h1 className="text-sm font-semibold text-gray-200 leading-tight">BizDocs Forwarder</h1>
|
||||
<p className="text-xs text-gray-500 mt-0.5">Admin</p>
|
||||
</div>
|
||||
<nav className="flex-1 p-3 space-y-1">
|
||||
{nav.map(({ to, label, icon: Icon }) => (
|
||||
<NavLink
|
||||
key={to}
|
||||
to={to}
|
||||
end={to === '/'}
|
||||
className={({ isActive }) =>
|
||||
`flex items-center gap-3 px-3 py-2 rounded-md text-sm transition-colors ${
|
||||
isActive
|
||||
? 'bg-indigo-600 text-white'
|
||||
: 'text-gray-400 hover:bg-gray-800 hover:text-white'
|
||||
}`
|
||||
}
|
||||
>
|
||||
<Icon size={16} />
|
||||
{label}
|
||||
</NavLink>
|
||||
))}
|
||||
</nav>
|
||||
</aside>
|
||||
<main className="flex-1 overflow-auto">
|
||||
<Outlet />
|
||||
</main>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
export default function Spinner({ className = '' }: { className?: string }) {
|
||||
return (
|
||||
<svg
|
||||
className={`animate-spin h-5 w-5 text-indigo-600 ${className}`}
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
|
||||
<path
|
||||
className="opacity-75"
|
||||
fill="currentColor"
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
|
||||
/>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
const styles: Record<string, string> = {
|
||||
pending: 'bg-yellow-100 text-yellow-800',
|
||||
forwarded: 'bg-green-100 text-green-800',
|
||||
failed: 'bg-red-100 text-red-800',
|
||||
active: 'bg-green-100 text-green-800',
|
||||
inactive: 'bg-gray-100 text-gray-600',
|
||||
}
|
||||
|
||||
export default function StatusBadge({ status }: { status: string }) {
|
||||
return (
|
||||
<span className={`inline-flex items-center px-2 py-0.5 rounded text-xs font-medium ${styles[status] ?? 'bg-gray-100 text-gray-700'}`}>
|
||||
{status}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
|
||||
export function useAsync<T>(
|
||||
fn: () => Promise<T>,
|
||||
deps: unknown[] = [],
|
||||
) {
|
||||
const [data, setData] = useState<T | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const run = useCallback(async () => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
const result = await fn()
|
||||
setData(result)
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e))
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, deps)
|
||||
|
||||
useEffect(() => { run() }, [run])
|
||||
|
||||
return { data, loading, error, reload: run }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
@layer base {
|
||||
body {
|
||||
@apply bg-gray-50 text-gray-900;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
const BASE = '/api'
|
||||
|
||||
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
headers: { 'Content-Type': 'application/json', ...init?.headers },
|
||||
...init,
|
||||
})
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => res.statusText)
|
||||
throw new Error(`${res.status} ${text}`)
|
||||
}
|
||||
if (res.status === 204) return undefined as T
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export interface Account {
|
||||
id: number
|
||||
name: string
|
||||
email: string
|
||||
imap_host: string
|
||||
imap_port: number
|
||||
imap_user: string
|
||||
smtp_host: string
|
||||
smtp_port: number
|
||||
smtp_user: string
|
||||
bizdocs_api_key: string
|
||||
bizdocs_entity_id: string
|
||||
active: boolean
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export type AccountCreate = Omit<Account, 'id' | 'created_at' | 'updated_at'> & {
|
||||
imap_password: string
|
||||
smtp_password: string
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
id: number
|
||||
account_id: number
|
||||
account_name: string
|
||||
message_uid: string
|
||||
subject: string
|
||||
sender: string
|
||||
received_at: string
|
||||
status: 'pending' | 'forwarded' | 'failed'
|
||||
error: string | null
|
||||
forwarded_at: string | null
|
||||
}
|
||||
|
||||
export interface Stats {
|
||||
accounts_total: number
|
||||
accounts_active: number
|
||||
messages_total: number
|
||||
messages_pending: number
|
||||
messages_forwarded: number
|
||||
messages_failed: number
|
||||
last_sync_at: string | null
|
||||
}
|
||||
|
||||
export interface ConnectionTestResult {
|
||||
imap_ok: boolean
|
||||
imap_error: string | null
|
||||
smtp_ok: boolean
|
||||
smtp_error: string | null
|
||||
bizdocs_ok: boolean
|
||||
bizdocs_error: string | null
|
||||
}
|
||||
|
||||
export interface TestPayload {
|
||||
imap_host: string
|
||||
imap_port: number
|
||||
imap_user: string
|
||||
imap_password: string
|
||||
smtp_host: string
|
||||
smtp_port: number
|
||||
smtp_user: string
|
||||
smtp_password: string
|
||||
bizdocs_api_key: string
|
||||
bizdocs_entity_id: string
|
||||
}
|
||||
|
||||
// Accounts
|
||||
export const api = {
|
||||
accounts: {
|
||||
list: () => request<Account[]>('/accounts'),
|
||||
get: (id: number) => request<Account>(`/accounts/${id}`),
|
||||
create: (data: AccountCreate) =>
|
||||
request<Account>('/accounts', { method: 'POST', body: JSON.stringify(data) }),
|
||||
update: (id: number, data: Partial<AccountCreate>) =>
|
||||
request<Account>(`/accounts/${id}`, { method: 'PUT', body: JSON.stringify(data) }),
|
||||
delete: (id: number) => request<void>(`/accounts/${id}`, { method: 'DELETE' }),
|
||||
},
|
||||
messages: {
|
||||
list: (params?: { account_id?: number; status?: string; limit?: number; offset?: number }) => {
|
||||
const q = new URLSearchParams()
|
||||
if (params?.account_id) q.set('account_id', String(params.account_id))
|
||||
if (params?.status) q.set('status', params.status)
|
||||
if (params?.limit) q.set('limit', String(params.limit))
|
||||
if (params?.offset) q.set('offset', String(params.offset))
|
||||
return request<Message[]>(`/messages${q.toString() ? '?' + q : ''}`)
|
||||
},
|
||||
retry: (id: number) => request<void>(`/messages/${id}/retry`, { method: 'POST' }),
|
||||
},
|
||||
stats: {
|
||||
get: () => request<Stats>('/stats'),
|
||||
},
|
||||
connection: {
|
||||
test: (payload: TestPayload) =>
|
||||
request<ConnectionTestResult>('/connection/test', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
}),
|
||||
testAccount: (accountId: number) =>
|
||||
request<ConnectionTestResult>(`/accounts/${accountId}/test`, { method: 'POST' }),
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import './index.css'
|
||||
import App from './App.tsx'
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
@@ -0,0 +1,333 @@
|
||||
import { useState } from 'react'
|
||||
import { api, Account, AccountCreate } from '../lib/api'
|
||||
import { useAsync } from '../hooks/useApi'
|
||||
import Spinner from '../components/Spinner'
|
||||
import StatusBadge from '../components/StatusBadge'
|
||||
import { Plus, Pencil, Trash2, Wifi, X, Check } from 'lucide-react'
|
||||
|
||||
const EMPTY_FORM: AccountCreate = {
|
||||
name: '',
|
||||
email: '',
|
||||
imap_host: '',
|
||||
imap_port: 993,
|
||||
imap_user: '',
|
||||
imap_password: '',
|
||||
smtp_host: '',
|
||||
smtp_port: 587,
|
||||
smtp_user: '',
|
||||
smtp_password: '',
|
||||
bizdocs_api_key: '',
|
||||
bizdocs_entity_id: '',
|
||||
active: true,
|
||||
}
|
||||
|
||||
function AccountModal({
|
||||
initial,
|
||||
onSave,
|
||||
onClose,
|
||||
}: {
|
||||
initial: Partial<AccountCreate> | null
|
||||
onSave: (data: AccountCreate) => Promise<void>
|
||||
onClose: () => void
|
||||
}) {
|
||||
const [form, setForm] = useState<AccountCreate>({ ...EMPTY_FORM, ...initial })
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const set = (k: keyof AccountCreate, v: string | number | boolean) =>
|
||||
setForm((f) => ({ ...f, [k]: v }))
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setSaving(true)
|
||||
setError(null)
|
||||
try {
|
||||
await onSave(form)
|
||||
onClose()
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e))
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const field = (
|
||||
label: string,
|
||||
key: keyof AccountCreate,
|
||||
type = 'text',
|
||||
placeholder = '',
|
||||
) => (
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-gray-700 mb-1">{label}</label>
|
||||
<input
|
||||
type={type}
|
||||
value={String(form[key])}
|
||||
placeholder={placeholder}
|
||||
onChange={(e) => set(key, type === 'number' ? Number(e.target.value) : e.target.value)}
|
||||
className="w-full border border-gray-300 rounded-md px-3 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 bg-black/40 flex items-center justify-center z-50 p-4">
|
||||
<div className="bg-white rounded-lg shadow-xl w-full max-w-2xl max-h-[90vh] overflow-y-auto">
|
||||
<div className="flex items-center justify-between px-5 py-4 border-b">
|
||||
<h3 className="font-semibold">{initial ? 'Editar conta' : 'Nova conta'}</h3>
|
||||
<button onClick={onClose} className="text-gray-400 hover:text-gray-600">
|
||||
<X size={18} />
|
||||
</button>
|
||||
</div>
|
||||
<form onSubmit={handleSubmit} className="p-5 space-y-4">
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
{field('Nome', 'name', 'text', 'ex. Empresa X')}
|
||||
{field('Email de origem', 'email', 'email', '[email protected]')}
|
||||
</div>
|
||||
|
||||
<fieldset className="border border-gray-200 rounded-md p-3">
|
||||
<legend className="text-xs font-semibold text-gray-500 px-1">IMAP</legend>
|
||||
<div className="grid grid-cols-3 gap-3 mt-1">
|
||||
{field('Host', 'imap_host', 'text', 'imap.gmail.com')}
|
||||
{field('Porto', 'imap_port', 'number')}
|
||||
{field('Utilizador', 'imap_user', 'text')}
|
||||
<div className="col-span-3">
|
||||
{field('Password', 'imap_password', 'password')}
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset className="border border-gray-200 rounded-md p-3">
|
||||
<legend className="text-xs font-semibold text-gray-500 px-1">SMTP</legend>
|
||||
<div className="grid grid-cols-3 gap-3 mt-1">
|
||||
{field('Host', 'smtp_host', 'text', 'smtp.gmail.com')}
|
||||
{field('Porto', 'smtp_port', 'number')}
|
||||
{field('Utilizador', 'smtp_user', 'text')}
|
||||
<div className="col-span-3">
|
||||
{field('Password', 'smtp_password', 'password')}
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset className="border border-gray-200 rounded-md p-3">
|
||||
<legend className="text-xs font-semibold text-gray-500 px-1">BizDocs</legend>
|
||||
<div className="grid grid-cols-2 gap-3 mt-1">
|
||||
{field('API Key', 'bizdocs_api_key', 'password')}
|
||||
{field('Entity ID', 'bizdocs_entity_id')}
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<label className="flex items-center gap-2 cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={form.active}
|
||||
onChange={(e) => set('active', e.target.checked)}
|
||||
className="rounded"
|
||||
/>
|
||||
<span className="text-sm">Conta ativa</span>
|
||||
</label>
|
||||
|
||||
{error && (
|
||||
<div className="text-sm text-red-600 bg-red-50 border border-red-200 rounded-md px-3 py-2">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex justify-end gap-2 pt-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClose}
|
||||
className="px-4 py-2 text-sm border border-gray-300 rounded-md hover:bg-gray-50"
|
||||
>
|
||||
Cancelar
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={saving}
|
||||
className="px-4 py-2 text-sm bg-indigo-600 text-white rounded-md hover:bg-indigo-700 disabled:opacity-50 flex items-center gap-2"
|
||||
>
|
||||
{saving ? <Spinner className="h-4 w-4" /> : <Check size={14} />}
|
||||
Guardar
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function Accounts() {
|
||||
const { data: accounts, loading, error, reload } = useAsync<Account[]>(
|
||||
() => api.accounts.list(),
|
||||
[],
|
||||
)
|
||||
const [modal, setModal] = useState<{ open: boolean; account: Account | null }>({
|
||||
open: false,
|
||||
account: null,
|
||||
})
|
||||
const [testResults, setTestResults] = useState<Record<number, { ok: boolean; msg: string }>>({})
|
||||
const [testingId, setTestingId] = useState<number | null>(null)
|
||||
|
||||
const openNew = () => setModal({ open: true, account: null })
|
||||
const openEdit = (a: Account) => setModal({ open: true, account: a })
|
||||
const closeModal = () => setModal({ open: false, account: null })
|
||||
|
||||
const handleSave = async (data: AccountCreate) => {
|
||||
if (modal.account) {
|
||||
await api.accounts.update(modal.account.id, data)
|
||||
} else {
|
||||
await api.accounts.create(data)
|
||||
}
|
||||
reload()
|
||||
}
|
||||
|
||||
const handleDelete = async (id: number) => {
|
||||
if (!confirm('Eliminar esta conta?')) return
|
||||
await api.accounts.delete(id)
|
||||
reload()
|
||||
}
|
||||
|
||||
const handleTest = async (id: number) => {
|
||||
setTestingId(id)
|
||||
try {
|
||||
const r = await api.connection.testAccount(id)
|
||||
const ok = r.imap_ok && r.smtp_ok && r.bizdocs_ok
|
||||
const parts: string[] = []
|
||||
if (!r.imap_ok) parts.push(`IMAP: ${r.imap_error}`)
|
||||
if (!r.smtp_ok) parts.push(`SMTP: ${r.smtp_error}`)
|
||||
if (!r.bizdocs_ok) parts.push(`BizDocs: ${r.bizdocs_error}`)
|
||||
setTestResults((prev) => ({
|
||||
...prev,
|
||||
[id]: { ok, msg: ok ? 'Ligação OK' : parts.join(' | ') },
|
||||
}))
|
||||
} catch (e) {
|
||||
setTestResults((prev) => ({
|
||||
...prev,
|
||||
[id]: { ok: false, msg: e instanceof Error ? e.message : 'Erro' },
|
||||
}))
|
||||
} finally {
|
||||
setTestingId(null)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold">Contas</h2>
|
||||
<p className="text-sm text-gray-500 mt-0.5">Contas IMAP configuradas</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={openNew}
|
||||
className="flex items-center gap-2 px-3 py-1.5 bg-indigo-600 text-white text-sm rounded-md hover:bg-indigo-700"
|
||||
>
|
||||
<Plus size={14} /> Nova conta
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{loading && (
|
||||
<div className="flex justify-center py-12">
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{accounts && accounts.length === 0 && (
|
||||
<div className="text-center py-12 text-gray-400 text-sm">
|
||||
Nenhuma conta configurada. Clique em "Nova conta" para começar.
|
||||
</div>
|
||||
)}
|
||||
|
||||
{accounts && accounts.length > 0 && (
|
||||
<div className="bg-white border border-gray-200 rounded-lg overflow-hidden">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 border-b border-gray-200">
|
||||
<tr>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Nome</th>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Email</th>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">IMAP</th>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Estado</th>
|
||||
<th className="text-right px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Ações</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{accounts.map((a) => (
|
||||
<tr key={a.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-3 font-medium">{a.name}</td>
|
||||
<td className="px-4 py-3 text-gray-600">{a.email}</td>
|
||||
<td className="px-4 py-3 text-gray-500 text-xs">
|
||||
{a.imap_host}:{a.imap_port}
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<StatusBadge status={a.active ? 'active' : 'inactive'} />
|
||||
{testResults[a.id] && (
|
||||
<span
|
||||
className={`text-xs ${testResults[a.id].ok ? 'text-green-600' : 'text-red-600'}`}
|
||||
>
|
||||
{testResults[a.id].msg}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div className="flex items-center justify-end gap-1">
|
||||
<button
|
||||
title="Testar ligação"
|
||||
onClick={() => handleTest(a.id)}
|
||||
disabled={testingId === a.id}
|
||||
className="p-1.5 text-gray-400 hover:text-indigo-600 disabled:opacity-40"
|
||||
>
|
||||
{testingId === a.id ? <Spinner className="h-4 w-4" /> : <Wifi size={14} />}
|
||||
</button>
|
||||
<button
|
||||
title="Editar"
|
||||
onClick={() => openEdit(a)}
|
||||
className="p-1.5 text-gray-400 hover:text-indigo-600"
|
||||
>
|
||||
<Pencil size={14} />
|
||||
</button>
|
||||
<button
|
||||
title="Eliminar"
|
||||
onClick={() => handleDelete(a.id)}
|
||||
className="p-1.5 text-gray-400 hover:text-red-600"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{modal.open && (
|
||||
<AccountModal
|
||||
initial={
|
||||
modal.account
|
||||
? {
|
||||
name: modal.account.name,
|
||||
email: modal.account.email,
|
||||
imap_host: modal.account.imap_host,
|
||||
imap_port: modal.account.imap_port,
|
||||
imap_user: modal.account.imap_user,
|
||||
smtp_host: modal.account.smtp_host,
|
||||
smtp_port: modal.account.smtp_port,
|
||||
smtp_user: modal.account.smtp_user,
|
||||
bizdocs_entity_id: modal.account.bizdocs_entity_id,
|
||||
active: modal.account.active,
|
||||
}
|
||||
: null
|
||||
}
|
||||
onSave={handleSave}
|
||||
onClose={closeModal}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
import { useState } from 'react'
|
||||
import { api, TestPayload, ConnectionTestResult } from '../lib/api'
|
||||
import Spinner from '../components/Spinner'
|
||||
import { CheckCircle, XCircle, Wifi } from 'lucide-react'
|
||||
|
||||
const EMPTY: TestPayload = {
|
||||
imap_host: '',
|
||||
imap_port: 993,
|
||||
imap_user: '',
|
||||
imap_password: '',
|
||||
smtp_host: '',
|
||||
smtp_port: 587,
|
||||
smtp_user: '',
|
||||
smtp_password: '',
|
||||
bizdocs_api_key: '',
|
||||
bizdocs_entity_id: '',
|
||||
}
|
||||
|
||||
function ResultRow({ label, ok, error }: { label: string; ok: boolean; error: string | null }) {
|
||||
return (
|
||||
<div className={`flex items-start gap-3 p-3 rounded-md ${ok ? 'bg-green-50' : 'bg-red-50'}`}>
|
||||
{ok ? (
|
||||
<CheckCircle size={18} className="text-green-600 mt-0.5 shrink-0" />
|
||||
) : (
|
||||
<XCircle size={18} className="text-red-500 mt-0.5 shrink-0" />
|
||||
)}
|
||||
<div>
|
||||
<p className={`text-sm font-medium ${ok ? 'text-green-800' : 'text-red-800'}`}>{label}</p>
|
||||
{!ok && error && <p className="text-xs text-red-600 mt-0.5">{error}</p>}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function ConnectionTest() {
|
||||
const [form, setForm] = useState<TestPayload>(EMPTY)
|
||||
const [testing, setTesting] = useState(false)
|
||||
const [result, setResult] = useState<ConnectionTestResult | null>(null)
|
||||
const [apiError, setApiError] = useState<string | null>(null)
|
||||
|
||||
const set = (k: keyof TestPayload, v: string | number) =>
|
||||
setForm((f) => ({ ...f, [k]: v }))
|
||||
|
||||
const handleTest = async (e: React.FormEvent) => {
|
||||
e.preventDefault()
|
||||
setTesting(true)
|
||||
setResult(null)
|
||||
setApiError(null)
|
||||
try {
|
||||
const r = await api.connection.test(form)
|
||||
setResult(r)
|
||||
} catch (e) {
|
||||
setApiError(e instanceof Error ? e.message : String(e))
|
||||
} finally {
|
||||
setTesting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const field = (
|
||||
label: string,
|
||||
key: keyof TestPayload,
|
||||
type = 'text',
|
||||
placeholder = '',
|
||||
) => (
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-gray-700 mb-1">{label}</label>
|
||||
<input
|
||||
type={type}
|
||||
value={String(form[key])}
|
||||
placeholder={placeholder}
|
||||
onChange={(e) => set(key, type === 'number' ? Number(e.target.value) : e.target.value)}
|
||||
className="w-full border border-gray-300 rounded-md px-3 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="p-6 max-w-2xl">
|
||||
<div className="mb-6">
|
||||
<h2 className="text-xl font-semibold">Teste de Ligação</h2>
|
||||
<p className="text-sm text-gray-500 mt-0.5">
|
||||
Verifica a conectividade IMAP, SMTP e BizDocs sem guardar dados
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleTest} className="space-y-4">
|
||||
<fieldset className="border border-gray-200 rounded-md p-4">
|
||||
<legend className="text-xs font-semibold text-gray-500 px-1">IMAP</legend>
|
||||
<div className="grid grid-cols-3 gap-3 mt-1">
|
||||
{field('Host', 'imap_host', 'text', 'imap.gmail.com')}
|
||||
{field('Porto', 'imap_port', 'number')}
|
||||
{field('Utilizador', 'imap_user', 'email', '[email protected]')}
|
||||
<div className="col-span-3">
|
||||
{field('Password', 'imap_password', 'password')}
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset className="border border-gray-200 rounded-md p-4">
|
||||
<legend className="text-xs font-semibold text-gray-500 px-1">SMTP</legend>
|
||||
<div className="grid grid-cols-3 gap-3 mt-1">
|
||||
{field('Host', 'smtp_host', 'text', 'smtp.gmail.com')}
|
||||
{field('Porto', 'smtp_port', 'number')}
|
||||
{field('Utilizador', 'smtp_user', 'email', '[email protected]')}
|
||||
<div className="col-span-3">
|
||||
{field('Password', 'smtp_password', 'password')}
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<fieldset className="border border-gray-200 rounded-md p-4">
|
||||
<legend className="text-xs font-semibold text-gray-500 px-1">BizDocs</legend>
|
||||
<div className="grid grid-cols-2 gap-3 mt-1">
|
||||
{field('API Key', 'bizdocs_api_key', 'password')}
|
||||
{field('Entity ID', 'bizdocs_entity_id')}
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={testing}
|
||||
className="flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white text-sm rounded-md hover:bg-indigo-700 disabled:opacity-50"
|
||||
>
|
||||
{testing ? <Spinner className="h-4 w-4" /> : <Wifi size={16} />}
|
||||
Testar ligação
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{apiError && (
|
||||
<div className="mt-4 bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md text-sm">
|
||||
Erro: {apiError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{result && (
|
||||
<div className="mt-6 space-y-2">
|
||||
<h3 className="text-sm font-semibold text-gray-700 mb-3">Resultado</h3>
|
||||
<ResultRow label="IMAP" ok={result.imap_ok} error={result.imap_error} />
|
||||
<ResultRow label="SMTP" ok={result.smtp_ok} error={result.smtp_error} />
|
||||
<ResultRow label="BizDocs API" ok={result.bizdocs_ok} error={result.bizdocs_error} />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import { api, Stats } from '../lib/api'
|
||||
import { useAsync } from '../hooks/useApi'
|
||||
import Spinner from '../components/Spinner'
|
||||
import { RefreshCw } from 'lucide-react'
|
||||
|
||||
function StatCard({ label, value, sub }: { label: string; value: number | string; sub?: string }) {
|
||||
return (
|
||||
<div className="bg-white rounded-lg border border-gray-200 p-5">
|
||||
<p className="text-sm text-gray-500">{label}</p>
|
||||
<p className="text-3xl font-bold mt-1">{value}</p>
|
||||
{sub && <p className="text-xs text-gray-400 mt-1">{sub}</p>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function Dashboard() {
|
||||
const { data: stats, loading, error, reload } = useAsync<Stats>(() => api.stats.get(), [])
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold">Dashboard</h2>
|
||||
<p className="text-sm text-gray-500 mt-0.5">Estado geral do forwarder</p>
|
||||
</div>
|
||||
<button
|
||||
onClick={reload}
|
||||
className="flex items-center gap-2 text-sm px-3 py-1.5 border border-gray-300 rounded-md hover:bg-gray-50"
|
||||
>
|
||||
<RefreshCw size={14} className={loading ? 'animate-spin' : ''} />
|
||||
Atualizar
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{loading && !stats && (
|
||||
<div className="flex justify-center py-12">
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md text-sm">
|
||||
Erro ao carregar estatísticas: {error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{stats && (
|
||||
<>
|
||||
<div className="grid grid-cols-2 lg:grid-cols-3 gap-4 mb-6">
|
||||
<StatCard label="Contas total" value={stats.accounts_total} />
|
||||
<StatCard label="Contas ativas" value={stats.accounts_active} />
|
||||
<StatCard label="Mensagens total" value={stats.messages_total} />
|
||||
<StatCard
|
||||
label="Pendentes"
|
||||
value={stats.messages_pending}
|
||||
sub="aguarda reenvio"
|
||||
/>
|
||||
<StatCard
|
||||
label="Reencaminhadas"
|
||||
value={stats.messages_forwarded}
|
||||
sub="com sucesso"
|
||||
/>
|
||||
<StatCard
|
||||
label="Falhadas"
|
||||
value={stats.messages_failed}
|
||||
sub="com erro"
|
||||
/>
|
||||
</div>
|
||||
{stats.last_sync_at && (
|
||||
<p className="text-xs text-gray-400">
|
||||
Última sincronização: {new Date(stats.last_sync_at).toLocaleString('pt-PT')}
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
import { useState } from 'react'
|
||||
import { api, Message } from '../lib/api'
|
||||
import { useAsync } from '../hooks/useApi'
|
||||
import Spinner from '../components/Spinner'
|
||||
import StatusBadge from '../components/StatusBadge'
|
||||
import { RefreshCw, RotateCcw } from 'lucide-react'
|
||||
|
||||
export default function Emails() {
|
||||
const [filterStatus, setFilterStatus] = useState('')
|
||||
const [retryingId, setRetryingId] = useState<number | null>(null)
|
||||
|
||||
const { data: messages, loading, error, reload } = useAsync<Message[]>(
|
||||
() => api.messages.list({ status: filterStatus || undefined, limit: 100 }),
|
||||
[filterStatus],
|
||||
)
|
||||
|
||||
const handleRetry = async (id: number) => {
|
||||
setRetryingId(id)
|
||||
try {
|
||||
await api.messages.retry(id)
|
||||
reload()
|
||||
} finally {
|
||||
setRetryingId(null)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold">Emails</h2>
|
||||
<p className="text-sm text-gray-500 mt-0.5">Histórico de mensagens processadas</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<select
|
||||
value={filterStatus}
|
||||
onChange={(e) => setFilterStatus(e.target.value)}
|
||||
className="border border-gray-300 rounded-md px-3 py-1.5 text-sm focus:outline-none focus:ring-2 focus:ring-indigo-500"
|
||||
>
|
||||
<option value="">Todos os estados</option>
|
||||
<option value="pending">Pendente</option>
|
||||
<option value="forwarded">Reencaminhado</option>
|
||||
<option value="failed">Falhado</option>
|
||||
</select>
|
||||
<button
|
||||
onClick={reload}
|
||||
className="flex items-center gap-2 text-sm px-3 py-1.5 border border-gray-300 rounded-md hover:bg-gray-50"
|
||||
>
|
||||
<RefreshCw size={14} className={loading ? 'animate-spin' : ''} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loading && !messages && (
|
||||
<div className="flex justify-center py-12">
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{messages && messages.length === 0 && (
|
||||
<div className="text-center py-12 text-gray-400 text-sm">
|
||||
Nenhuma mensagem{filterStatus ? ` com estado "${filterStatus}"` : ''}.
|
||||
</div>
|
||||
)}
|
||||
|
||||
{messages && messages.length > 0 && (
|
||||
<div className="bg-white border border-gray-200 rounded-lg overflow-hidden">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 border-b border-gray-200">
|
||||
<tr>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Conta</th>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Assunto</th>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Remetente</th>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Recebido</th>
|
||||
<th className="text-left px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Estado</th>
|
||||
<th className="text-right px-4 py-3 text-xs font-medium text-gray-500 uppercase tracking-wide">Ações</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{messages.map((m) => (
|
||||
<tr key={m.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-3 text-gray-500 text-xs">{m.account_name}</td>
|
||||
<td className="px-4 py-3 font-medium truncate max-w-xs" title={m.subject}>
|
||||
{m.subject || '(sem assunto)'}
|
||||
</td>
|
||||
<td className="px-4 py-3 text-gray-600 text-xs truncate max-w-[180px]">{m.sender}</td>
|
||||
<td className="px-4 py-3 text-gray-500 text-xs whitespace-nowrap">
|
||||
{new Date(m.received_at).toLocaleString('pt-PT')}
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<div>
|
||||
<StatusBadge status={m.status} />
|
||||
{m.error && (
|
||||
<p className="text-xs text-red-500 mt-0.5 truncate max-w-xs" title={m.error}>
|
||||
{m.error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-right">
|
||||
{m.status === 'failed' && (
|
||||
<button
|
||||
title="Tentar novamente"
|
||||
onClick={() => handleRetry(m.id)}
|
||||
disabled={retryingId === m.id}
|
||||
className="p-1.5 text-gray-400 hover:text-indigo-600 disabled:opacity-40"
|
||||
>
|
||||
{retryingId === m.id ? (
|
||||
<Spinner className="h-4 w-4" />
|
||||
) : (
|
||||
<RotateCcw size={14} />
|
||||
)}
|
||||
</button>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: ['./index.html', './src/**/*.{ts,tsx}'],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx",
|
||||
"strict": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": [{ "path": "./tsconfig.node.json" }]
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"skipLibCheck": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": ["vite.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
port: 5173,
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:8000',
|
||||
changeOrigin: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
build: {
|
||||
outDir: 'dist',
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user