Environment.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div
  3. class="flex items-stretch group"
  4. @contextmenu.prevent="options.tippy().show()"
  5. >
  6. <span
  7. class="flex items-center justify-center px-4 cursor-pointer"
  8. @click="emit('edit-environment')"
  9. >
  10. <SmartIcon class="svg-icons" name="layers" />
  11. </span>
  12. <span
  13. class="flex flex-1 min-w-0 py-2 pr-2 cursor-pointer transition group-hover:text-secondaryDark"
  14. @click="emit('edit-environment')"
  15. >
  16. <span class="truncate">
  17. {{ environment.name }}
  18. </span>
  19. </span>
  20. <span>
  21. <tippy
  22. ref="options"
  23. interactive
  24. trigger="click"
  25. theme="popover"
  26. arrow
  27. :on-shown="() => tippyActions.focus()"
  28. >
  29. <template #trigger>
  30. <ButtonSecondary
  31. v-tippy="{ theme: 'tooltip' }"
  32. :title="t('action.more')"
  33. svg="more-vertical"
  34. />
  35. </template>
  36. <div
  37. ref="tippyActions"
  38. class="flex flex-col focus:outline-none"
  39. tabindex="0"
  40. role="menu"
  41. @keyup.e="edit.$el.click()"
  42. @keyup.d="duplicate.$el.click()"
  43. @keyup.delete="
  44. !(environmentIndex === 'Global') ? deleteAction.$el.click() : null
  45. "
  46. @keyup.escape="options.tippy().hide()"
  47. >
  48. <SmartItem
  49. ref="edit"
  50. svg="edit"
  51. :label="`${t('action.edit')}`"
  52. :shortcut="['E']"
  53. @click.native="
  54. () => {
  55. emit('edit-environment')
  56. options.tippy().hide()
  57. }
  58. "
  59. />
  60. <SmartItem
  61. ref="duplicate"
  62. svg="copy"
  63. :label="`${t('action.duplicate')}`"
  64. :shortcut="['D']"
  65. @click.native="
  66. () => {
  67. duplicateEnvironments()
  68. options.tippy().hide()
  69. }
  70. "
  71. />
  72. <SmartItem
  73. v-if="!(environmentIndex === 'Global')"
  74. ref="deleteAction"
  75. svg="trash-2"
  76. :label="`${t('action.delete')}`"
  77. :shortcut="['⌫']"
  78. @click.native="
  79. () => {
  80. confirmRemove = true
  81. options.tippy().hide()
  82. }
  83. "
  84. />
  85. </div>
  86. </tippy>
  87. </span>
  88. <SmartConfirmModal
  89. :show="confirmRemove"
  90. :title="`${t('confirm.remove_environment')}`"
  91. @hide-modal="confirmRemove = false"
  92. @resolve="removeEnvironment"
  93. />
  94. </div>
  95. </template>
  96. <script setup lang="ts">
  97. import { ref } from "@nuxtjs/composition-api"
  98. import { Environment } from "@hoppscotch/data"
  99. import cloneDeep from "lodash/cloneDeep"
  100. import {
  101. deleteEnvironment,
  102. duplicateEnvironment,
  103. createEnvironment,
  104. setEnvironmentVariables,
  105. getGlobalVariables,
  106. environmentsStore,
  107. } from "~/newstore/environments"
  108. import { useI18n, useToast } from "~/helpers/utils/composables"
  109. const t = useI18n()
  110. const toast = useToast()
  111. const props = defineProps<{
  112. environment: Environment
  113. environmentIndex: number | "Global" | null
  114. }>()
  115. const emit = defineEmits<{
  116. (e: "edit-environment"): void
  117. }>()
  118. const confirmRemove = ref(false)
  119. const tippyActions = ref<any | null>(null)
  120. const options = ref<any | null>(null)
  121. const edit = ref<any | null>(null)
  122. const duplicate = ref<any | null>(null)
  123. const deleteAction = ref<any | null>(null)
  124. const removeEnvironment = () => {
  125. if (props.environmentIndex === null) return
  126. if (props.environmentIndex !== "Global")
  127. deleteEnvironment(props.environmentIndex)
  128. toast.success(`${t("state.deleted")}`)
  129. }
  130. const duplicateEnvironments = () => {
  131. if (props.environmentIndex === null) return
  132. if (props.environmentIndex === "Global") {
  133. createEnvironment(`Global - ${t("action.duplicate")}`)
  134. setEnvironmentVariables(
  135. environmentsStore.value.environments.length - 1,
  136. cloneDeep(getGlobalVariables())
  137. )
  138. } else duplicateEnvironment(props.environmentIndex)
  139. }
  140. </script>