import React, { useState } from 'react'; import { Phone, MapPin, Loader2, X } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { apiFetch } from '@/lib/api'; import { cn } from '@/lib/utils'; /** * "Call" / "Visit" icon buttons for a contact. Clicking one opens an inline textarea to note * what was discussed; saving POSTs to /api/contacts/{contactId}/activities. Self-contained — * manages its own open/saving state, calls `onLogged` after a successful save so the parent can * refresh whatever activity timeline is showing. */ export default function ContactActivityLogger({ contactId, onLogged, className, iconSize = 'h-3.5 w-3.5' }) { const [openType, setOpenType] = useState(null); // 'call' | 'visit' | null const [notes, setNotes] = useState(''); const [saving, setSaving] = useState(false); const [error, setError] = useState(''); if (!contactId) return null; const toggle = (type) => (e) => { e.stopPropagation(); setError(''); setOpenType((cur) => (cur === type ? null : type)); setNotes(''); }; const cancel = (e) => { e.stopPropagation(); setOpenType(null); setNotes(''); setError(''); }; const save = async (e) => { e.stopPropagation(); if (!openType) return; setSaving(true); setError(''); try { const res = await apiFetch(`/api/contacts/${contactId}/activities`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ activity_type: openType, notes: notes.trim() }), }); const data = await res.json().catch(() => ({})); if (!res.ok) { setError(typeof data.detail === 'string' ? data.detail : 'Could not save activity'); return; } setOpenType(null); setNotes(''); onLogged && onLogged(data); } catch (err) { console.error(err); setError('Network error'); } finally { setSaving(false); } }; return (
e.stopPropagation()}>
{openType ? (
{openType === 'call' ? 'What did you discuss on the call?' : 'What did you discuss on the visit?'}