Interceptor.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div class="flex flex-col p-4 space-y-4">
  3. <div class="flex flex-col">
  4. <h2 class="inline-flex pb-1 font-semibold text-secondaryDark">
  5. {{ t("settings.interceptor") }}
  6. </h2>
  7. <p class="inline-flex text-tiny">
  8. {{ t("settings.interceptor_description") }}
  9. </p>
  10. </div>
  11. <SmartRadioGroup
  12. :radios="interceptors"
  13. :selected="interceptorSelection"
  14. @change="toggleSettingKey"
  15. />
  16. <div
  17. v-if="interceptorSelection == 'EXTENSIONS_ENABLED' && !extensionVersion"
  18. class="flex space-x-2"
  19. >
  20. <ButtonSecondary
  21. to="https://chrome.google.com/webstore/detail/hoppscotch-browser-extens/amknoiejhlmhancpahfcfcfhllgkpbld"
  22. blank
  23. svg="brands/chrome"
  24. label="Chrome"
  25. outline
  26. class="!flex-1"
  27. />
  28. <ButtonSecondary
  29. to="https://addons.mozilla.org/en-US/firefox/addon/hoppscotch"
  30. blank
  31. svg="brands/firefox"
  32. label="Firefox"
  33. outline
  34. class="!flex-1"
  35. />
  36. </div>
  37. </div>
  38. </template>
  39. <script setup lang="ts">
  40. import { computed, ref, watchEffect } from "@nuxtjs/composition-api"
  41. import { KeysMatching } from "~/types/ts-utils"
  42. import {
  43. applySetting,
  44. SettingsType,
  45. toggleSetting,
  46. useSetting,
  47. } from "~/newstore/settings"
  48. import { hasExtensionInstalled } from "~/helpers/strategies/ExtensionStrategy"
  49. import { useI18n, usePolled } from "~/helpers/utils/composables"
  50. const t = useI18n()
  51. const PROXY_ENABLED = useSetting("PROXY_ENABLED")
  52. const EXTENSIONS_ENABLED = useSetting("EXTENSIONS_ENABLED")
  53. const toggleSettingKey = <
  54. K extends KeysMatching<SettingsType | "BROWSER_ENABLED", boolean>
  55. >(
  56. key: K
  57. ) => {
  58. interceptorSelection.value = key
  59. if (key === "EXTENSIONS_ENABLED") {
  60. applySetting("EXTENSIONS_ENABLED", true)
  61. if (PROXY_ENABLED.value) toggleSetting("PROXY_ENABLED")
  62. }
  63. if (key === "PROXY_ENABLED") {
  64. applySetting("PROXY_ENABLED", true)
  65. if (EXTENSIONS_ENABLED.value) toggleSetting("EXTENSIONS_ENABLED")
  66. }
  67. if (key === "BROWSER_ENABLED") {
  68. applySetting("PROXY_ENABLED", false)
  69. applySetting("EXTENSIONS_ENABLED", false)
  70. }
  71. }
  72. const extensionVersion = usePolled(5000, (stopPolling) => {
  73. const result = hasExtensionInstalled()
  74. ? window.__POSTWOMAN_EXTENSION_HOOK__.getVersion()
  75. : null
  76. // We don't need to poll anymore after we get value
  77. if (result) stopPolling()
  78. return result
  79. })
  80. const interceptors = computed(() => [
  81. { value: "BROWSER_ENABLED", label: t("state.none") },
  82. { value: "PROXY_ENABLED", label: t("settings.proxy") },
  83. {
  84. value: "EXTENSIONS_ENABLED",
  85. label:
  86. `${t("settings.extensions")}: ` +
  87. (extensionVersion.value !== null
  88. ? `v${extensionVersion.value.major}.${extensionVersion.value.minor}`
  89. : t("settings.extension_ver_not_reported")),
  90. },
  91. ])
  92. const interceptorSelection = ref("")
  93. watchEffect(() => {
  94. if (PROXY_ENABLED.value) {
  95. interceptorSelection.value = "PROXY_ENABLED"
  96. } else if (EXTENSIONS_ENABLED.value) {
  97. interceptorSelection.value = "EXTENSIONS_ENABLED"
  98. } else {
  99. interceptorSelection.value = "BROWSER_ENABLED"
  100. }
  101. })
  102. </script>