/** * External dependencies */ import moment from 'moment'; import type { Moment } from 'moment'; import type { SocialMessage, TimelinePeriod } from '@nelio-content/types'; export const filterUsingDate = ( period: TimelinePeriod, baseMoment: Moment ) => { const base = baseMoment.clone().startOf( 'day' ); return ( { schedule }: SocialMessage ): boolean => { const diff = moment( schedule ).diff( base, 'days' ); return isDiffInPeriod( period, diff ); }; }; export const filterUsingOffset = ( period: TimelinePeriod ) => ( message: SocialMessage ): boolean => { const { dateType, dateValue } = message; if ( 'exact' === dateType ) { return 'other' === period; } const diff = Math.abs( Number.parseInt( dateValue ) || 0 ); return isDiffInPeriod( period, diff ); }; export const getDateTimeAttrs = ( period: TimelinePeriod ): Partial< Pick< SocialMessage, 'dateType' | 'dateValue' | 'timeType' | 'timeValue' > > => { switch ( period ) { case 'day': return { dateType: 'predefined-offset', dateValue: '0', }; case 'week': return { dateType: 'predefined-offset', dateValue: '7', timeType: 'time-interval', timeValue: 'morning', }; case 'month': return { dateType: 'predefined-offset', dateValue: '28', timeType: 'time-interval', timeValue: 'morning', }; default: return { dateType: 'exact', timeType: 'time-interval', timeValue: 'morning', }; } }; // ======= // HELPERS // ======= const isDiffInPeriod = ( period: TimelinePeriod, diff: number ) => { switch ( period ) { case 'day': return 0 === diff; case 'week': return 1 <= diff && diff <= 7; case 'month': return 7 < diff && diff <= 30; default: return 30 < diff || diff < 0; } };