EntityDefinitionSection.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. const hasTable = computed(() => (props.definition.properties?.length ?? 0) > 0)
  69. const usedDashboards = computed(() => filteredDashboards(props.definition.usedOnDashboards ?? []))
  70. watch(searchText, () => {
  71. currentPage.value = 1
  72. scheduleFetchTableInstances()
  73. })
  74. watch([currentPage, pageSize], () => {
  75. scheduleFetchTableInstances()
  76. })
  77. function filteredDashboards(dashboards) {
  78. return dashboards.filter(d => d && !d.includes('{{'))
  79. }
  80. function isEntityDirectory(dashboardTitle) {
  81. return dashboardTitle.endsWith(' [Entity Directory]')
  82. }
  83. function getDashboardTitle(dashboardTitle) {
  84. if (isEntityDirectory(dashboardTitle)) {
  85. return dashboardTitle.slice(0, -' [Entity Directory]'.length)
  86. }
  87. return dashboardTitle
  88. }
  89. function scheduleFetchTableInstances() {
  90. if (!hasTable.value) {
  91. return
  92. }
  93. if (fetchTimer) {
  94. clearTimeout(fetchTimer)
  95. }
  96. fetchTimer = setTimeout(() => {
  97. fetchTableInstances()
  98. }, 250)
  99. }
  100. async function fetchTableInstances() {
  101. if (!hasTable.value) {
  102. return
  103. }
  104. tableError.value = ''
  105. try {
  106. const response = await window.client.getEntities({
  107. entityType: props.definition.title,
  108. filter: searchText.value.trim(),
  109. page: currentPage.value,
  110. pageSize: pageSize.value
  111. })
  112. const definition = response.entityDefinitions?.find(def => def.title === props.definition.title)
  113. tableInstances.value = definition?.instances ?? []
  114. totalInstances.value = definition?.totalInstances ?? 0
  115. } catch (err) {
  116. console.error('Failed to fetch entity instances:', err)
  117. tableError.value = 'Failed to load entity instances.'
  118. tableInstances.value = []
  119. totalInstances.value = 0
  120. }
  121. }
  122. onMounted(() => {
  123. if (hasTable.value) {
  124. fetchTableInstances()
  125. }
  126. })
  127. onBeforeUnmount(() => {
  128. if (fetchTimer) {
  129. clearTimeout(fetchTimer)
  130. fetchTimer = null
  131. }
  132. })
  133. </script>
  134. <style scoped>
  135. .section-title-with-icon {
  136. display: inline-flex;
  137. align-items: center;
  138. gap: 0.5em;
  139. }
  140. .entity-title-icon {
  141. font-size: 1.2em;
  142. }
  143. .table-error {
  144. color: var(--error, #c00);
  145. }
  146. </style>