-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathNotificationsCategory.tsx
More file actions
128 lines (112 loc) · 3.57 KB
/
Copy pathNotificationsCategory.tsx
File metadata and controls
128 lines (112 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { useCallback, useMemo, useState } from 'react';
import { useSelector } from 'react-redux';
import {
FilterButton,
FilterButtonGroup,
} from '@metamask/design-system-react-native';
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import { strings } from '../../../../../locales/i18n';
import { selectIsMetamaskNotificationsEnabled } from '../../../../selectors/notifications';
import { selectSocialLeaderboardEnabled } from '../../../../selectors/featureFlagController/socialLeaderboard';
import { selectPriceAlertsEnabled } from '../../../../selectors/featureFlagController/priceAlerts';
import {
ALL_NOTIFICATIONS_CATEGORY_ID,
getCategoryTitle,
getNotificationsSettingsSectionConfigs,
useNotificationCategories,
} from '../../../../util/notifications/categories';
import { NotificationsCategoryProps } from './NotificationsCategory.types';
import {
categoryTestID,
NotificationsCategorySelectorsIDs,
} from './NotificationsCategory.testIds';
import NotificationsCategorySkeleton from './NotificationsCategorySkeleton';
interface CategoryTab {
key: string;
label: string;
testID: string;
}
/**
* Horizontally scrollable category filter for the notifications list, built on
* the design-system `SegmentGroup` / `SegmentButton`. Tabs are driven by the
* BE-provided category catalog; the "Trading Signals" category is additionally
* filtered out when the social leaderboard flag is off.
*/
const NotificationsCategory = ({
onSelect,
testID,
}: NotificationsCategoryProps) => {
const tw = useTailwind();
const isMetamaskNotificationsEnabled = useSelector(
selectIsMetamaskNotificationsEnabled,
);
const isSocialLeaderboardEnabled = useSelector(
selectSocialLeaderboardEnabled,
);
const isPriceAlertsEnabled = useSelector(selectPriceAlertsEnabled);
const { categories, isLoading } = useNotificationCategories();
const [selectedCategory, setSelectedCategory] = useState<string>(
ALL_NOTIFICATIONS_CATEGORY_ID,
);
const tabs = useMemo<CategoryTab[]>(() => {
if (!isMetamaskNotificationsEnabled) {
return [];
}
const items: CategoryTab[] = [
{
key: ALL_NOTIFICATIONS_CATEGORY_ID,
label: strings('app_settings.notifications_opts.all_title'),
testID: NotificationsCategorySelectorsIDs.ALL,
},
];
const sectionConfigs = getNotificationsSettingsSectionConfigs(categories, {
isSocialLeaderboardEnabled,
isPriceAlertsEnabled,
});
sectionConfigs.forEach((category) => {
items.push({
key: category.categoryId,
label: getCategoryTitle(category.categoryId),
testID: categoryTestID(category.categoryId),
});
});
return items;
}, [
categories,
isMetamaskNotificationsEnabled,
isPriceAlertsEnabled,
isSocialLeaderboardEnabled,
]);
const handleSelect = useCallback(
(key: string) => {
setSelectedCategory(key);
onSelect(key);
},
[onSelect],
);
if (!isMetamaskNotificationsEnabled) {
return null;
}
if (isLoading) {
return <NotificationsCategorySkeleton />;
}
if (tabs.length === 0) {
return null;
}
return (
<FilterButtonGroup
value={selectedCategory}
onChange={handleSelect}
twClassName="gap-2 px-4 py-1"
style={tw.style('flex-grow-0')}
testID={testID ?? NotificationsCategorySelectorsIDs.CONTAINER}
>
{tabs.map((tab) => (
<FilterButton key={tab.key} value={tab.key} testID={tab.testID}>
{tab.label}
</FilterButton>
))}
</FilterButtonGroup>
);
};
export default NotificationsCategory;