Skip to content

Commit cd86a9c

Browse files
committed
Dedupe disable org permission query
1 parent f6dfce0 commit cd86a9c

File tree

5 files changed

+19
-27
lines changed

5 files changed

+19
-27
lines changed

site/src/api/queries/organizations.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,13 @@ export const organizations = () => {
110110

111111
/**
112112
* Fetch permissions for a single organization.
113+
*
114+
* If the ID is undefined, return a disabled query.
113115
*/
114-
export const organizationPermissions = (organizationId: string) => {
116+
export const organizationPermissions = (organizationId: string | undefined) => {
117+
if (!organizationId) {
118+
return { enabled: false };
119+
}
115120
return {
116121
queryKey: ["organization", organizationId, "permissions"],
117122
queryFn: () =>

site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ export const GroupsPage: FC = () => {
2929
// TODO: If we could query permissions based on the name then we would not
3030
// have to cascade off the organizations query.
3131
const organization = organizations?.find((o) => o.name === organizationName);
32-
const permissionsQuery = useQuery(
33-
organization
34-
? organizationPermissions(organization.id)
35-
: { enabled: false },
36-
);
32+
const permissionsQuery = useQuery(organizationPermissions(organization?.id));
3733

3834
useEffect(() => {
3935
if (groupsQuery.error) {

site/src/pages/ManagementSettingsPage/OrganizationMembersPage.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ const OrganizationMembersPage: FC = () => {
6464
// have to cascade off the organizations query.
6565
const { organizations } = useOrganizationSettings();
6666
const organization = organizations?.find((o) => o.name === organizationName);
67-
const permissionsQuery = useQuery(
68-
organization
69-
? organizationPermissions(organization.id)
70-
: { enabled: false },
71-
);
67+
const permissionsQuery = useQuery(organizationPermissions(organization?.id));
7268

7369
const permissions = permissionsQuery.data;
7470
if (!permissions) {

site/src/pages/ManagementSettingsPage/OrganizationSettingsPage.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,10 @@ const OrganizationSettingsPage: FC = () => {
3232
// TODO: If we could query permissions based on the name then we would not
3333
// have to cascade off the organizations query.
3434
const organization =
35-
organizations &&
36-
organizationName &&
37-
getOrganizationByName(organizations, organizationName);
38-
const permissionsQuery = useQuery(
39-
organization
40-
? organizationPermissions(organization.id)
41-
: { enabled: false },
42-
);
35+
organizations && organizationName
36+
? getOrganizationByName(organizations, organizationName)
37+
: undefined;
38+
const permissionsQuery = useQuery(organizationPermissions(organization?.id));
4339

4440
if (!organizations) {
4541
return <Loader />;

site/src/pages/ManagementSettingsPage/Sidebar.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,13 @@ interface OrganizationSettingsNavigationProps {
164164
export const OrganizationSettingsNavigation: FC<
165165
OrganizationSettingsNavigationProps
166166
> = ({ organization, active }) => {
167-
const permissionsQuery = useQuery({
168-
...organizationPermissions(organization.id),
169-
// The menu items only show while the menu is expanded, so we can wait until
170-
// expanded to run the query. Downside is that we have to show a loader
171-
// until the query finishes...maybe we should prefetch permissions for all
172-
// the orgs instead?
173-
enabled: active,
174-
});
167+
// The menu items only show while the menu is expanded, so we can wait until
168+
// expanded to run the query. Downside is that we have to show a loader
169+
// until the query finishes...maybe we should prefetch permissions for all
170+
// the orgs instead?
171+
const permissionsQuery = useQuery(
172+
organizationPermissions(active ? organization.id : undefined),
173+
);
175174

176175
return (
177176
<>

0 commit comments

Comments
 (0)