import { Close, NotificationsNone } from '@mui/icons-material' import { Box, Button, IconButton, Typography } from '@mui/joy' import { useEffect, useRef, useState } from 'react' import ModalActions from '../../components/common/ModalActions' import NotificationTemplate from '../../components/NotificationTemplate' import { useResponsiveModal } from '../../hooks/useResponsiveModal' const getDisplayLabel = templates => { if (!templates || templates.length === 0) return 'Remind' const count = templates.length if (count === 1) { const n = templates[0] const numericValue = Number(n.value) if (numericValue === 0) return 'On due date' const unitName = n.unit === 'm' ? 'min' : n.unit === 'h' ? 'hr' : 'day' const absValue = Math.abs(numericValue) const plural = absValue !== 1 ? 's' : '' return `${absValue} ${unitName}${plural} ${numericValue < 0 ? 'before' : 'after'}` } return `${count} reminders` } const NotificationPickerField = ({ value, onChange, onClear, emptyDisplay = 'icon-text', size = 'sm', }) => { const [isOpen, setIsOpen] = useState(false) const latestTemplatesRef = useRef(value?.templates || []) const { ResponsiveModal } = useResponsiveModal() useEffect(() => { if (isOpen) { latestTemplatesRef.current = value?.templates || [] } }, [isOpen, value]) const templates = value?.templates || [] const hasNotifications = templates.length > 0 const shouldShowLabel = hasNotifications || emptyDisplay === 'icon-text' const displayLabel = getDisplayLabel(templates) const handleSave = () => { onChange({ ...value, templates: latestTemplatesRef.current }) setIsOpen(false) } const footer = ( { onClear?.() setIsOpen(false) }, } : undefined } secondary={{ label: 'Cancel', onClick: () => setIsOpen(false) }} primary={{ label: 'Apply', onClick: handleSave }} /> ) return ( <> {hasNotifications && onClear && ( { e.stopPropagation() onClear?.() }} sx={{ position: 'absolute', top: -18, right: -18, zIndex: 10, borderRadius: '50%', '&:hover': { bgcolor: 'danger.softBg' }, }} > )} setIsOpen(false)} title='Reminders' footer={footer} > { latestTemplatesRef.current = notifications }} showTimeline /> ) } export default NotificationPickerField