-
Notifications
You must be signed in to change notification settings - Fork 616
feat: root keys table - Merge after #3331 #3332
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ac9ea0e
feat: add trpc and filters schema for root keys
ogzhanolguncu 558349b
refactor: get rid of extra round trip and simplify query
ogzhanolguncu 4d138bd
feat: add root keys table
ogzhanolguncu 45a6c2d
Merge branch 'main' of github.com:unkeyed/unkey into root-keys-table
ogzhanolguncu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...board/app/(app)/settings/root-keys-v2/components/table/components/assigned-items-cell.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { cn } from "@/lib/utils"; | ||
| import { Page2 } from "@unkey/icons"; | ||
|
|
||
| export const AssignedItemsCell = ({ | ||
| permissionSummary, | ||
| isSelected = false, | ||
| }: { | ||
| permissionSummary: { | ||
| total: number; | ||
| categories: Record<string, number>; | ||
| }; | ||
| isSelected?: boolean; | ||
| }) => { | ||
| const { total } = permissionSummary; | ||
|
|
||
| const itemClassName = cn( | ||
| "font-mono rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 transition-all duration-100 border border-dashed text-grayA-12", | ||
| isSelected ? "bg-grayA-4 border-grayA-7" : "bg-grayA-3 border-grayA-6 group-hover:bg-grayA-4", | ||
| ); | ||
|
|
||
| const emptyClassName = cn( | ||
| "rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 transition-all duration-100 border border-dashed bg-grayA-2", | ||
| isSelected ? "border-grayA-7 text-grayA-9" : "border-grayA-6 text-grayA-8", | ||
| ); | ||
|
|
||
| if (total === 0) { | ||
| return ( | ||
| <div className="flex flex-col gap-1 py-1 max-w-[300px]"> | ||
| <div className={emptyClassName}> | ||
| <Page2 className="size-3" /> | ||
| <span className="text-grayA-9 text-xs">None assigned</span> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex flex-wrap gap-1 py-2 max-w-[300px]"> | ||
| <div className={itemClassName}> | ||
| <Page2 className="size-3" /> | ||
| <span className="text-grayA-11 text-xs max-w-[150px] truncate">{total} Permissions</span> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; |
39 changes: 39 additions & 0 deletions
39
...ard/app/(app)/settings/root-keys-v2/components/table/components/critical-perm-warning.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import type { RootKey } from "@/lib/trpc/routers/settings/root-keys/query"; | ||
| import { InfoTooltip } from "@unkey/ui"; | ||
| import { cn } from "@unkey/ui/src/lib/utils"; | ||
|
|
||
| type CriticalPermissionIndicatorProps = { | ||
| rootKey: RootKey; | ||
| isSelected: boolean; | ||
| }; | ||
|
|
||
| export const CriticalPermissionIndicator = ({ | ||
| rootKey, | ||
| isSelected, | ||
| }: CriticalPermissionIndicatorProps) => { | ||
| const hasCriticalPerm = rootKey.permissionSummary.hasCriticalPerm; | ||
|
|
||
| if (!hasCriticalPerm) { | ||
| return <div className="w-2" />; | ||
| } | ||
| return ( | ||
| <InfoTooltip | ||
| variant="inverted" | ||
| content={ | ||
| <div className="text-xs"> | ||
| <div className="font-medium"> | ||
| This root key has critical permissions that can permanently destroy data or compromise | ||
| security. | ||
| </div> | ||
| </div> | ||
| } | ||
| > | ||
| <div | ||
| className={cn( | ||
| "size-2 rounded-full cursor-pointer ml-auto", | ||
| isSelected ? "bg-orange-11" : "bg-orange-10 hover:bg-orange-11", | ||
| )} | ||
| /> | ||
| </InfoTooltip> | ||
| ); | ||
| }; |
50 changes: 50 additions & 0 deletions
50
apps/dashboard/app/(app)/settings/root-keys-v2/components/table/components/last-updated.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { cn } from "@/lib/utils"; | ||
| import { ChartActivity2 } from "@unkey/icons"; | ||
| import { Badge, TimestampInfo } from "@unkey/ui"; | ||
| import { useRef, useState } from "react"; | ||
| import { STATUS_STYLES } from "../utils/get-row-class"; | ||
|
|
||
| export const LastUpdated = ({ | ||
| isSelected, | ||
| lastUpdated, | ||
| }: { | ||
| isSelected: boolean; | ||
| lastUpdated?: number | null; | ||
| }) => { | ||
| const badgeRef = useRef<HTMLDivElement>(null); | ||
| const [showTooltip, setShowTooltip] = useState(false); | ||
|
|
||
| return ( | ||
| <Badge | ||
| ref={badgeRef} | ||
| className={cn( | ||
| "px-1.5 rounded-md flex gap-2 items-center max-w-min h-[22px] border-none cursor-pointer", | ||
| isSelected ? STATUS_STYLES.badge.selected : STATUS_STYLES.badge.default, | ||
| )} | ||
| onMouseOver={() => { | ||
| setShowTooltip(true); | ||
| }} | ||
| onMouseLeave={() => { | ||
| setShowTooltip(false); | ||
| }} | ||
| > | ||
| <div> | ||
| <ChartActivity2 size="sm-regular" /> | ||
| </div> | ||
| <div className="truncate"> | ||
| {lastUpdated ? ( | ||
| <TimestampInfo | ||
| displayType="relative" | ||
| value={lastUpdated} | ||
| className="truncate" | ||
| triggerRef={badgeRef} | ||
| open={showTooltip} | ||
| onOpenChange={setShowTooltip} | ||
| /> | ||
| ) : ( | ||
| "Never used" | ||
| )} | ||
| </div> | ||
| </Badge> | ||
| ); | ||
| }; | ||
66 changes: 66 additions & 0 deletions
66
apps/dashboard/app/(app)/settings/root-keys-v2/components/table/components/skeletons.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { cn } from "@/lib/utils"; | ||
| import { ChartActivity2, Dots, Key2, Page2 } from "@unkey/icons"; | ||
|
|
||
| export const RootKeyColumnSkeleton = () => ( | ||
| <div className="flex flex-col items-start px-[18px] py-[6px]"> | ||
| <div className="flex gap-4 items-center"> | ||
| <div className="size-5 rounded flex items-center justify-center border border-grayA-3 bg-grayA-3 animate-pulse"> | ||
| <Key2 size="sm-regular" className="text-gray-12 opacity-50" /> | ||
| </div> | ||
| <div className="h-4 w-24 bg-grayA-3 rounded animate-pulse" /> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| export const CreatedAtColumnSkeleton = () => ( | ||
| <div className="flex flex-col items-start py-[6px]"> | ||
| <div className="h-4 w-24 bg-grayA-3 rounded animate-pulse" /> | ||
| </div> | ||
| ); | ||
| export const KeyColumnSkeleton = () => ( | ||
| <div className="rounded-lg border bg-grayA-2 border-grayA-3 text-transparent w-[160px] px-2 py-1 flex gap-2 items-center h-[28px] animate-pulse"> | ||
| <div className="h-2 w-2 bg-grayA-3 rounded-full animate-pulse" /> | ||
| <div className="h-2 w-full bg-grayA-3 rounded animate-pulse" /> | ||
| </div> | ||
| ); | ||
|
|
||
| export const AssignedKeysColumnSkeleton = () => ( | ||
| <div className="flex flex-col gap-1 py-2 max-w-[200px]"> | ||
| <div className="rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 border border-dashed bg-grayA-3 border-grayA-6 animate-pulse h-[22px]"> | ||
| <Key2 size="md-regular" className="opacity-50" /> | ||
| <div className="h-2 w-12 bg-grayA-3 rounded animate-pulse" /> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| export const PermissionsColumnSkeleton = () => ( | ||
| <div className="flex flex-col gap-1 py-2 max-w-[200px]"> | ||
| <div className="rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 border border-dashed bg-grayA-3 border-grayA-6 animate-pulse h-[22px]"> | ||
| <Page2 className="size-3 opacity-50" size="md-regular" /> | ||
| <div className="h-2 w-20 bg-grayA-3 rounded animate-pulse" /> | ||
| </div> | ||
| <div className="rounded-md py-[2px] px-1.5 items-center w-fit flex gap-2 border border-dashed bg-grayA-3 border-grayA-6 animate-pulse h-[22px]"> | ||
| <Page2 className="size-3 opacity-50" size="md-regular" /> | ||
| <div className="h-2 w-16 bg-grayA-3 rounded animate-pulse" /> | ||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| export const LastUpdatedColumnSkeleton = () => ( | ||
| <div className="px-1.5 rounded-md flex gap-2 items-center max-w-min h-[22px] bg-grayA-3 border-none animate-pulse"> | ||
| <ChartActivity2 size="sm-regular" className="opacity-50" /> | ||
| <div className="h-2 w-16 bg-grayA-3 rounded animate-pulse" /> | ||
| </div> | ||
| ); | ||
|
|
||
| export const ActionColumnSkeleton = () => ( | ||
| <button | ||
| type="button" | ||
| className={cn( | ||
| "group size-5 p-0 rounded m-0 items-center flex justify-center animate-pulse", | ||
| "border border-gray-6", | ||
| )} | ||
| > | ||
| <Dots className="text-gray-11" size="sm-regular" /> | ||
| </button> | ||
| ); |
82 changes: 82 additions & 0 deletions
82
...hboard/app/(app)/settings/root-keys-v2/components/table/hooks/use-root-keys-list-query.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { trpc } from "@/lib/trpc/client"; | ||
| import type { RootKey } from "@/lib/trpc/routers/settings/root-keys/query"; | ||
| import { useEffect, useMemo, useState } from "react"; | ||
| import { rootKeysFilterFieldConfig, rootKeysListFilterFieldNames } from "../../../filters.schema"; | ||
| import { useFilters } from "../../../hooks/use-filters"; | ||
| import type { RootKeysQueryPayload } from "../query-logs.schema"; | ||
|
|
||
| export function useRootKeysListQuery() { | ||
| const [totalCount, setTotalCount] = useState(0); | ||
| const [rootKeysMap, setRootKeysMap] = useState(() => new Map<string, RootKey>()); | ||
| const { filters } = useFilters(); | ||
|
|
||
| const rootKeys = useMemo(() => Array.from(rootKeysMap.values()), [rootKeysMap]); | ||
|
|
||
| const queryParams = useMemo(() => { | ||
| const params: RootKeysQueryPayload = { | ||
| ...Object.fromEntries(rootKeysListFilterFieldNames.map((field) => [field, []])), | ||
| }; | ||
|
|
||
| filters.forEach((filter) => { | ||
| if (!rootKeysListFilterFieldNames.includes(filter.field) || !params[filter.field]) { | ||
| return; | ||
| } | ||
|
|
||
| const fieldConfig = rootKeysFilterFieldConfig[filter.field]; | ||
| const validOperators = fieldConfig.operators; | ||
| if (!validOperators.includes(filter.operator)) { | ||
| throw new Error("Invalid operator"); | ||
| } | ||
|
|
||
| if (typeof filter.value === "string") { | ||
| params[filter.field]?.push({ | ||
| operator: filter.operator, | ||
| value: filter.value, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| return params; | ||
| }, [filters]); | ||
|
|
||
| const { | ||
| data: rootKeyData, | ||
| hasNextPage, | ||
| fetchNextPage, | ||
| isFetchingNextPage, | ||
| isLoading: isLoadingInitial, | ||
| } = trpc.settings.rootKeys.query.useInfiniteQuery(queryParams, { | ||
| getNextPageParam: (lastPage) => lastPage.nextCursor, | ||
| staleTime: Number.POSITIVE_INFINITY, | ||
| refetchOnMount: false, | ||
| refetchOnWindowFocus: false, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (rootKeyData) { | ||
| const newMap = new Map<string, RootKey>(); | ||
|
|
||
| rootKeyData.pages.forEach((page) => { | ||
| page.keys.forEach((rootKey) => { | ||
| // Use slug as the unique identifier | ||
| newMap.set(rootKey.id, rootKey); | ||
|
ogzhanolguncu marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
|
|
||
| if (rootKeyData.pages.length > 0) { | ||
| setTotalCount(rootKeyData.pages[0].total); | ||
| } | ||
|
|
||
| setRootKeysMap(newMap); | ||
| } | ||
| }, [rootKeyData]); | ||
|
|
||
| return { | ||
| rootKeys, | ||
| isLoading: isLoadingInitial, | ||
| hasMore: hasNextPage, | ||
| loadMore: fetchNextPage, | ||
| isLoadingMore: isFetchingNextPage, | ||
| totalCount, | ||
| }; | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
apps/dashboard/app/(app)/settings/root-keys-v2/components/table/query-logs.schema.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { z } from "zod"; | ||
| import { rootKeysFilterOperatorEnum, rootKeysListFilterFieldNames } from "../../filters.schema"; | ||
|
|
||
| const filterItemSchema = z.object({ | ||
| operator: rootKeysFilterOperatorEnum, | ||
| value: z.string(), | ||
| }); | ||
|
|
||
| const baseFilterArraySchema = z.array(filterItemSchema).nullish(); | ||
|
|
||
| const filterFieldsSchema = rootKeysListFilterFieldNames.reduce( | ||
| (acc, fieldName) => { | ||
| acc[fieldName] = baseFilterArraySchema; | ||
| return acc; | ||
| }, | ||
| {} as Record<string, typeof baseFilterArraySchema>, | ||
| ); | ||
|
|
||
| const baseRootKeysSchema = z.object(filterFieldsSchema); | ||
|
|
||
| export const rootKeysQueryPayload = baseRootKeysSchema.extend({ | ||
| cursor: z.number().nullish(), | ||
| }); | ||
|
|
||
| export type RootKeysQueryPayload = z.infer<typeof rootKeysQueryPayload>; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.