Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
"use client";

import Section from "@/components/common/Section";
import Page from "@/components/Page";
import { Policy } from "@/types/api/api";
import { FunctionComponent, useState } from "react";
import { toast } from "sonner";
import useSWR from "swr";
import EmptyParty from "../../../../components/common/EmptyParty";
import ListRenderer from "../../../../components/common/ListRenderer";
import PolicyListItem from "../../../../components/common/PolicyListItem";
import PolicyDialog from "../../../../components/PolicyDialog";
import { Button } from "../../../../components/ui/button";
import { fetcher } from "../../../../data-fetcher/fetcher";
import useDecodedParams from "../../../../hooks/useDecodedParams";
import { useOrganizationMenu } from "../../../../hooks/useOrganizationMenu";
import { browserApiClient } from "../../../../services/devGuardApi";

const ComplianceIndex: FunctionComponent = () => {
const menu = useOrganizationMenu();
const [open, setOpen] = useState(false);
const { organizationSlug } = useDecodedParams() as {
organizationSlug: string;
};
const {
data: policies,
isLoading,
error,
mutate,
} = useSWR<Array<Policy>>(
"/organizations/" + organizationSlug + "/policies/",
fetcher,
);

const handleCreatePolicy = async (policy: Policy) => {
mutate(
async (prev) => {
let url = "/organizations/" + organizationSlug + "/policies/";

const resp = await browserApiClient(url, {
method: "POST",
body: JSON.stringify(policy),
});

if (!resp.ok) {
toast.error("Failed to create policy");
return;
}
// update the policies
const newPolicy = await resp.json();
return [newPolicy, ...(prev || [])];
},
{
optimisticData: (prev) => [
{ ...policy, id: Math.random().toString() },
...(prev || []),
],
},
);

toast.success("Policy created successfully");
setOpen(false);
};

const handlePolicyUpdate = async (policy: Policy) => {
mutate(
async (prev) => {
let url =
"/organizations/" + organizationSlug + "/policies/" + policy.id;

const resp = await browserApiClient(url, {
method: "PUT",
body: JSON.stringify(policy),
});

if (!resp.ok) {
toast.error("Failed to update policy");
return;
}

// update the policies
const newPolicy = await resp.json();
toast.success("Policy updated successfully");
return prev?.map((p) => (p.id === newPolicy.id ? newPolicy : p));
},
{
optimisticData: (prev) =>
prev?.map((p) => (p.id === policy.id ? policy : p)) || [],
},
);
};

const handlePolicyDelete = async (policy: Policy) => {
mutate(
async (prev) => {
let url =
"/organizations/" + organizationSlug + "/policies/" + policy.id;

const resp = await browserApiClient(url, {
method: "DELETE",
});

if (!resp.ok) {
toast.error("Failed to delete policy");
return;
}

// update the policies
toast.success("Policy deleted successfully");
return prev?.filter((p) => p.id !== policy.id);
},
{
optimisticData: (prev) => prev?.filter((p) => p.id !== policy.id) || [],
},
);
};

return (
<Page Menu={menu} Title={null} title="">
<div className="flex flex-row">
<div className="flex-1">
<Section
primaryHeadline
description="Modify your organization compliance policies here."
title="Compliance Controls"
forceVertical
Button={
<Button onClick={() => setOpen(true)}>Upload new Policy</Button>
}
>
<ListRenderer
isLoading={isLoading}
error={error}
data={policies}
Empty={
<EmptyParty
title="No Policies"
description="Create a new policy to get started."
/>
}
renderItem={(policy) => (
<PolicyListItem
onPolicyDelete={handlePolicyDelete}
onPolicyUpdate={handlePolicyUpdate}
key={policy.id}
policy={policy}
/>
)}
/>
</Section>
</div>
</div>
<PolicyDialog
isOpen={open}
title="Create new Policy"
description="Create a new policy for your organization."
buttonTitle="Create Policy"
onOpenChange={setOpen}
onSubmit={handleCreatePolicy}
/>
</Page>
);
};

export default ComplianceIndex;
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ export const config = {
| false,
errorTrackingDsn: process.env.ERROR_TRACKING_DSN || "",
};

//Important Commit
8 changes: 8 additions & 0 deletions src/hooks/useOrganizationMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useActiveOrg } from "./useActiveOrg";
import { useCurrentUser } from "./useCurrentUser";
import useDecodedParams from "./useDecodedParams";
import { useCurrentUserRole } from "./useUserRole";
import { FolderSearch } from "lucide-react";

export const useOrganizationMenu = () => {
const pathName = usePathname() || "/";
Expand Down Expand Up @@ -57,6 +58,13 @@ export const useOrganizationMenu = () => {
currentUserRole === UserRole.Owner ||
currentUserRole === UserRole.Admin
) {
menu.push({
title: "Package Search",
href: "/" + decodedOrgSlug + "/org-dependency-search",
Icon: FolderSearch,
isActive:
decodedPathName === "/" + decodedOrgSlug + "/org-dependency-search",
});
menu.push({
title: "Settings",
href: "/" + decodedOrgSlug + "/settings",
Expand Down