| 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 | hideCreate?: boolean |
| 59 | hideDelete?: boolean |
| 60 | hideUpdate?: boolean |
| 61 | }; |
| 62 | |
| 63 | export function ApiKeys({ |
| 64 | className, |
| 65 | organizationId, |
| 66 | isPending: isPendingProp, |
| 67 | hideCreate, |
| 68 | hideDelete, |
| 69 | hideUpdate, |
| 70 | }: ApiKeysProps) |
| 71 | const { authClient |
| 72 | const { localization: |
| 73 | useAuthPlugin(apiKeyPlugin); |
| 74 | const tableState = |
| 75 | initialSorting: INITIAL_API_KEY_SORTING, |
| 76 | pageSize, |
| 77 | }); |
| 78 | const { pagination, |
| 79 | const primarySort = |
| 80 | const sortBy = |
| 81 | const sortDirection = |
| 82 | const sort = |
| 83 | |
| 84 | const { |
| 85 | data: listData, |
| 86 | isPending: isListPending, |
| 87 | isSuccess: isListSuccess, |
| 88 | } = useListApiKeys(authClient |
| 89 | enabled: !isPendingProp, |
| 90 | query: { |
| 91 | limit: pagination.pageSize |
| 92 | offset: pagination.pageIndex |
| 93 | sortBy, |
| 94 | sortDirection, |
| 95 | ...(organizationId ? |
| 96 | }, |
| 97 | }); |
| 98 | |
| 99 | const isPending = |
| 100 | const page = |
| 101 | () => |
| 102 | getLookaheadPage( |
| 103 | listData?.apiKeys ?? |
| 104 | pagination.pageSize, |
| 105 | ), |
| 106 | [listData?.apiKeys, |
| 107 | ); |
| 108 | useEffect(() => |
| 109 | if ( |
| 110 | isListSuccess && |
| 111 | !isPendingProp && |
| 112 | pagination.pageIndex > |
| 113 | page.rows.length |
| 114 | ) { |
| 115 | setPagination((current) |
| 116 | ...current, |
| 117 | pageIndex: Math.max |
| 118 | })); |
| 119 | } |
| 120 | }, [ |
| 121 | isListSuccess, |
| 122 | isPendingProp, |
| 123 | page.rows.length, |
| 124 | pagination.pageIndex, |
| 125 | setPagination, |
| 126 | ]); |
| 127 | |
| 128 | const table = |
| 129 | { |
| 130 | atoms: tableState.atoms |
| 131 | columns: apiKeyColumns, |
| 132 | data: page.rows |
| 133 | getRowId: (apiKey) |
| 134 | manualPagination: true, |
| 135 | pageCount: -1, |
| 136 | manualSorting: true, |
| 137 | }, |
| 138 | () => null, |
| 139 | ); |
| 140 | |
| 141 | const [createOpen, |
| 142 | |
| 143 | return ( |
| 144 | <div className={cn |
| 145 | <div className="flex items-end justify-between gap-3 |
| 146 | <h2 className="truncate text-sm font-semibold |
| 147 | {apiKeyLocalization.apiKeys} |
| 148 | </h2> |
| 149 | <div className="flex flex-row gap-2 items-center |
| 150 | <Select |
| 151 | value={sort} |
| 152 | onValueChange={(value) |
| 153 | const [id, |
| 154 | table.setSorting([{ |
| 155 | }} |
| 156 | > |
| 157 | <SelectTrigger aria-label={ |
| 158 | <SelectValue /> |
| 159 | </SelectTrigger> |
| 160 | <SelectContent> |
| 161 | <SelectItem value=" |
| 162 | {apiKeyLocalization.newest} |
| 163 | </SelectItem> |
| 164 | <SelectItem value=" |
| 165 | {apiKeyLocalization.oldest} |
| 166 | </SelectItem> |
| 167 | <SelectItem value=" |
| 168 | {apiKeyLocalization.nameAscending} |
| 169 | </SelectItem> |
| 170 | <SelectItem value=" |
| 171 | {apiKeyLocalization.nameDescending} |
| 172 | </SelectItem> |
| 173 | </SelectContent> |
| 174 | </Select> |
| 175 | |
| 176 | {!hideCreate && ( |
| 177 | <Button |
| 178 | className="shrink-0" |
| 179 | size="sm" |
| 180 | disabled={isPending} |
| 181 | onClick={() => |
| 182 | > |
| 183 | {apiKeyLocalization.createApiKey} |
| 184 | </Button> |
| 185 | )} |
| 186 | </div> |
| 187 | </div> |
| 188 | |
| 189 | <Card className=" |
| 190 | <CardContent className=" |
| 191 | {isPending ? ( |
| 192 | <ApiKeySkeleton /> |
| 193 | ) : page.rows |
| 194 | <ApiKeysEmpty |
| 195 | onCreatePress={() => |
| 196 | hideCreate={hideCreate} |
| 197 | /> |
| 198 | ) : ( |
| 199 | <ItemGroup className=" |
| 200 | {table.getRowModel(). |
| 201 | <Fragment key={ |
| 202 | {index > 0 |
| 203 | <ApiKey |
| 204 | apiKey={row.original |
| 205 | hideDelete={hideDelete} |
| 206 | hideUpdate={hideUpdate} |
| 207 | organizationId={organizationId} |
| 208 | /> |
| 209 | </Fragment> |
| 210 | ))} |
| 211 | </ItemGroup> |
| 212 | )} |
| 213 | </CardContent> |
| 214 | </Card> |
| 215 | {(pagination.pageIndex |
| 216 | <div className="flex justify-end gap-2 |
| 217 | <Button |
| 218 | variant="outline" |
| 219 | size="sm" |
| 220 | disabled={!table. |
| 221 | onClick={() => |
| 222 | > |
| 223 | {apiKeyLocalization.previousPage} |
| 224 | </Button> |
| 225 | <Button |
| 226 | variant="outline" |
| 227 | size="sm" |
| 228 | disabled={!page. |
| 229 | onClick={() => |
| 230 | > |
| 231 | {apiKeyLocalization.nextPage} |
| 232 | </Button> |
| 233 | </div> |
| 234 | )} |
| 235 | |
| 236 | {!hideCreate && ( |
| 237 | <CreateApiKeyDialog |
| 238 | open={createOpen} |
| 239 | onOpenChange={setCreateOpen} |
| 240 | organizationId={organizationId} |
| 241 | /> |
| 242 | )} |
| 243 | </div> |
| 244 | ); |
| 245 | } |