useTicketArticlesRows.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { i18n } from '@shared/i18n'
  3. import type { Ref } from 'vue'
  4. import { computed } from 'vue'
  5. import type { TicketArticle } from '@shared/entities/ticket/types'
  6. interface ArticleRow {
  7. type: 'article-bubble'
  8. article: TicketArticle
  9. }
  10. interface MoreRow {
  11. type: 'more'
  12. count: number
  13. }
  14. interface NewRow {
  15. type: 'new'
  16. }
  17. interface DateRow {
  18. type: 'date'
  19. date: string
  20. }
  21. interface SystemRaw {
  22. type: 'system'
  23. subject?: Maybe<string>
  24. to?: Maybe<string>
  25. }
  26. type TicketArticleRow = (
  27. | ArticleRow
  28. | SystemRaw
  29. | MoreRow
  30. | NewRow
  31. | DateRow
  32. ) & {
  33. key: string
  34. }
  35. export const useTicketArticleRows = (
  36. articles: Ref<TicketArticle[]>,
  37. totalCount: Ref<number>,
  38. ) => {
  39. const rows = computed(() => {
  40. const rows: TicketArticleRow[] = []
  41. const dates = new Set<string>()
  42. const needMoreButton = articles.value.length < totalCount.value
  43. // assuming it is sorted by createdAt
  44. articles.value.forEach((article, index) => {
  45. const date = i18n.date(article.createdAt)
  46. if (!dates.has(date)) {
  47. dates.add(date)
  48. rows.push({
  49. type: 'date',
  50. date: article.createdAt,
  51. key: date,
  52. })
  53. }
  54. if (article.sender?.name === 'System' && article.type?.name !== 'note') {
  55. rows.push({
  56. type: 'system',
  57. subject: article.subject,
  58. to: article.to?.raw || '',
  59. key: article.id,
  60. })
  61. } else {
  62. rows.push({
  63. type: 'article-bubble',
  64. article,
  65. key: article.id,
  66. })
  67. }
  68. // after "description" (always first) article is added, add "more" button
  69. if (index === 0 && needMoreButton) {
  70. rows.push({
  71. type: 'more',
  72. key: 'more',
  73. count: totalCount.value - articles.value.length,
  74. })
  75. }
  76. })
  77. return rows
  78. })
  79. return { rows }
  80. }