useTicketArticlesRows.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { controlledComputed } from '@vueuse/shared'
  3. import type { TicketArticle } from '#shared/entities/ticket/types.ts'
  4. import type { Ref } from 'vue'
  5. interface ArticleRow {
  6. type: 'article-bubble'
  7. article: TicketArticle
  8. }
  9. interface ArticleDeliveryRow {
  10. type: 'delivery'
  11. content: string
  12. }
  13. interface MoreRow {
  14. type: 'more'
  15. count: number
  16. }
  17. interface SystemRaw {
  18. type: 'system'
  19. subject?: Maybe<string>
  20. to?: Maybe<string>
  21. reaction?: Maybe<string>
  22. }
  23. export type TicketArticleRow = (
  24. | ArticleRow
  25. | SystemRaw
  26. | MoreRow
  27. | ArticleDeliveryRow
  28. ) & {
  29. key: string
  30. }
  31. export const useTicketArticleRows = (
  32. articles: Ref<TicketArticle[]>,
  33. leadingNodesCount: Ref<number>,
  34. totalCount: Ref<number>,
  35. ) => {
  36. const rows = controlledComputed(articles, () => {
  37. const needMoreButton = articles.value.length < totalCount.value
  38. return articles.value.reduce((memo, article, index) => {
  39. if (article.preferences?.delivery_message) {
  40. memo.push({
  41. type: 'delivery',
  42. content: article.bodyWithUrls,
  43. key: article.internalId.toString(),
  44. })
  45. } else if (
  46. article.sender?.name === 'System' &&
  47. article.type?.name !== 'note'
  48. ) {
  49. memo.push({
  50. type: 'system',
  51. subject: article.subject,
  52. to: article.to?.raw || '',
  53. reaction: article.preferences?.whatsapp?.reaction?.emoji,
  54. key: article.internalId.toString(),
  55. })
  56. } else {
  57. memo.push({
  58. type: 'article-bubble',
  59. article,
  60. key: article.internalId.toString(),
  61. })
  62. }
  63. if (index === leadingNodesCount.value - 1 && needMoreButton) {
  64. memo.push({
  65. type: 'more',
  66. key: 'more',
  67. count: totalCount.value - articles.value.length,
  68. })
  69. }
  70. return memo
  71. }, [] as TicketArticleRow[])
  72. })
  73. return { rows }
  74. }