import { useState } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { client } from '@/ui/api/client' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/ui/components/ui/card' import { Input } from '@/ui/components/ui/input' import { Button } from '@/ui/components/ui/button' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/ui/components/ui/table' import { Loader2, Plus, Trash2, Copy, Check } from 'lucide-react' import { toast } from 'sonner' import type { ApiTokenResponse } from '@shumai/dtos' export function DeveloperSettings({ teamId }: { teamId: string }) { const queryClient = useQueryClient() const [tokenName, setTokenName] = useState('') const [copiedId, setCopiedId] = useState(null) const { data: tokens, isLoading } = useQuery({ queryKey: ['teams', teamId, 'api-tokens'], queryFn: async () => { const res = await client.api.teams[':teamId']['api-tokens'].$get({ param: { teamId }, }) if (!res.ok) throw new Error('Failed to fetch API tokens') return (await res.json()) as ApiTokenResponse[] }, }) const { mutate: createToken, isPending: isCreating } = useMutation({ mutationFn: async (name: string) => { const res = await client.api.teams[':teamId']['api-tokens'].$post({ param: { teamId }, json: { name }, }) if (!res.ok) throw new Error('Failed to create API token') return (await res.json()) as ApiTokenResponse }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'api-tokens'] }) setTokenName('') toast.success('API token generated successfully') }, onError: (err) => { toast.error(err instanceof Error ? err.message : 'Failed to generate token') }, }) const { mutate: deleteToken } = useMutation({ mutationFn: async (tokenId: string) => { const res = await client.api.teams[':teamId']['api-tokens'][':tokenId'].$delete({ param: { teamId, tokenId }, }) if (!res.ok) throw new Error('Failed to delete API token') return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'api-tokens'] }) toast.success('API token revoked successfully') }, onError: (err) => { toast.error(err instanceof Error ? err.message : 'Failed to revoke token') }, }) const handleGenerate = (e: React.FormEvent) => { e.preventDefault() if (!tokenName.trim()) return createToken(tokenName.trim()) } const handleCopy = (id: string, token: string) => { navigator.clipboard.writeText(token) setCopiedId(id) toast.success('Copied API token to clipboard') setTimeout(() => setCopiedId(null), 2000) } if (isLoading) { return (
) } return (
API Tokens Generate and manage API tokens. These tokens allow you or your agents to run terminal commands via shumai-cli.
setTokenName(e.target.value)} placeholder="e.g. My CLI token" required />
Name Token Created At Actions {!tokens || tokens.length === 0 ? ( No API tokens found. Generate one above to get started. ) : ( tokens.map((token) => ( {token.name}
{token.token.substring(0, 8)}...
{new Date(token.createdAt).toLocaleDateString()}
)) )}
) }