useConfirmation.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ref } from 'vue'
  3. import type { ButtonVariant } from '#shared/types/button.ts'
  4. import getUuid from '#shared/utils/getUuid.ts'
  5. import { useAppName } from './useAppName.ts'
  6. import type { Except } from 'type-fest'
  7. export type ConfirmationVariant = 'delete' | 'unsaved' | 'confirm'
  8. export interface ConfirmationOptions {
  9. headerTitle?: string
  10. headerTitlePlaceholder?: string[]
  11. headerIcon?: string
  12. text?: string
  13. textPlaceholder?: string[]
  14. buttonLabel?: string
  15. buttonVariant?: ButtonVariant
  16. cancelLabel?: string
  17. fullscreen?: boolean
  18. // TODO: should maybe also be implemented for mobile, so that we have a better alignment for the code
  19. confirmationVariant?: ConfirmationVariant
  20. confirmCallback: () => void
  21. cancelCallback: () => void
  22. closeCallback: () => void
  23. }
  24. const confirmationOptions = ref(new Map<string, ConfirmationOptions>())
  25. const lastConfirmationUuid = ref<string | undefined>()
  26. const triggerConfirmation = ref(0)
  27. export const useConfirmation = () => {
  28. const appName = useAppName()
  29. const waitForConfirmation = (
  30. text: string,
  31. options: Except<
  32. ConfirmationOptions,
  33. 'text' | 'confirmCallback' | 'cancelCallback' | 'closeCallback'
  34. > = {},
  35. name: string | undefined = undefined,
  36. ) => {
  37. const uniqueName =
  38. appName === 'desktop' ? name || getUuid() : 'confirmation'
  39. if (confirmationOptions.value.has(uniqueName)) {
  40. return new Promise<undefined>((resolve) => {
  41. resolve(undefined)
  42. })
  43. }
  44. return new Promise<boolean | undefined>((resolve) => {
  45. confirmationOptions.value.set(uniqueName, {
  46. ...options,
  47. text,
  48. confirmCallback() {
  49. resolve(true)
  50. },
  51. cancelCallback() {
  52. resolve(false)
  53. },
  54. closeCallback() {
  55. resolve(undefined)
  56. },
  57. })
  58. lastConfirmationUuid.value = uniqueName
  59. triggerConfirmation.value += 1
  60. })
  61. }
  62. const waitForVariantConfirmation = (
  63. variant: ConfirmationVariant = 'confirm',
  64. options: Except<
  65. ConfirmationOptions,
  66. 'text' | 'confirmCallback' | 'cancelCallback' | 'closeCallback'
  67. > = {},
  68. name: string | undefined = undefined,
  69. ) => {
  70. return waitForConfirmation(
  71. '',
  72. {
  73. ...options,
  74. confirmationVariant: variant,
  75. },
  76. name,
  77. )
  78. }
  79. return {
  80. lastConfirmationUuid,
  81. triggerConfirmation,
  82. confirmationOptions,
  83. waitForConfirmation,
  84. waitForVariantConfirmation,
  85. }
  86. }