useTicketArticlesRows.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  22. export type TicketArticleRow = (
  23. | ArticleRow
  24. | SystemRaw
  25. | MoreRow
  26. | ArticleDeliveryRow
  27. ) & {
  28. key: string
  29. }
  30. export const useTicketArticleRows = (
  31. articles: Ref<TicketArticle[]>,
  32. leadingNodesCount: Ref<number>,
  33. totalCount: Ref<number>,
  34. ) => {
  35. const rows = controlledComputed(articles, () => {
  36. const needMoreButton = articles.value.length < totalCount.value
  37. return articles.value.reduce((memo, article, index) => {
  38. if (article.preferences?.delivery_message) {
  39. memo.push({
  40. type: 'delivery',
  41. content: article.bodyWithUrls,
  42. key: article.internalId.toString(),
  43. })
  44. } else if (
  45. article.sender?.name === 'System' &&
  46. article.type?.name !== 'note'
  47. ) {
  48. memo.push({
  49. type: 'system',
  50. subject: article.subject,
  51. to: article.to?.raw || '',
  52. key: article.internalId.toString(),
  53. })
  54. } else {
  55. memo.push({
  56. type: 'article-bubble',
  57. article,
  58. key: article.internalId.toString(),
  59. })
  60. }
  61. if (index === leadingNodesCount.value - 1 && needMoreButton) {
  62. memo.push({
  63. type: 'more',
  64. key: 'more',
  65. count: totalCount.value - articles.value.length,
  66. })
  67. }
  68. return memo
  69. }, [] as TicketArticleRow[])
  70. })
  71. return { rows }
  72. }