Skip to content

Commit

Permalink
Refactor API token display (#9168)
Browse files Browse the repository at this point in the history
* Refactor API token display

* Tweaks

- Fix accessor ID
- Add filtering

* Revert ordering fields
  • Loading branch information
SchrodingersGat authored Feb 24, 2025
1 parent 49eaeda commit 58a03d9
Showing 1 changed file with 61 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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: <IconCircleX />,
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) => (
<Table.Tr key={token.id}>
<Table.Td>
<YesNoButton value={token.active} />
</Table.Td>
<Table.Td>{token.expiry}</Table.Td>
<Table.Td>{token.last_seen}</Table.Td>
<Table.Td>{token.token}</Table.Td>
<Table.Td>{token.name}</Table.Td>
<Table.Td>
{token.in_use ? (
<Trans>Token is used - no actions</Trans>
) : (
<Button
onClick={() => revokeToken(token.id)}
color='red'
disabled={!token.active}
>
<Trans>Revoke</Trans>
</Button>
)}
</Table.Td>
</Table.Tr>
));
}, [data, isLoading]);

if (isLoading) return <Loader />;

if (data.length == 0)
return (
<Alert icon={<IconAlertCircle size='1rem' />} color='green'>
<Trans>No tokens configured</Trans>
</Alert>
);
.catch((error) => {
showApiErrorMessage({
error: error,
title: t`Error revoking token`
});
});
};

return (
<Table stickyHeader striped highlightOnHover withTableBorder>
<Table.Thead>
<Table.Tr>
<Table.Th>
<Trans>Active</Trans>
</Table.Th>
<Table.Th>
<Trans>Expiry</Trans>
</Table.Th>
<Table.Th>
<Trans>Last Seen</Trans>
</Table.Th>
<Table.Th>
<Trans>Token</Trans>
</Table.Th>
<Table.Th>
<Trans>Name</Trans>
</Table.Th>
<Table.Th>
<Trans>Actions</Trans>
</Table.Th>
</Table.Tr>
</Table.Thead>
<Table.Tbody>{rows}</Table.Tbody>
</Table>
<InvenTreeTable
tableState={table}
url={apiUrl(ApiEndpoints.user_tokens)}
columns={tableColumns}
props={{
rowActions: rowActions,
enableSearch: false,
enableColumnSwitching: false
}}
/>
);
}

0 comments on commit 58a03d9

Please sign in to comment.