ArticleReply.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <!-- Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { useActiveElement, useLocalStorage, useWindowSize } from '@vueuse/core'
  4. import { computed, nextTick, ref, watch, type MaybeRef } from 'vue'
  5. import type { TicketById } from '#shared/entities/ticket/types'
  6. import type { AppSpecificTicketArticleType } from '#shared/entities/ticket-article/action/plugins/types.ts'
  7. import { useSessionStore } from '#shared/stores/session.ts'
  8. import type { ButtonVariant } from '#shared/types/button.ts'
  9. import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
  10. import ResizeLine from '#desktop/components/ResizeLine/ResizeLine.vue'
  11. import { useResizeLine } from '#desktop/components/ResizeLine/useResizeLine.ts'
  12. import { useElementScroll } from '#desktop/composables/useElementScroll.ts'
  13. interface Props {
  14. ticket: TicketById
  15. newArticlePresent?: boolean
  16. createArticleType?: string | null
  17. ticketArticleTypes: AppSpecificTicketArticleType[]
  18. isTicketCustomer?: boolean
  19. hasInternalArticle?: boolean
  20. parentReachedBottomScroll: boolean
  21. }
  22. const props = defineProps<Props>()
  23. const emit = defineEmits<{
  24. 'show-article-form': [
  25. articleType: string,
  26. performReply: AppSpecificTicketArticleType['performReply'],
  27. ]
  28. 'discard-form': []
  29. }>()
  30. const currentTicketArticleType = computed(() => {
  31. if (props.isTicketCustomer) return 'web'
  32. if (props.createArticleType === 'phone') return 'email'
  33. return props.createArticleType
  34. })
  35. const allowedArticleTypes = computed(() => {
  36. return ['note', 'phone', currentTicketArticleType.value]
  37. })
  38. const availableArticleTypes = computed(() => {
  39. const availableArticleTypes = props.ticketArticleTypes.filter((type) =>
  40. allowedArticleTypes.value.includes(type.value),
  41. )
  42. const hasEmail = availableArticleTypes.some((type) => type.value === 'email')
  43. let primaryTicketArticleType = currentTicketArticleType.value
  44. if (availableArticleTypes.length === 2) {
  45. primaryTicketArticleType = props.createArticleType
  46. }
  47. return availableArticleTypes.map((type) => {
  48. return {
  49. articleType: type.value,
  50. label:
  51. primaryTicketArticleType === type.value && hasEmail
  52. ? __('Add reply')
  53. : type.buttonLabel,
  54. icon: type.icon,
  55. variant:
  56. primaryTicketArticleType === type.value ||
  57. (type.value === 'phone' &&
  58. !hasEmail &&
  59. availableArticleTypes.length === 2)
  60. ? 'primary'
  61. : 'secondary',
  62. performReply: (() =>
  63. type.performReply?.(
  64. props.ticket,
  65. )) as AppSpecificTicketArticleType['performReply'],
  66. }
  67. })
  68. })
  69. const { userId } = useSessionStore()
  70. const pinned = useLocalStorage(`${userId}-article-reply-pinned`, false)
  71. const togglePinned = () => {
  72. pinned.value = !pinned.value
  73. }
  74. const articlePanel = ref<HTMLElement>()
  75. // Scroll the new article panel into view whenever:
  76. // - an article is being added
  77. // - the panel is being unpinned
  78. watch(
  79. () => [props.newArticlePresent, pinned.value],
  80. ([newArticlePresent, pinned]) => {
  81. if (!newArticlePresent || pinned) return
  82. nextTick(() => {
  83. // NB: Give editor a chance to initialize its height.
  84. setTimeout(() => {
  85. articlePanel.value?.scrollIntoView?.(true)
  86. }, 300)
  87. })
  88. },
  89. )
  90. const DEFAULT_ARTICLE_PANEL_HEIGHT = 290
  91. const MINIMUM_ARTICLE_PANEL_HEIGHT = 150
  92. const articlePanelHeight = useLocalStorage(
  93. `${userId}-article-reply-height`,
  94. DEFAULT_ARTICLE_PANEL_HEIGHT,
  95. )
  96. const { height: screenHeight } = useWindowSize()
  97. const articlePanelMaxHeight = computed(() => screenHeight.value / 2)
  98. const resizeLine = ref<InstanceType<typeof ResizeLine>>()
  99. const resizeCallback = (valueY: number) => {
  100. if (
  101. valueY >= articlePanelMaxHeight.value ||
  102. valueY < MINIMUM_ARTICLE_PANEL_HEIGHT
  103. )
  104. return
  105. articlePanelHeight.value = valueY
  106. }
  107. // a11y keyboard navigation
  108. const activeElement = useActiveElement()
  109. const handleKeyStroke = (e: KeyboardEvent, adjustment: number) => {
  110. if (
  111. !articlePanelHeight.value ||
  112. activeElement.value !== resizeLine.value?.resizeLine
  113. )
  114. return
  115. e.preventDefault()
  116. const newHeight = articlePanelHeight.value + adjustment
  117. if (newHeight >= articlePanelMaxHeight.value) return
  118. resizeCallback(newHeight)
  119. }
  120. const { startResizing } = useResizeLine(
  121. resizeCallback,
  122. resizeLine.value?.resizeLine,
  123. handleKeyStroke,
  124. { orientation: 'horizontal', offsetThreshold: 56 }, // bottom bar height in px
  125. )
  126. const resetHeight = () => {
  127. articlePanelHeight.value = DEFAULT_ARTICLE_PANEL_HEIGHT
  128. }
  129. const articleForm = ref<HTMLElement>()
  130. const { reachedTop: articleFormReachedTop } = useElementScroll(
  131. articleForm as MaybeRef<HTMLElement>,
  132. )
  133. defineExpose({
  134. articlePanel,
  135. })
  136. </script>
  137. <template>
  138. <div
  139. v-if="newArticlePresent"
  140. ref="articlePanel"
  141. class="mx-auto w-full"
  142. :class="{
  143. 'max-w-6xl px-12 py-4': !pinned,
  144. 'sticky bottom-0 z-20 border-t border-t-neutral-300 bg-neutral-50 dark:border-t-gray-900 dark:bg-gray-500':
  145. pinned,
  146. }"
  147. aria-labelledby="article-reply-form-title"
  148. role="complementary"
  149. :aria-expanded="!pinned"
  150. >
  151. <div
  152. :class="{
  153. 'bg-stripes relative z-0 rounded-xl outline outline-1 outline-blue-700 before:rounded-2xl':
  154. hasInternalArticle && !pinned,
  155. 'border-stripes': hasInternalArticle && pinned,
  156. }"
  157. :style="{
  158. height: pinned ? `${articlePanelHeight}px` : 'auto',
  159. }"
  160. >
  161. <ResizeLine
  162. v-if="pinned"
  163. ref="resizeLine"
  164. class="group absolute h-3 w-full -translate-y-1.5 py-1"
  165. :label="$t('Resize article panel')"
  166. orientation="horizontal"
  167. :values="{
  168. max: articlePanelMaxHeight,
  169. min: MINIMUM_ARTICLE_PANEL_HEIGHT,
  170. current: articlePanelHeight,
  171. }"
  172. @mousedown-event="startResizing"
  173. @touchstart-event="startResizing"
  174. @dblclick="resetHeight"
  175. />
  176. <div
  177. class="flex h-full flex-col"
  178. :class="{
  179. 'rounded-xl border border-neutral-300 bg-neutral-50 dark:border-gray-900 dark:bg-gray-500':
  180. !pinned,
  181. }"
  182. >
  183. <div
  184. class="flex h-10 items-center p-3"
  185. :class="{
  186. 'bg-neutral-50 dark:bg-gray-500': pinned,
  187. 'border-b border-b-transparent': pinned && articleFormReachedTop,
  188. 'border-b border-b-neutral-300 dark:border-b-gray-900':
  189. pinned && !articleFormReachedTop,
  190. }"
  191. >
  192. <CommonLabel
  193. id="article-reply-form-title"
  194. class="text-stone-200 ltr:mr-auto rtl:ml-auto dark:text-neutral-500"
  195. tag="h2"
  196. size="small"
  197. >
  198. {{ $t('Reply') }}
  199. </CommonLabel>
  200. <CommonButton
  201. v-tooltip="$t('Discard unsaved reply')"
  202. class="text-red-500 ltr:mr-2 rtl:ml-2"
  203. variant="none"
  204. icon="trash"
  205. @click="$emit('discard-form')"
  206. />
  207. <CommonButton
  208. v-tooltip="pinned ? $t('Unpin this panel') : $t('Pin this panel')"
  209. :icon="pinned ? 'pin' : 'pin-angle'"
  210. variant="neutral"
  211. size="small"
  212. @click="togglePinned"
  213. />
  214. </div>
  215. <div
  216. id="ticketArticleReplyForm"
  217. ref="articleForm"
  218. class="h-full px-3 pb-3"
  219. :class="{
  220. 'overflow-y-auto': pinned,
  221. 'my-[5px] px-4 pt-2': hasInternalArticle && pinned,
  222. }"
  223. ></div>
  224. </div>
  225. </div>
  226. </div>
  227. <div
  228. v-else-if="newArticlePresent !== undefined"
  229. class="-:border-t-transparent sticky bottom-0 z-20 flex w-full justify-center gap-2.5 border-t py-1.5"
  230. :class="{
  231. 'border-t-neutral-100 bg-neutral-50 dark:border-t-gray-900 dark:bg-gray-500':
  232. !parentReachedBottomScroll,
  233. }"
  234. >
  235. <CommonButton
  236. v-for="button in availableArticleTypes"
  237. :key="button.articleType"
  238. :prefix-icon="button.icon"
  239. :variant="button.variant as ButtonVariant"
  240. size="large"
  241. @click="
  242. emit('show-article-form', button.articleType, button.performReply)
  243. "
  244. >
  245. {{ $t(button.label) }}
  246. </CommonButton>
  247. </div>
  248. </template>
  249. <style scoped>
  250. .border-stripes {
  251. position: relative;
  252. background-color: theme('colors.neutral.50');
  253. &::before {
  254. content: '';
  255. position: absolute;
  256. left: 0;
  257. top: 40px;
  258. bottom: 0;
  259. right: 0;
  260. border: 5px solid transparent;
  261. background-image: repeating-linear-gradient(
  262. 45deg,
  263. theme('colors.blue.400'),
  264. theme('colors.blue.400') 5px,
  265. theme('colors.blue.700') 5px,
  266. theme('colors.blue.700') 10px
  267. );
  268. background-position: -1px;
  269. background-attachment: fixed;
  270. mask:
  271. linear-gradient(#fff 0 0) padding-box,
  272. linear-gradient(#fff 0 0);
  273. mask-composite: exclude;
  274. }
  275. &::after {
  276. content: '';
  277. position: absolute;
  278. left: 0;
  279. top: 40px;
  280. bottom: 0;
  281. right: 0;
  282. outline: 1px solid theme('colors.blue.700');
  283. outline-offset: -5px;
  284. pointer-events: none;
  285. }
  286. }
  287. [data-theme='dark'] .border-stripes {
  288. background-color: theme('colors.gray.500');
  289. &::before {
  290. background-image: repeating-linear-gradient(
  291. 45deg,
  292. theme('colors.blue.700'),
  293. theme('colors.blue.700') 5px,
  294. theme('colors.blue.900') 5px,
  295. theme('colors.blue.900') 10px
  296. );
  297. }
  298. }
  299. </style>