Instant ai assistant toggle

This commit is contained in:
Emil
2026-02-21 17:28:54 +01:00
parent fa5e14de83
commit f5c9ebaf95
5 changed files with 47 additions and 14 deletions
+36 -1
View File
@@ -1,6 +1,6 @@
'use client'
import { useState } from 'react'
import { useState, useEffect } from 'react'
import { Button } from '@/components/ui/button'
import { ChatPanel } from './ChatPanel'
import { MessageCircle, X } from 'lucide-react'
@@ -8,6 +8,41 @@ import { cn } from '@/lib/utils'
export function ChatWidget() {
const [isOpen, setIsOpen] = useState(false)
// Default to true for legacy compatibility (ai-chat defaults to enabled)
const [enabled, setEnabled] = useState(true)
const [loaded, setLoaded] = useState(false)
// Fetch initial toggle state
useEffect(() => {
const check = async () => {
try {
const res = await fetch('/api/extensions/toggles/general/ai-chat')
if (res.ok) {
const { data } = await res.json()
// Legacy: enabled by default when no toggle row exists
setEnabled(data?.enabled ?? true)
}
} finally {
setLoaded(true)
}
}
check()
}, [])
// Listen for real-time toggle changes
useEffect(() => {
const handler = (e: Event) => {
const { sectorSlug, extensionSlug, enabled: newValue } = (e as CustomEvent).detail
if (sectorSlug === 'general' && extensionSlug === 'ai-chat') {
setEnabled(newValue)
if (!newValue) setIsOpen(false)
}
}
window.addEventListener('extension-toggle-changed', handler)
return () => window.removeEventListener('extension-toggle-changed', handler)
}, [])
if (!loaded || !enabled) return null
return (
<>