EntitiesView.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <Section v-if="!definitionsLoaded" title="Loading entity definitions..." />
  3. <Section
  4. v-else-if="totalInstances === 0"
  5. title="There are no entities to show yet."
  6. >
  7. <p>
  8. When OliveTin has registered entity instances (for example from entity files or your setup), they will be listed here.
  9. </p>
  10. </Section>
  11. <template v-else>
  12. <Section v-for="def in entityDefinitions" :key="def.title" :title="'Entity: ' + def.title ">
  13. <p>{{ def.instances.length }} instances.</p>
  14. <ul>
  15. <li v-for="inst in def.instances" :key="inst.uniqueKey">
  16. <router-link :to="{ name: 'EntityDetails', params: { entityType: inst.type, entityKey: inst.uniqueKey } }">
  17. {{ inst.title }}
  18. </router-link>
  19. </li>
  20. </ul>
  21. <h3>Used on Dashboards:</h3>
  22. <ul>
  23. <li v-for="dash in filteredDashboards(def.usedOnDashboards)" :key="dash">
  24. <template v-if="isEntityDirectory(dash)">
  25. {{ getDashboardTitle(dash) }} <span class="entity-directory-label">[Entity Directory]</span>
  26. </template>
  27. <router-link v-else-if="!dash.includes('entity:')" :to="{ name: 'Dashboard', params: { title: getDashboardTitle(dash) } }">
  28. {{ getDashboardTitle(dash) }}
  29. </router-link>
  30. <span v-else>{{ dash }}</span>
  31. </li>
  32. </ul>
  33. </Section>
  34. </template>
  35. </template>
  36. <script setup>
  37. import { ref, computed, onMounted } from 'vue'
  38. import Section from 'picocrank/vue/components/Section.vue'
  39. const definitionsLoaded = ref(false)
  40. const entityDefinitions = ref([])
  41. const totalInstances = computed(() =>
  42. entityDefinitions.value.reduce(
  43. (sum, def) => sum + (def.instances?.length ?? 0),
  44. 0,
  45. ),
  46. )
  47. async function fetchEntities() {
  48. try {
  49. const ret = await window.client.getEntities()
  50. entityDefinitions.value = ret.entityDefinitions ?? []
  51. } catch (err) {
  52. console.error('Failed to fetch entities:', err)
  53. window.showBigError('fetch-entities', 'getting entities', err, false)
  54. entityDefinitions.value = []
  55. } finally {
  56. definitionsLoaded.value = true
  57. }
  58. }
  59. function filteredDashboards(dashboards) {
  60. return dashboards.filter(d => d && !d.includes('{{'))
  61. }
  62. function isEntityDirectory(dashboardTitle) {
  63. return dashboardTitle.endsWith(' [Entity Directory]')
  64. }
  65. function getDashboardTitle(dashboardTitle) {
  66. if (isEntityDirectory(dashboardTitle)) {
  67. return dashboardTitle.slice(0, -' [Entity Directory]'.length)
  68. }
  69. return dashboardTitle
  70. }
  71. onMounted(() => {
  72. fetchEntities()
  73. })
  74. </script>