useTicketLiveUsersDisplay.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref, type Ref, watch } from 'vue'
  3. import { useReactiveNow } from '#shared/composables/useReactiveNow.ts'
  4. import type { TicketLiveAppUser } from '../types.ts'
  5. // Default idle time from 5 minutes.
  6. const IDLE_TIME_MS = 5 * 60 * 1000
  7. export const useTicketLiveUsersDisplay = (
  8. liveUsers: Ref<TicketLiveAppUser[]>,
  9. ) => {
  10. const viewingUsers = ref<TicketLiveAppUser[]>([])
  11. const idleUsers = ref<TicketLiveAppUser[]>([])
  12. const reactiveNow = useReactiveNow()
  13. watch(
  14. [liveUsers, reactiveNow],
  15. () => {
  16. const localViewingUsers: TicketLiveAppUser[] = []
  17. const localIdleUsers: TicketLiveAppUser[] = []
  18. liveUsers.value.forEach((liveUser) => {
  19. if (
  20. new Date(liveUser.lastInteraction).getTime() + IDLE_TIME_MS <
  21. reactiveNow.value.getTime()
  22. ) {
  23. localIdleUsers.push(liveUser)
  24. } else {
  25. localViewingUsers.push(liveUser)
  26. }
  27. })
  28. viewingUsers.value = localViewingUsers
  29. idleUsers.value = localIdleUsers
  30. },
  31. { immediate: true },
  32. )
  33. return {
  34. viewingUsers,
  35. idleUsers,
  36. }
  37. }