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' import { m } from '@/ui/paraglide/messages.js' 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(m.api_token_generated_successfully()) }, onError: (err) => { toast.error(err instanceof Error ? err.message : m.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(m.api_token_revoked_successfully()) }, onError: (err) => { toast.error(err instanceof Error ? err.message : m.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(m.copied_api_token_to_clipboard()) setTimeout(() => setCopiedId(null), 2000) } if (isLoading) { return (
) } return (
{m.api_tokens()} {m.api_tokens_description()}
setTokenName(e.target.value)} placeholder={m.token_name_placeholder()} required />
{m.name()} {m.token()} {m.created_at()} {m.actions()} {!tokens || tokens.length === 0 ? ( {m.no_api_tokens_found()} ) : ( tokens.map((token) => ( {token.name}
{token.token.substring(0, 8)}...
{new Date(token.createdAt).toLocaleDateString()}
)) )}
) }