/****************************************************************************** * Copyright (c) 2026 Contributors to the Eclipse Foundation. * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * https://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-2.0 *****************************************************************************/ import { FunctionComponent, useContext, useEffect, useState } from 'react'; import { Box, Typography, Divider, List, ListItem, ListItemText, IconButton, Paper, Button, type PaperProps } from '@mui/material'; import DeleteIcon from '@mui/icons-material/Delete'; import AddIcon from '@mui/icons-material/Add'; import { MainContext } from '../../../context'; import { Customer } from '../../../extension-registry-types'; import { GenerateTokenDialog } from '../../../components/generate-token-dialog'; import { Timestamp } from '../../../components/timestamp'; import { useCreateCustomerToken, useCustomerTokens, useDeleteCustomerToken } from './use-customers'; const sectionPaperProps: PaperProps = { elevation: 1, sx: { p: 3, mb: 3 } }; export const CustomerTokenList: FunctionComponent = props => { const { handleError } = useContext(MainContext); const [dialogOpen, setDialogOpen] = useState(false); const { data: tokens = [], error } = useCustomerTokens(props.customer.name); const { mutateAsync: createToken } = useCreateCustomerToken(props.customer.name); const { mutate: deleteToken } = useDeleteCustomerToken(props.customer.name); // Preserve the previous behaviour of surfacing fetch failures via the global error dialog. useEffect(() => { if (error) { handleError(error); } }, [error, handleError]); const handleGenerate = async (description: string): Promise => { const token = await createToken(description); return token.value ?? ''; }; const handleDelete = (tokenId: number) => { deleteToken(tokenId, { onError: err => handleError(err) }); }; return ( Tokens {tokens.length === 0 ? ( No rate limiting tokens for this customer. ) : ( {tokens.map(token => ( handleDelete(token.id)} title='Delete token'> }> {token.description || 'N/A'} } secondary={ Created: } /> ))} )} setDialogOpen(false)} onGenerate={handleGenerate} onError={handleError} title='Generate token' /> ); }; export interface CustomerTokenListProps { customer: Customer; }