'use client' import { useState } from 'react' import type { PaginationListColumnType } from '@/types' import PaginatedList from '@/components/PaginatedList' import PageNavbar from '@/components/layouts/PageNavbar' import Button from '@/components/formElements/Button' import FileCheckIcon from '@/components/icons/FileCheckIcon' import Modal from '@/components/modals/Modal' import AdminTableActions from '@/components/ui/AdminTableActions' import AdminTableViewButton from '@/components/ui/AdminTableViewButton' import StatusChip from '@/components/ui/StatusChip' import axiosInstance from '@/config/axios' import useAdminMutation from '@/hooks/useAdminMutation' import useAlertModal from '@/hooks/useAlertModal' import { coerceToString } from '@/helpers' import { formatIranianMobile, formatPersianDate, truncateValue } from '@/lib/formatters' import { getBooleanStatus } from '@/constants/status' import { API_ROUTES } from '@/services/config' interface ContactMessageRow { id: string fullName: string mobile: string subject: string message: string readAt?: string | null readBy?: string | null createdAt: string [key: string]: unknown } const columns: PaginationListColumnType[] = [ { field: 'fullName', label: 'نام', filterable: false, sortable: false, }, { field: 'mobile', label: 'شماره تماس', filterable: false, sortable: false, }, { field: 'subject', label: 'موضوع', filterable: false, sortable: false, }, { field: 'message', label: 'پیام', filterable: false, sortable: false, }, { field: 'readStatus', label: 'وضعیت', filterable: true, sortable: false, type: 'select', filterItems: [ { code: 'unread', name: 'خوانده‌نشده' }, { code: 'read', name: 'خوانده‌شده' }, ], }, { field: 'createdAt', label: 'تاریخ ثبت', filterable: false, sortable: true, }, { field: 'actions', label: 'عملیات', }, ] const ContactMessagesPage = () => { const { showAlert } = useAlertModal() const { pendingId, runAction } = useAdminMutation({ url: API_ROUTES.CONTACT_MESSAGES.ADMIN_LIST }) const [selected, setSelected] = useState(null) const handleMarkRead = (message: ContactMessageRow) => { showAlert('این پیام به‌عنوان خوانده‌شده علامت‌گذاری شود؟', () => runAction(message.id, () => axiosInstance.patch(API_ROUTES.CONTACT_MESSAGES.ADMIN_READ(message.id)), 'پیام خوانده شد') ) } return (
{{ fullName: (_row, cellValue) => coerceToString(cellValue), mobile: (_row, cellValue) => {formatIranianMobile(coerceToString(cellValue))}, subject: (_row, cellValue) => coerceToString(cellValue), message: (row, cellValue) => { const message = row as ContactMessageRow const body = coerceToString(cellValue) return (
{truncateValue(body)} {body.length > 80 ? ( ) : null}
) }, readStatus: (row) => { const message = row as ContactMessageRow const { label, chipColor } = getBooleanStatus(Boolean(message.readAt), { true: 'خوانده‌شده', false: 'خوانده‌نشده', }) return ( ) }, createdAt: (_row, cellValue) => formatPersianDate(cellValue), actions: (row) => { const message = row as ContactMessageRow return ( { setSelected(message) }} /> {message.readAt ? null : ( )} ) }, }}
{ if (!open) setSelected(null) }} > {selected ? (

فرستنده

{selected.fullName}

{formatIranianMobile(selected.mobile)}

پیام

{selected.message}

{formatPersianDate(selected.createdAt)}

) : null}
) } export default ContactMessagesPage