|
| 1 | +import useConfigAccessSummaryQuery from "@flanksource-ui/api/query-hooks/useConfigAccessSummaryQuery"; |
| 2 | +import { ConfigAccessSummary } from "@flanksource-ui/api/types/configs"; |
| 3 | +import { ConfigDetailsTabs } from "@flanksource-ui/components/Configs/ConfigDetailsTabs"; |
| 4 | +import { Age } from "@flanksource-ui/ui/Age"; |
| 5 | +import { Badge } from "@flanksource-ui/ui/Badge/Badge"; |
| 6 | +import { DataTable } from "@flanksource-ui/ui/DataTable"; |
| 7 | +import { CellContext, ColumnDef } from "@tanstack/table-core"; |
| 8 | +import { SortingState } from "@tanstack/react-table"; |
| 9 | +import { useMemo, useState } from "react"; |
| 10 | +import { useParams } from "react-router-dom"; |
| 11 | + |
| 12 | +const RoleCell = ({ getValue }: CellContext<ConfigAccessSummary, any>) => { |
| 13 | + const value = getValue(); |
| 14 | + return value ? ( |
| 15 | + <span>{value}</span> |
| 16 | + ) : ( |
| 17 | + <span className="text-gray-400">—</span> |
| 18 | + ); |
| 19 | +}; |
| 20 | + |
| 21 | +const LastSignedInCell = ({ |
| 22 | + getValue |
| 23 | +}: CellContext<ConfigAccessSummary, any>) => { |
| 24 | + const value = getValue(); |
| 25 | + if (!value) { |
| 26 | + return <span className="text-gray-400">Never</span>; |
| 27 | + } |
| 28 | + return <Age from={value} />; |
| 29 | +}; |
| 30 | + |
| 31 | +const OptionalDateCell = ({ |
| 32 | + getValue |
| 33 | +}: CellContext<ConfigAccessSummary, any>) => { |
| 34 | + const value = getValue(); |
| 35 | + if (!value) { |
| 36 | + return <span className="text-gray-400">—</span>; |
| 37 | + } |
| 38 | + return <Age from={value} />; |
| 39 | +}; |
| 40 | + |
| 41 | +const AccessTypeCell = ({ row }: CellContext<ConfigAccessSummary, any>) => { |
| 42 | + const groupId = row.original.external_group_id; |
| 43 | + if (groupId) { |
| 44 | + return <Badge text="Group" color="yellow" title={`Group ID: ${groupId}`} />; |
| 45 | + } |
| 46 | + |
| 47 | + return <Badge text="Direct" color="gray" />; |
| 48 | +}; |
| 49 | + |
| 50 | +export function ConfigDetailsAccessPage() { |
| 51 | + const { id } = useParams(); |
| 52 | + const { |
| 53 | + data: accessSummary, |
| 54 | + isLoading, |
| 55 | + refetch |
| 56 | + } = useConfigAccessSummaryQuery(id); |
| 57 | + const [sortBy, setSortBy] = useState<SortingState>([ |
| 58 | + { id: "user", desc: false } |
| 59 | + ]); |
| 60 | + |
| 61 | + const columns = useMemo<ColumnDef<ConfigAccessSummary>[]>( |
| 62 | + () => [ |
| 63 | + { |
| 64 | + header: "User", |
| 65 | + accessorKey: "user" |
| 66 | + }, |
| 67 | + { |
| 68 | + header: "Email", |
| 69 | + accessorKey: "email" |
| 70 | + }, |
| 71 | + { |
| 72 | + header: "Role", |
| 73 | + accessorKey: "role", |
| 74 | + cell: RoleCell |
| 75 | + }, |
| 76 | + { |
| 77 | + header: "Access", |
| 78 | + accessorKey: "external_group_id", |
| 79 | + cell: AccessTypeCell |
| 80 | + }, |
| 81 | + { |
| 82 | + header: "Last Signed In", |
| 83 | + accessorKey: "last_signed_in_at", |
| 84 | + cell: LastSignedInCell, |
| 85 | + sortingFn: "datetime" |
| 86 | + }, |
| 87 | + { |
| 88 | + header: "Last Reviewed", |
| 89 | + accessorKey: "last_reviewed_at", |
| 90 | + cell: OptionalDateCell, |
| 91 | + sortingFn: "datetime" |
| 92 | + }, |
| 93 | + { |
| 94 | + header: "Granted", |
| 95 | + accessorKey: "created_at", |
| 96 | + cell: OptionalDateCell, |
| 97 | + sortingFn: "datetime" |
| 98 | + } |
| 99 | + ], |
| 100 | + [] |
| 101 | + ); |
| 102 | + |
| 103 | + const rows = accessSummary?.data ?? []; |
| 104 | + |
| 105 | + return ( |
| 106 | + <ConfigDetailsTabs |
| 107 | + pageTitlePrefix={"Catalog Access"} |
| 108 | + isLoading={isLoading} |
| 109 | + refetch={refetch} |
| 110 | + activeTabName="Access" |
| 111 | + > |
| 112 | + <div className="flex h-full flex-1 flex-col gap-2 overflow-y-auto"> |
| 113 | + <div className="flex flex-1 flex-col overflow-y-auto"> |
| 114 | + <DataTable |
| 115 | + stickyHead |
| 116 | + columns={columns} |
| 117 | + data={rows} |
| 118 | + tableStyle={{ borderSpacing: "0" }} |
| 119 | + isLoading={isLoading} |
| 120 | + className="table-auto" |
| 121 | + tableSortByState={sortBy} |
| 122 | + onTableSortByChanged={setSortBy} |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + </ConfigDetailsTabs> |
| 127 | + ); |
| 128 | +} |
0 commit comments