import * as Dialog from '@radix-ui/react-dialog' import { useMutation, useQueryClient } from '@tanstack/react-query' import { devicesApi } from '../api' import toast from 'react-hot-toast' import { useState } from 'react' import CredentialsViewer from './CredentialsViewer' import type { AxiosError } from 'axios' import type { Device, DeviceRegistrationResponse } from '../types/api' interface RenewDialogProps { device: Device open: boolean onOpenChange: (open: boolean) => void } export default function RenewDialog({ device, open, onOpenChange }: RenewDialogProps) { const queryClient = useQueryClient() const [credentials, setCredentials] = useState(null) const renewMutation = useMutation({ mutationFn: () => devicesApi.renew(device.id), onSuccess: (response) => { queryClient.invalidateQueries({ queryKey: ['devices'] }) queryClient.invalidateQueries({ queryKey: ['device', device.id] }) setCredentials(response.data) toast.success(`Certificate for "${device.name}" renewed successfully`) }, onError: (error) => { const axiosError = error as AxiosError<{ detail?: string }> const message = axiosError.response?.data?.detail || axiosError.message toast.error(`Failed to renew certificate: ${message}`) }, }) const handleOpenChange = (nextOpen: boolean) => { if (!nextOpen) { setCredentials(null) onOpenChange(false) } } return ( {credentials ? 'Certificate Renewed' : 'Renew Certificate'} {!credentials ? ( <> This will generate a new certificate for {device.name}. You will need to update the device with the new credentials.
) : ( <>
Save these credentials now! They will not be shown again.
)}
) }