Spaces:
Running

axeronis commited on
Commit
b28d7b0
·
1 Parent(s): 0e36e84

fix: Phase 16 - booking sync fix and voice caching

Browse files

- Booking settings: invalidate appointments/resources/tenant queries after save
- Voice settings: cache generated audio URLs in state to avoid regeneration
- Voice UI: show loading spinner during generation, 'Воспроизвести' if cached

frontend/app/settings/brain/page.tsx CHANGED
@@ -275,7 +275,11 @@ export default function BrainSettingsPage() {
275
  await api.patch(`/api/v1/tenants/${user.tenant_id}`, {
276
  booking_settings: newSettings
277
  })
 
278
  queryClient.invalidateQueries({ queryKey: ['ai-settings'] })
 
 
 
279
  toast.success('Успех!', 'Настройки записи сохранены')
280
  setBookingSettings(newSettings)
281
  } catch (error) {
 
275
  await api.patch(`/api/v1/tenants/${user.tenant_id}`, {
276
  booking_settings: newSettings
277
  })
278
+ // Invalidate all related queries so calendar updates immediately
279
  queryClient.invalidateQueries({ queryKey: ['ai-settings'] })
280
+ queryClient.invalidateQueries({ queryKey: ['appointments'] })
281
+ queryClient.invalidateQueries({ queryKey: ['resources'] })
282
+ queryClient.invalidateQueries({ queryKey: ['tenant'] })
283
  toast.success('Успех!', 'Настройки записи сохранены')
284
  setBookingSettings(newSettings)
285
  } catch (error) {
frontend/app/settings/voice/page.tsx CHANGED
@@ -69,8 +69,11 @@ export default function VoiceSettingsPage() {
69
  })
70
  const [saving, setSaving] = useState(false)
71
  const [playingVoice, setPlayingVoice] = useState<string | null>(null)
 
72
  const [isServiceAvailable] = useState<boolean>(true)
73
  const audioRef = React.useRef<HTMLAudioElement | null>(null)
 
 
74
 
75
  // React Query for voice settings - cached!
76
  const { data: tenantData, isLoading: loading } = useQuery({
@@ -110,8 +113,26 @@ export default function VoiceSettingsPage() {
110
 
111
  setPlayingVoice(voiceId)
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  try {
114
- // Use Chatterbox API for real voice preview
115
  const response = await api.post('/api/v1/voice/synthesize', {
116
  text: text,
117
  voice: voiceId
@@ -123,11 +144,11 @@ export default function VoiceSettingsPage() {
123
  const audioBlob = new Blob([response.data], { type: 'audio/mpeg' })
124
  const audioUrl = URL.createObjectURL(audioBlob)
125
 
 
 
 
126
  audioRef.current = new Audio(audioUrl)
127
- audioRef.current.onended = () => {
128
- setPlayingVoice(null)
129
- URL.revokeObjectURL(audioUrl)
130
- }
131
  audioRef.current.onerror = () => {
132
  setPlayingVoice(null)
133
  toast.error(t('voice.playbackError'))
@@ -142,6 +163,8 @@ export default function VoiceSettingsPage() {
142
  utterance.onend = () => setPlayingVoice(null)
143
  utterance.onerror = () => setPlayingVoice(null)
144
  window.speechSynthesis.speak(utterance)
 
 
145
  }
146
  }
147
 
@@ -322,6 +345,8 @@ export default function VoiceSettingsPage() {
322
  {VOICE_OPTIONS.map((voice, index) => {
323
  const isSelected = settings.voice_id === voice.id
324
  const isPlaying = playingVoice === voice.id
 
 
325
  const IconComponent = voice.icon
326
 
327
  return (
@@ -385,19 +410,23 @@ export default function VoiceSettingsPage() {
385
  e.stopPropagation()
386
  playDemoVoice(voice.id, voice.demoText)
387
  }}
 
388
  className={cn(
389
  "w-full flex items-center justify-center gap-2 py-2.5 rounded-xl transition-all text-sm font-medium",
390
  isPlaying
391
  ? `bg-gradient-to-r ${voice.gradient} text-white shadow-lg`
392
  : isSelected
393
  ? "bg-white/10 text-white hover:bg-white/20"
394
- : "bg-zinc-800 text-zinc-400 hover:bg-zinc-700 hover:text-white"
 
395
  )}
396
  >
397
- {isPlaying ? (
 
 
398
  <><Square className="w-4 h-4" /> {t('voice.stop')}</>
399
  ) : (
400
- <><Play className="w-4 h-4" /> {t('voice.listenDemo')}</>
401
  )}
402
  </button>
403
  </motion.div>
 
69
  })
70
  const [saving, setSaving] = useState(false)
71
  const [playingVoice, setPlayingVoice] = useState<string | null>(null)
72
+ const [generatingVoice, setGeneratingVoice] = useState<string | null>(null)
73
  const [isServiceAvailable] = useState<boolean>(true)
74
  const audioRef = React.useRef<HTMLAudioElement | null>(null)
75
+ // Cache for generated voice samples to avoid regeneration
76
+ const [voiceCache, setVoiceCache] = useState<Record<string, string>>({})
77
 
78
  // React Query for voice settings - cached!
79
  const { data: tenantData, isLoading: loading } = useQuery({
 
113
 
114
  setPlayingVoice(voiceId)
115
 
116
+ // Check cache first - use cached audio if available
117
+ if (voiceCache[voiceId]) {
118
+ audioRef.current = new Audio(voiceCache[voiceId])
119
+ audioRef.current.onended = () => setPlayingVoice(null)
120
+ audioRef.current.onerror = () => {
121
+ setPlayingVoice(null)
122
+ // Clear bad cache entry
123
+ setVoiceCache(prev => {
124
+ const newCache = { ...prev }
125
+ delete newCache[voiceId]
126
+ return newCache
127
+ })
128
+ }
129
+ audioRef.current.play()
130
+ return
131
+ }
132
+
133
+ // Generate new audio and cache it
134
+ setGeneratingVoice(voiceId)
135
  try {
 
136
  const response = await api.post('/api/v1/voice/synthesize', {
137
  text: text,
138
  voice: voiceId
 
144
  const audioBlob = new Blob([response.data], { type: 'audio/mpeg' })
145
  const audioUrl = URL.createObjectURL(audioBlob)
146
 
147
+ // Cache the URL for future use
148
+ setVoiceCache(prev => ({ ...prev, [voiceId]: audioUrl }))
149
+
150
  audioRef.current = new Audio(audioUrl)
151
+ audioRef.current.onended = () => setPlayingVoice(null)
 
 
 
152
  audioRef.current.onerror = () => {
153
  setPlayingVoice(null)
154
  toast.error(t('voice.playbackError'))
 
163
  utterance.onend = () => setPlayingVoice(null)
164
  utterance.onerror = () => setPlayingVoice(null)
165
  window.speechSynthesis.speak(utterance)
166
+ } finally {
167
+ setGeneratingVoice(null)
168
  }
169
  }
170
 
 
345
  {VOICE_OPTIONS.map((voice, index) => {
346
  const isSelected = settings.voice_id === voice.id
347
  const isPlaying = playingVoice === voice.id
348
+ const isGenerating = generatingVoice === voice.id
349
+ const isCached = !!voiceCache[voice.id]
350
  const IconComponent = voice.icon
351
 
352
  return (
 
410
  e.stopPropagation()
411
  playDemoVoice(voice.id, voice.demoText)
412
  }}
413
+ disabled={isGenerating}
414
  className={cn(
415
  "w-full flex items-center justify-center gap-2 py-2.5 rounded-xl transition-all text-sm font-medium",
416
  isPlaying
417
  ? `bg-gradient-to-r ${voice.gradient} text-white shadow-lg`
418
  : isSelected
419
  ? "bg-white/10 text-white hover:bg-white/20"
420
+ : "bg-zinc-800 text-zinc-400 hover:bg-zinc-700 hover:text-white",
421
+ isGenerating && "opacity-70 cursor-wait"
422
  )}
423
  >
424
+ {isGenerating ? (
425
+ <><Loader2 className="w-4 h-4 animate-spin" /> Генерация...</>
426
+ ) : isPlaying ? (
427
  <><Square className="w-4 h-4" /> {t('voice.stop')}</>
428
  ) : (
429
+ <><Play className="w-4 h-4" /> {isCached ? '▶ Воспроизвести' : t('voice.listenDemo')}</>
430
  )}
431
  </button>
432
  </motion.div>