2024-10-19 15:49:16 -04:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import { ColumnDef } from "@tanstack/react-table";
|
|
|
|
import { ResourcesDataTable } from "./ResourcesDataTable";
|
|
|
|
import {
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
} from "@app/components/ui/dropdown-menu";
|
|
|
|
import { Button } from "@app/components/ui/button";
|
2024-11-12 23:59:47 -05:00
|
|
|
import { ArrowRight, ArrowUpDown, MoreHorizontal } from "lucide-react";
|
2024-10-19 15:49:16 -04:00
|
|
|
import Link from "next/link";
|
|
|
|
import { useRouter } from "next/navigation";
|
2024-10-19 22:39:46 -04:00
|
|
|
import api from "@app/api";
|
2024-11-11 00:00:16 -05:00
|
|
|
import CreateResourceForm from "./CreateResourceForm";
|
|
|
|
import { useState } from "react";
|
2024-11-11 23:00:51 -05:00
|
|
|
import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog";
|
|
|
|
import { set } from "zod";
|
2024-11-13 20:08:05 -05:00
|
|
|
import { formatAxiosError } from "@app/lib/utils";
|
|
|
|
import { useToast } from "@app/hooks/useToast";
|
2024-10-19 15:49:16 -04:00
|
|
|
|
|
|
|
export type ResourceRow = {
|
2024-10-26 12:15:03 -04:00
|
|
|
id: number;
|
2024-10-19 15:49:16 -04:00
|
|
|
name: string;
|
|
|
|
orgId: string;
|
2024-10-19 17:01:23 -04:00
|
|
|
domain: string;
|
|
|
|
site: string;
|
2024-10-19 15:49:16 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
type ResourcesTableProps = {
|
|
|
|
resources: ResourceRow[];
|
|
|
|
orgId: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function SitesTable({ resources, orgId }: ResourcesTableProps) {
|
|
|
|
const router = useRouter();
|
|
|
|
|
2024-11-13 20:08:05 -05:00
|
|
|
const { toast } = useToast();
|
|
|
|
|
2024-11-11 00:00:16 -05:00
|
|
|
const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
|
2024-11-11 23:00:51 -05:00
|
|
|
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
|
|
|
const [selectedResource, setSelectedResource] =
|
|
|
|
useState<ResourceRow | null>();
|
|
|
|
|
|
|
|
const deleteResource = (resourceId: number) => {
|
|
|
|
api.delete(`/resource/${resourceId}`)
|
|
|
|
.catch((e) => {
|
|
|
|
console.error("Error deleting resource", e);
|
2024-11-13 20:08:05 -05:00
|
|
|
toast({
|
|
|
|
variant: "destructive",
|
|
|
|
title: "Error deleting resource",
|
|
|
|
description: formatAxiosError(e, "Error deleting resource"),
|
|
|
|
});
|
2024-11-11 23:00:51 -05:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
router.refresh();
|
|
|
|
setIsDeleteModalOpen(false);
|
|
|
|
});
|
|
|
|
};
|
2024-11-11 00:00:16 -05:00
|
|
|
|
|
|
|
const columns: ColumnDef<ResourceRow>[] = [
|
|
|
|
{
|
|
|
|
accessorKey: "name",
|
|
|
|
header: ({ column }) => {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
variant="ghost"
|
|
|
|
onClick={() =>
|
|
|
|
column.toggleSorting(column.getIsSorted() === "asc")
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Name
|
|
|
|
<ArrowUpDown className="ml-2 h-4 w-4" />
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
accessorKey: "site",
|
|
|
|
header: ({ column }) => {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
variant="ghost"
|
|
|
|
onClick={() =>
|
|
|
|
column.toggleSorting(column.getIsSorted() === "asc")
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Site
|
|
|
|
<ArrowUpDown className="ml-2 h-4 w-4" />
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
accessorKey: "domain",
|
|
|
|
header: "Domain",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "actions",
|
|
|
|
cell: ({ row }) => {
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const resourceRow = row.original;
|
|
|
|
|
|
|
|
return (
|
2024-11-12 23:59:47 -05:00
|
|
|
<>
|
|
|
|
<div className="flex items-center justify-end">
|
|
|
|
<DropdownMenu>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button
|
|
|
|
variant="ghost"
|
|
|
|
className="h-8 w-8 p-0"
|
|
|
|
>
|
|
|
|
<span className="sr-only">
|
|
|
|
Open menu
|
|
|
|
</span>
|
|
|
|
<MoreHorizontal className="h-4 w-4" />
|
|
|
|
</Button>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent align="end">
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<Link
|
|
|
|
href={`/${resourceRow.orgId}/settings/resources/${resourceRow.id}`}
|
|
|
|
>
|
|
|
|
View settings
|
|
|
|
</Link>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
<DropdownMenuItem>
|
|
|
|
<button
|
|
|
|
onClick={() => {
|
|
|
|
setSelectedResource(
|
|
|
|
resourceRow
|
|
|
|
);
|
|
|
|
setIsDeleteModalOpen(true);
|
|
|
|
}}
|
|
|
|
className="text-red-600 hover:text-red-800 hover:underline cursor-pointer"
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</button>
|
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
<Button
|
|
|
|
variant={"gray"}
|
|
|
|
className="ml-2"
|
|
|
|
onClick={() =>
|
|
|
|
router.push(
|
|
|
|
`/${resourceRow.orgId}/settings/resources/${resourceRow.id}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Edit <ArrowRight className="ml-2 w-4 h-4" />
|
2024-11-11 00:00:16 -05:00
|
|
|
</Button>
|
2024-11-12 23:59:47 -05:00
|
|
|
</div>
|
|
|
|
</>
|
2024-11-11 00:00:16 -05:00
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-10-19 15:49:16 -04:00
|
|
|
return (
|
2024-11-11 00:00:16 -05:00
|
|
|
<>
|
|
|
|
<CreateResourceForm
|
|
|
|
open={isCreateModalOpen}
|
|
|
|
setOpen={setIsCreateModalOpen}
|
|
|
|
/>
|
|
|
|
|
2024-11-11 23:00:51 -05:00
|
|
|
{selectedResource && (
|
|
|
|
<ConfirmDeleteDialog
|
|
|
|
open={isDeleteModalOpen}
|
|
|
|
setOpen={(val) => {
|
|
|
|
setIsDeleteModalOpen(val);
|
|
|
|
setSelectedResource(null);
|
|
|
|
}}
|
|
|
|
dialog={
|
|
|
|
<div>
|
|
|
|
<p className="mb-2">
|
|
|
|
Are you sure you want to remove the resource{" "}
|
|
|
|
<b>
|
|
|
|
{selectedResource?.name ||
|
|
|
|
selectedResource?.id}
|
|
|
|
</b>{" "}
|
|
|
|
from the organization?
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p className="mb-2">
|
|
|
|
Once removed, the resource will no longer be
|
|
|
|
accessible. All targets attached to the resource
|
|
|
|
will be removed.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
To confirm, please type the name of the resource
|
|
|
|
below.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
}
|
2024-11-15 18:25:27 -05:00
|
|
|
buttonText="Confirm Delete Resource"
|
2024-11-11 23:00:51 -05:00
|
|
|
onConfirm={async () => deleteResource(selectedResource!.id)}
|
|
|
|
string={selectedResource.name}
|
2024-11-15 18:25:27 -05:00
|
|
|
title="Delete Resource"
|
2024-11-11 23:00:51 -05:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
2024-11-11 00:00:16 -05:00
|
|
|
<ResourcesDataTable
|
|
|
|
columns={columns}
|
|
|
|
data={resources}
|
|
|
|
addResource={() => {
|
|
|
|
setIsCreateModalOpen(true);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
2024-10-19 15:49:16 -04:00
|
|
|
);
|
|
|
|
}
|