EntityDefinitionSection.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <Section :padding="!hasTable">
  3. <template #title>
  4. <span class="section-title-with-icon">
  5. Entity:
  6. <ActionIconGlyph v-if="definition.icon" class="entity-title-icon" :glyph="definition.icon" />
  7. {{ definition.title }}
  8. </span>
  9. </template>
  10. <template v-if="hasTable" #toolbar>
  11. <EntityListFilter v-model="searchText" />
  12. </template>
  13. <p v-if="!hasTable">{{ definition.instances.length }} instances.</p>
  14. <template v-if="hasTable">
  15. <p v-if="tableError" class="table-error padding" role="alert">{{ tableError }}</p>
  16. <EntityInstancesTable
  17. v-else
  18. :instances="tableInstances"
  19. :properties="definition.properties"
  20. :total-instances="totalInstances"
  21. v-model:page="currentPage"
  22. v-model:page-size="pageSize"
  23. />
  24. </template>
  25. <ul v-else>
  26. <li v-for="inst in definition.instances" :key="inst.uniqueKey">
  27. <router-link :to="entityDetailsRoute(inst)">
  28. {{ inst.title }}
  29. </router-link>
  30. </li>
  31. </ul>
  32. <div v-if="usedDashboards.length > 0" :class="{ padding: hasTable }">
  33. <h3>Used on Dashboards:</h3>
  34. <ul>
  35. <li v-for="dash in usedDashboards" :key="dash">
  36. <template v-if="isEntityDirectory(dash)">
  37. {{ getDashboardTitle(dash) }} <span class="entity-directory-label">[Entity Directory]</span>
  38. </template>
  39. <router-link v-else-if="!dash.includes('entity:')" :to="{ name: 'Dashboard', params: { title: getDashboardTitle(dash) } }">
  40. {{ getDashboardTitle(dash) }}
  41. </router-link>
  42. <span v-else>{{ dash }}</span>
  43. </li>
  44. </ul>
  45. </div>
  46. </Section>
  47. </template>
  48. <script setup>
  49. import { computed, ref, watch, onMounted, onBeforeUnmount } from 'vue'
  50. import Section from 'picocrank/vue/components/Section.vue'
  51. import ActionIconGlyph from './ActionIconGlyph.vue'
  52. import EntityInstancesTable from './EntityInstancesTable.vue'
  53. import EntityListFilter from './EntityListFilter.vue'
  54. import { entityDetailsRoute } from '../utils/entityRoutes.js'
  55. const props = defineProps({
  56. definition: {
  57. type: Object,
  58. required: true
  59. }
  60. })
  61. const searchText = ref('')
  62. const tableInstances = ref([])
  63. const totalInstances = ref(0)
  64. const currentPage = ref(1)
  65. const pageSize = ref(10)
  66. const tableError = ref('')
  67. let fetchTimer = null
  68. let fetchSequence = 0
  69. const hasTable = computed(() => (props.definition.properties?.length ?? 0) > 0)
  70. const usedDashboards = computed(() => filteredDashboards(props.definition.usedOnDashboards ?? []))
  71. watch(searchText, () => {
  72. currentPage.value = 1
  73. scheduleFetchTableInstances()
  74. })
  75. watch([currentPage, pageSize], () => {
  76. scheduleFetchTableInstances()
  77. })
  78. function filteredDashboards(dashboards) {
  79. return dashboards.filter(d => d && !d.includes('{{'))
  80. }
  81. function isEntityDirectory(dashboardTitle) {
  82. return dashboardTitle.endsWith(' [Entity Directory]')
  83. }
  84. function getDashboardTitle(dashboardTitle) {
  85. if (isEntityDirectory(dashboardTitle)) {
  86. return dashboardTitle.slice(0, -' [Entity Directory]'.length)
  87. }
  88. return dashboardTitle
  89. }
  90. function scheduleFetchTableInstances() {
  91. if (!hasTable.value) {
  92. return
  93. }
  94. if (fetchTimer) {
  95. clearTimeout(fetchTimer)
  96. }
  97. fetchTimer = setTimeout(() => {
  98. fetchTableInstances()
  99. }, 250)
  100. }
  101. async function fetchTableInstances() {
  102. if (!hasTable.value) {
  103. return
  104. }
  105. const requestId = ++fetchSequence
  106. tableError.value = ''
  107. try {
  108. const response = await window.client.getEntities({
  109. entityType: props.definition.title,
  110. filter: searchText.value.trim(),
  111. page: currentPage.value,
  112. pageSize: pageSize.value
  113. })
  114. if (requestId !== fetchSequence) {
  115. return
  116. }
  117. const definition = response.entityDefinitions?.find(def => def.title === props.definition.title)
  118. tableInstances.value = definition?.instances ?? []
  119. totalInstances.value = definition?.totalInstances ?? 0
  120. } catch (err) {
  121. if (requestId !== fetchSequence) {
  122. return
  123. }
  124. console.error('Failed to fetch entity instances:', err)
  125. tableError.value = 'Failed to load entity instances.'
  126. tableInstances.value = []
  127. totalInstances.value = 0
  128. }
  129. }
  130. onMounted(() => {
  131. if (hasTable.value) {
  132. fetchTableInstances()
  133. }
  134. })
  135. onBeforeUnmount(() => {
  136. if (fetchTimer) {
  137. clearTimeout(fetchTimer)
  138. fetchTimer = null
  139. }
  140. })
  141. </script>
  142. <style scoped>
  143. .section-title-with-icon {
  144. display: inline-flex;
  145. align-items: center;
  146. gap: 0.5em;
  147. }
  148. .entity-title-icon {
  149. font-size: 1.2em;
  150. }
  151. .table-error {
  152. color: var(--error, #c00);
  153. }
  154. </style>