realtime.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <SmartTabs v-model="currentTab">
  3. <SmartTab
  4. v-for="{ target, title } in REALTIME_NAVIGATION"
  5. :id="target"
  6. :key="target"
  7. :label="title"
  8. >
  9. <NuxtChild />
  10. </SmartTab>
  11. </SmartTabs>
  12. </template>
  13. <script setup lang="ts">
  14. import { watch, ref, useRouter, useRoute } from "@nuxtjs/composition-api"
  15. import { useI18n, useI18nPathInfo } from "~/helpers/utils/composables"
  16. const t = useI18n()
  17. const { localePath, getRouteBaseName } = useI18nPathInfo()
  18. const router = useRouter()
  19. const route = useRoute()
  20. const REALTIME_NAVIGATION = [
  21. {
  22. target: "websocket",
  23. title: t("tab.websocket"),
  24. },
  25. {
  26. target: "sse",
  27. title: t("tab.sse"),
  28. },
  29. {
  30. target: "socketio",
  31. title: t("tab.socketio"),
  32. },
  33. {
  34. target: "mqtt",
  35. title: t("tab.mqtt"),
  36. },
  37. ] as const
  38. type RealtimeNavTab = typeof REALTIME_NAVIGATION[number]["target"]
  39. const currentTab = ref<RealtimeNavTab>("websocket")
  40. // Update the router when the tab is updated
  41. watch(currentTab, (newTab) => {
  42. router.push(localePath(`/realtime/${newTab}`))
  43. })
  44. // Update the tab when router is upgrad
  45. watch(
  46. route,
  47. (updateRoute) => {
  48. const path = getRouteBaseName(updateRoute)
  49. if (path.endsWith("realtime")) {
  50. router.replace(localePath(`/realtime/websocket`))
  51. return
  52. }
  53. const destination: string | undefined = path.split("realtime-")[1]
  54. const target = REALTIME_NAVIGATION.find(
  55. ({ target }) => target === destination
  56. )?.target
  57. if (target) currentTab.value = target
  58. },
  59. { immediate: true }
  60. )
  61. </script>