Header.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <template>
  2. <div>
  3. <header
  4. class="flex space-x-2 flex-1 py-2 px-2 items-center justify-between"
  5. >
  6. <div class="space-x-2 inline-flex items-center">
  7. <ButtonSecondary
  8. class="tracking-wide !font-bold !text-secondaryDark"
  9. label="HOPPSCOTCH"
  10. to="/"
  11. />
  12. <AppGitHubStarButton class="mt-1.5 transition hidden sm:flex" />
  13. </div>
  14. <div class="space-x-2 inline-flex items-center">
  15. <ButtonSecondary
  16. id="installPWA"
  17. v-tippy="{ theme: 'tooltip' }"
  18. :title="$t('header.install_pwa')"
  19. svg="download"
  20. class="rounded"
  21. @click.native="showInstallPrompt()"
  22. />
  23. <ButtonSecondary
  24. v-tippy="{ theme: 'tooltip' }"
  25. :title="`${$t('app.search')} <kbd>/</kbd>`"
  26. svg="search"
  27. class="rounded"
  28. @click.native="showSearch = true"
  29. />
  30. <ButtonSecondary
  31. v-tippy="{ theme: 'tooltip' }"
  32. :title="`${$t('support.title')} <kbd>?</kbd>`"
  33. svg="life-buoy"
  34. class="rounded"
  35. @click.native="showSupport = true"
  36. />
  37. <ButtonSecondary
  38. v-if="currentUser === null"
  39. svg="upload-cloud"
  40. :label="$t('header.save_workspace')"
  41. filled
  42. class="hidden md:flex"
  43. @click.native="showLogin = true"
  44. />
  45. <ButtonPrimary
  46. v-if="currentUser === null"
  47. :label="$t('header.login')"
  48. @click.native="showLogin = true"
  49. />
  50. <div v-else class="space-x-2 inline-flex items-center">
  51. <ButtonPrimary
  52. v-tippy="{ theme: 'tooltip' }"
  53. :title="$t('team.invite_tooltip')"
  54. :label="$t('team.invite')"
  55. svg="user-plus"
  56. class="
  57. !bg-green-500
  58. !text-green-500
  59. !bg-opacity-15
  60. !hover:bg-opacity-10 !hover:text-green-600 !hover:bg-green-400
  61. "
  62. @click.native="showTeamsModal = true"
  63. />
  64. <span class="px-2">
  65. <tippy ref="user" interactive trigger="click" theme="popover" arrow>
  66. <template #trigger>
  67. <ProfilePicture
  68. v-if="currentUser.photoURL"
  69. v-tippy="{
  70. theme: 'tooltip',
  71. }"
  72. :url="currentUser.photoURL"
  73. :alt="currentUser.displayName"
  74. :title="currentUser.displayName"
  75. indicator
  76. :indicator-styles="isOnLine ? 'bg-green-500' : 'bg-red-500'"
  77. />
  78. <ButtonSecondary
  79. v-else
  80. v-tippy="{ theme: 'tooltip' }"
  81. :title="$t('header.account')"
  82. class="rounded"
  83. svg="user"
  84. />
  85. </template>
  86. <SmartItem
  87. to="/profile"
  88. svg="user"
  89. :label="$t('navigation.profile')"
  90. @click.native="$refs.user.tippy().hide()"
  91. />
  92. <SmartItem
  93. to="/settings"
  94. svg="settings"
  95. :label="$t('navigation.settings')"
  96. @click.native="$refs.user.tippy().hide()"
  97. />
  98. <FirebaseLogout @confirm-logout="$refs.user.tippy().hide()" />
  99. </tippy>
  100. </span>
  101. </div>
  102. </div>
  103. </header>
  104. <AppAnnouncement v-if="!isOnLine" />
  105. <FirebaseLogin :show="showLogin" @hide-modal="showLogin = false" />
  106. <AppSupport :show="showSupport" @hide-modal="showSupport = false" />
  107. <AppPowerSearch :show="showSearch" @hide-modal="showSearch = false" />
  108. <TeamsModal :show="showTeamsModal" @hide-modal="showTeamsModal = false" />
  109. </div>
  110. </template>
  111. <script setup lang="ts">
  112. import { onMounted, ref, useContext } from "@nuxtjs/composition-api"
  113. import intializePwa from "~/helpers/pwa"
  114. import { probableUser$ } from "~/helpers/fb/auth"
  115. import { getLocalConfig, setLocalConfig } from "~/newstore/localpersistence"
  116. import { useReadonlyStream } from "~/helpers/utils/composables"
  117. import { defineActionHandler } from "~/helpers/actions"
  118. const {
  119. $toast,
  120. app: { i18n },
  121. } = useContext()
  122. const t = i18n.t.bind(i18n)
  123. /**
  124. * Once the PWA code is initialized, this holds a method
  125. * that can be called to show the user the installation
  126. * prompt.
  127. */
  128. const showInstallPrompt = ref(() => Promise.resolve()) // Async no-op till it is initialized
  129. const showSupport = ref(false)
  130. const showSearch = ref(false)
  131. const showLogin = ref(false)
  132. const showTeamsModal = ref(false)
  133. const isOnLine = ref(navigator.onLine)
  134. const currentUser = useReadonlyStream(probableUser$, null)
  135. defineActionHandler("modals.support.toggle", () => {
  136. showSupport.value = !showSupport.value
  137. })
  138. defineActionHandler("modals.search.toggle", () => {
  139. showSearch.value = !showSearch.value
  140. })
  141. onMounted(() => {
  142. window.addEventListener("online", () => {
  143. isOnLine.value = true
  144. })
  145. window.addEventListener("offline", () => {
  146. isOnLine.value = false
  147. })
  148. // Initializes the PWA code - checks if the app is installed,
  149. // etc.
  150. showInstallPrompt.value = intializePwa()
  151. const cookiesAllowed = getLocalConfig("cookiesAllowed") === "yes"
  152. if (!cookiesAllowed) {
  153. $toast.show(t("app.we_use_cookies").toString(), {
  154. icon: "cookie",
  155. duration: 0,
  156. action: [
  157. {
  158. text: t("action.learn_more").toString(),
  159. onClick: (_, toastObject) => {
  160. setLocalConfig("cookiesAllowed", "yes")
  161. toastObject.goAway(0)
  162. window.open("https://docs.hoppscotch.io/privacy", "_blank")?.focus()
  163. },
  164. },
  165. {
  166. text: t("action.dismiss").toString(),
  167. onClick: (_, toastObject) => {
  168. setLocalConfig("cookiesAllowed", "yes")
  169. toastObject.goAway(0)
  170. },
  171. },
  172. ],
  173. })
  174. }
  175. })
  176. </script>