import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Spinner } from '@/components/ui/spinner' import { ApiEndpoints } from '@/config/api' import { Routes } from '@/config/routes' import { useToast } from '@/hooks/use-toast' import httpBrowserClient from '@/lib/httpBrowserClient' import { useMutation, useQueryClient } from '@tanstack/react-query' import { QrCode, Copy, Smartphone, Download, AlertTriangle } from 'lucide-react' import React, { useState } from 'react' import QRCode from 'react-qr-code' export default function GenerateApiKey() { const [isGenerateKeyModalOpen, setIsGenerateKeyModalOpen] = useState(false) const [isConfirmGenerateKeyModalOpen, setIsConfirmGenerateKeyModalOpen] = useState(false) const handleConfirmGenerateKey = () => { setIsConfirmGenerateKeyModalOpen(true) } const queryClient = useQueryClient() // invalidate devices query after successful api key generation const { isPending: isGeneratingApiKey, error: generateApiKeyError, mutateAsync: generateApiKey, data: generatedApiKey, } = useMutation({ mutationKey: ['generate-api-key'], onSuccess: (data) => { setIsConfirmGenerateKeyModalOpen(false) setIsGenerateKeyModalOpen(true) queryClient.invalidateQueries({ queryKey: ['apiKeys', 'stats'] }) queryClient.refetchQueries({ queryKey: ['apiKeys', 'stats'] }) }, mutationFn: () => httpBrowserClient .post(ApiEndpoints.auth.generateApiKey()) .then((res) => res.data), }) const { toast } = useToast() const handleCopyKey = () => { navigator.clipboard.writeText(generatedApiKey?.data) toast({ title: 'API key copied to clipboard', }) } return ( <> Create new API Key

By clicking generate, you will be able to view your API key. Make sure to save it before closing the modal as you will not be able to view it again.

Your API Key Use this API key to connect your device or authenticate your service.
{generatedApiKey?.data && ( )}
{generatedApiKey?.data}

For Device Registration

Open the TextBee app and scan the QR code, or manually enter the API key in the app and click register/update.

Don't have the app?

Download the APK from{' '} {Routes.downloadAndroidApp} {' '} and install it.

For External Services

Copy the API key and store it securely for authenticating your external service with TextBee.

Important

Once you close this modal, you will not be able to view your API key again. Make sure to save it before closing.

) }