import { useState } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { client } from '@/ui/api/client' import { ScrollArea } from '@/ui/components/ui/scroll-area' 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 { Switch } from '@/ui/components/ui/switch' import { Loader2, Plus, X, Globe, Check, Trash2, ShieldAlert, Info } from 'lucide-react' import { toast } from 'sonner' import { m } from '@/ui/paraglide/messages.js' export function SandboxSettings({ teamId }: { teamId: string }) { const queryClient = useQueryClient() const [newDomain, setNewDomain] = useState('') const { data: sandbox, isLoading } = useQuery({ queryKey: ['teams', teamId, 'sandbox'], queryFn: async () => { const res = await client.api.teams[':teamId'].sandbox.$get({ param: { teamId }, }) if (!res.ok) throw new Error(m.failed_load_settings()) return await res.json() }, }) const { mutate: updateSandbox, isPending: isUpdating } = useMutation({ mutationFn: async (params: { networkSandboxEnabled?: boolean allowedDomains?: string[] pendingDomains?: string[] }) => { const res = await client.api.teams[':teamId'].sandbox.$put({ param: { teamId }, json: params, }) if (!res.ok) throw new Error(m.failed_to_update_sandbox_settings()) return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'sandbox'] }) toast.success(m.sandbox_settings_updated()) }, onError: (err) => { toast.error(err instanceof Error ? err.message : m.failed_to_update_sandbox_settings()) }, }) const handleToggleNetworkSandbox = (enabled: boolean) => { updateSandbox({ networkSandboxEnabled: enabled, }) } const handleAddDomain = (e: React.FormEvent) => { e.preventDefault() if (!newDomain.trim()) return if (sandbox?.allowedDomains.includes(newDomain.trim())) { toast.error(m.domain_already_exists()) return } const updatedAllowed = [...(sandbox?.allowedDomains || []), newDomain.trim()] const updatedPending = sandbox?.pendingDomains || [] updateSandbox({ allowedDomains: updatedAllowed, pendingDomains: updatedPending }) setNewDomain('') } const handleRemoveDomain = (domain: string) => { const updatedAllowed = (sandbox?.allowedDomains || []).filter((d) => d !== domain) const updatedPending = sandbox?.pendingDomains || [] updateSandbox({ allowedDomains: updatedAllowed, pendingDomains: updatedPending }) } const handleApproveDomain = (domain: string) => { const updatedAllowed = [...(sandbox?.allowedDomains || []), domain] const updatedPending = (sandbox?.pendingDomains || []).filter((d) => d !== domain) updateSandbox({ allowedDomains: updatedAllowed, pendingDomains: updatedPending }) } const handleDeletePendingDomain = (domain: string) => { const updatedPending = (sandbox?.pendingDomains || []).filter((d) => d !== domain) updateSandbox({ allowedDomains: sandbox?.allowedDomains || [], pendingDomains: updatedPending }) } if (isLoading) { return (
{m.network_sandbox_enabled_description()}