'use client'; import {useState} from 'react'; import {proportional, pixel} from '@astryxdesign/core/Table'; import {ResponsiveTable} from '@/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 '@/components/patterns/PanelCard'; 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; } const INITIAL_KEYS: ApiKeyItem[] = [ { id: 'key-1', name: 'Production POS Key', keyPrefix: 'lmer_live_9f8a••••••••4b12', created: '10 Jan 2026', lastUsed: 'Just now', status: 'active', }, { id: 'key-2', name: 'Staging Integration Key', keyPrefix: 'lmer_test_3d2c••••••••8e91', created: '15 Jun 2026', lastUsed: 'Yesterday', status: 'active', }, ]; const INITIAL_WEBHOOKS: WebhookEndpointItem[] = [ { id: 'wh-1', url: 'https://api.nearle.in/webhooks/loyaly', events: 'order.created, lyt.redeemed', secret: 'whsec_8841••••••••9932', status: 'healthy', }, { id: 'wh-2', url: 'https://hooks.zapier.com/hooks/catch/19284', events: 'staff.checkin, store.updated', secret: 'whsec_7712••••••••1102', status: 'healthy', }, ]; const WEBHOOK_LOGS: WebhookLogItem[] = [ { id: 'log-101', event: 'lyt.redeemed', url: 'https://api.nearle.in/webhooks/loyaly', code: 200, latency: '142ms', timestamp: 'Today, 15:42:01', }, { id: 'log-102', event: 'order.created', url: 'https://api.nearle.in/webhooks/loyaly', code: 200, latency: '185ms', timestamp: 'Today, 15:38:12', }, { id: 'log-103', event: 'staff.checkin', url: 'https://hooks.zapier.com/hooks/catch/19284', code: 200, latency: '95ms', timestamp: 'Today, 09:01:44', }, ]; export function ApiWebhooksManager() { const toast = useToast(); const [keys, setKeys] = useState(INITIAL_KEYS); const [webhooks, setWebhooks] = useState(INITIAL_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: `lmer_live_${Math.random().toString(36).substring(2, 6)}••••••••${Math.random().toString(36).substring(2, 6)}`, 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: `whsec_${Math.random().toString(36).substring(2, 8)}••••`, 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', width: pixel(100), renderCell: (row) => (