useEscalationState.ts 923 B

123456789101112131415161718192021222324252627282930313233
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, type ToRef, toValue } from 'vue'
  3. import { useReactiveNow } from './useReactiveNow.ts'
  4. export enum EscalationState {
  5. Escalated = 'escalated',
  6. Warning = 'warning',
  7. None = 'none',
  8. }
  9. export const useEscalationState = (
  10. escalationAt: Readonly<ToRef<string | undefined | null>>,
  11. ) => {
  12. const reactiveNow = useReactiveNow()
  13. return computed(() => {
  14. const escalationString = toValue(escalationAt)
  15. if (!escalationString) return EscalationState.None
  16. const date = new Date(escalationString)
  17. if (Number.isNaN(date.getTime())) return EscalationState.None
  18. const diffSeconds = (reactiveNow.value.getTime() - date.getTime()) / 1000
  19. // Escalation is in the past.
  20. if (diffSeconds > -1) return EscalationState.Escalated
  21. // Escalation is in the future.
  22. return EscalationState.Warning
  23. })
  24. }