'use client'; import {useState} from 'react'; import {proportional, pixel} from '@astryxdesign/core/Table'; import {ResponsiveTable} from '@/shared/components/patterns/ResponsiveTable'; import type {TableColumn} from '@astryxdesign/core/Table'; import {VStack, HStack} from '@astryxdesign/core/Layout'; import {Text} from '@astryxdesign/core/Text'; import {Badge} from '@astryxdesign/core/Badge'; import {Button} from '@astryxdesign/core/Button'; import {TextInput} from '@astryxdesign/core/TextInput'; import {useToast} from '@astryxdesign/core/Toast'; import {StaticPanel} from '@/shared/components/patterns/PanelCard'; import { SAMPLE_API_KEYS, SAMPLE_WEBHOOKS, SAMPLE_WEBHOOK_LOGS, sampleApiKeyPrefix, sampleWebhookSecret, } from '@/shared/mocks/sampleData'; export interface ApiKeyItem extends Record { id: string; name: string; keyPrefix: string; created: string; lastUsed: string; status: 'active' | 'revoked'; } export interface WebhookEndpointItem extends Record { id: string; url: string; events: string; secret: string; status: 'healthy' | 'failing'; } export interface WebhookLogItem extends Record { id: string; event: string; url: string; code: number; latency: string; timestamp: string; } export function ApiWebhooksManager() { const toast = useToast(); const [keys, setKeys] = useState(SAMPLE_API_KEYS); const [webhooks, setWebhooks] = useState(SAMPLE_WEBHOOKS); const [newKeyName, setNewKeyName] = useState(''); const [isGenerating, setIsGenerating] = useState(false); const [newWebhookUrl, setNewWebhookUrl] = useState(''); const handleGenerateKey = () => { if (!newKeyName.trim()) { toast({type: 'error', body: 'Key name is required'}); return; } const createdKey: ApiKeyItem = { id: `key-${Date.now()}`, name: newKeyName, keyPrefix: sampleApiKeyPrefix(), created: 'Just now', lastUsed: 'Never', status: 'active', }; setKeys((prev) => [createdKey, ...prev]); setNewKeyName(''); setIsGenerating(false); toast({body: `API Key "${newKeyName}" generated successfully`}); }; const handleRevokeKey = (id: string, name: string) => { setKeys((prev) => prev.filter((k) => k.id !== id)); toast({body: `Revoked API Key "${name}"`}); }; const handleAddWebhook = () => { if (!newWebhookUrl.trim()) { toast({type: 'error', body: 'Webhook URL is required'}); return; } const newWh: WebhookEndpointItem = { id: `wh-${Date.now()}`, url: newWebhookUrl, events: 'order.created, lyt.redeemed, staff.checkin', secret: sampleWebhookSecret(), status: 'healthy', }; setWebhooks((prev) => [newWh, ...prev]); setNewWebhookUrl(''); toast({body: 'Webhook endpoint registered successfully'}); }; const keyColumns: TableColumn[] = [ { key: 'name', header: 'Key Name', width: proportional(1.5), renderCell: (row) => ( {row.name} {row.keyPrefix} ), }, { key: 'created', header: 'Created Date', width: proportional(1.2), renderCell: (row) => {row.created}, }, { key: 'lastUsed', header: 'Last Active', width: proportional(1.2), renderCell: (row) => ( {row.lastUsed} ), }, { key: 'status', header: 'Status', width: pixel(100), renderCell: (row) => ( ), }, { key: 'actions', header: 'Action', align: 'center', width: pixel(120), renderCell: (row) => (