import React, { useState, useEffect } from 'react'; import { Table, Button, Stack, Title, Modal, TextInput, MultiSelect, Group, ActionIcon, Badge, Card, Text, Loader, Alert, Textarea, Select, NumberInput, } from '@mantine/core'; import { IconPlus, IconEdit, IconTrash, IconEye, IconCopy, IconAlertCircle, } from '@tabler/icons-react'; import { useForm } from '@mantine/form'; import { notifications } from '@mantine/notifications'; import { apiService, Application, CreateApplicationRequest } from '../services/apiService'; import dayjs from 'dayjs'; const Applications: React.FC = () => { const [applications, setApplications] = useState([]); const [loading, setLoading] = useState(false); const [modalOpen, setModalOpen] = useState(false); const [editingApp, setEditingApp] = useState(null); const [detailModalOpen, setDetailModalOpen] = useState(false); const [selectedApp, setSelectedApp] = useState(null); const form = useForm({ initialValues: { app_id: '', app_link: '', type: [], callback_url: '', token_prefix: '', token_renewal_duration: '24h', max_token_duration: '168h', owner: { type: 'user', name: 'Admin User', owner: 'admin@example.com', }, }, validate: { app_id: (value) => value.length < 1 ? 'App ID is required' : null, app_link: (value) => value.length < 1 ? 'App Link is required' : null, callback_url: (value) => value.length < 1 ? 'Callback URL is required' : null, }, }); useEffect(() => { loadApplications(); }, []); const loadApplications = async () => { try { setLoading(true); const response = await apiService.getApplications(100, 0); setApplications(response.data); } catch (error) { console.error('Failed to load applications:', error); notifications.show({ title: 'Error', message: 'Failed to load applications', color: 'red', }); } finally { setLoading(false); } }; const handleSubmit = async (values: CreateApplicationRequest) => { try { if (editingApp) { await apiService.updateApplication(editingApp.app_id, values); notifications.show({ title: 'Success', message: 'Application updated successfully', color: 'green', }); } else { await apiService.createApplication(values); notifications.show({ title: 'Success', message: 'Application created successfully', color: 'green', }); } setModalOpen(false); setEditingApp(null); form.reset(); loadApplications(); } catch (error) { console.error('Failed to save application:', error); notifications.show({ title: 'Error', message: 'Failed to save application', color: 'red', }); } }; const handleEdit = (app: Application) => { setEditingApp(app); form.setValues({ app_id: app.app_id, app_link: app.app_link, type: app.type, callback_url: app.callback_url, token_prefix: app.token_prefix || '', token_renewal_duration: `${app.token_renewal_duration / 3600}h`, max_token_duration: `${app.max_token_duration / 3600}h`, owner: app.owner, }); setModalOpen(true); }; const handleDelete = async (appId: string) => { if (window.confirm('Are you sure you want to delete this application?')) { try { await apiService.deleteApplication(appId); notifications.show({ title: 'Success', message: 'Application deleted successfully', color: 'green', }); loadApplications(); } catch (error) { console.error('Failed to delete application:', error); notifications.show({ title: 'Error', message: 'Failed to delete application', color: 'red', }); } } }; const handleViewDetails = (app: Application) => { setSelectedApp(app); setDetailModalOpen(true); }; const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text); notifications.show({ title: 'Copied', message: 'Copied to clipboard', color: 'blue', }); }; const appTypeOptions = [ { value: 'web', label: 'Web Application' }, { value: 'mobile', label: 'Mobile Application' }, { value: 'api', label: 'API Service' }, { value: 'cli', label: 'CLI Tool' }, { value: 'service', label: 'Background Service' }, ]; const rows = applications.map((app) => ( {app.app_id} {app.type.map((type) => ( {type} ))} {app.owner.name} ({app.owner.owner}) {dayjs(app.created_at).format('MMM DD, YYYY')} handleViewDetails(app)} title="View Details" > handleEdit(app)} title="Edit" > handleDelete(app.app_id)} title="Delete" > )); return (
Applications Manage your registered applications and their configurations
{loading ? ( Loading applications... ) : applications.length === 0 ? (
No applications found Create your first application to get started with the key management system
) : ( Application ID Type Owner Created Actions {rows}
)} {/* Create/Edit Modal */} { setModalOpen(false); setEditingApp(null); form.reset(); }} title={editingApp ? 'Edit Application' : 'Create New Application'} size="lg" >
{/* Detail Modal */} setDetailModalOpen(false)} title="Application Details" size="md" > {selectedApp && ( Application ID: {selectedApp.app_id} copyToClipboard(selectedApp.app_id)} > HMAC Key: {selectedApp.hmac_key.substring(0, 16)}... copyToClipboard(selectedApp.hmac_key)} > Application Link: {selectedApp.app_link} Callback URL: {selectedApp.callback_url} Token Renewal: {selectedApp.token_renewal_duration / 3600}h Max Duration: {selectedApp.max_token_duration / 3600}h Created: {dayjs(selectedApp.created_at).format('MMM DD, YYYY HH:mm')} )}
); }; export default Applications;