Files
pn-new-crm/src/components/email/compose-dialog.tsx

154 lines
4.5 KiB
TypeScript
Raw Normal View History

feat(platform): residential module + admin UI + reliability fixes Residential platform - New schema: residentialClients, residentialInterests (separate from marina/yacht clients) with migration 0010 - Service layer with CRUD + audit + sockets + per-port portal toggle - v1 + public API routes (/api/v1/residential/*, /api/public/residential-inquiries) - List + detail pages with inline editing for clients and interests - Per-user residentialAccess toggle on userPortRoles (migration 0011) - Permission keys: residential_clients, residential_interests - Sidebar nav + role form integration - Smoke spec covering page loads, UI create flow, public endpoint Admin & shared UI - Admin → Forms (form templates CRUD) with validators + service - Notification preferences page (in-app + email per type) - Email composition + accounts list + threads view - Branded auth shell shared across CRM + portal auth surfaces - Inline editing extended to yacht/company/interest detail pages - InlineTagEditor + per-entity tags endpoints (yachts, companies) - Notes service polymorphic across clients/interests/yachts/companies - Client list columns: yachtCount + companyCount badges - Reservation file-download via presigned URL (replaces stale <a href>) Route handler refactor - Extracted yachts/companies/berths reservation handlers to sibling handlers.ts files (Next.js 15 route.ts only allows specific exports) Reliability fixes - apiFetch double-stringify bug fixed across 13 components (apiFetch already JSON.stringifies its body; passing a stringified body produced double-encoded JSON which failed zod validation) - SocketProvider gated behind useSyncExternalStore-based mount check to avoid useSession() SSR crashes under React 19 + Next 15 - apiFetch falls back to URL-pathname → port-id resolution when the Zustand store hasn't hydrated yet (fresh contexts, e2e tests) - CRM invite flow (schema, service, route, email, dev script) - Dashboard route → [portSlug]/dashboard/page.tsx + redirect - Document the dev-server restart-after-migration gotcha in CLAUDE.md Tests - 5-case residential smoke spec - Integration test updates for new service signatures Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 21:54:32 +02:00
'use client';
import { useState } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { apiFetch } from '@/lib/api/client';
interface Account {
id: string;
emailAddress: string;
isActive: boolean;
}
interface ComposeDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
defaultTo?: string;
defaultSubject?: string;
}
export function ComposeDialog({
open,
onOpenChange,
defaultTo = '',
defaultSubject = '',
}: ComposeDialogProps) {
const qc = useQueryClient();
const [accountId, setAccountId] = useState('');
const [to, setTo] = useState(defaultTo);
const [cc, setCc] = useState('');
const [subject, setSubject] = useState(defaultSubject);
const [body, setBody] = useState('');
const { data: accounts = [] } = useQuery<Account[]>({
queryKey: ['email', 'accounts'],
queryFn: () => apiFetch<{ data: Account[] }>('/api/v1/email/accounts').then((r) => r.data),
});
const activeAccounts = accounts.filter((a) => a.isActive);
const sendMutation = useMutation({
mutationFn: () =>
apiFetch('/api/v1/email/compose', {
method: 'POST',
body: {
accountId,
to: to
.split(',')
.map((s) => s.trim())
.filter(Boolean),
cc: cc
? cc
.split(',')
.map((s) => s.trim())
.filter(Boolean)
: undefined,
subject,
bodyHtml: body.replace(/\n/g, '<br>'),
},
}),
onSuccess: () => {
toast.success('Email sent');
qc.invalidateQueries({ queryKey: ['email', 'threads'] });
onOpenChange(false);
setTo('');
setCc('');
setSubject('');
setBody('');
},
onError: (err) => toast.error(err instanceof Error ? err.message : 'Send failed'),
});
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>Compose email</DialogTitle>
</DialogHeader>
<div className="space-y-3">
<div className="space-y-1">
<Label>From</Label>
<Select value={accountId} onValueChange={setAccountId}>
<SelectTrigger>
<SelectValue placeholder="Choose account" />
</SelectTrigger>
<SelectContent>
{activeAccounts.length === 0 ? (
<div className="px-2 py-1.5 text-sm text-muted-foreground">
No active accounts
</div>
) : (
activeAccounts.map((a) => (
<SelectItem key={a.id} value={a.id}>
{a.emailAddress}
</SelectItem>
))
)}
</SelectContent>
</Select>
</div>
<div className="space-y-1">
<Label>To (comma-separated)</Label>
<Input value={to} onChange={(e) => setTo(e.target.value)} />
</div>
<div className="space-y-1">
<Label>CC</Label>
<Input value={cc} onChange={(e) => setCc(e.target.value)} />
</div>
<div className="space-y-1">
<Label>Subject</Label>
<Input value={subject} onChange={(e) => setSubject(e.target.value)} />
</div>
<div className="space-y-1">
<Label>Message</Label>
<Textarea rows={10} value={body} onChange={(e) => setBody(e.target.value)} />
</div>
</div>
<DialogFooter>
<Button variant="ghost" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button
onClick={() => sendMutation.mutate()}
disabled={
sendMutation.isPending || !accountId || !to.trim() || !subject.trim() || !body.trim()
}
>
{sendMutation.isPending ? 'Sending…' : 'Send'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}