import { useEffect } from 'react'; import { api, type Conversation, type Hotel, type Knowledge, type Mailboxes } from './api'; type Run = (action: () => Promise) => Promise; export function ReplyActions({message,hotel,mailboxes,knowledge,dirty,busy,run,onUpdate}:{message:Conversation;hotel:Hotel;mailboxes:Mailboxes;knowledge:Knowledge[];dirty:boolean;busy:boolean;run:Run;onUpdate:(c:Conversation)=>void}) { const delivery=message.delivery; useEffect(()=>{ if(!delivery || !['Pending','Sending'].includes(delivery.state))return; let active=true; const timer=setInterval(()=>{api(`/conversations/${message.id}`).then(c=>{if(active)onUpdate(c);}).catch(()=>{});},5000); return()=>{active=false;clearInterval(timer);}; },[message.id,delivery?.state,onUpdate]); const generate=()=>run(async()=>{ if(message.draft && !window.confirm('Replace the saved draft with a new AI suggestion?'))return; onUpdate(await api(`/conversations/${message.id}/generate`,'POST',{version:message.version})); }); const send=()=>run(async()=>{ if(!window.confirm(`Send this saved reply to ${message.replyAddress}?\n\n${message.draft}\n\nThis submits the reply to Gmail. You cannot undo it here.`))return; onUpdate(await api(`/conversations/${message.id}/send`,'POST',{version:message.version,recipient:message.replyAddress})); }); const act=(action:string)=>run(async()=>onUpdate(await api(`/conversations/${message.id}/delivery/${action}`,'POST',{version:message.version}))); const canSend=hotel.staffSendingEnabled&&mailboxes.sendingConfigured&&mailboxes.items.some(m=>m.id===message.mailboxId&&m.canSend&&m.status==='Connected'); return
{message.autoReplyDetail&&

FAQ check: {message.autoReplyDetail}

} {delivery?.automatic&&

This reply uses an owner-approved FAQ rule.

} {message.draftReviewNote&&

AI review: {message.draftReviewNote}

} {!!message.draftSources?.length&&
Hotel answers referenced by this draft{message.draftSources.map(id=>{const source=knowledge.find(k=>k.id===id);return
{source?.title||'Answer no longer available'}

{source?.answer||'Ask your hotel owner to check this information.'}

{source&&!source.approved&&

This answer is no longer approved. Check the draft before sending.

}
;})}
} {delivery?
Delivery: {delivery.state==='NeedsReview'?'Needs verification':delivery.state}

{delivery.detail}

To: {delivery.recipient}

{delivery.state==='Rejected'&&}{delivery.state==='NeedsReview'&&<>

Reference: {delivery.messageId}. An uncertain reply is never automatically resent.

}
:<>

{dirty?'Save your changes before generating or sending.':canSend?`Reply to: ${message.replyAddress||'Unavailable — open this message in Gmail'}. This manual reply requires your approval.`:'Staff sending is not enabled for this mailbox. Your hotel owner can configure it in Settings.'}

{!hotel.aiDraftsEnabled&&

AI drafts are off. Your hotel owner can enable them after the administrator configures AI.

} }
; } export function ReplyControls({hotel,mailboxes,owner,busy,run,onSave}:{hotel:Hotel;mailboxes:Mailboxes;owner:boolean;busy:boolean;run:Run;onSave:(hotel:Hotel)=>void}) { const update=(field:'aiDraftsEnabled'|'staffSendingEnabled',value:boolean)=>run(async()=>{ if(value&&!window.confirm(field==='aiDraftsEnabled'?'Enable AI drafts? Message text and selected approved hotel answers will be sent to the configured OpenAI service when staff request a draft.':'Enable staff-approved Gmail sending for this hotel? Staff replies require review. If FAQ live mode is enabled separately, its approved rules can also send automatically.'))return; onSave(await api('/reply-controls','PUT',{version:hotel.version,aiDraftsEnabled:hotel.aiDraftsEnabled,staffSendingEnabled:hotel.staffSendingEnabled,[field]:value})); }); return

Reply controls

Staff replies require review. FAQ automation is controlled separately on the FAQ automation page.

{mailboxes.aiConfigured?'Only requested drafts use AI. Check all facts before sending.':'Your administrator must configure the AI API key and model first.'}

{mailboxes.sendingConfigured?'Reconnect Google to grant send permission if it was previously read-only.':'Your administrator must enable Google sending first.'}

; }