import { useConversationsContext } from '../contexts/conversations_context' import { CurrentPersonResource } from '../types' import { JoltConversationEvent } from '../types/jolt_events' import { completeMessageCreationConversationTracking } from '../utils/performance_tracking' import { useApiGet } from './use_api' import { useApiClient } from './use_api_client' import { useConversationsCache } from './use_conversations_cache' import { currentPersonRequestArgs, useCurrentPersonCache } from './use_current_person' import { useJoltChannel, useJoltEvent } from './use_jolt' const useCurrentPerson = () => { return useApiGet(currentPersonRequestArgs) } export function useConversationsJoltEvents() { const { data: currentPerson } = useCurrentPerson() const { args } = useConversationsContext() const cache = useConversationsCache(args) const currentPersonCache = useCurrentPersonCache() const apiClient = useApiClient() const enabled = currentPerson?.id ? true : false const joltChannel = useJoltChannel(`chat.people.${currentPerson?.id}`, enabled) useJoltEvent(joltChannel, 'conversation.updated', (e: JoltConversationEvent) => { if ( e.data.data.last_message_idempotent_key && e.data.data.last_message_author_id === currentPerson?.id ) { completeMessageCreationConversationTracking({ apiClient, idempotentKey: e.data.data.last_message_idempotent_key, }) } cache.fetchUpdate({ id: e.data.data.id }) }) useJoltEvent(joltChannel, 'conversation.created', (e: JoltConversationEvent) => cache.fetchCreate({ id: e.data.data.id }) ) useJoltEvent(joltChannel, 'conversation.destroyed', (e: JoltConversationEvent) => cache.destroy({ id: e.data.data.id }) ) useJoltEvent(joltChannel, 'STREAM_USER_UPDATED', currentPersonCache.invalidate) // Would be nice to have a non-stream chat event useJoltEvent(joltChannel, 'conversation.read', currentPersonCache.invalidate) }