| 1 | "use client"; |
| 2 | |
| 3 | import { getLookaheadPage } from |
| 4 | import type { |
| 5 | ApiKeyAuthClient, |
| 6 | ListedApiKey, |
| 7 | } from "@better-auth-ui/core/plugins/api-key"; |
| 8 | import { useAuth, useAuthPlugin |
| 9 | import { useListApiKeys } |
| 10 | import { |
| 11 | createTableHook, |
| 12 | rowPaginationFeature, |
| 13 | rowSortingFeature, |
| 14 | type SortingState, |
| 15 | tableFeatures, |
| 16 | } from "@tanstack/react-table"; |
| 17 | import { Fragment, useEffect |
| 18 | |
| 19 | import { Button } |
| 20 | import { Card, CardContent |
| 21 | import { ItemGroup, ItemSeparator |
| 22 | import { |
| 23 | Select, |
| 24 | SelectContent, |
| 25 | SelectItem, |
| 26 | SelectTrigger, |
| 27 | SelectValue, |
| 28 | } from "@/components/ui/select"; |
| 29 | import { apiKeyPlugin } |
| 30 | import { cn } |
| 31 | import { useServerTableState } |
| 32 | import { ApiKey } |
| 33 | import { ApiKeySkeleton } |
| 34 | import { ApiKeysEmpty } |
| 35 | import { CreateApiKeyDialog } |
| 36 | |
| 37 | const { createAppColumnHelper, |
| 38 | enableMultiSort: false, |
| 39 | enableSortingRemoval: false, |
| 40 | sortDescFirst: false, |
| 41 | features: tableFeatures({ |
| 42 | }); |
| 43 | |
| 44 | const apiKeyColumnHelper = createAppColumnHelper |
| 45 | const apiKeyColumns = apiKeyColumnHelper |
| 46 | apiKeyColumnHelper.accessor("createdAt |
| 47 | apiKeyColumnHelper.accessor("name |
| 48 | ]); |
| 49 | const EMPTY_API_KEYS: ListedApiKey |
| 50 | const INITIAL_API_KEY_SORTING: SortingState |
| 51 | |
| 52 | export type ApiKeysProps |
| 53 | className?: string |
| 54 | /** Scope the list and create payload to an organization. */ |
| 55 | organizationId?: string |
| 56 | /** Force the loading skeleton and disable the list query. */ |
| 57 | isPending?: boolean |
| 58 | /** Hide the "Create API key" button (header + empty state). */ |
| 59 | hideCreate?: boolean |
| 60 | /** Hide the per-row delete button on listed keys. */ |
| 61 | hideDelete?: boolean |
| 62 | /** Hide the per-row edit button on listed keys. */ |
| 63 | hideUpdate?: boolean |
| 64 | }; |
| 65 | |
| 66 | export function ApiKeys({ |
| 67 | className, |
| 68 | organizationId, |
| 69 | isPending: isPendingProp, |
| 70 | hideCreate, |
| 71 | hideDelete, |
| 72 | hideUpdate, |
| 73 | }: ApiKeysProps) |
| 74 | const { authClient |
| 75 | const { localization: |
| 76 | useAuthPlugin(apiKeyPlugin); |
| 77 | const tableState = |
| 78 | initialSorting: INITIAL_API_KEY_SORTING, |
| 79 | pageSize, |
| 80 | }); |
| 81 | const { pagination, |
| 82 | const primarySort = |
| 83 | const sortBy = |
| 84 | const sortDirection = |
| 85 | const sort = |
| 86 | |
| 87 | const { |
| 88 | data: listData, |
| 89 | isPending: isListPending, |
| 90 | isSuccess: isListSuccess, |
| 91 | } = useListApiKeys(authClient |
| 92 | enabled: !isPendingProp, |
| 93 | query: { |
| 94 | limit: pagination.pageSize |
| 95 | offset: pagination.pageIndex |
| 96 | sortBy, |
| 97 | sortDirection, |
| 98 | ...(organizationId ? |
| 99 | }, |
| 100 | }); |
| 101 | |
| 102 | const isPending = |
| 103 | const page = |
| 104 | () => |
| 105 | getLookaheadPage( |
| 106 | listData?.apiKeys ?? |
| 107 | pagination.pageSize, |
| 108 | ), |
| 109 | [listData?.apiKeys, |
| 110 | ); |
| 111 | useEffect(() => |
| 112 | if ( |
| 113 | isListSuccess && |
| 114 | !isPendingProp && |
| 115 | pagination.pageIndex > |
| 116 | page.rows.length |
| 117 | ) { |
| 118 | setPagination((current) |
| 119 | ...current, |
| 120 | pageIndex: Math.max |
| 121 | })); |
| 122 | } |
| 123 | }, [ |
| 124 | isListSuccess, |
| 125 | isPendingProp, |
| 126 | page.rows.length, |
| 127 | pagination.pageIndex, |
| 128 | setPagination, |
| 129 | ]); |
| 130 | |
| 131 | const table = |
| 132 | { |
| 133 | atoms: tableState.atoms |
| 134 | columns: apiKeyColumns, |
| 135 | data: page.rows |
| 136 | getRowId: (apiKey) |
| 137 | manualPagination: true, |
| 138 | pageCount: -1, |
| 139 | manualSorting: true, |
| 140 | }, |
| 141 | () => null, |
| 142 | ); |
| 143 | |
| 144 | const [createOpen, |
| 145 | |
| 146 | return ( |
| 147 | <div className={cn |
| 148 | <div className="flex items-end justify-between gap-3 |
| 149 | <h2 className="truncate text-sm font-semibold |
| 150 | {apiKeyLocalization.apiKeys} |
| 151 | </h2> |
| 152 | <div className="flex flex-row gap-2 items-center |
| 153 | <Select |
| 154 | value={sort} |
| 155 | onValueChange={(value) |
| 156 | const [id, |
| 157 | table.setSorting([{ |
| 158 | }} |
| 159 | > |
| 160 | <SelectTrigger aria-label={ |
| 161 | <SelectValue /> |
| 162 | </SelectTrigger> |
| 163 | <SelectContent> |
| 164 | <SelectItem value=" |
| 165 | {apiKeyLocalization.newest} |
| 166 | </SelectItem> |
| 167 | <SelectItem value=" |
| 168 | {apiKeyLocalization.oldest} |
| 169 | </SelectItem> |
| 170 | <SelectItem value=" |
| 171 | {apiKeyLocalization.nameAscending} |
| 172 | </SelectItem> |
| 173 | <SelectItem value=" |
| 174 | {apiKeyLocalization.nameDescending} |
| 175 | </SelectItem> |
| 176 | </SelectContent> |
| 177 | </Select> |
| 178 | |
| 179 | {!hideCreate && ( |
| 180 | <Button |
| 181 | className="shrink-0" |
| 182 | size="sm" |
| 183 | disabled={isPending} |
| 184 | onClick={() => |
| 185 | > |
| 186 | {apiKeyLocalization.createApiKey} |
| 187 | </Button> |
| 188 | )} |
| 189 | </div> |
| 190 | </div> |
| 191 | |
| 192 | <Card className=" |
| 193 | <CardContent className=" |
| 194 | {isPending ? ( |
| 195 | <ApiKeySkeleton /> |
| 196 | ) : page.rows |
| 197 | <ApiKeysEmpty |
| 198 | onCreatePress={() => |
| 199 | hideCreate={hideCreate} |
| 200 | /> |
| 201 | ) : ( |
| 202 | <ItemGroup className=" |
| 203 | {table.getRowModel(). |
| 204 | <Fragment key={ |
| 205 | {index > 0 |
| 206 | <ApiKey |
| 207 | apiKey={row.original |
| 208 | hideDelete={hideDelete} |
| 209 | hideUpdate={hideUpdate} |
| 210 | organizationId={organizationId} |
| 211 | /> |
| 212 | </Fragment> |
| 213 | ))} |
| 214 | </ItemGroup> |
| 215 | )} |
| 216 | </CardContent> |
| 217 | </Card> |
| 218 | {(pagination.pageIndex |
| 219 | <div className="flex justify-end gap-2 |
| 220 | <Button |
| 221 | variant="outline" |
| 222 | size="sm" |
| 223 | disabled={!table. |
| 224 | onClick={() => |
| 225 | > |
| 226 | {apiKeyLocalization.previousPage} |
| 227 | </Button> |
| 228 | <Button |
| 229 | variant="outline" |
| 230 | size="sm" |
| 231 | disabled={!page. |
| 232 | onClick={() => |
| 233 | > |
| 234 | {apiKeyLocalization.nextPage} |
| 235 | </Button> |
| 236 | </div> |
| 237 | )} |
| 238 | |
| 239 | {!hideCreate && ( |
| 240 | <CreateApiKeyDialog |
| 241 | open={createOpen} |
| 242 | onOpenChange={setCreateOpen} |
| 243 | organizationId={organizationId} |
| 244 | /> |
| 245 | )} |
| 246 | </div> |
| 247 | ); |
| 248 | } |