Headers.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <div class="flex flex-col flex-1">
  3. <div
  4. class="sticky z-10 flex items-center justify-between pl-4 border-b bg-primary border-dividerLight top-upperMobileSecondaryStickyFold sm:top-upperSecondaryStickyFold"
  5. >
  6. <label class="font-semibold text-secondaryLight">
  7. {{ t("request.header_list") }}
  8. </label>
  9. <div class="flex">
  10. <ButtonSecondary
  11. v-tippy="{ theme: 'tooltip' }"
  12. to="https://docs.hoppscotch.io/features/headers"
  13. blank
  14. :title="t('app.wiki')"
  15. svg="help-circle"
  16. />
  17. <ButtonSecondary
  18. v-tippy="{ theme: 'tooltip' }"
  19. :title="t('action.clear_all')"
  20. svg="trash-2"
  21. @click.native="clearContent()"
  22. />
  23. <ButtonSecondary
  24. v-tippy="{ theme: 'tooltip' }"
  25. :title="t('state.bulk_mode')"
  26. svg="edit"
  27. :class="{ '!text-accent': bulkMode }"
  28. @click.native="bulkMode = !bulkMode"
  29. />
  30. <ButtonSecondary
  31. v-tippy="{ theme: 'tooltip' }"
  32. :title="t('add.new')"
  33. svg="plus"
  34. :disabled="bulkMode"
  35. @click.native="addHeader"
  36. />
  37. </div>
  38. </div>
  39. <div v-if="bulkMode" ref="bulkEditor" class="flex flex-col flex-1"></div>
  40. <div v-else>
  41. <draggable
  42. v-model="workingHeaders"
  43. animation="250"
  44. handle=".draggable-handle"
  45. draggable=".draggable-content"
  46. ghost-class="cursor-move"
  47. chosen-class="bg-primaryLight"
  48. drag-class="cursor-grabbing"
  49. >
  50. <div
  51. v-for="(header, index) in workingHeaders"
  52. :key="`header-${header.id}-${index}`"
  53. class="flex border-b divide-x divide-dividerLight border-dividerLight draggable-content group"
  54. >
  55. <span>
  56. <ButtonSecondary
  57. svg="grip-vertical"
  58. class="cursor-auto text-primary hover:text-primary"
  59. :class="{
  60. 'draggable-handle group-hover:text-secondaryLight !cursor-grab':
  61. index !== workingHeaders?.length - 1,
  62. }"
  63. tabindex="-1"
  64. />
  65. </span>
  66. <SmartAutoComplete
  67. :placeholder="`${t('count.header', { count: index + 1 })}`"
  68. :source="commonHeaders"
  69. :spellcheck="false"
  70. :value="header.key"
  71. autofocus
  72. styles="
  73. bg-transparent
  74. flex
  75. flex-1
  76. py-1
  77. px-4
  78. truncate
  79. "
  80. class="flex-1 !flex"
  81. @input="
  82. updateHeader(index, {
  83. id: header.id,
  84. key: $event,
  85. value: header.value,
  86. active: header.active,
  87. })
  88. "
  89. />
  90. <SmartEnvInput
  91. v-model="header.value"
  92. :placeholder="`${t('count.value', { count: index + 1 })}`"
  93. @change="
  94. updateHeader(index, {
  95. id: header.id,
  96. key: header.key,
  97. value: $event,
  98. active: header.active,
  99. })
  100. "
  101. />
  102. <span>
  103. <ButtonSecondary
  104. v-tippy="{ theme: 'tooltip' }"
  105. :title="
  106. header.hasOwnProperty('active')
  107. ? header.active
  108. ? t('action.turn_off')
  109. : t('action.turn_on')
  110. : t('action.turn_off')
  111. "
  112. :svg="
  113. header.hasOwnProperty('active')
  114. ? header.active
  115. ? 'check-circle'
  116. : 'circle'
  117. : 'check-circle'
  118. "
  119. color="green"
  120. @click.native="
  121. updateHeader(index, {
  122. id: header.id,
  123. key: header.key,
  124. value: header.value,
  125. active: !header.active,
  126. })
  127. "
  128. />
  129. </span>
  130. <span>
  131. <ButtonSecondary
  132. v-tippy="{ theme: 'tooltip' }"
  133. :title="t('action.remove')"
  134. svg="trash"
  135. color="red"
  136. @click.native="deleteHeader(index)"
  137. />
  138. </span>
  139. </div>
  140. </draggable>
  141. <div
  142. v-if="workingHeaders.length === 0"
  143. class="flex flex-col items-center justify-center p-4 text-secondaryLight"
  144. >
  145. <img
  146. :src="`/images/states/${$colorMode.value}/add_category.svg`"
  147. loading="lazy"
  148. class="inline-flex flex-col object-contain object-center w-16 h-16 my-4"
  149. :alt="`${t('empty.headers')}`"
  150. />
  151. <span class="pb-4 text-center">{{ t("empty.headers") }}</span>
  152. <ButtonSecondary
  153. filled
  154. :label="`${t('add.new')}`"
  155. svg="plus"
  156. class="mb-4"
  157. @click.native="addHeader"
  158. />
  159. </div>
  160. </div>
  161. </div>
  162. </template>
  163. <script setup lang="ts">
  164. import { Ref, ref, watch } from "@nuxtjs/composition-api"
  165. import isEqual from "lodash/isEqual"
  166. import {
  167. HoppRESTHeader,
  168. parseRawKeyValueEntriesE,
  169. rawKeyValueEntriesToString,
  170. RawKeyValueEntry,
  171. } from "@hoppscotch/data"
  172. import { flow, pipe } from "fp-ts/function"
  173. import * as RA from "fp-ts/ReadonlyArray"
  174. import * as E from "fp-ts/Either"
  175. import * as O from "fp-ts/Option"
  176. import * as A from "fp-ts/Array"
  177. import cloneDeep from "lodash/cloneDeep"
  178. import draggable from "vuedraggable"
  179. import { useCodemirror } from "~/helpers/editor/codemirror"
  180. import { restHeaders$, setRESTHeaders } from "~/newstore/RESTSession"
  181. import { commonHeaders } from "~/helpers/headers"
  182. import { useI18n, useStream, useToast } from "~/helpers/utils/composables"
  183. import linter from "~/helpers/editor/linting/rawKeyValue"
  184. import { throwError } from "~/helpers/functional/error"
  185. import { objRemoveKey } from "~/helpers/functional/object"
  186. const t = useI18n()
  187. const toast = useToast()
  188. const idTicker = ref(0)
  189. const bulkMode = ref(false)
  190. const bulkHeaders = ref("")
  191. const bulkEditor = ref<any | null>(null)
  192. const deletionToast = ref<{ goAway: (delay: number) => void } | null>(null)
  193. useCodemirror(bulkEditor, bulkHeaders, {
  194. extendedEditorConfig: {
  195. mode: "text/x-yaml",
  196. placeholder: `${t("state.bulk_mode_placeholder")}`,
  197. },
  198. linter,
  199. completer: null,
  200. environmentHighlights: true,
  201. })
  202. // The functional headers list (the headers actually in the system)
  203. const headers = useStream(restHeaders$, [], setRESTHeaders) as Ref<
  204. HoppRESTHeader[]
  205. >
  206. // The UI representation of the headers list (has the empty end headers)
  207. const workingHeaders = ref<Array<HoppRESTHeader & { id: number }>>([
  208. {
  209. id: idTicker.value++,
  210. key: "",
  211. value: "",
  212. active: true,
  213. },
  214. ])
  215. // Rule: Working Headers always have last element is always an empty header
  216. watch(workingHeaders, (headersList) => {
  217. if (
  218. headersList.length > 0 &&
  219. headersList[headersList.length - 1].key !== ""
  220. ) {
  221. workingHeaders.value.push({
  222. id: idTicker.value++,
  223. key: "",
  224. value: "",
  225. active: true,
  226. })
  227. }
  228. })
  229. // Sync logic between headers and working/bulk headers
  230. watch(
  231. headers,
  232. (newHeadersList) => {
  233. // Sync should overwrite working headers
  234. const filteredWorkingHeaders = pipe(
  235. workingHeaders.value,
  236. A.filterMap(
  237. flow(
  238. O.fromPredicate((e) => e.key !== ""),
  239. O.map(objRemoveKey("id"))
  240. )
  241. )
  242. )
  243. const filteredBulkHeaders = pipe(
  244. parseRawKeyValueEntriesE(bulkHeaders.value),
  245. E.map(
  246. flow(
  247. RA.filter((e) => e.key !== ""),
  248. RA.toArray
  249. )
  250. ),
  251. E.getOrElse(() => [] as RawKeyValueEntry[])
  252. )
  253. if (!isEqual(newHeadersList, filteredWorkingHeaders)) {
  254. workingHeaders.value = pipe(
  255. newHeadersList,
  256. A.map((x) => ({ id: idTicker.value++, ...x }))
  257. )
  258. }
  259. if (!isEqual(newHeadersList, filteredBulkHeaders)) {
  260. bulkHeaders.value = rawKeyValueEntriesToString(newHeadersList)
  261. }
  262. },
  263. { immediate: true }
  264. )
  265. watch(workingHeaders, (newWorkingHeaders) => {
  266. const fixedHeaders = pipe(
  267. newWorkingHeaders,
  268. A.filterMap(
  269. flow(
  270. O.fromPredicate((e) => e.key !== ""),
  271. O.map(objRemoveKey("id"))
  272. )
  273. )
  274. )
  275. if (!isEqual(headers.value, fixedHeaders)) {
  276. headers.value = cloneDeep(fixedHeaders)
  277. }
  278. })
  279. watch(bulkHeaders, (newBulkHeaders) => {
  280. const filteredBulkHeaders = pipe(
  281. parseRawKeyValueEntriesE(newBulkHeaders),
  282. E.map(
  283. flow(
  284. RA.filter((e) => e.key !== ""),
  285. RA.toArray
  286. )
  287. ),
  288. E.getOrElse(() => [] as RawKeyValueEntry[])
  289. )
  290. if (!isEqual(headers.value, filteredBulkHeaders)) {
  291. headers.value = filteredBulkHeaders
  292. }
  293. })
  294. const addHeader = () => {
  295. workingHeaders.value.push({
  296. id: idTicker.value++,
  297. key: "",
  298. value: "",
  299. active: true,
  300. })
  301. }
  302. const updateHeader = (
  303. index: number,
  304. header: HoppRESTHeader & { id: number }
  305. ) => {
  306. workingHeaders.value = workingHeaders.value.map((h, i) =>
  307. i === index ? header : h
  308. )
  309. }
  310. const deleteHeader = (index: number) => {
  311. const headersBeforeDeletion = cloneDeep(workingHeaders.value)
  312. if (
  313. !(
  314. headersBeforeDeletion.length > 0 &&
  315. index === headersBeforeDeletion.length - 1
  316. )
  317. ) {
  318. if (deletionToast.value) {
  319. deletionToast.value.goAway(0)
  320. deletionToast.value = null
  321. }
  322. deletionToast.value = toast.success(`${t("state.deleted")}`, {
  323. action: [
  324. {
  325. text: `${t("action.undo")}`,
  326. onClick: (_, toastObject) => {
  327. workingHeaders.value = headersBeforeDeletion
  328. toastObject.goAway(0)
  329. deletionToast.value = null
  330. },
  331. },
  332. ],
  333. onComplete: () => {
  334. deletionToast.value = null
  335. },
  336. })
  337. }
  338. workingHeaders.value = pipe(
  339. workingHeaders.value,
  340. A.deleteAt(index),
  341. O.getOrElseW(() => throwError("Working Headers Deletion Out of Bounds"))
  342. )
  343. }
  344. const clearContent = () => {
  345. // set params list to the initial state
  346. workingHeaders.value = [
  347. {
  348. id: idTicker.value++,
  349. key: "",
  350. value: "",
  351. active: true,
  352. },
  353. ]
  354. bulkHeaders.value = ""
  355. }
  356. </script>