import { useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Search } from 'lucide-react'; import { useQuery, useZino } from '../api/provider'; import { RV_ENQUIRIES, STATES } from '../api/config'; import type { Enquiry } from '../api/types'; import { Badge, Empty, ErrorNote, StateBadge, qtyText } from '../components/ui'; /** Which enquiries are waiting on THIS user. */ function needsMe(e: Enquiry, has: (...r: string[]) => boolean): boolean { if (e.current_state_name === 'Awaiting Ops Input') return has('bunker_ops', 'trading_head'); if (e.current_state_name === 'Awaiting Offer') return has('bunker_trader', 'trading_head'); if (e.current_state_name === 'Offer Sent' && e.customer_decision === 'query') return has('bunker_trader', 'trading_head'); if (e.current_state_name === 'Confirmed') return has('bunker_ops', 'trading_head'); return false; } export function Inbox() { const { client, has } = useZino(); const nav = useNavigate(); const [state, setState] = useState('Mine'); const [q, setQ] = useState(''); const { data, error, loading } = useQuery(() => client.page(RV_ENQUIRIES, { limit: 200, sortBy: 'instance_id', sortDir: 'desc' }), [], 5000); const rows = (data?.rows ?? []) as unknown as Enquiry[]; const counts = useMemo(() => { const c: Record = { Mine: 0, All: rows.length }; for (const s of STATES) c[s] = 0; for (const r of rows) { c[r.current_state_name] = (c[r.current_state_name] ?? 0) + 1; if (needsMe(r, has)) c.Mine++; } return c; }, [rows, has]); const shown = rows.filter((r) => { if (state === 'Mine' && !needsMe(r, has)) return false; if (state !== 'Mine' && state !== 'All' && r.current_state_name !== state) return false; if (!q.trim()) return true; const hay = [r.enquiry_ref, r.vessel_name, r.imo_number, r.port, r.product, r.buyer_account, r.customer_contact].join(' ').toLowerCase(); return hay.includes(q.trim().toLowerCase()); }); const tabs = ['Mine', 'All', ...STATES]; return (

Enquiries

Every bunker enquiry received on WhatsApp and email, from first message to confirmed stem.

{tabs.map((t) => ( ))}
{error && {error}}
{shown.map((r) => ( nav(`/enquiries/${r.instance_id}`)} className="cursor-pointer border-b border-line last:border-0 hover:bg-surface-2"> ))}
ReferenceVesselPort ETA / LaycanProductQuantity BuyerTouch pointsStatus
{r.enquiry_ref ?? `#${r.instance_id}`} {r.vessel_name ?? -}{r.imo_number &&
IMO {r.imo_number}
}
{r.port ?? '-'} {r.eta_laycan ?? '-'} {r.product ?? '-'} {qtyText(r.min_qty_mt, r.max_qty_mt) || '-'} {r.buyer_account ?? '-'} {r.touch_points_found ?? 0}/8
{r.current_state_name === 'Offer Sent' && r.customer_decision === 'query' && Customer replied}
{!loading && shown.length === 0 && {state === 'Mine' ? 'Nothing is waiting on you.' : 'No enquiries here.'}} {loading && rows.length === 0 && Loading…}
); }