import { removeCalendar , updateCalendarUserSettings } from '@proton/shared/lib/api/calendars' ; import { MAX_CALENDARS_PER_FREE_USER , MAX_CALENDARS_PER_USER } from '@proton/shared/lib/calendar/constants' ; import { Address , UserModel } from '@proton/shared/lib/interfaces' ; import { Calendar } from '@proton/shared/lib/interfaces/calendar' ; import { useState } from 'react' ; import { c } from 'ttag' ; import { Alert , ConfirmModal , ErrorButton } from '../../../components' ; import { useApi , useEventManager , useModals , useNotifications } from '../../../hooks' ; import { CalendarModal } from '../calendarModal/CalendarModal' ; import { ExportModal } from '../exportModal/ExportModal' ; import CalendarsSection from './CalendarsSection' ; export interface PersonalCalendarsSectionProps { activeAddresses : Address [ ] ; calendars : Calendar [ ] ; activeCalendars : Calendar [ ] ; defaultCalendar ? : Calendar ; user : UserModel ; } const PersonalCalendarsSection = ( { activeAddresses , calendars = [ ] , defaultCalendar , activeCalendars = [ ] , user , } ) => { const api = useApi ( ) ; const { call } = useEventManager ( ) ; const { createNotification } = useNotifications ( ) ; const { createModal } = useModals ( ) ; const [ loadingMap , setLoadingMap ] = useState ( { } ) ; const defaultCalendarID = defaultCalendar ?. ID ; const handleCreate = ( ) => { createModal ( < CalendarModal activeCalendars = { activeCalendars } defaultCalendarID = { defaultCalendarID } / > ) ; } ; const handleEdit = ( calendar ) => { createModal ( < CalendarModal calendar = { calendar } / > ) ; } ; const handleSetDefault = async ( calendarID ) => { try { setLoadingMap ( ( old ) => ( { ... old , [ calendarID ] : true } ) ) ; await api ( updateCalendarUserSettings ( { DefaultCalendarID : calendarID } ) ) ; await call ( ) ; createNotification ( { text : c ( 'Success' ) . t `Default calendar updated` } ) ; } finally { setLoadingMap ( ( old ) => ( { ... old , [ calendarID ] : false } ) ) ; } } ; const handleDelete = async ( id ) => { const isDeleteDefaultCalendar = id === defaultCalendarID ; const firstRemainingCalendar = activeCalendars . find ( ( { ID : calendarID } ) => calendarID !== id ) ; const newDefaultCalendarID = isDeleteDefaultCalendar ? ( firstRemainingCalendar && firstRemainingCalendar . ID ) || null : undefined ; await new Promise < void > ( ( resolve , reject ) => { const calendarName = firstRemainingCalendar ? ( < span key = "calendar-name" className = "text-break" > { firstRemainingCalendar . Name } < /span>) : (''); createModal ( < ConfirmModal title = { c ( 'Title' ) . t `Delete calendar` } confirm = { < ErrorButton type = "submit" > { c ( 'Action' ) . t `Delete` } < /ErrorButton>} onClose={reject} onConfirm={resolve}> < Alert className = "mb1" type = "error" > { c ( 'Info' ) . t `Are you sure you want to delete this calendar?` } < /Alert> { isDeleteDefaultCalendar && firstRemainingCalendar && ( < Alert className = "mb1" type = "warning" > { c ( 'Info' ) . jt `${ calendarName } will be set as default calendar.` } < /Alert>)} < /ConfirmModal>); } ) ; try { setLoadingMap ( ( old ) => ( { ... old , [ newDefaultCalendarID || '' ] : true , [ id ] : true , } ) ) ; await api ( removeCalendar ( id ) ) ; if ( newDefaultCalendarID !== undefined ) { await api ( updateCalendarUserSettings ( { DefaultCalendarID : newDefaultCalendarID } ) ) ; } await call ( ) ; createNotification ( { text : c ( 'Success' ) . t `Calendar removed` } ) ; } finally { setLoadingMap ( ( old ) => ( { ... old , [ newDefaultCalendarID || '' ] : false , [ id ] : false } ) ) ; } } ; const handleExport = ( calendar ) => createModal ( < ExportModal calendar = { calendar } / > ) ; const calendarsLimit = user . isFree ? MAX_CALENDARS_PER_FREE_USER : MAX_CALENDARS_PER_USER ; const calendarLimitReachedText = c ( 'Calendar limit warning' ) . t `You have reached the maximum number of personal calendars you can create within your plan.` ; const isBelowLimit = calendars . length < calendarsLimit ; return ( < CalendarsSection calendars = { calendars } user = { user } defaultCalendarID = { defaultCalendar ?. ID } loadingMap = { loadingMap } add = { c ( 'Action' ) . t `Create calendar` } calendarLimitReachedText = { calendarLimitReachedText } canAdd = { activeAddresses . length > 0 && isBelowLimit && user . hasNonDelinquentScope } onAdd = { handleCreate } onSetDefault = { handleSetDefault } onEdit = { handleEdit } onDelete = { handleDelete } onExport = { handleExport } / > ) ; } ; export default PersonalCalendarsSection ;