All files / pages/ApiKeys ApiKeysPage.tsx

93.54% Statements 29/31
100% Branches 18/18
90% Functions 9/10
93.1% Lines 27/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                        1x 49x 49x 49x 49x 49x   49x 23x 23x 23x 22x       22x       49x 21x     49x 2x 2x     49x 107x 78x     49x 54x 51x     49x                 4x                                                                                       81x                                                                       2x                                                              
import { useEffect, useState } from 'react';
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from '@/components/ui/table';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Plus, Trash2, Key, Clock, Calendar } from 'lucide-react';
import { apiKeysService } from '@/services/api-keys.service';
import type { APIKeyListItem } from '@/services/api-keys.service';
import { CreateApiKeyDialog } from './CreateApiKeyDialog';
import { RevokeApiKeyDialog } from './RevokeApiKeyDialog';
import { format } from 'date-fns';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Skeleton } from '@/components/ui/skeleton';
 
export const ApiKeysPage = () => {
    const [keys, setKeys] = useState<APIKeyListItem[]>([]);
    const [loading, setLoading] = useState(true);
    const [createDialogOpen, setCreateDialogOpen] = useState(false);
    const [revokeDialogOpen, setRevokeDialogOpen] = useState(false);
    const [selectedKey, setSelectedKey] = useState<APIKeyListItem | null>(null);
 
    const fetchKeys = async () => {
        setLoading(true);
        try {
            const response = await apiKeysService.getApiKeys();
            setKeys(response.items);
        } catch (error) {
            console.error('Failed to fetch API keys', error);
        } finally {
            setLoading(false);
        }
    };
 
    useEffect(() => {
        fetchKeys();
    }, []);
 
    const handleRevokeClick = (key: APIKeyListItem) => {
        setSelectedKey(key);
        setRevokeDialogOpen(true);
    };
 
    const formatDate = (dateStr: string | null) => {
        if (!dateStr) return 'Never';
        return format(new Date(dateStr), 'MMM d, yyyy HH:mm');
    };
 
    const isExpired = (expiry: string | null) => {
        if (!expiry) return false;
        return new Date(expiry) < new Date();
    };
 
    return (
        <div className="p-8 space-y-6">
            <div className="flex justify-between items-center">
                <div>
                    <h1 className="text-3xl font-bold tracking-tight">Superadmin API Keys</h1>
                    <p className="text-muted-foreground">
                        Manage persistent keys for programmatic access to the SnackBase API.
                    </p>
                </div>
                <Button onClick={() => setCreateDialogOpen(true)}>
                    <Plus className="mr-2 h-4 w-4" /> Create API Key
                </Button>
            </div>
 
            <Card>
                <CardHeader>
                    <CardTitle>Your API Keys</CardTitle>
                    <CardDescription>
                        These keys provide full superadmin access. Manage them carefully.
                    </CardDescription>
                </CardHeader>
                <CardContent>
                    {loading ? (
                        <div className="space-y-2">
                            <Skeleton className="h-10 w-full" />
                            <Skeleton className="h-10 w-full" />
                            <Skeleton className="h-10 w-full" />
                        </div>
                    ) : keys.length === 0 ? (
                        <div className="flex flex-col items-center justify-center py-12 text-center">
                            <Key className="h-12 w-12 text-muted-foreground mb-4 opacity-20" />
                            <h3 className="text-lg font-medium">No API keys found</h3>
                            <p className="text-muted-foreground mb-6">
                                You haven't created any API keys yet.
                            </p>
                            <Button variant="outline" onClick={() => setCreateDialogOpen(true)}>
                                <Plus className="mr-2 h-4 w-4" /> Create your first key
                            </Button>
                        </div>
                    ) : (
                        <Table>
                            <TableHeader>
                                <TableRow>
                                    <TableHead>Name</TableHead>
                                    <TableHead>Key</TableHead>
                                    <TableHead>Last Used</TableHead>
                                    <TableHead>Expires</TableHead>
                                    <TableHead>Status</TableHead>
                                    <TableHead className="text-right">Actions</TableHead>
                                </TableRow>
                            </TableHeader>
                            <TableBody>
                                {keys.map((key) => (
                                    <TableRow key={key.id} className={!key.is_active ? 'opacity-50' : ''}>
                                        <TableCell className="font-medium">{key.name}</TableCell>
                                        <TableCell>
                                            <code className="bg-muted px-1 py-0.5 rounded text-xs select-all">
                                                {key.key}
                                            </code>
                                        </TableCell>
                                        <TableCell>
                                            <div className="flex items-center text-xs text-muted-foreground">
                                                <Clock className="mr-1 h-3 w-3" />
                                                {key.last_used_at ? formatDate(key.last_used_at) : 'Never'}
                                            </div>
                                        </TableCell>
                                        <TableCell>
                                            <div className="flex items-center text-xs text-muted-foreground">
                                                <Calendar className="mr-1 h-3 w-3" />
                                                {formatDate(key.expires_at)}
                                            </div>
                                        </TableCell>
                                        <TableCell>
                                            {!key.is_active ? (
                                                <Badge variant="outline">Revoked</Badge>
                                            ) : isExpired(key.expires_at) ? (
                                                <Badge variant="destructive">Expired</Badge>
                                            ) : (
                                                <Badge variant="success">
                                                    Active
                                                </Badge>
                                            )}
                                        </TableCell>
                                        <TableCell className="text-right">
                                            {key.is_active && (
                                                <Button
                                                    variant="ghost"
                                                    size="sm"
                                                    className="text-red-600 hover:text-red-700 hover:bg-red-50"
                                                    onClick={() => handleRevokeClick(key)}
                                                >
                                                    <Trash2 className="h-4 w-4" />
                                                </Button>
                                            )}
                                        </TableCell>
                                    </TableRow>
                                ))}
                            </TableBody>
                        </Table>
                    )}
                </CardContent>
            </Card>
 
            <CreateApiKeyDialog
                open={createDialogOpen}
                onOpenChange={setCreateDialogOpen}
                onCreated={fetchKeys}
            />
 
            <RevokeApiKeyDialog
                apiKey={selectedKey}
                open={revokeDialogOpen}
                onOpenChange={setRevokeDialogOpen}
                onRevoked={fetchKeys}
            />
        </div>
    );
};
 
export default ApiKeysPage;