|
| 1 | +import { routeAuthBeforeLoad } from '@/utils/route'; |
| 2 | +import { createFileRoute } from '@tanstack/react-router'; |
| 3 | +import { useTranslation } from '@i18next-toolkit/react'; |
| 4 | +import { CommonWrapper } from '@/components/CommonWrapper'; |
| 5 | +import { ScrollArea } from '@/components/ui/scroll-area'; |
| 6 | +import { CommonHeader } from '@/components/CommonHeader'; |
| 7 | +import { Card, CardContent, CardHeader } from '@/components/ui/card'; |
| 8 | +import { AppRouterOutput, defaultErrorHandler, trpc } from '@/api/trpc'; |
| 9 | +import { createColumnHelper, DataTable } from '@/components/DataTable'; |
| 10 | +import { useMemo } from 'react'; |
| 11 | +import { Button } from '@/components/ui/button'; |
| 12 | +import { useEvent } from '@/hooks/useEvent'; |
| 13 | +import dayjs from 'dayjs'; |
| 14 | +import { LuPlus, LuTrash } from 'react-icons/lu'; |
| 15 | +import copy from 'copy-to-clipboard'; |
| 16 | +import { toast } from 'sonner'; |
| 17 | +import { CopyableText } from '@/components/CopyableText'; |
| 18 | +import { AlertConfirm } from '@/components/AlertConfirm'; |
| 19 | +import { formatNumber } from '@/utils/common'; |
| 20 | + |
| 21 | +export const Route = createFileRoute('/settings/apiKey')({ |
| 22 | + beforeLoad: routeAuthBeforeLoad, |
| 23 | + component: PageComponent, |
| 24 | +}); |
| 25 | + |
| 26 | +type ApiKeyInfo = AppRouterOutput['user']['allApiKeys'][number]; |
| 27 | +const columnHelper = createColumnHelper<ApiKeyInfo>(); |
| 28 | + |
| 29 | +function PageComponent() { |
| 30 | + const { t } = useTranslation(); |
| 31 | + const { data: apiKeys = [], refetch: refetchApiKeys } = |
| 32 | + trpc.user.allApiKeys.useQuery(); |
| 33 | + const generateApiKeyMutation = trpc.user.generateApiKey.useMutation({ |
| 34 | + onError: defaultErrorHandler, |
| 35 | + }); |
| 36 | + const deleteApiKeyMutation = trpc.user.deleteApiKey.useMutation({ |
| 37 | + onError: defaultErrorHandler, |
| 38 | + }); |
| 39 | + |
| 40 | + const columns = useMemo(() => { |
| 41 | + return [ |
| 42 | + columnHelper.accessor('apiKey', { |
| 43 | + header: t('Key'), |
| 44 | + size: 300, |
| 45 | + cell: (props) => { |
| 46 | + return ( |
| 47 | + <CopyableText text={props.getValue()}> |
| 48 | + {props.getValue().slice(0, 20)}... |
| 49 | + </CopyableText> |
| 50 | + ); |
| 51 | + }, |
| 52 | + }), |
| 53 | + columnHelper.accessor('usage', { |
| 54 | + header: t('Usage'), |
| 55 | + size: 80, |
| 56 | + cell: (props) => { |
| 57 | + return ( |
| 58 | + <div className="text-right"> |
| 59 | + {formatNumber(Number(props.getValue()))} |
| 60 | + </div> |
| 61 | + ); |
| 62 | + }, |
| 63 | + }), |
| 64 | + columnHelper.accessor('createdAt', { |
| 65 | + header: t('Created At'), |
| 66 | + size: 130, |
| 67 | + cell: (props) => { |
| 68 | + const date = props.getValue(); |
| 69 | + return ( |
| 70 | + <span> |
| 71 | + {date ? dayjs(date).format('YYYY-MM-DD HH:mm:ss') : '-'} |
| 72 | + </span> |
| 73 | + ); |
| 74 | + }, |
| 75 | + }), |
| 76 | + columnHelper.accessor('updatedAt', { |
| 77 | + header: t('Last Use At'), |
| 78 | + size: 130, |
| 79 | + cell: (props) => { |
| 80 | + const date = props.getValue(); |
| 81 | + return ( |
| 82 | + <span> |
| 83 | + {date ? dayjs(date).format('YYYY-MM-DD HH:mm:ss') : '-'} |
| 84 | + </span> |
| 85 | + ); |
| 86 | + }, |
| 87 | + }), |
| 88 | + columnHelper.accessor('expiredAt', { |
| 89 | + header: t('Expired At'), |
| 90 | + size: 130, |
| 91 | + cell: (props) => { |
| 92 | + const date = props.getValue(); |
| 93 | + return ( |
| 94 | + <span> |
| 95 | + {date ? dayjs(date).format('YYYY-MM-DD HH:mm:ss') : '-'} |
| 96 | + </span> |
| 97 | + ); |
| 98 | + }, |
| 99 | + }), |
| 100 | + columnHelper.display({ |
| 101 | + id: 'action', |
| 102 | + header: t('Action'), |
| 103 | + size: 130, |
| 104 | + cell: (props) => { |
| 105 | + return ( |
| 106 | + <div> |
| 107 | + <AlertConfirm |
| 108 | + onConfirm={async () => { |
| 109 | + await deleteApiKeyMutation.mutateAsync({ |
| 110 | + apiKey: props.row.original.apiKey, |
| 111 | + }); |
| 112 | + refetchApiKeys(); |
| 113 | + }} |
| 114 | + > |
| 115 | + <Button variant="outline" size="icon" Icon={LuTrash} /> |
| 116 | + </AlertConfirm> |
| 117 | + </div> |
| 118 | + ); |
| 119 | + }, |
| 120 | + }), |
| 121 | + ]; |
| 122 | + }, [t]); |
| 123 | + |
| 124 | + const handleGenerateApiKey = useEvent(async () => { |
| 125 | + const apiKey = await generateApiKeyMutation.mutateAsync(); |
| 126 | + |
| 127 | + copy(apiKey); |
| 128 | + toast.success(t('New api key has been copied into your clipboard!')); |
| 129 | + refetchApiKeys(); |
| 130 | + }); |
| 131 | + |
| 132 | + return ( |
| 133 | + <CommonWrapper header={<CommonHeader title={t('Api Keys')} />}> |
| 134 | + <ScrollArea className="h-full overflow-hidden p-4"> |
| 135 | + <div className="flex flex-col gap-4"> |
| 136 | + <Card> |
| 137 | + <CardHeader className="text-lg font-bold"> |
| 138 | + <div className="flex items-center justify-between gap-2"> |
| 139 | + <div>{t('Api Keys')}</div> |
| 140 | + |
| 141 | + <Button |
| 142 | + Icon={LuPlus} |
| 143 | + size="icon" |
| 144 | + variant="outline" |
| 145 | + onClick={handleGenerateApiKey} |
| 146 | + /> |
| 147 | + </div> |
| 148 | + </CardHeader> |
| 149 | + <CardContent> |
| 150 | + <DataTable columns={columns} data={apiKeys} /> |
| 151 | + </CardContent> |
| 152 | + </Card> |
| 153 | + </div> |
| 154 | + </ScrollArea> |
| 155 | + </CommonWrapper> |
| 156 | + ); |
| 157 | +} |
0 commit comments