usePage.ts 988 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. computed,
  4. onActivated,
  5. onDeactivated,
  6. ref,
  7. watch,
  8. type ComputedRef,
  9. type WatchHandle,
  10. } from 'vue'
  11. import useMetaTitle from '#shared/composables/useMetaTitle.ts'
  12. interface PageOptions {
  13. metaTitle?: ComputedRef<string>
  14. }
  15. export const usePage = (pageOptions: PageOptions) => {
  16. const pageActive = ref(true)
  17. const pageInactive = computed(() => !pageActive.value)
  18. const { metaTitle } = pageOptions
  19. let stopMetaTitleWatcher: WatchHandle | undefined
  20. const { setViewTitle } = useMetaTitle()
  21. onActivated(() => {
  22. pageActive.value = true
  23. if (metaTitle) {
  24. stopMetaTitleWatcher = watch(
  25. metaTitle,
  26. (newValue) => {
  27. setViewTitle(newValue)
  28. },
  29. { immediate: true },
  30. )
  31. }
  32. })
  33. onDeactivated(() => {
  34. pageActive.value = false
  35. stopMetaTitleWatcher?.()
  36. })
  37. return {
  38. pageActive,
  39. pageInactive,
  40. }
  41. }