useTicketLiveUserList.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { type Ref, type ComputedRef } from 'vue'
  3. import { ref } from 'vue'
  4. import { useAppName } from '#shared/composables/useAppName.ts'
  5. import { useTicketLiveUserUpdatesSubscription } from '#shared/entities/ticket/graphql/subscriptions/ticketLiveUserUpdates.api.ts'
  6. import type { TicketLiveAppUser } from '#shared/entities/ticket/types.ts'
  7. import { EnumTaskbarApp } from '#shared/graphql/types.ts'
  8. import type { TicketLiveUser } from '#shared/graphql/types.ts'
  9. import { SubscriptionHandler } from '#shared/server/apollo/handler/index.ts'
  10. import { useSessionStore } from '#shared/stores/session.ts'
  11. export const useTicketLiveUserList = (
  12. ticketInternalId: Ref<string>,
  13. isTicketAgent: ComputedRef<boolean>,
  14. app: EnumTaskbarApp,
  15. ) => {
  16. const liveUserList = ref<TicketLiveAppUser[]>([])
  17. const { userId } = useSessionStore()
  18. const appName = useAppName()
  19. const updateLiveUserList = (liveUsers: TicketLiveUser[]) => {
  20. const mappedLiveUsers: TicketLiveAppUser[] = []
  21. liveUsers.forEach((liveUser) => {
  22. let appItems = liveUser.apps.filter((data) => data.editing)
  23. // Skip own live user item, when it's holds only the current app and is not editing on the other one.
  24. if (liveUser.user.id === userId) {
  25. if (appItems.length === 0) return
  26. appItems = appItems.filter((item) => item.name !== appName)
  27. if (appItems.length === 0) return
  28. }
  29. if (appItems.length === 0) {
  30. appItems = liveUser.apps
  31. }
  32. // Sort app items by last interaction.
  33. appItems.sort((a, b) => {
  34. return (
  35. new Date(b.lastInteraction).getTime() -
  36. new Date(a.lastInteraction).getTime()
  37. )
  38. })
  39. mappedLiveUsers.push({
  40. user: liveUser.user,
  41. ...appItems[0],
  42. app: appItems[0].name,
  43. })
  44. })
  45. return mappedLiveUsers
  46. }
  47. const liveUserSubscription = new SubscriptionHandler(
  48. useTicketLiveUserUpdatesSubscription(
  49. () => ({
  50. key: `Ticket-${ticketInternalId.value}`,
  51. app,
  52. }),
  53. () => ({
  54. // We need to disable the cache here, because otherwise we have the following error, when
  55. // a ticket is open again which is already in the subscription cache:
  56. // "ApolloError: 'get' on proxy: property 'liveUsers' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value (expected '[object Array]' but got '[object Array]')"
  57. // At the end a cache for the subscription is not really needed, but we should create an issue on
  58. // apollo client side, when we have a minimal reproduction.
  59. fetchPolicy: 'no-cache',
  60. enabled: isTicketAgent.value,
  61. }),
  62. ),
  63. )
  64. liveUserSubscription.onResult((result) => {
  65. liveUserList.value = updateLiveUserList(
  66. (result.data?.ticketLiveUserUpdates.liveUsers as TicketLiveUser[]) || [],
  67. )
  68. })
  69. return {
  70. liveUserList,
  71. }
  72. }