import { Head, Link, router, useForm } from '@inertiajs/react';
import {
ArrowLeft,
FileDown,
FileUp,
KeyRound,
Lock,
Send,
Unlock,
User,
} from 'lucide-react';
import type { FormEvent, ReactNode } from 'react';
import { useRef } from 'react';
import { SupportAttachmentErrors } from '@/components/support-attachment-errors';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { cn } from '@/lib/utils';
import { dashboard as adminDashboard } from '@/routes/admin';
import {
index as adminSupportIndex,
update as updateConversation,
} from '@/routes/admin/support';
import { store as storeAdminMessage } from '@/routes/admin/support/messages';
import { show as showChannel } from '@/routes/channels';
import { show as showAttachment } from '@/routes/support/attachments';
import type {
SupportAttachment,
SupportConversation,
SupportMessage,
} from '@/types';
type Props = {
conversation: SupportConversation;
};
export default function AdminSupportShow({ conversation }: Props) {
const fileInputRef = useRef(null);
const form = useForm({
body: '',
attachments: [] as File[],
});
const isClosed = conversation.status === 'closed';
function submit(event: FormEvent) {
event.preventDefault();
form.post(storeAdminMessage.url(conversation.id), {
forceFormData: true,
preserveScroll: true,
onSuccess: () => {
form.reset();
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
},
});
}
function setStatus(status: 'open' | 'closed') {
router.patch(
updateConversation(conversation.id),
{ status },
{ preserveScroll: true },
);
}
function setAttachments(files: FileList | null) {
form.setData('attachments', Array.from(files ?? []).slice(0, 3));
}
return (
<>
{conversation.category_label}
{conversation.subject}
{conversation.messages.map((message) => (
))}
{isClosed ? (
Reopen the conversation to reply.
) : (
)}
>
);
}
AdminSupportShow.layout = {
breadcrumbs: [
{
title: 'Support inbox',
href: adminSupportIndex(),
},
],
};
function MessageBubble({ message }: { message: SupportMessage }) {
const isAdmin = message.author.is_admin;
return (
{message.author.name}
{isAdmin ? 'ADMIN' : 'USER'}
{message.created_at && (
{formatDateTime(message.created_at)}
)}
{message.body}
{message.attachments && message.attachments.length > 0 && (
)}
);
}
function AttachmentList({ attachments }: { attachments: SupportAttachment[] }) {
return (
);
}
function Field({
label,
error,
children,
}: {
label: string;
error?: string;
children: ReactNode;
}) {
return (
);
}
function StatusBadge({ status }: { status: 'open' | 'closed' }) {
return (
{status === 'open' ? 'OPEN' : 'CLOSED'}
);
}
function formatDateTime(value: string): string {
return new Date(value).toLocaleString([], {
dateStyle: 'medium',
timeStyle: 'short',
});
}
function formatBytes(value: number): string {
if (value < 1024) {
return `${value} B`;
}
if (value < 1024 * 1024) {
return `${Math.round(value / 102.4) / 10} KB`;
}
return `${Math.round(value / 1024 / 102.4) / 10} MB`;
}