Skip to content

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

5.9 KB
Raw Blame
<template>
  <div class="flex flex-col justify-center h-screen">
    <div
      v-if="sharedRequestDetails.loading"
      class="flex justify-center items-center py-5"
    >
      <HoppSmartSpinner />
    </div>

    <div
      v-else-if="E.isLeft(sharedRequestDetails.data) || invalidLink"
      class="flex flex-1 flex-col items-center justify-center p-8"
    >
      <icon-lucide-alert-triangle class="svg-icons mb-2 opacity-75" />
      <h1 class="heading text-center">
        {{ t("error.invalid_link") }}
      </h1>
      <p class="mt-2 text-center">
        {{ t("error.invalid_embed_link") }}
      </p>
    </div>

    <Embeds
      v-else-if="protocol === 'rest' && restTab"
      v-model:model-tab="restTab"
      :properties="properties"
      :shared-request-i-d="sharedRequestID"
    />

    <EmbedsGQLIndex
      v-else-if="protocol === 'gql' && gqlTab"
      v-model:model-tab="gqlTab"
      :properties="properties"
      :shared-request-i-d="sharedRequestID"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onMounted, onBeforeUnmount } from "vue"
import { useRoute } from "vue-router"
import { useGQLQuery } from "~/composables/graphql"
import {
  ResolveShortcodeDocument,
  ResolveShortcodeQuery,
  ResolveShortcodeQueryVariables,
} from "~/helpers/backend/graphql"
import * as E from "fp-ts/Either"
import {
  HoppGQLRequest,
  HoppRESTRequest,
  getDefaultRESTRequest,
  isGQLRequest,
  makeGQLRequest,
  safelyExtractRESTRequest,
  translateToGQLRequest,
} from "@hoppscotch/data"
import { HoppTab } from "~/services/tab"
import {
  HoppGQLRequestDocument,
  HoppRequestDocument,
} from "~/helpers/tab/document"
import { useI18n } from "~/composables/i18n"

// Sharer-controlled theme — apply to this embed document only; persisting
// it via applySetting would write into the viewer's settings store.
// "system" tracks later OS color-scheme changes via a media listener.
let systemThemeQuery: MediaQueryList | null = null
const onSystemThemeChange = (e: MediaQueryListEvent) => {
  document.documentElement.setAttribute("class", e.matches ? "dark" : "light")
}
const applyEmbedTheme = (theme: "dark" | "light" | "system") => {
  systemThemeQuery?.removeEventListener("change", onSystemThemeChange)
  systemThemeQuery = null
  if (theme === "system") {
    systemThemeQuery = window.matchMedia("(prefers-color-scheme: dark)")
    systemThemeQuery.addEventListener("change", onSystemThemeChange)
    document.documentElement.setAttribute(
      "class",
      systemThemeQuery.matches ? "dark" : "light"
    )
    return
  }
  document.documentElement.setAttribute("class", theme)
}
onBeforeUnmount(() => {
  systemThemeQuery?.removeEventListener("change", onSystemThemeChange)
})

const t = useI18n()

const route = useRoute()

const sharedRequestID = ref("")
const invalidLink = ref(false)
const properties = ref<string[]>([])

// Discriminator for which embed shell to render. Set when the shortcode
// payload is parsed; null until then (loading state).
const protocol = ref<"rest" | "gql" | null>(null)

const sharedRequestDetails = useGQLQuery<
  ResolveShortcodeQuery,
  ResolveShortcodeQueryVariables,
  ""
>({
  query: ResolveShortcodeDocument,
  variables: {
    code: route.params.id.toString(),
  },
})

// Tab ids unique per page mount so two embeds opened back-to-back via SPA
// navigation don't share connection-service state (the GQL connection
// service keys schema/socket/subscription by tabId). `crypto.randomUUID`
// is available in all evergreen browsers and the desktop renderer.
const embedTabId = `embed-${crypto.randomUUID()}`

// One tab ref per protocol so each child component gets a tightly-typed
// model and we don't need to widen the embed components themselves.
const restTab = ref<HoppTab<HoppRequestDocument>>({
  id: embedTabId,
  document: {
    request: getDefaultRESTRequest(),
    response: null,
    isDirty: false,
    type: "request",
  },
})

const gqlTab = ref<HoppTab<HoppGQLRequestDocument>>({
  id: embedTabId,
  document: {
    request: makeGQLRequest({
      name: "Untitled Request",
      url: "",
      headers: [],
      query: "",
      variables: "",
      auth: { authType: "none", authActive: true },
      description: null,
      responses: {},
      preRequestScript: "",
      testScript: "",
    }),
    response: null,
    isDirty: false,
    type: "gql-request",
  },
})

const isGQLShortcodePayload = (req: unknown): boolean =>
  !!req &&
  typeof req === "object" &&
  isGQLRequest(req as HoppRESTRequest | HoppGQLRequest)

watch(
  () => sharedRequestDetails.data,
  () => {
    if (sharedRequestDetails.loading) return

    const data = sharedRequestDetails.data

    if (E.isRight(data)) {
      if (!data.right.shortcode?.request) {
        invalidLink.value = true
        return
      }

      const request: unknown = JSON.parse(
        data.right.shortcode?.request as string
      )

      if (isGQLShortcodePayload(request)) {
        // Versioned parser (safeParse + migrations, default fallback) — same
        // as `share/Request.vue` / `PublishedDocs.ts` for identical payloads
        gqlTab.value.document.request = translateToGQLRequest(request)
        protocol.value = "gql"
      } else {
        restTab.value.document.request = safelyExtractRESTRequest(
          request,
          getDefaultRESTRequest()
        )
        protocol.value = "rest"
      }

      if (data.right.shortcode && data.right.shortcode.properties) {
        const parsedProperties = JSON.parse(data.right.shortcode.properties)
        if (["dark", "light", "system"].includes(parsedProperties.theme)) {
          applyEmbedTheme(parsedProperties.theme)
        }
        properties.value = parsedProperties.options
      }
    }
  }
)

onMounted(() => {
  if (typeof route.params.id === "string") {
    sharedRequestID.value = route.params.id
    sharedRequestDetails.execute()
  }
  invalidLink.value = !sharedRequestID.value
})
</script>

<route lang="yaml">
meta:
  layout: empty
</route>