|
| 1 | +<template> |
| 2 | + <div class="min-h-full w-full"> |
| 3 | + <div |
| 4 | + v-if="isLoading" |
| 5 | + class="p-6 text-sm text-lightListTableText dark:text-darkListTableText" |
| 6 | + > |
| 7 | + Loading dashboard... |
| 8 | + </div> |
| 9 | + |
| 10 | + <div |
| 11 | + v-else-if="error" |
| 12 | + class="p-6 text-sm text-lightInputErrorColor" |
| 13 | + > |
| 14 | + <div>Failed to load dashboard</div> |
| 15 | + |
| 16 | + <Button |
| 17 | + type="button" |
| 18 | + class="mt-3" |
| 19 | + @click="refetch" |
| 20 | + > |
| 21 | + Retry |
| 22 | + </Button> |
| 23 | + </div> |
| 24 | + |
| 25 | + <DashboardRuntime |
| 26 | + v-else-if="dashboard" |
| 27 | + :dashboard-slug="dashboardSlug" |
| 28 | + :dashboard-id="dashboard.id" |
| 29 | + :label="dashboard.label" |
| 30 | + :config="dashboard.config" |
| 31 | + :revision="dashboard.revision" |
| 32 | + :is-admin="isAdmin" |
| 33 | + :is-refreshing="isFetching" |
| 34 | + /> |
| 35 | + |
| 36 | + <div |
| 37 | + v-else |
| 38 | + class="dashboard-page__state" |
| 39 | + > |
| 40 | + Dashboard not found |
| 41 | + </div> |
| 42 | + </div> |
| 43 | +</template> |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +<script setup lang="ts"> |
| 48 | +import { computed, onMounted, onUnmounted, ref, watch } from 'vue' |
| 49 | +import { useRoute } from 'vue-router' |
| 50 | +import { Button } from '@/afcl' |
| 51 | +import { useCoreStore } from '@/stores/core' |
| 52 | +import websocket from '@/websocket' |
| 53 | +import DashboardRuntime from './DashboardRuntime.vue' |
| 54 | +import { useDashboardConfig } from '../queries/useDashboardConfig.js' |
| 55 | +
|
| 56 | +const route = useRoute() |
| 57 | +const coreStore = useCoreStore() |
| 58 | +
|
| 59 | +const dashboardSlug = computed(() => { |
| 60 | + const slug = route.params.slug |
| 61 | +
|
| 62 | + if (Array.isArray(slug)) { |
| 63 | + return slug[0] || 'default' |
| 64 | + } |
| 65 | +
|
| 66 | + return (slug as string) || 'default' |
| 67 | +}) |
| 68 | +
|
| 69 | +const { |
| 70 | + data: dashboard, |
| 71 | + isLoading, |
| 72 | + isFetching, |
| 73 | + error, |
| 74 | + refetch, |
| 75 | +} = useDashboardConfig(dashboardSlug) |
| 76 | +
|
| 77 | +const isAdmin = computed(() => { |
| 78 | + return coreStore.adminUser?.dbUser.role === 'superadmin' |
| 79 | +}) |
| 80 | +
|
| 81 | +const DASHBOARD_CONFIG_UPDATED_TOPIC_PREFIX = '/opentopic/dashboard-config-updated' |
| 82 | +const subscribedTopic = ref<string | null>(null) |
| 83 | +
|
| 84 | +const dashboardConfigUpdatedTopic = computed(() => { |
| 85 | + return `${DASHBOARD_CONFIG_UPDATED_TOPIC_PREFIX}/${dashboardSlug.value}` |
| 86 | +}) |
| 87 | +
|
| 88 | +function handleDashboardConfigUpdated(data: { slug?: string; revision?: number }) { |
| 89 | + if (data.slug && data.slug !== dashboardSlug.value) { |
| 90 | + return |
| 91 | + } |
| 92 | +
|
| 93 | + if (typeof data.revision === 'number' && dashboard.value && data.revision <= dashboard.value.revision) { |
| 94 | + return |
| 95 | + } |
| 96 | +
|
| 97 | + void refetch() |
| 98 | +} |
| 99 | +
|
| 100 | +function subscribeToDashboardUpdates() { |
| 101 | + if (subscribedTopic.value) { |
| 102 | + websocket.unsubscribe(subscribedTopic.value) |
| 103 | + } |
| 104 | +
|
| 105 | + subscribedTopic.value = dashboardConfigUpdatedTopic.value |
| 106 | + websocket.subscribe(subscribedTopic.value, handleDashboardConfigUpdated) |
| 107 | +} |
| 108 | +
|
| 109 | +watch(dashboardConfigUpdatedTopic, subscribeToDashboardUpdates) |
| 110 | +
|
| 111 | +onMounted(() => { |
| 112 | + subscribeToDashboardUpdates() |
| 113 | +}) |
| 114 | +
|
| 115 | +onUnmounted(() => { |
| 116 | + if (!subscribedTopic.value) { |
| 117 | + return |
| 118 | + } |
| 119 | +
|
| 120 | + websocket.unsubscribe(subscribedTopic.value) |
| 121 | +}) |
| 122 | +</script> |
0 commit comments