SurfSense/surfsense_web/components/providers/ElectricProvider.tsx
Anish Sarkar f441c7b0ce feat: Enhance Electric SQL integration and update notification handling
- Added initialization script for Electric SQL user in Docker setup.
- Updated Electric SQL client to support new PGlite architecture and sync functionality.
- Improved notification fetching and syncing logic in useNotifications hook.
- Refactored ElectricProvider to handle initialization state and errors more gracefully.
- Removed deprecated electric.config.ts file and adjusted package dependencies accordingly.
2026-01-12 14:53:18 +05:30

69 lines
1.7 KiB
TypeScript

"use client"
import { useEffect, useState } from 'react'
import { initElectric, isElectricInitialized } from '@/lib/electric/client'
interface ElectricProviderProps {
children: React.ReactNode
}
/**
* ElectricProvider initializes the Electric SQL client with PGlite
*
* This provider ensures Electric is initialized before rendering children,
* but doesn't block if initialization fails (app can still work without real-time sync)
*/
export function ElectricProvider({ children }: ElectricProviderProps) {
const [initialized, setInitialized] = useState(false)
const [error, setError] = useState<Error | null>(null)
useEffect(() => {
// Skip if already initialized
if (isElectricInitialized()) {
setInitialized(true)
return
}
let mounted = true
async function init() {
try {
await initElectric()
if (mounted) {
setInitialized(true)
setError(null)
}
} catch (err) {
console.error('Failed to initialize Electric SQL:', err)
if (mounted) {
setError(err instanceof Error ? err : new Error('Failed to initialize Electric SQL'))
// Don't block rendering if Electric SQL fails - app can still work
setInitialized(true)
}
}
}
init()
return () => {
mounted = false
}
}, [])
// Show loading state only briefly, then render children
// Electric SQL will sync in the background
if (!initialized) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="text-muted-foreground">Initializing...</div>
</div>
)
}
// If there's an error, still render children but log the error
if (error) {
console.warn('Electric SQL initialization failed, notifications may not sync:', error.message)
}
return <>{children}</>
}