Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions site/src/api/queries/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import type {
PatchGroupRequest,
} from "api/typesGenerated";

const GROUPS_QUERY_KEY = ["groups"];
type GroupSortOrder = "asc" | "desc";

const getGroupQueryKey = (organizationId: string, groupName: string) => [
const getGroupsQueryKey = (organizationId: string) => [
"organization",
organizationId,
"group",
groupName,
"groups",
];

export const groups = (organizationId: string) => {
return {
queryKey: GROUPS_QUERY_KEY,
queryKey: getGroupsQueryKey(organizationId),
queryFn: () => API.getGroups(organizationId),
} satisfies UseQueryOptions<Group[]>;
};

const getGroupQueryKey = (organizationId: string, groupName: string) => [
"organization",
organizationId,
"group",
groupName,
];

export const group = (organizationId: string, groupName: string) => {
return {
queryKey: getGroupQueryKey(organizationId, groupName),
Expand Down Expand Up @@ -97,7 +103,7 @@ export const createGroup = (
mutationFn: (request: CreateGroupRequest) =>
API.createGroup(organizationId, request),
onSuccess: async () => {
await queryClient.invalidateQueries(GROUPS_QUERY_KEY);
await queryClient.invalidateQueries(getGroupsQueryKey(organizationId));
},
};
};
Expand Down Expand Up @@ -146,7 +152,7 @@ export const invalidateGroup = (
groupId: string,
) =>
Promise.all([
queryClient.invalidateQueries(GROUPS_QUERY_KEY),
queryClient.invalidateQueries(getGroupsQueryKey(organizationId)),
queryClient.invalidateQueries(getGroupQueryKey(organizationId, groupId)),
]);

Expand Down
5 changes: 4 additions & 1 deletion site/src/modules/dashboard/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const Navbar: FC = () => {
const canViewAuditLog =
featureVisibility["audit_log"] && Boolean(permissions.viewAuditLog);
const canViewDeployment = Boolean(permissions.viewDeploymentValues);
const canViewOrganizations =
featureVisibility.multiple_organizations &&
experiments.includes("multi-organization");
const canViewAllUsers = Boolean(permissions.readAllUsers);
const proxyContextValue = useProxy();
const canViewHealth = canViewDeployment;
Expand All @@ -30,7 +33,7 @@ export const Navbar: FC = () => {
supportLinks={appearance.support_links}
onSignOut={signOut}
canViewDeployment={canViewDeployment}
canViewOrganizations={experiments.includes("multi-organization")}
canViewOrganizations={canViewOrganizations}
canViewAllUsers={canViewAllUsers}
canViewHealth={canViewHealth}
canViewAuditLog={canViewAuditLog}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const CreateGroupPage: FC = () => {
const navigate = useNavigate();
const { organization } = useParams() as { organization: string };
const createGroupMutation = useMutation(
createGroup(queryClient, organization),
createGroup(queryClient, organization ?? "default"),
);

return (
Expand All @@ -22,7 +22,11 @@ export const CreateGroupPage: FC = () => {
<CreateGroupPageView
onSubmit={async (data) => {
const newGroup = await createGroupMutation.mutateAsync(data);
navigate(`/organizations/${organization}/groups/${newGroup.name}`);
navigate(
organization
? `/organizations/${organization}/groups/${newGroup.name}`
: `/deployment/groups/${newGroup.name}`,
);
}}
error={createGroupMutation.error}
isLoading={createGroupMutation.isLoading}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { isEveryoneGroup } from "utils/groups";
import { pageTitle } from "utils/page";

export const GroupPage: FC = () => {
const { organization, groupName } = useParams() as {
const { organization = "default", groupName } = useParams() as {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the constant "default" used in multiple places. We should define it in a const.ts file for consistency. Makes sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally, I really dread adding imports like that which end up being more typing that the thing you could just say directly

organization: string;
groupName: string;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { pageTitle } from "utils/page";
import GroupSettingsPageView from "./GroupSettingsPageView";

export const GroupSettingsPage: FC = () => {
const { organization, groupName } = useParams() as {
const { organization = "default", groupName } = useParams() as {
organization: string;
groupName: string;
};
Expand Down
36 changes: 31 additions & 5 deletions site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,36 @@
import { type FC, useEffect } from "react";
import { Helmet } from "react-helmet-async";
import { useQuery } from "react-query";
import { Link as RouterLink } from "react-router-dom";
import {
Navigate,
Link as RouterLink,
useLocation,
useParams,
} from "react-router-dom";
import { getErrorMessage } from "api/errors";
import { groups } from "api/queries/groups";
import { displayError } from "components/GlobalSnackbar/utils";
import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader";
import { useAuthenticated } from "contexts/auth/RequireAuth";
import { useDashboard } from "modules/dashboard/useDashboard";
import { useFeatureVisibility } from "modules/dashboard/useFeatureVisibility";
import { pageTitle } from "utils/page";
import { useOrganizationSettings } from "../ManagementSettingsLayout";
import GroupsPageView from "./GroupsPageView";
import { useOrganizationSettings } from "../ManagementSettingsLayout";

Check failure on line 21 in site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

`../ManagementSettingsLayout` import should occur before import of `./GroupsPageView`

Check failure on line 21 in site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

`../ManagementSettingsLayout` import should occur before import of `./GroupsPageView`
import { Organization } from "api/typesGenerated";

Check failure on line 22 in site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 22 in site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

`api/typesGenerated` import should occur before import of `components/GlobalSnackbar/utils`

Check failure on line 22 in site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 22 in site/src/pages/ManagementSettingsPage/GroupsPage/GroupsPage.tsx

View workflow job for this annotation

GitHub Actions / lint

`api/typesGenerated` import should occur before import of `components/GlobalSnackbar/utils`

export const GroupsPage: FC = () => {
const { permissions } = useAuthenticated();
const { currentOrganizationId } = useOrganizationSettings();
const { createGroup: canCreateGroup } = permissions;
const { template_rbac: isTemplateRBACEnabled } = useFeatureVisibility();
const groupsQuery = useQuery(groups(currentOrganizationId!));
const {
multiple_organizations: organizationsEnabled,
template_rbac: isTemplateRBACEnabled,
} = useFeatureVisibility();
const { experiments } = useDashboard();
const location = useLocation();
const { organization = "default" } = useParams() as { organization: string };
const groupsQuery = useQuery(groups(organization));
const { organizations } = useOrganizationSettings();

useEffect(() => {
if (groupsQuery.error) {
Expand All @@ -29,6 +42,16 @@
}
}, [groupsQuery.error]);

if (
organizationsEnabled &&
experiments.includes("multi-organization") &&
location.pathname === "/deployment/groups"
) {
const defaultName =
getOrganizationNameByDefault(organizations) ?? "default";
return <Navigate to={`/organizations/${defaultName}/groups`} replace />;
}

return (
<>
<Helmet>
Expand Down Expand Up @@ -63,3 +86,6 @@
};

export default GroupsPage;

export const getOrganizationNameByDefault = (organizations: Organization[]) =>
organizations.find((org) => org.is_default)?.name;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, type FC, Suspense, useContext } from "react";
import { useQuery } from "react-query";
import { Outlet, useLocation, useParams } from "react-router-dom";
import { Outlet } from "react-router-dom";
import { deploymentConfig } from "api/queries/deployment";
import { organizations } from "api/queries/organizations";
import type { Organization } from "api/typesGenerated";
Expand All @@ -15,7 +15,6 @@ import { DeploySettingsContext } from "../DeploySettingsPage/DeploySettingsLayou
import { Sidebar } from "./Sidebar";

type OrganizationSettingsContextValue = {
currentOrganizationId?: string;
organizations: Organization[];
};

Expand All @@ -34,19 +33,13 @@ export const useOrganizationSettings = (): OrganizationSettingsContextValue => {
};

export const ManagementSettingsLayout: FC = () => {
const location = useLocation();
const { permissions } = useAuthenticated();
const { experiments } = useDashboard();
const { organization } = useParams() as { organization: string };
const deploymentConfigQuery = useQuery(deploymentConfig());
const organizationsQuery = useQuery(organizations());

const multiOrgExperimentEnabled = experiments.includes("multi-organization");

const inOrganizationSettings =
location.pathname.startsWith("/organizations") &&
location.pathname !== "/organizations/new";

if (!multiOrgExperimentEnabled) {
return <NotFoundPage />;
}
Expand All @@ -57,17 +50,7 @@ export const ManagementSettingsLayout: FC = () => {
<Stack css={{ padding: "48px 0" }} direction="row" spacing={6}>
{organizationsQuery.data ? (
<OrganizationSettingsContext.Provider
value={{
currentOrganizationId: !inOrganizationSettings
? undefined
: !organization
? getOrganizationIdByDefault(organizationsQuery.data)
: getOrganizationIdByName(
organizationsQuery.data,
organization,
),
organizations: organizationsQuery.data,
}}
value={{ organizations: organizationsQuery.data }}
>
<Sidebar />
<main css={{ width: "100%" }}>
Expand All @@ -94,9 +77,3 @@ export const ManagementSettingsLayout: FC = () => {
</RequirePermission>
);
};

const getOrganizationIdByName = (organizations: Organization[], name: string) =>
organizations.find((org) => org.name === name)?.id;

const getOrganizationIdByDefault = (organizations: Organization[]) =>
organizations.find((org) => org.is_default)?.id;
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import type { FC } from "react";
import { useMutation, useQueryClient } from "react-query";
import { useNavigate } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";
import {
updateOrganization,
deleteOrganization,
} from "api/queries/organizations";
import type { Organization } from "api/typesGenerated";
import { EmptyState } from "components/EmptyState/EmptyState";
import { displaySuccess } from "components/GlobalSnackbar/utils";
import { useOrganizationSettings } from "./ManagementSettingsLayout";
import { OrganizationSettingsPageView } from "./OrganizationSettingsPageView";

const OrganizationSettingsPage: FC = () => {
const navigate = useNavigate();
const { organization: organizationName } = useParams() as {
organization?: string;
};
const { organizations } = useOrganizationSettings();

const navigate = useNavigate();
const queryClient = useQueryClient();
const updateOrganizationMutation = useMutation(
updateOrganization(queryClient),
Expand All @@ -21,14 +26,14 @@ const OrganizationSettingsPage: FC = () => {
deleteOrganization(queryClient),
);

const { currentOrganizationId, organizations } = useOrganizationSettings();

const org = organizations.find((org) => org.id === currentOrganizationId);
const org = organizationName
? getOrganizationByName(organizations, organizationName)
: getOrganizationByDefault(organizations);

const error =
updateOrganizationMutation.error ?? deleteOrganizationMutation.error;

if (!currentOrganizationId || !org) {
if (!org) {
return <EmptyState message="Organization not found" />;
}

Expand All @@ -55,3 +60,9 @@ const OrganizationSettingsPage: FC = () => {
};

export default OrganizationSettingsPage;

const getOrganizationByDefault = (organizations: Organization[]) =>
organizations.find((org) => org.is_default);

const getOrganizationByName = (organizations: Organization[], name: string) =>
organizations.find((org) => org.name === name);

This file was deleted.

Loading
Loading