import { Sync, SyncDisabled } from '@mui/icons-material'
import { Box, Card, Chip, FormHelperText, Switch, Typography } from '@mui/joy'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useSSEContext } from '../hooks/useSSEContext'
import { useUserProfile } from '../queries/UserQueries'
import { isPlusAccount } from '../utils/Helpers'
import SSEConnectionStatus from './SSEConnectionStatus'
const REALTIME_TYPES = {
DISABLED: 'disabled',
SSE: 'sse',
}
const RealTimeSettings = () => {
const { t } = useTranslation('settings')
const { data: userProfile } = useUserProfile()
// SSE context
const sseContext = useSSEContext()
// Get current realtime type from localStorage
const getCurrentRealtimeType = () => {
const sseEnabled = localStorage.getItem('sse_enabled') === 'true'
return sseEnabled ? REALTIME_TYPES.SSE : REALTIME_TYPES.DISABLED
}
const [realtimeType, setRealtimeType] = useState(getCurrentRealtimeType())
const handleRealtimeTypeChange = (event, newValue) => {
if (!isPlusAccount(userProfile)) {
return // Don't allow changes for non-Plus users
}
setRealtimeType(newValue)
// Update localStorage and toggle connections
switch (newValue) {
case REALTIME_TYPES.DISABLED:
localStorage.setItem('sse_enabled', 'false')
sseContext.disconnect()
break
case REALTIME_TYPES.SSE:
localStorage.setItem('sse_enabled', 'true')
sseContext.connect()
break
}
}
const getCurrentContext = () => {
switch (realtimeType) {
case REALTIME_TYPES.SSE:
return sseContext
default:
return {
isConnected: false,
isConnecting: false,
error: null,
getConnectionStatus: () => 'disabled',
}
}
}
const context = getCurrentContext()
const getStatusDescription = () => {
if (!isPlusAccount(userProfile)) {
return t('advanced.realtime.basicPlan')
}
if (realtimeType === REALTIME_TYPES.DISABLED) {
return t('advanced.realtime.disabled')
}
if (context.isConnected) {
return t('advanced.realtime.connected')
}
if (context.isConnecting) {
return t('advanced.realtime.connecting')
}
if (context.error) {
return t('advanced.realtime.errored', { error: context.error })
}
return t('advanced.realtime.notConnected')
}
const getConnectionStatusComponent = () => {
switch (realtimeType) {
case REALTIME_TYPES.SSE:
return
default:
return null
}
}
return (
{
handleRealtimeTypeChange(
null,
e.target.checked ? REALTIME_TYPES.SSE : REALTIME_TYPES.DISABLED,
)
}}
color={
realtimeType !== REALTIME_TYPES.DISABLED ? 'success' : 'neutral'
}
disabled={!isPlusAccount(userProfile)}
inputProps={{ 'aria-label': t('advanced.realtime.toggleLabel') }}
/>
{t('advanced.realtime.title')}
{!isPlusAccount(userProfile) && (
{t('common.plusFeature')}
)}
{realtimeType !== REALTIME_TYPES.DISABLED &&
isPlusAccount(userProfile) ? (
) : (
)}
{t('advanced.realtime.subtitle')}
{/*
Real-time Connection Type
Choose how to receive real-time updates
*/}
{getStatusDescription()}
{realtimeType !== REALTIME_TYPES.DISABLED &&
isPlusAccount(userProfile) && (
{t('advanced.realtime.statusLabel')}
{getConnectionStatusComponent()}
{context.error && (
{context.error}
)}
)}
{!isPlusAccount(userProfile) && (
{t('advanced.realtime.basicPlanNotice')}
)}
)
}
export default RealTimeSettings