-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboardView.vue
More file actions
157 lines (132 loc) · 3.45 KB
/
DashboardView.vue
File metadata and controls
157 lines (132 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<template>
<section class="dashboard-view">
<header class="dashboard-header">
<h1>Dashboard</h1>
<div class="dashboard-meta">
<span class="status" :class="{ updating: isUpdating }">
{{ isUpdating ? '更新中' : '已更新' }}
</span>
<span class="last-updated">最後更新時間:{{ lastUpdatedLabel }}</span>
</div>
</header>
<p v-if="pollingError" class="error-banner" role="status">
{{ pollingError }}
</p>
<div class="card-grid">
<article v-for="item in sortedItems" :key="item.id" class="card">
<h2>{{ item.title }}</h2>
<p>{{ item.description }}</p>
<small>最後活動:{{ formatDate(item.updatedAt) }}</small>
</article>
</div>
</section>
</template>
<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue';
type DashboardItem = {
id: string | number;
title: string;
description?: string;
updatedAt: string | number | Date;
};
const items = ref<DashboardItem[]>([]);
const isUpdating = ref(false);
const pollingError = ref('');
const lastSuccessfulSyncAt = ref<Date | null>(null);
let refreshTimer: ReturnType<typeof setInterval> | null = null;
const sortedItems = computed(() => {
return [...items.value].sort((a, b) => {
const aTime = new Date(a.updatedAt).getTime();
const bTime = new Date(b.updatedAt).getTime();
return bTime - aTime;
});
});
const lastUpdatedLabel = computed(() => {
if (!lastSuccessfulSyncAt.value) {
return '尚未更新';
}
return formatDate(lastSuccessfulSyncAt.value);
});
function formatDate(value: string | number | Date): string {
const date = new Date(value);
if (Number.isNaN(date.getTime())) {
return '時間未知';
}
return date.toLocaleString();
}
async function fetchDashboardItems(): Promise<DashboardItem[]> {
const response = await fetch('/api/dashboard');
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = (await response.json()) as DashboardItem[];
return data;
}
async function refreshData(): Promise<void> {
isUpdating.value = true;
try {
const latestItems = await fetchDashboardItems();
items.value = [...latestItems].sort((a, b) => {
const aTime = new Date(a.updatedAt).getTime();
const bTime = new Date(b.updatedAt).getTime();
return bTime - aTime;
});
lastSuccessfulSyncAt.value = new Date();
pollingError.value = '';
} catch (error) {
console.error('Polling refresh failed:', error);
pollingError.value = '資料更新失敗,已保留上次成功資料。請稍後再試。';
} finally {
isUpdating.value = false;
}
}
onMounted(async () => {
await refreshData();
refreshTimer = setInterval(() => {
void refreshData();
}, 30_000);
});
onUnmounted(() => {
if (!refreshTimer) {
return;
}
clearInterval(refreshTimer);
refreshTimer = null;
});
</script>
<style scoped>
.dashboard-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
}
.dashboard-meta {
display: flex;
gap: 1rem;
align-items: center;
}
.status {
font-weight: 600;
}
.status.updating {
color: #2563eb;
}
.error-banner {
margin: 0 0 1rem;
padding: 0.75rem 1rem;
border: 1px solid #fca5a5;
border-radius: 0.5rem;
background: #fef2f2;
color: #991b1b;
}
.card-grid {
display: grid;
gap: 1rem;
}
.card {
padding: 1rem;
border: 1px solid #e5e7eb;
border-radius: 0.5rem;
}
</style>