Dashboard.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <section
  3. v-if="!dashboard && !initError && !loadError"
  4. style="text-align: center; padding: 2em;"
  5. >
  6. <HugeiconsIcon
  7. :icon="Loading03Icon"
  8. width="3em"
  9. height="3em"
  10. style="animation: spin 1s linear infinite;"
  11. />
  12. <p>Loading dashboard...</p>
  13. <p style="color: var(--fg2);">
  14. {{ loadingTime }}s
  15. </p>
  16. </section>
  17. <section
  18. v-if="initError"
  19. style="text-align: center; padding: 2em;"
  20. class="bad"
  21. >
  22. <h2 style="color: var(--error);">
  23. Initialization Failed
  24. </h2>
  25. <p>{{ initError }}</p>
  26. <p style="color: var(--fg2);">
  27. Please check your configuration and try again.
  28. </p>
  29. </section>
  30. <section
  31. v-else-if="loadError"
  32. style="text-align: center; padding: 2em;"
  33. class="bad"
  34. >
  35. <h2 style="color: var(--error);">
  36. Failed to Load Dashboard
  37. </h2>
  38. <p>{{ loadError }}</p>
  39. <p style="color: var(--fg2);">
  40. Please check your configuration and try again.
  41. </p>
  42. </section>
  43. <template v-else-if="dashboard">
  44. <section v-if="dashboard.contents.length == 0">
  45. <div
  46. v-if="isDirectory"
  47. class="back-button-container"
  48. >
  49. <button
  50. type="button"
  51. class="back-button inline-icon"
  52. @click="goBack"
  53. >
  54. <HugeiconsIcon
  55. :icon="ArrowLeftIcon"
  56. width="1.2em"
  57. height="1.2em"
  58. />
  59. Back
  60. </button>
  61. </div>
  62. <h2>{{ dashboard.title }}</h2>
  63. <p
  64. style="text-align: center"
  65. class="padding"
  66. >
  67. This dashboard is empty.
  68. </p>
  69. </section>
  70. <section
  71. v-else
  72. class="transparent"
  73. >
  74. <div
  75. v-if="isDirectory"
  76. class="back-button-container"
  77. >
  78. <button
  79. type="button"
  80. class="back-button inline-icon"
  81. @click="goBack"
  82. >
  83. <HugeiconsIcon
  84. :icon="ArrowLeftIcon"
  85. width="1.2em"
  86. height="1.2em"
  87. />
  88. Back
  89. </button>
  90. </div>
  91. <div
  92. v-for="component in dashboard.contents"
  93. :key="component.title"
  94. class="dashboard-row"
  95. >
  96. <h2 v-if="dashboard.title != 'Default'">
  97. <router-link
  98. v-if="component.entityType && component.entityKey"
  99. :to="{
  100. name: 'EntityDetails',
  101. params: {
  102. entityType: component.entityType,
  103. entityKey: component.entityKey
  104. }
  105. }"
  106. class="entity-link"
  107. >
  108. {{ component.title }}
  109. </router-link>
  110. <span v-else>{{ component.title }}</span>
  111. </h2>
  112. <fieldset :class="component.cssClass">
  113. <template
  114. v-for="subcomponent in component.contents"
  115. :key="subcomponent.title || subcomponent.type"
  116. >
  117. <DashboardComponent :component="subcomponent" />
  118. </template>
  119. </fieldset>
  120. </div>
  121. </section>
  122. </template>
  123. </template>
  124. <script setup>
  125. import DashboardComponent from './components/DashboardComponent.vue'
  126. import { onMounted, onUnmounted, ref, computed, watch } from 'vue'
  127. import { useRouter } from 'vue-router'
  128. import { HugeiconsIcon } from '@hugeicons/vue'
  129. import { Loading03Icon, ArrowLeftIcon } from '@hugeicons/core-free-icons'
  130. const props = defineProps({
  131. title: {
  132. type: String,
  133. required: false
  134. },
  135. entityType: {
  136. type: String,
  137. required: false
  138. },
  139. entityKey: {
  140. type: String,
  141. required: false
  142. }
  143. })
  144. const router = useRouter()
  145. const dashboard = ref(null)
  146. const loadingTime = ref(0)
  147. const initError = ref(null)
  148. const loadError = ref(null)
  149. let loadingTimer = null
  150. let checkInitInterval = null
  151. let dashboardRequestId = 0
  152. const isDirectory = computed(() => {
  153. if (!dashboard.value || !window.initResponse) {
  154. return false
  155. }
  156. const rootDashboards = window.initResponse.rootDashboards || []
  157. return !rootDashboards.includes(dashboard.value.title) && dashboard.value.title !== 'Actions'
  158. })
  159. function goBack () {
  160. if (window.history.length > 1) {
  161. router.back()
  162. } else {
  163. const rootDashboards = window.initResponse?.rootDashboards || []
  164. if (rootDashboards.length > 0) {
  165. router.push({ name: 'Dashboard', params: { title: rootDashboards[0] } })
  166. } else {
  167. router.push({ name: 'Actions' })
  168. }
  169. }
  170. }
  171. async function getDashboard () {
  172. const requestId = ++dashboardRequestId
  173. let title = props.title
  174. // If no specific title was provided or it's the placeholder 'default',
  175. // prefer the first configured root dashboard (e.g., "Test").
  176. if ((!title || title === 'default') && window.initResponse.rootDashboards && window.initResponse.rootDashboards.length > 0) {
  177. title = window.initResponse.rootDashboards[0]
  178. }
  179. try {
  180. const request = {
  181. title
  182. }
  183. if (props.entityType && props.entityKey) {
  184. request.entityType = props.entityType
  185. request.entityKey = props.entityKey
  186. }
  187. const ret = await window.client.getDashboard(request)
  188. if (requestId !== dashboardRequestId) {
  189. return
  190. }
  191. if (!ret || !ret.dashboard) {
  192. throw new Error('No dashboard found')
  193. }
  194. dashboard.value = ret.dashboard
  195. const pageTitle = window.initResponse?.pageTitle || 'OliveTin'
  196. document.title = ret.dashboard.title + ' - ' + pageTitle
  197. // Clear any previous errors since we successfully loaded
  198. initError.value = null
  199. loadError.value = null
  200. // Stop the loading timer once dashboard is loaded
  201. if (loadingTimer) {
  202. clearInterval(loadingTimer)
  203. loadingTimer = null
  204. }
  205. // Set attribute to indicate dashboard is loaded successfully
  206. document.body.setAttribute('loaded-dashboard', title || 'default')
  207. } catch (e) {
  208. if (requestId !== dashboardRequestId) {
  209. return
  210. }
  211. console.error('Failed to load dashboard', e)
  212. dashboard.value = null
  213. loadError.value = e.message || 'Failed to load dashboard'
  214. const pageTitle = window.initResponse?.pageTitle || 'OliveTin'
  215. document.title = 'Error - ' + pageTitle
  216. // Stop the loading timer on error
  217. if (loadingTimer) {
  218. clearInterval(loadingTimer)
  219. loadingTimer = null
  220. }
  221. }
  222. }
  223. function waitForInitAndLoadDashboard () {
  224. document.body.removeAttribute('loaded-dashboard')
  225. loadError.value = null
  226. dashboard.value = null
  227. if (loadingTimer) {
  228. clearInterval(loadingTimer)
  229. loadingTimer = null
  230. }
  231. if (checkInitInterval) {
  232. clearInterval(checkInitInterval)
  233. checkInitInterval = null
  234. }
  235. loadingTime.value = 0
  236. loadingTimer = setInterval(() => {
  237. loadingTime.value++
  238. }, 1000)
  239. // Check if init has completed successfully
  240. if (window.initResponse) {
  241. getDashboard()
  242. } else if (window.initError) {
  243. // Init failed, show error immediately
  244. initError.value = window.initErrorMessage || 'Initialization failed. Please check your configuration and try again.'
  245. // Stop the loading timer since we're showing an error
  246. if (loadingTimer) {
  247. clearInterval(loadingTimer)
  248. loadingTimer = null
  249. }
  250. } else {
  251. // Init hasn't completed yet, poll for completion
  252. checkInitInterval = setInterval(() => {
  253. if (window.initResponse) {
  254. clearInterval(checkInitInterval)
  255. checkInitInterval = null
  256. getDashboard()
  257. } else if (window.initError) {
  258. clearInterval(checkInitInterval)
  259. checkInitInterval = null
  260. initError.value = window.initErrorMessage || 'Initialization failed. Please check your configuration and try again.'
  261. // Stop the loading timer since we're showing an error
  262. if (loadingTimer) {
  263. clearInterval(loadingTimer)
  264. loadingTimer = null
  265. }
  266. }
  267. }, 100) // Check every 100ms
  268. }
  269. }
  270. function refreshDashboardAfterLiveUpdate () {
  271. if (window.initResponse) {
  272. getDashboard()
  273. }
  274. }
  275. onMounted(() => {
  276. waitForInitAndLoadDashboard()
  277. window.addEventListener('EventEntityChanged', refreshDashboardAfterLiveUpdate)
  278. window.addEventListener('EventConfigChanged', refreshDashboardAfterLiveUpdate)
  279. })
  280. watch(
  281. () => [props.title, props.entityType, props.entityKey],
  282. () => {
  283. dashboard.value = null
  284. waitForInitAndLoadDashboard()
  285. }
  286. )
  287. onUnmounted(() => {
  288. document.body.removeAttribute('loaded-dashboard')
  289. window.removeEventListener('EventEntityChanged', refreshDashboardAfterLiveUpdate)
  290. window.removeEventListener('EventConfigChanged', refreshDashboardAfterLiveUpdate)
  291. // Clean up the timers when component is unmounted
  292. if (loadingTimer) {
  293. clearInterval(loadingTimer)
  294. loadingTimer = null
  295. }
  296. if (checkInitInterval) {
  297. clearInterval(checkInitInterval)
  298. checkInitInterval = null
  299. }
  300. })
  301. </script>
  302. <style scoped>
  303. h2 {
  304. font-weight: bold;
  305. text-align: center;
  306. padding: 1em;
  307. padding-top: 1.5em;
  308. grid-column: 1 / -1;
  309. }
  310. h2 .entity-link {
  311. color: inherit;
  312. text-decoration: none;
  313. transition: opacity 0.2s;
  314. }
  315. h2 .entity-link:hover {
  316. opacity: 0.7;
  317. text-decoration: underline;
  318. }
  319. fieldset {
  320. display: grid;
  321. grid-template-columns: repeat(auto-fit, 180px);
  322. grid-auto-rows: 1fr;
  323. justify-content: center;
  324. place-items: stretch;
  325. }
  326. @keyframes spin {
  327. from {
  328. transform: rotate(0deg);
  329. }
  330. to {
  331. transform: rotate(360deg);
  332. }
  333. }
  334. .back-button-container {
  335. display: flex;
  336. justify-content: flex-start;
  337. padding: 1em;
  338. padding-bottom: 0;
  339. }
  340. .back-button {
  341. display: flex;
  342. align-items: center;
  343. gap: 0.5em;
  344. padding: 0.5em 1em;
  345. background-color: var(--bg, #fff);
  346. border: 1px solid var(--border-color, #ccc);
  347. border-radius: 0.5em;
  348. cursor: pointer;
  349. font-size: 0.9em;
  350. box-shadow: 0 0 .3em rgba(0, 0, 0, 0.1);
  351. transition: background-color 0.2s, box-shadow 0.2s;
  352. }
  353. .back-button:hover {
  354. background-color: var(--bg-hover, #f5f5f5);
  355. box-shadow: 0 0 .5em rgba(0, 0, 0, 0.15);
  356. }
  357. @media (prefers-color-scheme: dark) {
  358. .back-button {
  359. background-color: var(--bg, #111);
  360. border-color: var(--border-color, #333);
  361. }
  362. .back-button:hover {
  363. background-color: var(--bg-hover, #222);
  364. }
  365. }
  366. </style>