EntitiesView.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <template>
  2. <Section
  3. v-if="!definitionsLoaded"
  4. title="Loading entity definitions..."
  5. :icon="CellsIcon"
  6. />
  7. <Section
  8. v-else-if="totalInstances === 0"
  9. title="There are no entities to show yet."
  10. :icon="CellsIcon"
  11. >
  12. <p>
  13. When OliveTin has registered entity instances (for example from entity files or your setup), they will be listed here.
  14. </p>
  15. </Section>
  16. <template v-else>
  17. <EntityDefinitionSection
  18. v-for="def in entityDefinitions"
  19. :key="def.title"
  20. :definition="def"
  21. />
  22. </template>
  23. </template>
  24. <script setup>
  25. import { ref, computed, onMounted, onUnmounted } from 'vue'
  26. import { CellsIcon } from '@hugeicons/core-free-icons'
  27. import Section from 'picocrank/vue/components/Section.vue'
  28. import EntityDefinitionSection from '../components/EntityDefinitionSection.vue'
  29. const definitionsLoaded = ref(false)
  30. const entityDefinitions = ref([])
  31. let entityFetchGeneration = 0
  32. const totalInstances = computed(() =>
  33. entityDefinitions.value.reduce(
  34. (sum, def) => sum + (def.instances?.length ?? 0),
  35. 0
  36. )
  37. )
  38. async function fetchEntities () {
  39. const fetchGeneration = ++entityFetchGeneration
  40. try {
  41. const ret = await window.client.getEntities()
  42. if (fetchGeneration !== entityFetchGeneration) return
  43. entityDefinitions.value = ret.entityDefinitions ?? []
  44. } catch (err) {
  45. if (fetchGeneration !== entityFetchGeneration) return
  46. console.error('Failed to fetch entities:', err)
  47. window.showBigError('fetch-entities', 'getting entities', err, false)
  48. entityDefinitions.value = []
  49. } finally {
  50. if (fetchGeneration === entityFetchGeneration) {
  51. definitionsLoaded.value = true
  52. }
  53. }
  54. }
  55. onMounted(() => {
  56. fetchEntities()
  57. window.addEventListener('EventEntityChanged', fetchEntities)
  58. window.addEventListener('EventConfigChanged', fetchEntities)
  59. })
  60. onUnmounted(() => {
  61. window.removeEventListener('EventEntityChanged', fetchEntities)
  62. window.removeEventListener('EventConfigChanged', fetchEntities)
  63. })
  64. </script>