import { useEffect, useState } from 'react'; import { useParams } from 'react-router-dom'; import { useQueryClient } from '@tanstack/react-query'; import { AlertTriangle, Globe } from 'lucide-react'; import { useBuckets } from '@/hooks/useApi'; import { bucketsApi } from '@/lib/api'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Switch } from '@/components/ui/switch'; import { Badge } from '@/components/ui/badge'; import { EmptyState } from '@/components/ui/empty-state'; import { toast } from 'sonner'; export function BucketWebsite() { const { bucketName = '' } = useParams<{ bucketName: string }>(); const queryClient = useQueryClient(); const { data: buckets = [], isLoading } = useBuckets(); const bucket = buckets.find((b) => b.name === bucketName); const [enabled, setEnabled] = useState(false); const [indexDocument, setIndexDocument] = useState('index.html'); const [errorDocument, setErrorDocument] = useState(''); const [saving, setSaving] = useState(false); // Sync local form state whenever the underlying bucket changes. useEffect(() => { if (!bucket) return; setEnabled(bucket.websiteAccess); setIndexDocument(bucket.websiteConfig?.indexDocument ?? 'index.html'); setErrorDocument(bucket.websiteConfig?.errorDocument ?? ''); }, [bucket?.name, bucket?.websiteAccess, bucket?.websiteConfig?.indexDocument, bucket?.websiteConfig?.errorDocument]); if (isLoading) { return
Loading…
; } if (!bucket) { return (
} tone="neutral" title="Bucket not found" description="The bucket you're looking for doesn't exist or you don't have access." />
); } const wasEnabled = bucket.websiteAccess; const disabling = wasEnabled && !enabled; const handleReset = () => { setEnabled(bucket.websiteAccess); setIndexDocument(bucket.websiteConfig?.indexDocument ?? 'index.html'); setErrorDocument(bucket.websiteConfig?.errorDocument ?? ''); }; const handleSave = async () => { setSaving(true); try { await bucketsApi.updateBucketWebsite(bucketName, { enabled, indexDocument: enabled ? indexDocument : undefined, errorDocument: enabled && errorDocument ? errorDocument : undefined, }); await queryClient.invalidateQueries({ queryKey: ['buckets'] }); toast.success(disabling ? 'Website disabled' : 'Website configuration updated'); } catch { // error toast handled by axios interceptor } finally { setSaving(false); } }; const saveDisabled = saving || (enabled && !indexDocument); return (

Static website hosting

Website access

Allow public HTTP access to bucket objects

{enabled ? 'Enabled' : 'Disabled'}
{enabled && (
setIndexDocument(e.target.value)} placeholder="index.html" />

The file served when a directory is requested (e.g. index.html)

setErrorDocument(e.target.value)} placeholder="404.html (optional)" />

The file served when an object is not found (optional)

)}
); }