useTicketLiveUsersDisplay.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. liveUser.lastInteraction &&
  21. new Date(liveUser.lastInteraction).getTime() + IDLE_TIME_MS <
  22. reactiveNow.value.getTime()
  23. ) {
  24. localLiveUsers.push({
  25. user: liveUser.user,
  26. editing: liveUser.editing,
  27. app: liveUser.app,
  28. isIdle: true,
  29. })
  30. } else {
  31. localLiveUsers.push({
  32. user: liveUser.user,
  33. editing: liveUser.editing,
  34. app: liveUser.app,
  35. isIdle: false,
  36. })
  37. }
  38. })
  39. liveUsers.value = localLiveUsers
  40. viewingUsers.value = localLiveUsers.filter((user) => !user.isIdle)
  41. idleUsers.value = localLiveUsers.filter((user) => user.isIdle)
  42. },
  43. { immediate: true },
  44. )
  45. return {
  46. liveUsers,
  47. viewingUsers,
  48. idleUsers,
  49. }
  50. }