import { useEffect } from 'react' ; import { PureQueryOptions } from '@apollo/client' ; import { Timings , TimingsVariables } from '../../generated/server/Timings' ; import { MESSAGE } from '../../graphql/server/message' ; import { TIMINGS } from '../../graphql/server/timings' ; import { MessageContent } from '../../types' ; import { useStorage } from '../../helpers' ; import { useMutation } from '../../utils' ; type Posts = Array < MessageContent | number > ; function useTiming ( topicId , startIndex , posts ? , skip = false , refetchQueries ? ) { const [ submitTiming ] = useMutation < Timings , TimingsVariables > ( TIMINGS , { refetchQueries , awaitRefetchQueries : ! ! refetchQueries , } ) ; useEffect ( ( ) => { if ( skip || ! posts ) { return ; } const postNumbers = posts . map ( ( _ , index ) => index + startIndex + 1 ) ?? [ ] ; submitTiming ( { variables : { postNumbers , topicId } } ) ; } , [ skip , posts , topicId , submitTiming , startIndex ] ) ; } /**
 * useMessageTiming reports timing information to Discourse based on which messages
 * a user has read in a given message thread.
 *
 * Discourse uses this to provide reporting and analytics about activity on the forum.
 *
 * This reporting is skipped if, for some reason, the user's username is not set.
 */ export function useMessageTiming ( topicId , startIndex = 0 , posts ? ) { const storage = useStorage ( ) ; const user = storage . getItem ( 'user' ) ; const username = user ?. username ; const skip = ! username ; let refetchQueries = [ { query : MESSAGE , variables : { username } } ] ; useTiming ( topicId , startIndex , posts , skip , refetchQueries ) ; } /**
 * useTopicTiming reports timing information to Discourse based on which posts
 * a user has read in a given topic, and how long they have spent on a given post.
 *
 * Discourse uses this to provide reporting and analytics about activity on the forum.
 *
 * This reporting is skipped if the user is not currently logged in (the user's ID is not set).
 */ export function useTopicTiming ( topicId , startIndex = 0 , posts ? ) { const storage = useStorage ( ) ; const user = storage . getItem ( 'user' ) ; const skip = user ?. id === undefined ; useTiming ( topicId , startIndex , posts , skip ) ; }