Skip to content

Commit 370abd6

Browse files
feat: separate filter into functions
1 parent 533817e commit 370abd6

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

packages/ui-react/src/hooks/useFilteredContent.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,29 @@ const getCountryByTimezone = () => {
5959

6060
const getCurrentDay = () => new Date().toLocaleString('en-US', { weekday: 'long' });
6161

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+
6274
export const useFilterContent = (content: Content[]) => {
6375
const { isMobile, isTablet, isDesktop } = useDeviceType();
6476
const currentDay = getCurrentDay();
6577
const country = getCountryByTimezone();
6678

6779
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+
);
7586
});
7687
};

0 commit comments

Comments
 (0)