| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <template>
- <Section :padding="!hasTable">
- <template #title>
- <span class="section-title-with-icon">
- Entity:
- <ActionIconGlyph v-if="definition.icon" class="entity-title-icon" :glyph="definition.icon" />
- {{ definition.title }}
- </span>
- </template>
- <template v-if="hasTable" #toolbar>
- <EntityListFilter v-model="searchText" />
- </template>
- <p v-if="!hasTable">{{ definition.instances.length }} instances.</p>
- <template v-if="hasTable">
- <p v-if="tableError" class="table-error padding" role="alert">{{ tableError }}</p>
- <EntityInstancesTable
- v-else
- :instances="tableInstances"
- :properties="definition.properties"
- :total-instances="totalInstances"
- v-model:page="currentPage"
- v-model:page-size="pageSize"
- />
- </template>
- <ul v-else>
- <li v-for="inst in definition.instances" :key="inst.uniqueKey">
- <router-link :to="entityDetailsRoute(inst)">
- {{ inst.title }}
- </router-link>
- </li>
- </ul>
- <div v-if="usedDashboards.length > 0" :class="{ padding: hasTable }">
- <h3>Used on Dashboards:</h3>
- <ul>
- <li v-for="dash in usedDashboards" :key="dash">
- <template v-if="isEntityDirectory(dash)">
- {{ getDashboardTitle(dash) }} <span class="entity-directory-label">[Entity Directory]</span>
- </template>
- <router-link v-else-if="!dash.includes('entity:')" :to="{ name: 'Dashboard', params: { title: getDashboardTitle(dash) } }">
- {{ getDashboardTitle(dash) }}
- </router-link>
- <span v-else>{{ dash }}</span>
- </li>
- </ul>
- </div>
- </Section>
- </template>
- <script setup>
- import { computed, ref, watch, onMounted, onBeforeUnmount } from 'vue'
- import Section from 'picocrank/vue/components/Section.vue'
- import ActionIconGlyph from './ActionIconGlyph.vue'
- import EntityInstancesTable from './EntityInstancesTable.vue'
- import EntityListFilter from './EntityListFilter.vue'
- import { entityDetailsRoute } from '../utils/entityRoutes.js'
- const props = defineProps({
- definition: {
- type: Object,
- required: true
- }
- })
- const searchText = ref('')
- const tableInstances = ref([])
- const totalInstances = ref(0)
- const currentPage = ref(1)
- const pageSize = ref(10)
- const tableError = ref('')
- let fetchTimer = null
- let fetchSequence = 0
- const hasTable = computed(() => (props.definition.properties?.length ?? 0) > 0)
- const usedDashboards = computed(() => filteredDashboards(props.definition.usedOnDashboards ?? []))
- watch(searchText, () => {
- currentPage.value = 1
- scheduleFetchTableInstances()
- })
- watch([currentPage, pageSize], () => {
- scheduleFetchTableInstances()
- })
- function filteredDashboards(dashboards) {
- return dashboards.filter(d => d && !d.includes('{{'))
- }
- function isEntityDirectory(dashboardTitle) {
- return dashboardTitle.endsWith(' [Entity Directory]')
- }
- function getDashboardTitle(dashboardTitle) {
- if (isEntityDirectory(dashboardTitle)) {
- return dashboardTitle.slice(0, -' [Entity Directory]'.length)
- }
- return dashboardTitle
- }
- function scheduleFetchTableInstances() {
- if (!hasTable.value) {
- return
- }
- if (fetchTimer) {
- clearTimeout(fetchTimer)
- }
- fetchTimer = setTimeout(() => {
- fetchTableInstances()
- }, 250)
- }
- async function fetchTableInstances() {
- if (!hasTable.value) {
- return
- }
- const requestId = ++fetchSequence
- tableError.value = ''
- try {
- const response = await window.client.getEntities({
- entityType: props.definition.title,
- filter: searchText.value.trim(),
- page: currentPage.value,
- pageSize: pageSize.value
- })
- if (requestId !== fetchSequence) {
- return
- }
- const definition = response.entityDefinitions?.find(def => def.title === props.definition.title)
- tableInstances.value = definition?.instances ?? []
- totalInstances.value = definition?.totalInstances ?? 0
- } catch (err) {
- if (requestId !== fetchSequence) {
- return
- }
- console.error('Failed to fetch entity instances:', err)
- tableError.value = 'Failed to load entity instances.'
- tableInstances.value = []
- totalInstances.value = 0
- }
- }
- onMounted(() => {
- if (hasTable.value) {
- fetchTableInstances()
- }
- })
- onBeforeUnmount(() => {
- if (fetchTimer) {
- clearTimeout(fetchTimer)
- fetchTimer = null
- }
- })
- </script>
- <style scoped>
- .section-title-with-icon {
- display: inline-flex;
- align-items: center;
- gap: 0.5em;
- }
- .entity-title-icon {
- font-size: 1.2em;
- }
- .table-error {
- color: var(--error, #c00);
- }
- </style>
|