Skip to content

fix: fix loading states and permissions checks in organization settings #16465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
oh yeah
  • Loading branch information
aslilac committed Feb 7, 2025
commit 379bacd1556803a71f286121e16b3b7d63612812
9 changes: 7 additions & 2 deletions site/src/modules/management/OrganizationSettingsLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export const OrganizationSettingsContext = createContext<

type OrganizationSettingsValue = Readonly<{
organizations: readonly Organization[];
permissionsByOrganizationId: Record<string, OrganizationPermissions>;
organizationPermissionsByOrganizationId: Record<
string,
OrganizationPermissions
>;
organization?: Organization;
organizationPermissions?: OrganizationPermissions;
}>;
Expand Down Expand Up @@ -80,12 +83,14 @@ const OrganizationSettingsLayout: FC = () => {
return <NotFoundPage />;
}

console.log(orgPermissionsQuery.data);

return (
<RequirePermission isFeatureVisible={canViewOrganizationSettings}>
<OrganizationSettingsContext.Provider
value={{
organizations: viewableOrganizations,
permissionsByOrganizationId: orgPermissionsQuery.data,
organizationPermissionsByOrganizationId: orgPermissionsQuery.data,
organization,
organizationPermissions,
}}
Expand Down
8 changes: 5 additions & 3 deletions site/src/modules/management/OrganizationSidebarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ export const OrganizationSidebarView: FC<SidebarProps> = ({
))}
</div>
) : (
<span className="select-none text-content-disabled text-center rounded-sm px-2 py-2 text-sm font-medium">
No more organizations
</span>
!permissions.createOrganization && (
<span className="select-none text-content-disabled text-center rounded-sm px-2 py-2 text-sm font-medium">
No more organizations
</span>
)
)}
{permissions.createOrganization && (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ import { Navigate } from "react-router-dom";
import { canEditOrganization } from "modules/management/organizationPermissions";

const DefaultOrganizationRedirect: FC = () => {
const { organizations, permissionsByOrganizationId } =
useOrganizationSettings();
const {
organizations,
organizationPermissionsByOrganizationId: organizationPermissions,
} = useOrganizationSettings();

// Redirect /organizations => /organizations/default-org, or if they cannot edit
// the default org, then the first org they can edit, if any.
// .find will stop at the first match found; make sure default
// organizations are placed first
// Redirect /organizations => /organizations/some-organization-name
// If they can edit the default org, we should redirect to the default.
// If they cannot edit the default, we should redirect to the first org that
// they can edit.
const editableOrg = [...organizations]
.sort((a, b) => (b.is_default ? 1 : 0) - (a.is_default ? 1 : 0))
.find((org) => canEditOrganization(permissionsByOrganizationId[org.id]));
.find((org) => canEditOrganization(organizationPermissions[org.id]));
if (editableOrg) {
return <Navigate to={`/organizations/${editableOrg.name}`} replace />;
}
// If they cannot edit any org, just redirect to an org they can read.
if (organizations.length > 0) {
return <Navigate to={`/organizations/${organizations[0].name}`} replace />;
}
return <EmptyState message="No organizations found" />;
};

Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/storybook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const withOrganizationSettingsProvider = (Story: FC) => {
<OrganizationSettingsContext.Provider
value={{
organizations: [MockDefaultOrganization],
permissionsByOrganizationId: {
organizationPermissionsByOrganizationId: {
[MockDefaultOrganization.id]: MockOrganizationPermissions,
},
organization: MockDefaultOrganization,
Expand Down
Loading