useTicketLiveUsersDisplay.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. liveUserList: Ref<TicketLiveAppUser[]>,
  9. ) => {
  10. const liveUsers = ref<TicketLiveAppUser[]>([])
  11. const viewingUsers = ref<TicketLiveAppUser[]>([])
  12. const idleUsers = ref<TicketLiveAppUser[]>([])
  13. const reactiveNow = useReactiveNow()
  14. watch(
  15. [liveUserList, reactiveNow],
  16. () => {
  17. const localLiveUsers: TicketLiveAppUser[] = []
  18. liveUserList.value?.forEach((liveUser) => {
  19. if (
  20. new Date(liveUser.lastInteraction).getTime() + IDLE_TIME_MS <
  21. reactiveNow.value.getTime()
  22. ) {
  23. localLiveUsers.push({
  24. ...liveUser,
  25. isIdle: true,
  26. })
  27. } else {
  28. localLiveUsers.push({
  29. ...liveUser,
  30. isIdle: false,
  31. })
  32. }
  33. })
  34. liveUsers.value = localLiveUsers
  35. viewingUsers.value = localLiveUsers.filter((user) => !user.isIdle)
  36. idleUsers.value = localLiveUsers.filter((user) => user.isIdle)
  37. },
  38. { immediate: true },
  39. )
  40. return {
  41. liveUsers,
  42. viewingUsers,
  43. idleUsers,
  44. }
  45. }