Shortcode.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div
  3. class="table-row-groups lg:flex block my-6 lg:my-0 w-full border lg:border-0 divide-y lg:divide-y-0 lg:divide-x divide-dividerLight border-dividerLight"
  4. >
  5. <div
  6. class="table-column font-mono text-tiny"
  7. :data-label="t('shortcodes.short_code')"
  8. >
  9. {{ shortcode.id }}
  10. </div>
  11. <div
  12. class="table-column"
  13. :class="requestLabelColor"
  14. :data-label="t('shortcodes.method')"
  15. >
  16. {{ parseShortcodeRequest.method }}
  17. </div>
  18. <div class="table-column" :data-label="t('shortcodes.url')">
  19. {{ parseShortcodeRequest.endpoint }}
  20. </div>
  21. <div
  22. ref="timeStampRef"
  23. class="table-column"
  24. :data-label="t('shortcodes.created_on')"
  25. >
  26. <span v-tippy="{ theme: 'tooltip' }" :title="timeStamp">
  27. {{ dateStamp }}
  28. </span>
  29. </div>
  30. <div
  31. class="flex flex-1 items-center justify-center px-3"
  32. :data-label="t('shortcodes.actions')"
  33. >
  34. <SmartAnchor
  35. v-tippy="{ theme: 'tooltip' }"
  36. :title="t('action.open_workspace')"
  37. :to="`https://hopp.sh/r/${shortcode.id}`"
  38. blank
  39. svg="external-link"
  40. class="px-3 text-accent hover:text-accent"
  41. />
  42. <ButtonSecondary
  43. v-tippy="{ theme: 'tooltip' }"
  44. :title="t('action.copy')"
  45. color="green"
  46. :svg="copyIconRefs"
  47. class="px-3"
  48. @click.native="copyShortcode(shortcode.id)"
  49. />
  50. <ButtonSecondary
  51. v-tippy="{ theme: 'tooltip' }"
  52. :title="t('action.delete')"
  53. svg="trash"
  54. color="red"
  55. class="px-3"
  56. @click.native="deleteShortcode(shortcode.id)"
  57. />
  58. </div>
  59. </div>
  60. </template>
  61. <script setup lang="ts">
  62. import { ref, computed } from "@nuxtjs/composition-api"
  63. import { pipe } from "fp-ts/function"
  64. import * as RR from "fp-ts/ReadonlyRecord"
  65. import * as O from "fp-ts/Option"
  66. import { translateToNewRequest } from "@hoppscotch/data"
  67. import { refAutoReset } from "@vueuse/core"
  68. import { useI18n, useToast } from "~/helpers/utils/composables"
  69. import { copyToClipboard } from "~/helpers/utils/clipboard"
  70. import { Shortcode } from "~/helpers/shortcodes/Shortcode"
  71. const t = useI18n()
  72. const toast = useToast()
  73. const props = defineProps<{
  74. shortcode: Shortcode
  75. }>()
  76. const emit = defineEmits<{
  77. (e: "delete-shortcode", codeID: string): void
  78. }>()
  79. const deleteShortcode = (codeID: string) => {
  80. emit("delete-shortcode", codeID)
  81. }
  82. const requestMethodLabels = {
  83. get: "text-green-500",
  84. post: "text-yellow-500",
  85. put: "text-blue-500",
  86. delete: "text-red-500",
  87. default: "text-gray-500",
  88. } as const
  89. const timeStampRef = ref()
  90. const copyIconRefs = refAutoReset<"copy" | "check">("copy", 1000)
  91. const parseShortcodeRequest = computed(() =>
  92. pipe(props.shortcode.request, JSON.parse, translateToNewRequest)
  93. )
  94. const requestLabelColor = computed(() =>
  95. pipe(
  96. requestMethodLabels,
  97. RR.lookup(parseShortcodeRequest.value.method.toLowerCase()),
  98. O.getOrElseW(() => requestMethodLabels.default)
  99. )
  100. )
  101. const dateStamp = computed(() =>
  102. new Date(props.shortcode.createdOn).toLocaleDateString()
  103. )
  104. const timeStamp = computed(() =>
  105. new Date(props.shortcode.createdOn).toLocaleTimeString()
  106. )
  107. const copyShortcode = (codeID: string) => {
  108. copyToClipboard(`https://hopp.sh/r/${codeID}`)
  109. toast.success(`${t("state.copied_to_clipboard")}`)
  110. copyIconRefs.value = "check"
  111. }
  112. </script>
  113. <style lang="scss">
  114. .table-column {
  115. @apply flex flex-1 items-center px-3 py-3 truncate;
  116. }
  117. .table-row-groups {
  118. .table-column {
  119. @apply before:text-secondary before:font-bold before:content-[attr(data-label)] lg:before:hidden;
  120. }
  121. }
  122. </style>