mirror of
https://github.com/portainer/portainer.git
synced 2025-08-04 21:35:23 +02:00
refactor(account): migrate access tokens table to react [EE-4701] (#10669)
This commit is contained in:
parent
48aab77058
commit
3f3db75d85
16 changed files with 219 additions and 249 deletions
|
@ -0,0 +1,32 @@
|
|||
import { Key } from 'lucide-react';
|
||||
|
||||
import { Datatable } from '@@/datatables';
|
||||
import { createPersistedStore } from '@@/datatables/types';
|
||||
import { useTableState } from '@@/datatables/useTableState';
|
||||
|
||||
import { useAccessTokens } from '../../access-tokens/queries/useAccessTokens';
|
||||
|
||||
import { columns } from './columns';
|
||||
import { TableActions } from './TableActions';
|
||||
|
||||
const tableKey = 'access-tokens';
|
||||
const store = createPersistedStore(tableKey);
|
||||
|
||||
export function AccessTokensDatatable({ canExit }: { canExit?: boolean }) {
|
||||
const query = useAccessTokens();
|
||||
const tableState = useTableState(store, tableKey);
|
||||
|
||||
return (
|
||||
<Datatable
|
||||
columns={columns}
|
||||
isLoading={query.isLoading}
|
||||
dataset={query.data || []}
|
||||
settingsManager={tableState}
|
||||
title="Access tokens"
|
||||
titleIcon={Key}
|
||||
renderTableActions={(selectedItems) => (
|
||||
<TableActions selectedItems={selectedItems} canExit={canExit} />
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
import { notifySuccess } from '@/portainer/services/notifications';
|
||||
|
||||
import { DeleteButton } from '@@/buttons/DeleteButton';
|
||||
import { AddButton } from '@@/buttons';
|
||||
|
||||
import { AccessToken } from '../../access-tokens/types';
|
||||
|
||||
import { useDeleteAccessTokensMutation } from './useDeleteAccessTokensMutation';
|
||||
|
||||
export function TableActions({
|
||||
selectedItems,
|
||||
canExit,
|
||||
}: {
|
||||
selectedItems: AccessToken[];
|
||||
canExit?: boolean;
|
||||
}) {
|
||||
const deleteMutation = useDeleteAccessTokensMutation();
|
||||
|
||||
return (
|
||||
<>
|
||||
<DeleteButton
|
||||
disabled={selectedItems.length === 0}
|
||||
confirmMessage="Do you want to remove the selected access token(s)? Any script or application using these tokens will no longer be able to invoke the Portainer API."
|
||||
onConfirmed={handleRemove}
|
||||
/>
|
||||
|
||||
<AddButton to=".new-access-token" disabled={!canExit}>
|
||||
Add access token
|
||||
</AddButton>
|
||||
</>
|
||||
);
|
||||
|
||||
function handleRemove() {
|
||||
const ids = selectedItems.map((item) => item.id);
|
||||
deleteMutation.mutate(ids, {
|
||||
onSuccess() {
|
||||
notifySuccess('Success', 'Access token(s) removed');
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
import { createColumnHelper } from '@tanstack/react-table';
|
||||
|
||||
import { isoDateFromTimestamp } from '@/portainer/filters/filters';
|
||||
|
||||
import { AccessToken } from '../../access-tokens/types';
|
||||
|
||||
const columnHelper = createColumnHelper<AccessToken>();
|
||||
|
||||
export const columns = [
|
||||
columnHelper.accessor('description', {
|
||||
header: 'Description',
|
||||
}),
|
||||
columnHelper.accessor('prefix', {
|
||||
header: 'Prefix',
|
||||
}),
|
||||
columnHelper.accessor('dateCreated', {
|
||||
header: 'Created',
|
||||
cell: ({ getValue }) => isoDateFromTimestamp(getValue()),
|
||||
}),
|
||||
columnHelper.accessor('lastUsed', {
|
||||
header: 'Last Used',
|
||||
cell: ({ getValue }) => isoDateFromTimestamp(getValue()),
|
||||
}),
|
||||
];
|
|
@ -0,0 +1 @@
|
|||
export { AccessTokensDatatable } from './AccessTokensDatatable';
|
|
@ -0,0 +1,40 @@
|
|||
import { useMutation, useQueryClient } from 'react-query';
|
||||
|
||||
import { withError, withInvalidate } from '@/react-tools/react-query';
|
||||
import { useCurrentUser } from '@/react/hooks/useUser';
|
||||
import { promiseSequence } from '@/portainer/helpers/promise-utils';
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
|
||||
import { AccessToken } from '../../access-tokens/types';
|
||||
import { buildUrl } from '../../access-tokens/queries/build-url';
|
||||
import { queryKeys } from '../../access-tokens/queries/query-keys';
|
||||
|
||||
export function useDeleteAccessTokensMutation() {
|
||||
const { user } = useCurrentUser();
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (ids: Array<AccessToken['id']>) =>
|
||||
deleteAccessTokens(user.Id, ids),
|
||||
...withError('Failed to delete access tokens'),
|
||||
...withInvalidate(queryClient, [queryKeys.base(user.Id)]),
|
||||
});
|
||||
}
|
||||
|
||||
async function deleteAccessTokens(
|
||||
userId: number,
|
||||
tokenIds: Array<AccessToken['id']>
|
||||
) {
|
||||
return promiseSequence(
|
||||
tokenIds.map((tokenId) => () => deleteAccessToken(userId, tokenId))
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteAccessToken(userId: number, id: AccessToken['id']) {
|
||||
try {
|
||||
await axios.delete(buildUrl(userId, id));
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e, 'Unable to delete access token');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
import { UserId } from '@/portainer/users/types';
|
||||
|
||||
import { AccessToken } from '../types';
|
||||
|
||||
export function buildUrl(userId: UserId, id?: AccessToken['id']) {
|
||||
const baseUrl = `/users/${userId}/tokens`;
|
||||
return id ? `${baseUrl}/${id}` : baseUrl;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
import { userQueryKeys } from '@/portainer/users/queries/queryKeys';
|
||||
import { UserId } from '@/portainer/users/types';
|
||||
|
||||
export const queryKeys = {
|
||||
base: (userId: UserId) => [...userQueryKeys.user(userId), 'tokens'] as const,
|
||||
};
|
|
@ -0,0 +1,27 @@
|
|||
import { useQuery } from 'react-query';
|
||||
|
||||
import { useCurrentUser } from '@/react/hooks/useUser';
|
||||
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
||||
|
||||
import { AccessToken } from '../types';
|
||||
|
||||
import { queryKeys } from './query-keys';
|
||||
import { buildUrl } from './build-url';
|
||||
|
||||
export function useAccessTokens() {
|
||||
const { user } = useCurrentUser();
|
||||
|
||||
return useQuery({
|
||||
queryKey: queryKeys.base(user.Id),
|
||||
queryFn: () => getAccessTokens(user.Id),
|
||||
});
|
||||
}
|
||||
|
||||
async function getAccessTokens(userId: number) {
|
||||
try {
|
||||
const { data } = await axios.get<Array<AccessToken>>(buildUrl(userId));
|
||||
return data;
|
||||
} catch (e) {
|
||||
throw parseAxiosError(e, 'Unable to get access tokens');
|
||||
}
|
||||
}
|
22
app/react/portainer/account/access-tokens/types.ts
Normal file
22
app/react/portainer/account/access-tokens/types.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* AccessToken represents an API key
|
||||
*/
|
||||
export interface AccessToken {
|
||||
id: number;
|
||||
|
||||
userId: number;
|
||||
|
||||
description: string;
|
||||
|
||||
/** API key identifier (7 char prefix) */
|
||||
prefix: string;
|
||||
|
||||
/** Unix timestamp (UTC) when the API key was created */
|
||||
dateCreated: number;
|
||||
|
||||
/** Unix timestamp (UTC) when the API key was last used */
|
||||
lastUsed: number;
|
||||
|
||||
/** Digest represents SHA256 hash of the raw API key */
|
||||
digest?: string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue