'use client' import { useEffect, useState, useTransition } from 'react' import { createClient } from '@/lib/supabase/client' import { Switch } from '@/components/ui/switch' import { Loader2 } from 'lucide-react' interface ModuleToggleProps { sectorSlug: string moduleSlug: string } export function ModuleToggle({ sectorSlug, moduleSlug }: ModuleToggleProps) { const [enabled, setEnabled] = useState(false) const [loading, setLoading] = useState(true) const [isPending, startTransition] = useTransition() const supabase = createClient() useEffect(() => { async function fetchToggle() { const { data: { user } } = await supabase.auth.getUser() if (!user) { setLoading(false) return } const { data } = await supabase .from('module_toggles') .select('enabled') .eq('user_id', user.id) .eq('sector_slug', sectorSlug) .eq('module_slug', moduleSlug) .maybeSingle() setEnabled(data?.enabled ?? false) setLoading(false) } fetchToggle() }, [sectorSlug, moduleSlug, supabase]) function handleToggle(checked: boolean) { setEnabled(checked) startTransition(async () => { const { data: { user } } = await supabase.auth.getUser() if (!user) return await supabase .from('module_toggles') .upsert( { user_id: user.id, sector_slug: sectorSlug, module_slug: moduleSlug, enabled: checked, }, { onConflict: 'user_id,sector_slug,module_slug' } ) }) } if (loading) { return (