diff --git a/src/frontend/src/pages/Index/Settings/AccountSettings/SecurityContent.tsx b/src/frontend/src/pages/Index/Settings/AccountSettings/SecurityContent.tsx index 1d23e34503..1b5b32d4f1 100644 --- a/src/frontend/src/pages/Index/Settings/AccountSettings/SecurityContent.tsx +++ b/src/frontend/src/pages/Index/Settings/AccountSettings/SecurityContent.tsx @@ -23,18 +23,23 @@ import { hideNotification, showNotification } from '@mantine/notifications'; import { IconAlertCircle, IconAt, + IconCircleX, IconExclamationCircle, IconX } from '@tabler/icons-react'; import { useQuery } from '@tanstack/react-query'; -import { useMemo, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import { api } from '../../../../App'; -import { YesNoButton } from '../../../../components/buttons/YesNoButton'; import { StylishText } from '../../../../components/items/StylishText'; import { ApiEndpoints } from '../../../../enums/ApiEndpoints'; import { ProviderLogin, authApi } from '../../../../functions/auth'; +import { showApiErrorMessage } from '../../../../functions/notifications'; +import { useTable } from '../../../../hooks/UseTable'; import { apiUrl, useServerApiState } from '../../../../states/ApiState'; import type { AuthConfig, Provider } from '../../../../states/states'; +import { BooleanColumn } from '../../../../tables/ColumnRenderers'; +import { InvenTreeTable } from '../../../../tables/InvenTreeTable'; +import type { RowAction } from '../../../../tables/RowActions'; import { QrRegistrationForm } from './QrRegistrationForm'; import { useReauth } from './useConfirm'; @@ -710,83 +715,66 @@ async function runActionWithFallback( } function TokenSection() { - const { isLoading, data, refetch } = useQuery({ - queryKey: ['token-list'], - queryFn: () => - api.get(apiUrl(ApiEndpoints.user_tokens)).then((res) => res.data) - }); + const table = useTable('api-tokens', 'id'); - function revokeToken(id: string) { + const tableColumns = useMemo(() => { + return [ + { + accessor: 'name' + }, + BooleanColumn({ + accessor: 'active' + }), + { + accessor: 'token' + }, + { + accessor: 'last_seen' + }, + { + accessor: 'expiry' + } + ]; + }, []); + + const rowActions = useCallback((record: any): RowAction[] => { + return [ + { + title: t`Revoke`, + color: 'red', + hidden: !record.active || record.in_use, + icon: , + onClick: () => { + revokeToken(record.id); + } + } + ]; + }, []); + + const revokeToken = async (id: string) => { api .delete(apiUrl(ApiEndpoints.user_tokens, id)) .then(() => { - refetch(); + table.refreshTable(); }) - .catch((res) => console.log(res.data)); - } - - const rows = useMemo(() => { - if (isLoading || !data) return null; - return data.map((token: any) => ( - - - - - {token.expiry} - {token.last_seen} - {token.token} - {token.name} - - {token.in_use ? ( - Token is used - no actions - ) : ( - - )} - - - )); - }, [data, isLoading]); - - if (isLoading) return ; - - if (data.length == 0) - return ( - } color='green'> - No tokens configured - - ); + .catch((error) => { + showApiErrorMessage({ + error: error, + title: t`Error revoking token` + }); + }); + }; return ( - - - - - Active - - - Expiry - - - Last Seen - - - Token - - - Name - - - Actions - - - - {rows} -
+ ); }