|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { ActionType, ProColumns, ProTable } from '@ant-design/pro-components'; |
| 4 | +import { Button } from '@lobehub/ui'; |
| 5 | +import { useMutation } from '@tanstack/react-query'; |
| 6 | +import { Popconfirm, Switch } from 'antd'; |
| 7 | +import { createStyles } from 'antd-style'; |
| 8 | +import { Trash } from 'lucide-react'; |
| 9 | +import { FC, useRef, useState } from 'react'; |
| 10 | +import { useTranslation } from 'react-i18next'; |
| 11 | + |
| 12 | +import { lambdaClient } from '@/libs/trpc/client'; |
| 13 | +import { ApiKeyItem, CreateApiKeyParams, UpdateApiKeyParams } from '@/types/apiKey'; |
| 14 | + |
| 15 | +import { ApiKeyDisplay, ApiKeyModal, EditableCell } from './features'; |
| 16 | + |
| 17 | +const useStyles = createStyles(({ css, token }) => ({ |
| 18 | + container: css` |
| 19 | + .ant-pro-card-body { |
| 20 | + padding-inline: 0; |
| 21 | +
|
| 22 | + .ant-pro-table-list-toolbar-container { |
| 23 | + padding-block-start: 0; |
| 24 | + } |
| 25 | + } |
| 26 | + `, |
| 27 | + header: css` |
| 28 | + display: flex; |
| 29 | + justify-content: flex-end; |
| 30 | + margin-block-end: ${token.margin}px; |
| 31 | + `, |
| 32 | + table: css` |
| 33 | + border-radius: ${token.borderRadius}px; |
| 34 | + background: ${token.colorBgContainer}; |
| 35 | + `, |
| 36 | +})); |
| 37 | + |
| 38 | +const Client: FC = () => { |
| 39 | + const { styles } = useStyles(); |
| 40 | + const { t } = useTranslation('auth'); |
| 41 | + const [modalOpen, setModalOpen] = useState(false); |
| 42 | + |
| 43 | + const actionRef = useRef<ActionType>(null); |
| 44 | + |
| 45 | + const createMutation = useMutation({ |
| 46 | + mutationFn: (params: CreateApiKeyParams) => lambdaClient.apiKey.createApiKey.mutate(params), |
| 47 | + onSuccess: () => { |
| 48 | + actionRef.current?.reload(); |
| 49 | + setModalOpen(false); |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const updateMutation = useMutation({ |
| 54 | + mutationFn: ({ id, params }: { id: number; params: UpdateApiKeyParams }) => |
| 55 | + lambdaClient.apiKey.updateApiKey.mutate({ id, value: params }), |
| 56 | + onSuccess: () => { |
| 57 | + actionRef.current?.reload(); |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + const deleteMutation = useMutation({ |
| 62 | + mutationFn: (id: number) => lambdaClient.apiKey.deleteApiKey.mutate({ id }), |
| 63 | + onSuccess: () => { |
| 64 | + actionRef.current?.reload(); |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + const handleCreate = () => { |
| 69 | + setModalOpen(true); |
| 70 | + }; |
| 71 | + |
| 72 | + const handleModalOk = (values: CreateApiKeyParams) => { |
| 73 | + createMutation.mutate(values); |
| 74 | + }; |
| 75 | + |
| 76 | + const columns: ProColumns<ApiKeyItem>[] = [ |
| 77 | + { |
| 78 | + dataIndex: 'name', |
| 79 | + key: 'name', |
| 80 | + render: (_, apiKey) => ( |
| 81 | + <EditableCell |
| 82 | + onSubmit={(name) => { |
| 83 | + if (!name || name === apiKey.name) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + updateMutation.mutate({ id: apiKey.id!, params: { name: name as string } }); |
| 88 | + }} |
| 89 | + placeholder={t('apikey.display.enterPlaceholder')} |
| 90 | + type="text" |
| 91 | + value={apiKey.name} |
| 92 | + /> |
| 93 | + ), |
| 94 | + title: t('apikey.list.columns.name'), |
| 95 | + }, |
| 96 | + { |
| 97 | + dataIndex: 'key', |
| 98 | + ellipsis: true, |
| 99 | + key: 'key', |
| 100 | + render: (_, apiKey) => <ApiKeyDisplay apiKey={apiKey.key} />, |
| 101 | + title: t('apikey.list.columns.key'), |
| 102 | + width: 230, |
| 103 | + }, |
| 104 | + { |
| 105 | + dataIndex: 'enabled', |
| 106 | + key: 'enabled', |
| 107 | + render: (_, apiKey: ApiKeyItem) => ( |
| 108 | + <Switch |
| 109 | + checked={!!apiKey.enabled} |
| 110 | + onChange={(checked) => { |
| 111 | + updateMutation.mutate({ id: apiKey.id!, params: { enabled: checked } }); |
| 112 | + }} |
| 113 | + /> |
| 114 | + ), |
| 115 | + title: t('apikey.list.columns.status'), |
| 116 | + width: 100, |
| 117 | + }, |
| 118 | + { |
| 119 | + dataIndex: 'expiresAt', |
| 120 | + key: 'expiresAt', |
| 121 | + render: (_, apiKey) => ( |
| 122 | + <EditableCell |
| 123 | + onSubmit={(expiresAt) => { |
| 124 | + if (expiresAt === apiKey.expiresAt) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + updateMutation.mutate({ |
| 129 | + id: apiKey.id!, |
| 130 | + params: { expiresAt: expiresAt ? new Date(expiresAt as string) : null }, |
| 131 | + }); |
| 132 | + }} |
| 133 | + placeholder={t('apikey.display.neverExpires')} |
| 134 | + type="date" |
| 135 | + value={apiKey.expiresAt?.toLocaleString() || t('apikey.display.neverExpires')} |
| 136 | + /> |
| 137 | + ), |
| 138 | + title: t('apikey.list.columns.expiresAt'), |
| 139 | + width: 170, |
| 140 | + }, |
| 141 | + { |
| 142 | + dataIndex: 'lastUsedAt', |
| 143 | + key: 'lastUsedAt', |
| 144 | + renderText: (_, apiKey: ApiKeyItem) => |
| 145 | + apiKey.lastUsedAt?.toLocaleString() || t('apikey.display.neverUsed'), |
| 146 | + title: t('apikey.list.columns.lastUsedAt'), |
| 147 | + }, |
| 148 | + { |
| 149 | + key: 'action', |
| 150 | + render: (_: any, apiKey: ApiKeyItem) => ( |
| 151 | + <Popconfirm |
| 152 | + cancelText={t('apikey.list.actions.deleteConfirm.actions.cancel')} |
| 153 | + description={t('apikey.list.actions.deleteConfirm.content')} |
| 154 | + okText={t('apikey.list.actions.deleteConfirm.actions.ok')} |
| 155 | + onConfirm={() => deleteMutation.mutate(apiKey.id!)} |
| 156 | + title={t('apikey.list.actions.deleteConfirm.title')} |
| 157 | + > |
| 158 | + <Button |
| 159 | + icon={Trash} |
| 160 | + size="small" |
| 161 | + style={{ verticalAlign: 'middle' }} |
| 162 | + title={t('apikey.list.actions.delete')} |
| 163 | + type="text" |
| 164 | + /> |
| 165 | + </Popconfirm> |
| 166 | + ), |
| 167 | + title: t('apikey.list.columns.actions'), |
| 168 | + width: 100, |
| 169 | + }, |
| 170 | + ]; |
| 171 | + |
| 172 | + return ( |
| 173 | + <div className={styles.container}> |
| 174 | + <ProTable |
| 175 | + actionRef={actionRef} |
| 176 | + className={styles.table} |
| 177 | + columns={columns} |
| 178 | + headerTitle={t('apikey.list.title')} |
| 179 | + options={false} |
| 180 | + pagination={false} |
| 181 | + request={async () => { |
| 182 | + const apiKeys = await lambdaClient.apiKey.getApiKeys.query(); |
| 183 | + |
| 184 | + return { |
| 185 | + data: apiKeys, |
| 186 | + success: true, |
| 187 | + }; |
| 188 | + }} |
| 189 | + rowKey="id" |
| 190 | + search={false} |
| 191 | + toolbar={{ |
| 192 | + actions: [ |
| 193 | + <Button key="create" onClick={handleCreate} type="primary"> |
| 194 | + {t('apikey.list.actions.create')} |
| 195 | + </Button>, |
| 196 | + ], |
| 197 | + }} |
| 198 | + /> |
| 199 | + <ApiKeyModal |
| 200 | + onCancel={() => setModalOpen(false)} |
| 201 | + onOk={handleModalOk} |
| 202 | + open={modalOpen} |
| 203 | + submitLoading={createMutation.isPending} |
| 204 | + /> |
| 205 | + </div> |
| 206 | + ); |
| 207 | +}; |
| 208 | + |
| 209 | +export default Client; |
0 commit comments