EntityDetailsView.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <Section :icon="CellsIcon">
  3. <template #title>
  4. <span class="section-title-with-icon">
  5. Entity Details:
  6. <ActionIconGlyph
  7. v-if="entityIcon"
  8. class="entity-title-icon"
  9. :glyph="entityIcon"
  10. />
  11. <span v-if="entityDetails?.title">{{ entityDetails.title }}</span>
  12. </span>
  13. </template>
  14. <template #toolbar>
  15. <button
  16. type="button"
  17. class="button neutral inline-icon"
  18. title="Back"
  19. @click="goBack"
  20. >
  21. <HugeiconsIcon
  22. :icon="ArrowLeftIcon"
  23. width="1.2em"
  24. height="1.2em"
  25. />
  26. Back
  27. </button>
  28. </template>
  29. <div v-if="entityNotFound">
  30. <p>Entity not found.</p>
  31. </div>
  32. <div v-else-if="!entityDetails">
  33. <p>Loading entity details...</p>
  34. </div>
  35. <template v-else>
  36. <dl>
  37. <dt>Type</dt>
  38. <dd>
  39. <router-link
  40. :to="{ name: 'Entities' }"
  41. class="entity-type-link"
  42. >
  43. {{ entityType }}
  44. </router-link>
  45. </dd>
  46. <template v-if="entityDetails.fields">
  47. <template
  48. v-for="(value, key) in entityDetails.fields"
  49. :key="key"
  50. >
  51. <dt>{{ key }}</dt>
  52. <dd>{{ value }}</dd>
  53. </template>
  54. </template>
  55. </dl>
  56. <p v-if="!entityDetails.title && (!entityDetails.fields || Object.keys(entityDetails.fields).length === 0)">
  57. No details available for this entity.
  58. </p>
  59. </template>
  60. </Section>
  61. <Section
  62. v-if="entityDetails"
  63. title="Dashboard Entity Directories"
  64. :icon="Folder01Icon"
  65. >
  66. <div
  67. v-if="filteredDirectories.length > 0"
  68. class="directories-section"
  69. >
  70. <ul class="directory-list">
  71. <li
  72. v-for="(directory, idx) in filteredDirectories"
  73. :key="idx"
  74. >
  75. <router-link
  76. :to="{
  77. name: 'Dashboard',
  78. params: {
  79. title: directory,
  80. entityType: entityType,
  81. entityKey: entityKey
  82. }
  83. }"
  84. >
  85. {{ directory }}
  86. </router-link>
  87. </li>
  88. </ul>
  89. </div>
  90. <p v-else>
  91. No directories found for this entity.
  92. <a
  93. href="https://docs.olivetin.app/dashboards/entity-directories.html"
  94. target="_blank"
  95. rel="noopener noreferrer"
  96. >Learn more</a>
  97. </p>
  98. </Section>
  99. <section
  100. v-if="entityDetails && relatedActions.length > 0"
  101. class="transparent"
  102. >
  103. <div class="dashboard-row">
  104. <fieldset>
  105. <legend class="visually-hidden">
  106. Related actions
  107. </legend>
  108. <template
  109. v-for="(related, idx) in relatedActions"
  110. :key="related.action?.bindingId || idx"
  111. >
  112. <ActionButton
  113. v-if="related.action"
  114. :action-data="related.action"
  115. :prefilled-arguments="related.prefilledArguments"
  116. />
  117. </template>
  118. </fieldset>
  119. </div>
  120. </section>
  121. </template>
  122. <script setup>
  123. import { ref, computed, onMounted, onUnmounted } from 'vue'
  124. import { useRouter } from 'vue-router'
  125. import { HugeiconsIcon } from '@hugeicons/vue'
  126. import { ArrowLeftIcon, CellsIcon, Folder01Icon } from '@hugeicons/core-free-icons'
  127. import Section from 'picocrank/vue/components/Section.vue'
  128. import ActionButton from '../ActionButton.vue'
  129. import ActionIconGlyph from '../components/ActionIconGlyph.vue'
  130. const router = useRouter()
  131. const entityDetails = ref(null)
  132. const entityNotFound = ref(false)
  133. const props = defineProps({
  134. entityType: String,
  135. entityKey: String
  136. })
  137. const filteredDirectories = computed(() => {
  138. if (!entityDetails.value?.directories) {
  139. return []
  140. }
  141. return entityDetails.value.directories.filter(d => d)
  142. })
  143. const relatedActions = computed(() => entityDetails.value?.relatedActions ?? [])
  144. const entityIcon = computed(() => entityDetails.value?.icon ?? '')
  145. function goBack () {
  146. router.push({ name: 'Entities' })
  147. }
  148. function isEntityNotFoundError (err) {
  149. return err.status === 404 || err.code === 'NotFound' || err.message?.includes('not found')
  150. }
  151. async function fetchEntityDetails () {
  152. try {
  153. const response = await window.client.getEntity({
  154. type: props.entityType,
  155. uniqueKey: props.entityKey
  156. })
  157. entityDetails.value = response
  158. entityNotFound.value = false
  159. } catch (err) {
  160. if (isEntityNotFoundError(err)) {
  161. entityDetails.value = null
  162. entityNotFound.value = true
  163. return
  164. }
  165. console.error('Failed to fetch entity details:', err)
  166. window.showBigError('fetch-entity-details', 'getting entity details', err, false)
  167. }
  168. }
  169. onMounted(() => {
  170. fetchEntityDetails()
  171. window.addEventListener('EventEntityChanged', fetchEntityDetails)
  172. window.addEventListener('EventConfigChanged', fetchEntityDetails)
  173. })
  174. onUnmounted(() => {
  175. window.removeEventListener('EventEntityChanged', fetchEntityDetails)
  176. window.removeEventListener('EventConfigChanged', fetchEntityDetails)
  177. })
  178. </script>
  179. <style scoped>
  180. .directory-list a {
  181. text-decoration: none;
  182. padding: 0.5em;
  183. display: inline-block;
  184. border-radius: 0.3em;
  185. transition: background-color 0.2s;
  186. }
  187. .directory-list a:hover {
  188. background-color: var(--bg-hover, #f5f5f5);
  189. text-decoration: underline;
  190. }
  191. .entity-type-link {
  192. text-decoration: none;
  193. transition: opacity 0.2s;
  194. }
  195. .entity-type-link:hover {
  196. text-decoration: underline;
  197. opacity: 0.8;
  198. }
  199. .section-title-with-icon {
  200. display: inline-flex;
  201. align-items: center;
  202. gap: 0.5em;
  203. }
  204. .entity-title-icon {
  205. font-size: 1.2em;
  206. }
  207. fieldset {
  208. display: grid;
  209. grid-template-columns: repeat(auto-fit, 180px);
  210. grid-auto-rows: 1fr;
  211. justify-content: center;
  212. place-items: stretch;
  213. }
  214. @media (prefers-color-scheme: dark) {
  215. .directory-list a:hover {
  216. background-color: var(--bg-hover, #222);
  217. }
  218. }
  219. </style>