useAttachments.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, type ComputedRef, type Ref } from 'vue'
  3. import { getAttachmentLinks } from '#shared/composables/getAttachmentLinks.ts'
  4. import type { Attachment } from '#shared/entities/attachment/types.ts'
  5. import { useApplicationStore } from '#shared/stores/application.ts'
  6. interface AttachmentsOptions {
  7. attachments: ComputedRef<Attachment[]> | Ref<Attachment[]>
  8. }
  9. export interface AttachmentWithUrls extends Attachment {
  10. preview: string
  11. inline: string
  12. canDownload: boolean
  13. downloadUrl: string
  14. }
  15. export const useAttachments = (options: AttachmentsOptions) => {
  16. const application = useApplicationStore()
  17. const attachments = computed<AttachmentWithUrls[]>(() => {
  18. return options.attachments.value.map((attachment) => {
  19. const { previewUrl, inlineUrl, canDownload, downloadUrl } =
  20. getAttachmentLinks(
  21. {
  22. internalId: attachment.internalId,
  23. type: attachment.type,
  24. },
  25. application.config.api_path,
  26. )
  27. return {
  28. ...attachment,
  29. preview: previewUrl,
  30. inline: inlineUrl,
  31. canDownload,
  32. downloadUrl,
  33. }
  34. })
  35. })
  36. return {
  37. attachments,
  38. }
  39. }