mirror of
https://github.com/chillpadclub/bedolaga-cabinet.git
synced 2026-07-28 17:43:47 +00:00
@@ -1,16 +1,56 @@
|
|||||||
|
import { useEffect, useState } from 'react'
|
||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { brandingApi } from '../api/branding'
|
import { brandingApi } from '../api/branding'
|
||||||
|
|
||||||
|
const ANIMATION_CACHE_KEY = 'cabinet_animation_enabled'
|
||||||
|
|
||||||
|
// Get cached value from localStorage
|
||||||
|
const getCachedAnimationEnabled = (): boolean | null => {
|
||||||
|
try {
|
||||||
|
const cached = localStorage.getItem(ANIMATION_CACHE_KEY)
|
||||||
|
if (cached !== null) {
|
||||||
|
return cached === 'true'
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// localStorage not available
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update cache in localStorage
|
||||||
|
export const setCachedAnimationEnabled = (enabled: boolean) => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(ANIMATION_CACHE_KEY, String(enabled))
|
||||||
|
} catch {
|
||||||
|
// localStorage not available
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default function AnimatedBackground() {
|
export default function AnimatedBackground() {
|
||||||
|
// Start with cached value (null means unknown yet)
|
||||||
|
const [isEnabled, setIsEnabled] = useState<boolean | null>(() => getCachedAnimationEnabled())
|
||||||
|
|
||||||
const { data: animationSettings } = useQuery({
|
const { data: animationSettings } = useQuery({
|
||||||
queryKey: ['animation-enabled'],
|
queryKey: ['animation-enabled'],
|
||||||
queryFn: brandingApi.getAnimationEnabled,
|
queryFn: brandingApi.getAnimationEnabled,
|
||||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
staleTime: 1000 * 60, // 1 minute - быстрее обновляется при изменении админом
|
||||||
|
refetchOnWindowFocus: true, // Обновить при возврате в окно
|
||||||
retry: false,
|
retry: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Don't render if animation is disabled
|
// Update state and cache when data arrives
|
||||||
if (animationSettings && !animationSettings.enabled) {
|
useEffect(() => {
|
||||||
|
if (animationSettings !== undefined) {
|
||||||
|
const enabled = animationSettings.enabled
|
||||||
|
setIsEnabled(enabled)
|
||||||
|
setCachedAnimationEnabled(enabled)
|
||||||
|
}
|
||||||
|
}, [animationSettings])
|
||||||
|
|
||||||
|
// Don't render anything until we know the state (from cache or server)
|
||||||
|
// If cache says disabled, don't render
|
||||||
|
// If cache is null (first visit), default to not showing to avoid flash
|
||||||
|
if (isEnabled !== true) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { useTranslation } from 'react-i18next'
|
|||||||
import { Link } from 'react-router-dom'
|
import { Link } from 'react-router-dom'
|
||||||
import { adminSettingsApi, SettingDefinition, SettingCategorySummary } from '../api/adminSettings'
|
import { adminSettingsApi, SettingDefinition, SettingCategorySummary } from '../api/adminSettings'
|
||||||
import { brandingApi } from '../api/branding'
|
import { brandingApi } from '../api/branding'
|
||||||
|
import { setCachedAnimationEnabled } from '../components/AnimatedBackground'
|
||||||
import { themeColorsApi } from '../api/themeColors'
|
import { themeColorsApi } from '../api/themeColors'
|
||||||
import { DEFAULT_THEME_COLORS, DEFAULT_ENABLED_THEMES } from '../types/theme'
|
import { DEFAULT_THEME_COLORS, DEFAULT_ENABLED_THEMES } from '../types/theme'
|
||||||
import { ColorPicker } from '../components/ColorPicker'
|
import { ColorPicker } from '../components/ColorPicker'
|
||||||
@@ -872,7 +873,9 @@ export default function AdminSettings() {
|
|||||||
|
|
||||||
const updateAnimationMutation = useMutation({
|
const updateAnimationMutation = useMutation({
|
||||||
mutationFn: (enabled: boolean) => brandingApi.updateAnimationEnabled(enabled),
|
mutationFn: (enabled: boolean) => brandingApi.updateAnimationEnabled(enabled),
|
||||||
onSuccess: () => {
|
onSuccess: (data) => {
|
||||||
|
// Update local cache immediately for instant effect
|
||||||
|
setCachedAnimationEnabled(data.enabled)
|
||||||
queryClient.invalidateQueries({ queryKey: ['animation-enabled'] })
|
queryClient.invalidateQueries({ queryKey: ['animation-enabled'] })
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user