ArticleReply.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <!-- Copyright (C) 2012-2024 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. 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 pinned = defineModel<boolean>('pinned')
  70. const togglePinned = () => {
  71. pinned.value = !pinned.value
  72. }
  73. const articlePanel = ref<HTMLElement>()
  74. // Scroll the new article panel into view whenever:
  75. // - an article is being added
  76. // - the panel is being unpinned
  77. watch(
  78. () => [props.newArticlePresent, pinned.value],
  79. ([newArticlePresent, newPinned]) => {
  80. if (!newArticlePresent || newPinned) return
  81. nextTick(() => {
  82. // NB: Give editor a chance to initialize its height.
  83. setTimeout(() => {
  84. articlePanel.value?.scrollIntoView?.(true)
  85. }, 300)
  86. })
  87. },
  88. )
  89. // Reset the pinned state whenever the article is removed.
  90. watch(
  91. () => props.newArticlePresent,
  92. (newArticlePresent) => {
  93. if (newArticlePresent) return
  94. pinned.value = false
  95. },
  96. )
  97. const DEFAULT_ARTICLE_PANEL_HEIGHT = 290
  98. const MINIMUM_ARTICLE_PANEL_HEIGHT = 150
  99. const { userId } = useSessionStore()
  100. const articlePanelHeight = useLocalStorage(
  101. `${userId}-article-reply-height`,
  102. DEFAULT_ARTICLE_PANEL_HEIGHT,
  103. )
  104. const { height: screenHeight } = useWindowSize()
  105. const articlePanelMaxHeight = computed(() => screenHeight.value / 2)
  106. const resizeLine = ref<InstanceType<typeof ResizeLine>>()
  107. const resizeCallback = (valueY: number) => {
  108. if (
  109. valueY >= articlePanelMaxHeight.value ||
  110. valueY < MINIMUM_ARTICLE_PANEL_HEIGHT
  111. )
  112. return
  113. articlePanelHeight.value = valueY
  114. }
  115. // a11y keyboard navigation
  116. const activeElement = useActiveElement()
  117. const handleKeyStroke = (e: KeyboardEvent, adjustment: number) => {
  118. if (
  119. !articlePanelHeight.value ||
  120. activeElement.value !== resizeLine.value?.resizeLine
  121. )
  122. return
  123. e.preventDefault()
  124. const newHeight = articlePanelHeight.value + adjustment
  125. if (newHeight >= articlePanelMaxHeight.value) return
  126. resizeCallback(newHeight)
  127. }
  128. const { startResizing } = useResizeLine(
  129. resizeCallback,
  130. resizeLine.value?.resizeLine,
  131. handleKeyStroke,
  132. { orientation: 'horizontal', offsetThreshold: 56 }, // bottom bar height in px
  133. )
  134. const resetHeight = () => {
  135. articlePanelHeight.value = DEFAULT_ARTICLE_PANEL_HEIGHT
  136. }
  137. const articleForm = ref<HTMLElement>()
  138. const { reachedTop: articleFormReachedTop } = useElementScroll(
  139. articleForm as MaybeRef<HTMLElement>,
  140. )
  141. defineExpose({
  142. articlePanel,
  143. })
  144. </script>
  145. <template>
  146. <div
  147. v-if="newArticlePresent"
  148. ref="articlePanel"
  149. class="relative mx-auto flex w-full flex-col"
  150. :class="{
  151. 'max-w-6xl px-12 py-4': !pinned,
  152. 'sticky bottom-0 z-20 overflow-hidden border-t border-t-neutral-300 bg-neutral-50 dark:border-t-gray-900 dark:bg-gray-500':
  153. pinned,
  154. }"
  155. :style="{
  156. height: pinned ? `${articlePanelHeight}px` : 'auto',
  157. }"
  158. aria-labelledby="article-reply-form-title"
  159. role="complementary"
  160. :aria-expanded="!pinned"
  161. v-bind="$attrs"
  162. >
  163. <ResizeLine
  164. v-if="pinned"
  165. ref="resizeLine"
  166. class="group absolute top-0 z-10 h-3 w-full"
  167. :label="$t('Resize article panel')"
  168. orientation="horizontal"
  169. :values="{
  170. max: articlePanelMaxHeight,
  171. min: MINIMUM_ARTICLE_PANEL_HEIGHT,
  172. current: articlePanelHeight,
  173. }"
  174. @mousedown-event="startResizing"
  175. @touchstart-event="startResizing"
  176. @dblclick="resetHeight"
  177. />
  178. <div
  179. class="flex h-full grow flex-col"
  180. data-test-id="article-reply-stripes-panel"
  181. :class="{
  182. 'bg-stripes relative z-10 rounded-xl outline outline-1 outline-blue-700 before:rounded-2xl':
  183. hasInternalArticle && !pinned,
  184. 'border-stripes': hasInternalArticle && pinned,
  185. }"
  186. >
  187. <div
  188. class="isolate flex h-full grow flex-col"
  189. :class="{
  190. 'rounded-xl border border-neutral-300 bg-neutral-50 dark:border-gray-900 dark:bg-gray-500':
  191. !pinned,
  192. }"
  193. >
  194. <div
  195. class="flex h-10 items-center p-3"
  196. :class="{
  197. 'bg-neutral-50 dark:bg-gray-500': pinned,
  198. 'border-b border-b-transparent': pinned && articleFormReachedTop,
  199. 'border-b border-b-neutral-300 dark:border-b-gray-900':
  200. pinned && !articleFormReachedTop,
  201. }"
  202. >
  203. <CommonLabel
  204. id="article-reply-form-title"
  205. class="text-stone-200 ltr:mr-auto rtl:ml-auto dark:text-neutral-500"
  206. tag="h2"
  207. size="small"
  208. >
  209. {{ $t('Reply') }}
  210. </CommonLabel>
  211. <CommonButton
  212. v-tooltip="$t('Discard unsaved reply')"
  213. class="text-red-500 ltr:mr-2 rtl:ml-2"
  214. variant="none"
  215. icon="trash"
  216. @click="$emit('discard-form')"
  217. />
  218. <CommonButton
  219. v-tooltip="pinned ? $t('Unpin this panel') : $t('Pin this panel')"
  220. :icon="pinned ? 'pin' : 'pin-angle'"
  221. variant="neutral"
  222. size="small"
  223. @click="togglePinned"
  224. />
  225. </div>
  226. <div
  227. id="ticketArticleReplyForm"
  228. ref="articleForm"
  229. class="grow px-3 pb-3"
  230. :class="{
  231. 'overflow-y-auto': pinned,
  232. 'my-[5px] px-4 pt-2': hasInternalArticle && pinned,
  233. }"
  234. ></div>
  235. </div>
  236. </div>
  237. </div>
  238. <div
  239. v-else-if="newArticlePresent !== undefined"
  240. class="-:border-t-transparent sticky bottom-0 z-20 flex w-full justify-center gap-2.5 border-t py-1.5"
  241. :class="{
  242. 'border-t-neutral-100 bg-neutral-50 dark:border-t-gray-900 dark:bg-gray-500':
  243. parentReachedBottomScroll,
  244. }"
  245. >
  246. <CommonButton
  247. v-for="button in availableArticleTypes"
  248. :key="button.articleType"
  249. :prefix-icon="button.icon"
  250. :variant="button.variant as ButtonVariant"
  251. size="large"
  252. @click="
  253. $emit('show-article-form', button.articleType, button.performReply)
  254. "
  255. >
  256. {{ $t(button.label) }}
  257. </CommonButton>
  258. </div>
  259. </template>
  260. <style scoped>
  261. .border-stripes {
  262. position: relative;
  263. z-index: -10;
  264. background-color: theme('colors.neutral.50');
  265. &::before {
  266. content: '';
  267. position: absolute;
  268. left: 0;
  269. top: 40px;
  270. bottom: 0;
  271. right: 0;
  272. border: 5px solid transparent;
  273. background-image: repeating-linear-gradient(
  274. 45deg,
  275. theme('colors.blue.400'),
  276. theme('colors.blue.400') 5px,
  277. theme('colors.blue.700') 5px,
  278. theme('colors.blue.700') 10px
  279. );
  280. background-position: -1px;
  281. background-attachment: fixed;
  282. mask:
  283. linear-gradient(#fff 0 0) padding-box,
  284. linear-gradient(#fff 0 0);
  285. mask-composite: exclude;
  286. }
  287. &::after {
  288. content: '';
  289. position: absolute;
  290. left: 0;
  291. top: 40px;
  292. bottom: 0;
  293. right: 0;
  294. outline: 1px solid theme('colors.blue.700');
  295. outline-offset: -5px;
  296. pointer-events: none;
  297. }
  298. }
  299. [data-theme='dark'] .border-stripes {
  300. background-color: theme('colors.gray.500');
  301. &::before {
  302. background-image: repeating-linear-gradient(
  303. 45deg,
  304. theme('colors.blue.700'),
  305. theme('colors.blue.700') 5px,
  306. theme('colors.blue.900') 5px,
  307. theme('colors.blue.900') 10px
  308. );
  309. }
  310. }
  311. </style>