@@ -59,18 +59,29 @@ const getCountryByTimezone = () => {
59
59
60
60
const getCurrentDay = ( ) => new Date ( ) . toLocaleString ( 'en-US' , { weekday : 'long' } ) ;
61
61
62
+ const filterDefaultContent = ( item : Content ) => item ?. filterTags ?. length === 0 ;
63
+
64
+ const filterContentByDevice = ( item : Content , isMobile : boolean , isTablet : boolean , isDesktop : boolean ) => {
65
+ if ( item ?. filterTags ?. includes ( DEVICE_FILTER_LABELS . mobile ) && isMobile ) return true ;
66
+ if ( item ?. filterTags ?. includes ( DEVICE_FILTER_LABELS . tablet ) && isTablet ) return true ;
67
+ if ( item ?. filterTags ?. includes ( DEVICE_FILTER_LABELS . desktop ) && isDesktop ) return true ;
68
+ } ;
69
+
70
+ const filterContentByWeekDay = ( item : Content , currentDay : string ) => item . filterTags ?. includes ( currentDay ) ;
71
+
72
+ const filterContentByCountry = ( item : Content , country : string | undefined ) => country && item . filterTags ?. includes ( country ) ;
73
+
62
74
export const useFilterContent = ( content : Content [ ] ) => {
63
75
const { isMobile, isTablet, isDesktop } = useDeviceType ( ) ;
64
76
const currentDay = getCurrentDay ( ) ;
65
77
const country = getCountryByTimezone ( ) ;
66
78
67
79
return content ?. filter ( ( item ) => {
68
- if ( item ?. filterTags ?. length === 0 ) return true ;
69
- if ( item ?. filterTags ?. includes ( DEVICE_FILTER_LABELS . mobile ) && isMobile ) return true ;
70
- if ( item ?. filterTags ?. includes ( DEVICE_FILTER_LABELS . tablet ) && isTablet ) return true ;
71
- if ( item ?. filterTags ?. includes ( DEVICE_FILTER_LABELS . desktop ) && isDesktop ) return true ;
72
- if ( item ?. filterTags ?. includes ( currentDay ) ) return true ;
73
- if ( country && item ?. filterTags ?. includes ( country ) ) return true ;
74
- return false ;
80
+ return (
81
+ filterDefaultContent ( item ) ||
82
+ filterContentByDevice ( item , isMobile , isTablet , isDesktop ) ||
83
+ filterContentByWeekDay ( item , currentDay ) ||
84
+ filterContentByCountry ( item , country )
85
+ ) ;
75
86
} ) ;
76
87
} ;
0 commit comments