Skip to content

Commit 97956e2

Browse files
committed
filter groups at the render level, rather than the data fetching level
1 parent e23db57 commit 97956e2

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

site/src/pages/UserSettingsPage/NotificationsPage/NotificationsPage.stories.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const meta = {
4040
},
4141
],
4242
user: MockUserOwner,
43-
permissions: { viewDeploymentConfig: true },
43+
permissions: { createTemplates: true, createUser: true },
4444
},
4545
decorators: [withGlobalSnackbar, withAuthProvider, withDashboardProvider],
4646
} satisfies Meta<typeof NotificationsPage>;
@@ -74,13 +74,19 @@ export const ToggleNotification: Story = {
7474

7575
export const NonAdmin: Story = {
7676
parameters: {
77-
permissions: { viewDeploymentConfig: false },
77+
permissions: { createTemplates: false, createUser: false },
7878
},
7979
};
8080

81-
export const TemplateCreator: Story = {
81+
export const TemplateAdmin: Story = {
8282
parameters: {
83-
permissions: { viewDeploymentConfig: false, createTemplates: true },
83+
permissions: { createTemplates: true, createUser: false },
84+
},
85+
};
86+
87+
export const UserAdmin: Story = {
88+
parameters: {
89+
permissions: { createTemplates: false, createUser: true },
8490
},
8591
};
8692

site/src/pages/UserSettingsPage/NotificationsPage/NotificationsPage.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
methodIcons,
2929
methodLabels,
3030
} from "modules/notifications/utils";
31+
import type { Permissions } from "modules/permissions";
3132
import { type FC, Fragment } from "react";
3233
import { useEffect } from "react";
3334
import { Helmet } from "react-helmet-async";
@@ -46,22 +47,7 @@ const NotificationsPage: FC = () => {
4647
},
4748
{
4849
...systemNotificationTemplates(),
49-
select: (data: NotificationTemplate[]) => {
50-
const groups = selectTemplatesByGroup(data);
51-
52-
let displayedGroups: Record<string, NotificationTemplate[]> = {
53-
// Members only have access to the "Workspace Notifications" group.
54-
"Workspace Events": groups["Workspace Events"],
55-
};
56-
57-
if (permissions.viewDeploymentConfig) {
58-
displayedGroups = groups;
59-
} else if (permissions.createTemplates) {
60-
displayedGroups["Template Events"] = groups["Template Events"];
61-
}
62-
63-
return displayedGroups;
64-
},
50+
select: (data: NotificationTemplate[]) => selectTemplatesByGroup(data),
6551
},
6652
notificationDispatchMethods(),
6753
],
@@ -110,6 +96,10 @@ const NotificationsPage: FC = () => {
11096
{ready ? (
11197
<Stack spacing={4}>
11298
{Object.entries(templatesByGroup.data).map(([group, templates]) => {
99+
if (!canSeeNotificationGroup(group, permissions)) {
100+
return null;
101+
}
102+
113103
const allDisabled = templates.some((tpl) => {
114104
return notificationIsDisabled(disabledPreferences.data, tpl);
115105
});
@@ -218,6 +208,22 @@ const NotificationsPage: FC = () => {
218208

219209
export default NotificationsPage;
220210

211+
function canSeeNotificationGroup(
212+
group: string,
213+
permissions: Permissions,
214+
): boolean {
215+
switch (group) {
216+
case "Workspace Events":
217+
return true;
218+
case "Template Events":
219+
return permissions.createTemplates;
220+
case "User Events":
221+
return permissions.createUser;
222+
default:
223+
return false;
224+
}
225+
}
226+
221227
function notificationIsDisabled(
222228
disabledPreferences: Record<string, boolean>,
223229
tmpl: NotificationTemplate,

0 commit comments

Comments
 (0)