715 lines
26 KiB
TypeScript
715 lines
26 KiB
TypeScript
import {
|
|
Alert,
|
|
Box,
|
|
Button,
|
|
Chip,
|
|
Divider,
|
|
FormControl,
|
|
IconButton,
|
|
InputAdornment,
|
|
InputLabel,
|
|
MenuItem,
|
|
Pagination,
|
|
Paper,
|
|
Select,
|
|
Stack,
|
|
TextField,
|
|
Typography,
|
|
} from '@mui/material';
|
|
import AttachFileRoundedIcon from '@mui/icons-material/AttachFileRounded';
|
|
import RefreshRoundedIcon from '@mui/icons-material/RefreshRounded';
|
|
import SendRoundedIcon from '@mui/icons-material/SendRounded';
|
|
import HeadsetMicRoundedIcon from '@mui/icons-material/HeadsetMicRounded';
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useAuth } from '../../shared/auth/AuthContext';
|
|
import { PermissionGate } from '../../shared/auth/PermissionGate';
|
|
import {
|
|
adminFetchMessages,
|
|
adminGetConversation,
|
|
adminListConversations,
|
|
adminSendAttachmentMessage,
|
|
adminSendMessage,
|
|
adminSetTyping,
|
|
adminUpdateConversation,
|
|
adminUploadFile,
|
|
adminDeleteMessage,
|
|
adminEditMessage,
|
|
attachmentFullUrl,
|
|
isSupportImage,
|
|
supportActionOpen,
|
|
} from './api/supportApi';
|
|
import { useSupportSocket } from './hooks/useSupportSocket';
|
|
import { notifySupportMessage, requestSupportNotificationPermission } from './utils/notifications';
|
|
import type { SupportConversation, SupportMessage } from './types';
|
|
import type { SupportWsServerMessage } from './ws/protocol';
|
|
|
|
const STATUS_KEYS = ['open', 'pending', 'resolved', 'closed'] as const;
|
|
|
|
const STATUS_COLORS: Record<string, 'success' | 'warning' | 'default' | 'info'> = {
|
|
open: 'success',
|
|
pending: 'warning',
|
|
resolved: 'info',
|
|
closed: 'default',
|
|
};
|
|
|
|
function AdminMessageList({
|
|
messages,
|
|
staffTyping,
|
|
t,
|
|
nowMs,
|
|
editingId,
|
|
editDraft,
|
|
onEditDraft,
|
|
onStartEdit,
|
|
onCancelEdit,
|
|
onSaveEdit,
|
|
onDelete,
|
|
busy,
|
|
}: {
|
|
messages: SupportMessage[];
|
|
staffTyping?: boolean;
|
|
t: (key: string) => string;
|
|
nowMs: number;
|
|
editingId: string | null;
|
|
editDraft: string;
|
|
onEditDraft: (value: string) => void;
|
|
onStartEdit: (msg: SupportMessage) => void;
|
|
onCancelEdit: () => void;
|
|
onSaveEdit: () => void;
|
|
onDelete: (msg: SupportMessage) => void;
|
|
busy: boolean;
|
|
}) {
|
|
return (
|
|
<Box sx={{ flex: 1, minHeight: 0, overflowY: 'auto', p: 2 }}>
|
|
{messages.map((msg) => {
|
|
const isStaff = msg.senderType === 'staff';
|
|
const deleted = !!msg.deletedAt;
|
|
const editing = editingId === msg.id;
|
|
const canEdit = supportActionOpen(msg.canEdit, msg.editAvailableUntil, nowMs);
|
|
const canDelete = supportActionOpen(msg.canDelete, msg.deleteAvailableUntil, nowMs);
|
|
return (
|
|
<Box key={msg.id} sx={{ display: 'flex', justifyContent: isStaff ? 'flex-end' : 'flex-start', mb: 1 }}>
|
|
<Paper
|
|
sx={{
|
|
maxWidth: '78%',
|
|
px: 1.5,
|
|
py: 1,
|
|
borderRadius: isStaff ? '14px 14px 4px 14px' : '14px 14px 14px 4px',
|
|
bgcolor: isStaff ? 'primary.dark' : 'background.default',
|
|
}}
|
|
>
|
|
{!isStaff && (
|
|
<Typography variant="caption" sx={{ fontWeight: 700, opacity: 0.7 }}>
|
|
{msg.senderType === 'visitor' ? t('admin.supportAdmin.visitor') : t('admin.supportAdmin.user')}
|
|
</Typography>
|
|
)}
|
|
{deleted ? (
|
|
<Typography variant="body2" sx={{ fontStyle: 'italic', opacity: 0.7 }}>
|
|
{t('admin.supportAdmin.deleted')}
|
|
</Typography>
|
|
) : editing ? (
|
|
<Stack spacing={1}>
|
|
<TextField
|
|
size="small"
|
|
multiline
|
|
maxRows={4}
|
|
value={editDraft}
|
|
onChange={(e) => onEditDraft(e.target.value)}
|
|
/>
|
|
<Stack direction="row" spacing={1} justifyContent="flex-end">
|
|
<Button size="small" onClick={onCancelEdit} disabled={busy}>
|
|
{t('common.cancel')}
|
|
</Button>
|
|
<Button size="small" variant="contained" onClick={onSaveEdit} disabled={busy || !editDraft.trim()}>
|
|
{t('admin.supportAdmin.edit')}
|
|
</Button>
|
|
</Stack>
|
|
</Stack>
|
|
) : (
|
|
msg.body && (
|
|
<Typography variant="body2" sx={{ whiteSpace: 'pre-wrap' }}>
|
|
{msg.body}
|
|
</Typography>
|
|
)
|
|
)}
|
|
{!deleted && msg.attachmentUrl &&
|
|
(isSupportImage(msg) ? (
|
|
<Box
|
|
component="a"
|
|
href={attachmentFullUrl(msg.attachmentUrl)}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
sx={{ display: 'block', mt: msg.body ? 0.5 : 0 }}
|
|
>
|
|
<Box
|
|
component="img"
|
|
src={attachmentFullUrl(msg.attachmentUrl)}
|
|
alt={msg.attachmentName ?? t('admin.supportAdmin.visitor')}
|
|
sx={{
|
|
maxWidth: '100%',
|
|
maxHeight: 280,
|
|
borderRadius: 1.5,
|
|
display: 'block',
|
|
}}
|
|
/>
|
|
</Box>
|
|
) : (
|
|
<Typography
|
|
component="a"
|
|
href={attachmentFullUrl(msg.attachmentUrl)}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
variant="body2"
|
|
sx={{ display: 'block', mt: 0.5 }}
|
|
>
|
|
📎 {msg.attachmentName}
|
|
</Typography>
|
|
))}
|
|
<Stack direction="row" spacing={0.5} sx={{ alignItems: 'center', justifyContent: 'flex-end', mt: 0.5 }}>
|
|
{isStaff && !deleted && (canEdit || canDelete) && !editing && (
|
|
<>
|
|
{canEdit && (
|
|
<Button size="small" onClick={() => onStartEdit(msg)} disabled={busy}>
|
|
{t('admin.supportAdmin.edit')}
|
|
</Button>
|
|
)}
|
|
{canDelete && (
|
|
<Button size="small" color="error" onClick={() => onDelete(msg)} disabled={busy}>
|
|
{t('admin.supportAdmin.delete')}
|
|
</Button>
|
|
)}
|
|
</>
|
|
)}
|
|
<Typography variant="caption" sx={{ opacity: 0.6 }}>
|
|
{msg.editedAt && !deleted ? `${t('admin.supportAdmin.edited')} · ` : ''}
|
|
{new Date(msg.createdAt).toLocaleString()}
|
|
{msg.readAt ? t('admin.supportAdmin.read') : ''}
|
|
</Typography>
|
|
</Stack>
|
|
</Paper>
|
|
</Box>
|
|
);
|
|
})}
|
|
{staffTyping && (
|
|
<Typography variant="caption" sx={{ opacity: 0.6, fontStyle: 'italic' }}>
|
|
{t('admin.supportAdmin.visitorTyping')}
|
|
</Typography>
|
|
)}
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
/** Admin / moderator support inbox. */
|
|
export function SupportAdminPage() {
|
|
const { t } = useTranslation();
|
|
const { auth } = useAuth();
|
|
const qc = useQueryClient();
|
|
const [selectedId, setSelectedId] = useState<string | null>(null);
|
|
const [statusFilter, setStatusFilter] = useState<string>('');
|
|
const [search, setSearch] = useState('');
|
|
const [page, setPage] = useState(1);
|
|
const [draft, setDraft] = useState('');
|
|
const [messages, setMessages] = useState<SupportMessage[]>([]);
|
|
const [nowMs, setNowMs] = useState(() => Date.now());
|
|
const [editingId, setEditingId] = useState<string | null>(null);
|
|
const [editDraft, setEditDraft] = useState('');
|
|
const [userTyping, setUserTyping] = useState(false);
|
|
const fileRef = useRef<HTMLInputElement>(null);
|
|
const typingTimeout = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
const handleWsEvent = useCallback(
|
|
(msg: SupportWsServerMessage) => {
|
|
if (
|
|
msg.type === 'conversation.updated' ||
|
|
msg.type === 'message.new' ||
|
|
msg.type === 'message.updated' ||
|
|
msg.type === 'message.deleted'
|
|
) {
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-conversations'] });
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-unread'] });
|
|
}
|
|
if (
|
|
(msg.type === 'message.updated' || msg.type === 'message.deleted') &&
|
|
msg.conversationId === selectedId &&
|
|
msg.message
|
|
) {
|
|
setMessages((prev) => {
|
|
const index = prev.findIndex((m) => m.id === msg.message!.id);
|
|
if (index === -1) return [...prev, msg.message!];
|
|
const copy = [...prev];
|
|
copy[index] = msg.message!;
|
|
return copy;
|
|
});
|
|
}
|
|
if (msg.type === 'message.new') {
|
|
if (msg.conversationId === selectedId && msg.message) {
|
|
setMessages((prev) => {
|
|
if (prev.some((m) => m.id === msg.message!.id)) return prev;
|
|
return [...prev, msg.message!];
|
|
});
|
|
} else if (msg.message && msg.message.senderType !== 'staff') {
|
|
notifySupportMessage({
|
|
title: t('admin.supportAdmin.newMessageTitle'),
|
|
body: msg.message.body ?? t('admin.supportAdmin.newMessageBody'),
|
|
tag: `admin-support-${msg.conversationId}`,
|
|
});
|
|
}
|
|
if (msg.conversationId === selectedId && !msg.message) {
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-messages', selectedId] });
|
|
}
|
|
}
|
|
if (msg.type === 'typing' && msg.conversationId === selectedId && msg.party === 'user') {
|
|
setUserTyping(msg.active);
|
|
}
|
|
},
|
|
[qc, selectedId]
|
|
);
|
|
|
|
const { connected: wsConnected } = useSupportSocket({
|
|
inbox: true,
|
|
conversationId: selectedId,
|
|
onEvent: handleWsEvent,
|
|
});
|
|
|
|
useEffect(() => {
|
|
void requestSupportNotificationPermission();
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
const id = window.setInterval(() => setNowMs(Date.now()), 5000);
|
|
return () => window.clearInterval(id);
|
|
}, []);
|
|
|
|
const listQ = useQuery({
|
|
queryKey: ['admin-support-conversations', statusFilter, search, page],
|
|
queryFn: () =>
|
|
adminListConversations({
|
|
status: statusFilter || undefined,
|
|
search: search || undefined,
|
|
page,
|
|
pageSize: 25,
|
|
}),
|
|
refetchInterval: wsConnected ? 30_000 : 5000,
|
|
});
|
|
|
|
const detailQ = useQuery({
|
|
queryKey: ['admin-support-detail', selectedId],
|
|
queryFn: () => adminGetConversation(selectedId!),
|
|
enabled: !!selectedId,
|
|
});
|
|
|
|
const messagesQ = useQuery({
|
|
queryKey: ['admin-support-messages', selectedId],
|
|
queryFn: () => adminFetchMessages(selectedId!),
|
|
enabled: !!selectedId,
|
|
refetchInterval: wsConnected ? false : 2500,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (messagesQ.data?.messages) setMessages(messagesQ.data.messages);
|
|
if (messagesQ.data?.typing) setUserTyping(messagesQ.data.typing.userTyping);
|
|
}, [messagesQ.data?.messages, messagesQ.data?.typing]);
|
|
|
|
useEffect(() => {
|
|
if (!selectedId && listQ.data?.items[0]) {
|
|
setSelectedId(listQ.data.items[0].id);
|
|
}
|
|
}, [listQ.data?.items, selectedId]);
|
|
|
|
const updateMut = useMutation({
|
|
mutationFn: (patch: { status?: string; assignedTo?: string | null }) =>
|
|
adminUpdateConversation(selectedId!, patch),
|
|
onSuccess: () => {
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-conversations'] });
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-detail', selectedId] });
|
|
},
|
|
});
|
|
|
|
const sendMut = useMutation({
|
|
mutationFn: (body: string) => adminSendMessage(selectedId!, body),
|
|
onSuccess: () => {
|
|
setDraft('');
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-messages', selectedId] });
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-conversations'] });
|
|
},
|
|
});
|
|
|
|
const attachMut = useMutation({
|
|
mutationFn: async (file: File) => {
|
|
const uploaded = await adminUploadFile(selectedId!, file);
|
|
return adminSendAttachmentMessage(selectedId!, uploaded);
|
|
},
|
|
onSuccess: () => {
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-messages', selectedId] });
|
|
},
|
|
});
|
|
|
|
const editMut = useMutation({
|
|
mutationFn: (input: { messageId: string; body: string }) =>
|
|
adminEditMessage(selectedId!, input.messageId, input.body),
|
|
onSuccess: (msg) => {
|
|
setEditingId(null);
|
|
setMessages((prev) => prev.map((m) => (m.id === msg.id ? msg : m)));
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-conversations'] });
|
|
},
|
|
});
|
|
|
|
const deleteMut = useMutation({
|
|
mutationFn: (messageId: string) => adminDeleteMessage(selectedId!, messageId),
|
|
onSuccess: (msg) => {
|
|
setMessages((prev) => prev.map((m) => (m.id === msg.id ? msg : m)));
|
|
void qc.invalidateQueries({ queryKey: ['admin-support-conversations'] });
|
|
},
|
|
});
|
|
|
|
const notifyTyping = useCallback(
|
|
(active: boolean) => {
|
|
if (!selectedId) return;
|
|
void adminSetTyping(selectedId, active);
|
|
},
|
|
[selectedId]
|
|
);
|
|
|
|
const conv = detailQ.data?.conversation;
|
|
const user = detailQ.data?.user;
|
|
|
|
return (
|
|
<Box
|
|
sx={{
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
height: { lg: 'calc(100vh - 120px)' },
|
|
minHeight: 0,
|
|
overflow: { lg: 'hidden' },
|
|
}}
|
|
>
|
|
<Stack direction="row" spacing={1} sx={{ alignItems: 'center', mb: 2, flexShrink: 0 }}>
|
|
<HeadsetMicRoundedIcon color="primary" />
|
|
<Typography variant="h4" sx={{ fontWeight: 900, flex: 1 }}>
|
|
{t('admin.supportAdmin.title')}
|
|
</Typography>
|
|
<IconButton onClick={() => void listQ.refetch()}>
|
|
<RefreshRoundedIcon />
|
|
</IconButton>
|
|
</Stack>
|
|
|
|
<Box
|
|
sx={{
|
|
display: 'grid',
|
|
gridTemplateColumns: { xs: '1fr', lg: '320px 1fr 280px' },
|
|
gap: 2,
|
|
flex: { lg: 1 },
|
|
minHeight: 0,
|
|
alignItems: 'stretch',
|
|
overflow: { lg: 'hidden' },
|
|
}}
|
|
>
|
|
{/* Conversation list */}
|
|
<Paper
|
|
sx={{
|
|
p: 2,
|
|
borderRadius: 2,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
height: { xs: 420, lg: '100%' },
|
|
maxHeight: { xs: '70vh', lg: '100%' },
|
|
minHeight: 0,
|
|
overflow: 'hidden',
|
|
}}
|
|
>
|
|
<TextField
|
|
size="small"
|
|
placeholder={t('admin.supportAdmin.searchPlaceholder')}
|
|
value={search}
|
|
onChange={(e) => {
|
|
setSearch(e.target.value);
|
|
setPage(1);
|
|
}}
|
|
sx={{ mb: 1.5 }}
|
|
/>
|
|
<FormControl size="small" sx={{ mb: 1.5 }}>
|
|
<InputLabel>{t('admin.supportAdmin.status')}</InputLabel>
|
|
<Select
|
|
label={t('admin.supportAdmin.status')}
|
|
value={statusFilter}
|
|
onChange={(e) => {
|
|
setStatusFilter(e.target.value);
|
|
setPage(1);
|
|
}}
|
|
>
|
|
<MenuItem value="">{t('admin.supportAdmin.allStatuses')}</MenuItem>
|
|
<MenuItem value="open">{t('admin.supportAdmin.statuses.open')}</MenuItem>
|
|
<MenuItem value="pending">{t('admin.supportAdmin.statuses.pending')}</MenuItem>
|
|
<MenuItem value="resolved">{t('admin.supportAdmin.statuses.resolved')}</MenuItem>
|
|
<MenuItem value="closed">{t('admin.supportAdmin.statuses.closed')}</MenuItem>
|
|
</Select>
|
|
</FormControl>
|
|
|
|
<Box sx={{ flex: 1, minHeight: 0, overflowY: 'auto' }}>
|
|
{(listQ.data?.items ?? []).map((c: SupportConversation) => (
|
|
<Box
|
|
key={c.id}
|
|
onClick={() => setSelectedId(c.id)}
|
|
sx={{
|
|
p: 1.5,
|
|
mb: 1,
|
|
borderRadius: 2,
|
|
cursor: 'pointer',
|
|
bgcolor: selectedId === c.id ? 'action.selected' : 'transparent',
|
|
border: '1px solid',
|
|
borderColor: selectedId === c.id ? 'primary.main' : 'divider',
|
|
'&:hover': { bgcolor: 'action.hover' },
|
|
}}
|
|
>
|
|
<Stack direction="row" sx={{ justifyContent: 'space-between', alignItems: 'center' }}>
|
|
<Typography variant="subtitle2" sx={{ fontWeight: 800 }}>
|
|
{c.isAnonymous ? t('admin.supportAdmin.anonymousVisitor') : t('admin.supportAdmin.userPrefix', { id: c.userId?.slice(0, 8) })}
|
|
</Typography>
|
|
{c.unreadStaff > 0 && (
|
|
<Chip size="small" color="error" label={c.unreadStaff} />
|
|
)}
|
|
</Stack>
|
|
<Typography variant="caption" sx={{ opacity: 0.7, display: 'block' }}>
|
|
{c.lastMessagePreview ?? '—'}
|
|
</Typography>
|
|
<Chip
|
|
size="small"
|
|
label={
|
|
(STATUS_KEYS as readonly string[]).includes(c.status)
|
|
? t(`admin.supportAdmin.statuses.${c.status as (typeof STATUS_KEYS)[number]}`)
|
|
: c.status
|
|
}
|
|
color={STATUS_COLORS[c.status] ?? 'default'}
|
|
sx={{ mt: 0.5 }}
|
|
/>
|
|
</Box>
|
|
))}
|
|
{!listQ.data?.items.length && (
|
|
<Typography variant="body2" sx={{ opacity: 0.6, py: 2 }}>
|
|
{t('admin.supportAdmin.noConversations')}
|
|
</Typography>
|
|
)}
|
|
</Box>
|
|
|
|
{(listQ.data?.totalPages ?? 1) > 1 && (
|
|
<Pagination
|
|
count={listQ.data?.totalPages ?? 1}
|
|
page={page}
|
|
onChange={(_, p) => setPage(p)}
|
|
size="small"
|
|
sx={{ mt: 1, alignSelf: 'center' }}
|
|
/>
|
|
)}
|
|
</Paper>
|
|
|
|
{/* Chat */}
|
|
<Paper
|
|
sx={{
|
|
borderRadius: 2,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
height: { xs: 520, lg: '100%' },
|
|
maxHeight: { xs: '80vh', lg: '100%' },
|
|
minHeight: 0,
|
|
overflow: 'hidden',
|
|
}}
|
|
>
|
|
{!selectedId ? (
|
|
<Typography sx={{ p: 3, opacity: 0.6 }}>{t('admin.supportAdmin.selectConversation')}</Typography>
|
|
) : (
|
|
<>
|
|
<PermissionGate permission="admin.support.manage">
|
|
<Stack direction="row" spacing={1} sx={{ p: 2, borderBottom: '1px solid', borderColor: 'divider' }}>
|
|
<FormControl size="small" sx={{ minWidth: 130 }}>
|
|
<InputLabel>{t('admin.supportAdmin.status')}</InputLabel>
|
|
<Select
|
|
label={t('admin.supportAdmin.status')}
|
|
value={conv?.status ?? 'open'}
|
|
onChange={(e) => updateMut.mutate({ status: e.target.value })}
|
|
>
|
|
<MenuItem value="open">{t('admin.supportAdmin.statuses.open')}</MenuItem>
|
|
<MenuItem value="pending">{t('admin.supportAdmin.statuses.pending')}</MenuItem>
|
|
<MenuItem value="resolved">{t('admin.supportAdmin.statuses.resolved')}</MenuItem>
|
|
<MenuItem value="closed">{t('admin.supportAdmin.statuses.closed')}</MenuItem>
|
|
</Select>
|
|
</FormControl>
|
|
<Button
|
|
size="small"
|
|
variant="outlined"
|
|
onClick={() => updateMut.mutate({ assignedTo: auth.userId })}
|
|
>
|
|
{t('admin.supportAdmin.assignSelf')}
|
|
</Button>
|
|
<Button
|
|
size="small"
|
|
variant="outlined"
|
|
onClick={() => updateMut.mutate({ status: 'resolved' })}
|
|
>
|
|
{t('admin.supportAdmin.markResolved')}
|
|
</Button>
|
|
</Stack>
|
|
</PermissionGate>
|
|
|
|
<AdminMessageList
|
|
messages={messages}
|
|
staffTyping={userTyping}
|
|
t={t}
|
|
nowMs={nowMs}
|
|
editingId={editingId}
|
|
editDraft={editDraft}
|
|
onEditDraft={setEditDraft}
|
|
onStartEdit={(msg) => {
|
|
setEditingId(msg.id);
|
|
setEditDraft(msg.body ?? '');
|
|
}}
|
|
onCancelEdit={() => setEditingId(null)}
|
|
onSaveEdit={() => {
|
|
if (!editingId || !editDraft.trim()) return;
|
|
editMut.mutate({ messageId: editingId, body: editDraft.trim() });
|
|
}}
|
|
onDelete={(msg) => {
|
|
if (window.confirm(t('admin.supportAdmin.confirmDelete'))) {
|
|
deleteMut.mutate(msg.id);
|
|
}
|
|
}}
|
|
busy={editMut.isPending || deleteMut.isPending}
|
|
/>
|
|
|
|
<PermissionGate
|
|
permission="admin.support.manage"
|
|
fallback={
|
|
<Box sx={{ p: 2, borderTop: '1px solid', borderColor: 'divider' }}>
|
|
<Typography variant="body2" color="text.secondary">
|
|
{t('admin.supportAdmin.readOnlyHint')}
|
|
</Typography>
|
|
</Box>
|
|
}
|
|
>
|
|
<Box sx={{ p: 1.5, borderTop: '1px solid', borderColor: 'divider' }}>
|
|
<input
|
|
ref={fileRef}
|
|
type="file"
|
|
hidden
|
|
accept="image/*,.pdf,.txt,.doc,.docx"
|
|
onChange={(e) => {
|
|
const file = e.target.files?.[0];
|
|
if (file) attachMut.mutate(file);
|
|
e.target.value = '';
|
|
}}
|
|
/>
|
|
<TextField
|
|
fullWidth
|
|
size="small"
|
|
multiline
|
|
maxRows={3}
|
|
placeholder={t('admin.supportAdmin.replyPlaceholder')}
|
|
value={draft}
|
|
onChange={(e) => {
|
|
setDraft(e.target.value);
|
|
notifyTyping(true);
|
|
if (typingTimeout.current) clearTimeout(typingTimeout.current);
|
|
typingTimeout.current = setTimeout(() => notifyTyping(false), 2000);
|
|
}}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
e.preventDefault();
|
|
if (draft.trim()) sendMut.mutate(draft.trim());
|
|
}
|
|
}}
|
|
slotProps={{
|
|
input: {
|
|
endAdornment: (
|
|
<InputAdornment position="end">
|
|
<IconButton size="small" onClick={() => fileRef.current?.click()}>
|
|
<AttachFileRoundedIcon fontSize="small" />
|
|
</IconButton>
|
|
<IconButton
|
|
size="small"
|
|
color="primary"
|
|
disabled={!draft.trim() || sendMut.isPending}
|
|
onClick={() => sendMut.mutate(draft.trim())}
|
|
>
|
|
<SendRoundedIcon fontSize="small" />
|
|
</IconButton>
|
|
</InputAdornment>
|
|
),
|
|
},
|
|
}}
|
|
/>
|
|
</Box>
|
|
</PermissionGate>
|
|
</>
|
|
)}
|
|
</Paper>
|
|
|
|
{/* User context */}
|
|
<Paper
|
|
sx={{
|
|
p: 2,
|
|
borderRadius: 2,
|
|
display: { xs: 'none', lg: 'block' },
|
|
height: '100%',
|
|
minHeight: 0,
|
|
overflowY: 'auto',
|
|
}}
|
|
>
|
|
<Typography variant="h6" sx={{ fontWeight: 800, mb: 1 }}>
|
|
{t('admin.supportAdmin.contextTitle')}
|
|
</Typography>
|
|
<Divider sx={{ mb: 2, opacity: 0.2 }} />
|
|
{conv?.isAnonymous && (
|
|
<Alert severity="info" sx={{ mb: 2 }}>
|
|
{t('admin.supportAdmin.anonymousAlert')}
|
|
</Alert>
|
|
)}
|
|
{user ? (
|
|
<Stack spacing={1}>
|
|
<Typography variant="body2">
|
|
<strong>{t('admin.supportAdmin.name')}</strong> {user.displayName ?? '—'}
|
|
</Typography>
|
|
<Typography variant="body2">
|
|
<strong>{t('admin.supportAdmin.id')}</strong> {user.id}
|
|
</Typography>
|
|
<Typography variant="body2">
|
|
<strong>{t('admin.supportAdmin.email')}</strong> {user.email ?? '—'}
|
|
</Typography>
|
|
<Typography variant="body2">
|
|
<strong>{t('admin.supportAdmin.role')}</strong> {user.role}
|
|
</Typography>
|
|
<Typography variant="body2">
|
|
<strong>{t('admin.supportAdmin.accountStatus')}</strong>{' '}
|
|
{user.isEnabled ? (
|
|
<Chip size="small" color="success" label={t('admin.supportAdmin.accountActive')} />
|
|
) : (
|
|
<Chip size="small" color="default" label={t('admin.supportAdmin.accountDisabled')} />
|
|
)}
|
|
</Typography>
|
|
<Typography variant="body2">
|
|
<strong>{t('admin.supportAdmin.emailVerified')}</strong> {user.emailVerified ? t('common.yes') : t('common.no')}
|
|
</Typography>
|
|
<Typography variant="caption" sx={{ opacity: 0.7 }}>
|
|
{t('admin.supportAdmin.registeredOn', { date: new Date(user.createdAt).toLocaleDateString() })}
|
|
</Typography>
|
|
</Stack>
|
|
) : (
|
|
!conv?.isAnonymous && (
|
|
<Typography variant="body2" sx={{ opacity: 0.6 }}>
|
|
{t('common.loading')}
|
|
</Typography>
|
|
)
|
|
)}
|
|
{conv && (
|
|
<Box sx={{ mt: 3 }}>
|
|
<Typography variant="caption" sx={{ opacity: 0.6 }}>
|
|
{t('admin.supportAdmin.conversationCreated', { date: new Date(conv.createdAt).toLocaleString() })}
|
|
</Typography>
|
|
</Box>
|
|
)}
|
|
</Paper>
|
|
</Box>
|
|
</Box>
|
|
);
|
|
}
|