Skip to content

Commit b86684d

Browse files
committed
Tuples-b-gone
1 parent bfa0000 commit b86684d

File tree

3 files changed

+61
-53
lines changed

3 files changed

+61
-53
lines changed

site/src/pages/ManagementSettingsPage/Sidebar.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import type { FC } from "react";
22
import { useQuery } from "react-query";
33
import { useLocation, useParams } from "react-router-dom";
44
import { organizationsPermissions } from "api/queries/organizations";
5-
import type { AuthorizationResponse, Organization } from "api/typesGenerated";
65
import { useAuthenticated } from "contexts/auth/RequireAuth";
76
import {
87
canEditOrganization,
98
useOrganizationSettings,
109
} from "./ManagementSettingsLayout";
11-
import { SidebarView } from "./SidebarView";
10+
import { type OrganizationWithPermissions, SidebarView } from "./SidebarView";
1211

1312
/**
1413
* A combined deployment settings and organization menu.
@@ -34,11 +33,19 @@ export const Sidebar: FC = () => {
3433
// can manage in some way.
3534
const editableOrgs = organizations
3635
?.map((org) => {
37-
const permissions = orgPermissionsQuery.data?.[org.id];
38-
return [org, permissions] as [Organization, AuthorizationResponse];
36+
return {
37+
...org,
38+
permissions: orgPermissionsQuery.data?.[org.id],
39+
};
3940
})
40-
.filter(([_, permissions]) => {
41-
return canEditOrganization(permissions);
41+
// TypeScript is not able to infer whether permissions are defined from the
42+
// canEditOrganization call, so although redundant this helps figure it out.
43+
.filter(
44+
(org): org is OrganizationWithPermissions =>
45+
org.permissions !== undefined,
46+
)
47+
.filter((org) => {
48+
return canEditOrganization(org.permissions);
4249
});
4350

4451
return (

site/src/pages/ManagementSettingsPage/SidebarView.stories.tsx

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ const meta: Meta<typeof SidebarView> = {
1313
activeSettings: true,
1414
activeOrganizationName: undefined,
1515
organizations: [
16-
[
17-
MockOrganization,
18-
{
16+
{
17+
...MockOrganization,
18+
permissions: {
1919
editOrganization: true,
2020
editMembers: true,
2121
editGroups: true,
2222
auditOrganization: true,
2323
},
24-
],
25-
[
26-
MockOrganization2,
27-
{
24+
},
25+
{
26+
...MockOrganization2,
27+
permissions: {
2828
editOrganization: true,
2929
editMembers: true,
3030
editGroups: true,
3131
auditOrganization: true,
3232
},
33-
],
33+
},
3434
],
3535
permissions: MockPermissions,
3636
},
@@ -114,15 +114,15 @@ export const SelectedOrgAdmin: Story = {
114114
args: {
115115
activeOrganizationName: MockOrganization.name,
116116
organizations: [
117-
[
118-
MockOrganization,
119-
{
117+
{
118+
...MockOrganization,
119+
permissions: {
120120
editOrganization: true,
121121
editMembers: true,
122122
editGroups: true,
123123
auditOrganization: true,
124124
},
125-
],
125+
},
126126
],
127127
},
128128
};
@@ -135,15 +135,15 @@ export const SelectedOrgAuditor: Story = {
135135
createOrganization: false,
136136
},
137137
organizations: [
138-
[
139-
MockOrganization,
140-
{
138+
{
139+
...MockOrganization,
140+
permissions: {
141141
editOrganization: false,
142142
editMembers: false,
143143
editGroups: false,
144144
auditOrganization: true,
145145
},
146-
],
146+
},
147147
],
148148
},
149149
};
@@ -156,40 +156,40 @@ export const SelectedOrgUserAdmin: Story = {
156156
createOrganization: false,
157157
},
158158
organizations: [
159-
[
160-
MockOrganization,
161-
{
159+
{
160+
...MockOrganization,
161+
permissions: {
162162
editOrganization: false,
163163
editMembers: true,
164164
editGroups: true,
165165
auditOrganization: false,
166166
},
167-
],
167+
},
168168
],
169169
},
170170
};
171171

172172
export const MultiOrgAdminAndUserAdmin: Story = {
173173
args: {
174174
organizations: [
175-
[
176-
MockOrganization,
177-
{
175+
{
176+
...MockOrganization,
177+
permissions: {
178178
editOrganization: false,
179179
editMembers: false,
180180
editGroups: false,
181181
auditOrganization: true,
182182
},
183-
],
184-
[
185-
MockOrganization2,
186-
{
183+
},
184+
{
185+
...MockOrganization2,
186+
permissions: {
187187
editOrganization: false,
188188
editMembers: true,
189189
editGroups: true,
190190
auditOrganization: false,
191191
},
192-
],
192+
},
193193
],
194194
},
195195
};
@@ -198,24 +198,24 @@ export const SelectedMultiOrgAdminAndUserAdmin: Story = {
198198
args: {
199199
activeOrganizationName: MockOrganization2.name,
200200
organizations: [
201-
[
202-
MockOrganization,
203-
{
201+
{
202+
...MockOrganization,
203+
permissions: {
204204
editOrganization: false,
205205
editMembers: false,
206206
editGroups: false,
207207
auditOrganization: true,
208208
},
209-
],
210-
[
211-
MockOrganization2,
212-
{
209+
},
210+
{
211+
...MockOrganization2,
212+
permissions: {
213213
editOrganization: false,
214214
editMembers: true,
215215
editGroups: true,
216216
auditOrganization: false,
217217
},
218-
],
218+
},
219219
],
220220
},
221221
};

site/src/pages/ManagementSettingsPage/SidebarView.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import { UserAvatar } from "components/UserAvatar/UserAvatar";
1212
import { type ClassName, useClassName } from "hooks/useClassName";
1313
import { linkToAuditing, linkToUsers, withFilter } from "modules/navigation";
1414

15+
export interface OrganizationWithPermissions extends Organization {
16+
permissions: AuthorizationResponse;
17+
}
18+
1519
interface SidebarProps {
1620
/** True if a settings page is being viewed. */
1721
activeSettings: boolean;
1822
/** The active org name, if any. Overrides activeSettings. */
1923
activeOrganizationName: string | undefined;
2024
/** Organizations and their permissions or undefined if still fetching. */
21-
organizations: [Organization, AuthorizationResponse][] | undefined;
25+
organizations: OrganizationWithPermissions[] | undefined;
2226
/** Site-wide permissions. */
2327
permissions: AuthorizationResponse;
2428
}
@@ -137,7 +141,7 @@ interface OrganizationsSettingsNavigationProps {
137141
/** The active org name if an org is being viewed. */
138142
activeOrganizationName: string | undefined;
139143
/** Organizations and their permissions or undefined if still fetching. */
140-
organizations: [Organization, AuthorizationResponse][] | undefined;
144+
organizations: OrganizationWithPermissions[] | undefined;
141145
/** Site-wide permissions. */
142146
permissions: AuthorizationResponse;
143147
}
@@ -177,11 +181,10 @@ const OrganizationsSettingsNavigation: FC<
177181
New organization
178182
</SidebarNavItem>
179183
)}
180-
{props.organizations.map(([org, permissions]) => (
184+
{props.organizations.map((org) => (
181185
<OrganizationSettingsNavigation
182186
key={org.id}
183187
organization={org}
184-
permissions={permissions}
185188
active={org.name === props.activeOrganizationName}
186189
/>
187190
))}
@@ -193,9 +196,7 @@ interface OrganizationSettingsNavigationProps {
193196
/** Whether this organization is currently selected. */
194197
active: boolean;
195198
/** The organization to display in the navigation. */
196-
organization: Organization;
197-
/** The permissions for this organization. */
198-
permissions: AuthorizationResponse;
199+
organization: OrganizationWithPermissions;
199200
}
200201

201202
/**
@@ -226,22 +227,22 @@ const OrganizationSettingsNavigation: FC<
226227
</SidebarNavItem>
227228
{props.active && (
228229
<Stack spacing={0.5} css={{ marginBottom: 8, marginTop: 8 }}>
229-
{props.permissions.editOrganization && (
230+
{props.organization.permissions.editOrganization && (
230231
<SidebarNavSubItem
231232
end
232233
href={urlForSubpage(props.organization.name)}
233234
>
234235
Organization settings
235236
</SidebarNavSubItem>
236237
)}
237-
{props.permissions.editMembers && (
238+
{props.organization.permissions.editMembers && (
238239
<SidebarNavSubItem
239240
href={urlForSubpage(props.organization.name, "members")}
240241
>
241242
Members
242243
</SidebarNavSubItem>
243244
)}
244-
{props.permissions.editGroups && (
245+
{props.organization.permissions.editGroups && (
245246
<SidebarNavSubItem
246247
href={urlForSubpage(props.organization.name, "groups")}
247248
>
@@ -251,7 +252,7 @@ const OrganizationSettingsNavigation: FC<
251252
{/* For now redirect to the site-wide audit page with the organization
252253
pre-filled into the filter. Based on user feedback we might want
253254
to serve a copy of the audit page or even delete this link. */}
254-
{props.permissions.auditOrganization && (
255+
{props.organization.permissions.auditOrganization && (
255256
<SidebarNavSubItem
256257
href={`/deployment${withFilter(
257258
linkToAuditing,

0 commit comments

Comments
 (0)