Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

1.6 KB
Raw Blame
<template>
  <span class="inline-flex items-center gap-1.5">
    <button
      v-if="showAddField"
      class="flex items-center justify-center rounded transition hover:text-accent"
      :class="{ 'text-accent': isAdded, 'text-secondaryLight': !isAdded }"
      type="button"
      @click.stop="addField"
    >
      <icon-lucide-plus-circle v-if="!isAdded" class="svg-icons" />
      <icon-lucide-circle-check v-else class="svg-icons" />
    </button>
    <span
      class="hopp-doc-explorer-field-name text-sm font-medium"
      @click="clickable && handleClick($event)"
    >
      {{ field.name }}
    </span>
  </span>
</template>

<script setup lang="ts">
import { onBeforeUnmount } from "vue"
import { type ExplorerFieldDef, useExplorer } from "~/helpers/graphql/explorer"
import { debounce } from "lodash-es"

const props = withDefaults(
  defineProps<{
    field: ExplorerFieldDef
    clickable?: boolean
    showAddField: boolean
    isAdded?: boolean
  }>(),
  {
    clickable: true,
    showAddField: true,
    isAdded: false,
  }
)

const emit = defineEmits<{
  (event: "add-field", field: ExplorerFieldDef): void
}>()

const addField = debounce(() => {
  emit("add-field", props.field)
}, 50)

// Cancel pending debounced inserts on unmount so they can't write to the
// long-lived query-builder service after the component is gone (mirrors
// Argument.vue)
onBeforeUnmount(() => {
  addField.cancel()
})

const { push } = useExplorer()

const handleClick = (event: MouseEvent) => {
  event.preventDefault()
  push({ name: props.field.name, def: props.field })
}
</script>