Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
feat(dashboard): add Recent Activity component to display recent tasks and their statuses
Ahmet Ömer420f4abe8fab
diff --git a/app/components/dashboard/RecentActivity.vue b/app/components/dashboard/RecentActivity.vuenew file mode 100644index 00000000..366b7848--- /dev/null+++ b/app/components/dashboard/RecentActivity.vue@@ -0,0 +1,191 @@+<script lang="ts" setup>+type ActivityStatus = 'pending' | 'in-stages' | 'completed' | 'failed'++type ActivityItem = {+ id: number+ type: string+ status: ActivityStatus+ statusStage: string | null+ errorMessage: string | null+ createdAt: string | Date+ completedAt: string | Date | null+ fileName: string | null+ photo: {+ id: string+ title: string | null+ thumbnailUrl: string | null+ thumbnailHash: string | null+ originalUrl: string | null+ } | null+}++const dayjs = useDayjs()++const {+ data: activityData,+ pending,+ refresh,+} = await useFetch('/api/queue/recent')++const refreshInterval = setInterval(() => {+ refresh()+}, 5000)++onBeforeUnmount(() => {+ clearInterval(refreshInterval)+})++const items = computed<ActivityItem[]>(+ () => (activityData.value?.tasks as ActivityItem[] | undefined) || [],+)++const getStatusColor = (status: ActivityStatus) => {+ switch (status) {+ case 'pending':+ return 'warning'+ case 'in-stages':+ return 'info'+ case 'completed':+ return 'success'+ case 'failed':+ return 'error'+ default:+ return 'neutral'+ }+}++const getItemTitle = (item: ActivityItem) => {+ return (+ item.photo?.title ||+ item.fileName ||+ $t(`dashboard.queue.types.${item.type}`)+ )+}++const getItemMeta = (item: ActivityItem) => {+ const parts: string[] = []+ const titleIsTaskType = !item.photo?.title && !item.fileName++ if (!titleIsTaskType) {+ parts.push($t(`dashboard.queue.types.${item.type}`))+ }++ if (item.status === 'in-stages' && item.statusStage) {+ parts.push($t(`dashboard.queue.stages.${item.statusStage}`))+ }++ const timestamp = item.completedAt || item.createdAt+ if (timestamp) {+ parts.push(dayjs(timestamp).fromNow())+ }++ return parts.join(' · ')+}+</script>++<template>+ <UCard>+ <template #header>+ <div class="flex items-center justify-between gap-3 pb-1.5">+ <h2 class="text-lg font-semibold">+ {{ $t('dashboard.overview.section.recentActivity.title') }}+ </h2>+ <UButton+ to="/dashboard/queue"+ size="xs"+ variant="ghost"+ color="neutral"+ trailing-icon="tabler:arrow-right"+ >+ {{ $t('dashboard.overview.section.recentActivity.viewQueue') }}+ </UButton>+ </div>+ </template>++ <div+ v-if="pending && items.length === 0"+ class="flex items-center justify-center py-10"+ >+ <Icon+ name="svg-spinners:180-ring-with-bg"+ class="size-8 opacity-50"+ mode="svg"+ />+ </div>++ <div+ v-else-if="items.length === 0"+ class="flex flex-col items-center justify-center gap-2 py-10 text-center"+ >+ <UIcon+ name="tabler:inbox"+ class="size-8 text-neutral-400"+ />+ <p class="text-sm text-neutral-500 dark:text-neutral-400">+ {{ $t('dashboard.overview.section.recentActivity.empty') }}+ </p>+ </div>++ <ul+ v-else+ class="divide-y divide-neutral-100 dark:divide-neutral-800"+ >+ <li+ v-for="item in items"+ :key="item.id"+ class="flex items-center gap-3 py-3 first:pt-0 last:pb-0"+ >+ <NuxtLink+ :to="item.photo ? '/dashboard/photos' : '/dashboard/queue'"+ class="shrink-0"+ >+ <ThumbImage+ v-if="item.photo?.thumbnailUrl || item.photo?.originalUrl"+ :src="item.photo.thumbnailUrl || item.photo.originalUrl || ''"+ :alt="getItemTitle(item)"+ :thumbhash="item.photo.thumbnailHash || ''"+ class="size-12 rounded-md shadow-sm bg-neutral-100 dark:bg-neutral-800"+ :lazy="false"+ />+ <div+ v-else+ class="size-12 rounded-md bg-neutral-100 dark:bg-neutral-800 flex items-center justify-center"+ >+ <UIcon+ name="tabler:list-check"+ class="size-5 text-neutral-400"+ />+ </div>+ </NuxtLink>++ <div class="min-w-0 flex-1">+ <NuxtLink+ to="/dashboard/queue"+ class="block truncate text-sm font-medium hover:text-primary transition-colors"+ >+ {{ getItemTitle(item) }}+ </NuxtLink>+ <p+ class="truncate text-xs text-neutral-500 dark:text-neutral-400 mt-0.5"+ >+ {{ getItemMeta(item) }}+ </p>+ <p+ v-if="item.status === 'failed' && item.errorMessage"+ class="truncate text-xs text-error mt-0.5"+ >+ {{ item.errorMessage }}+ </p>+ </div>++ <UBadge+ :label="$t(`dashboard.queue.status.${item.status}`)"+ variant="soft"+ :color="getStatusColor(item.status)"+ size="sm"+ class="shrink-0"+ />+ </li>+ </ul>+ </UCard>+</template>diff --git a/app/pages/dashboard/index.vue b/app/pages/dashboard/index.vueindex e07d4b7f..8ad7aa21 100644--- a/app/pages/dashboard/index.vue+++ b/app/pages/dashboard/index.vue@@ -471,6 +471,8 @@ const onShareSite = () => {</UCard></div></div>++ <DashboardRecentActivity /></div></template></UDashboardPanel>diff --git a/i18n/locales/en.json b/i18n/locales/en.jsonindex 6284a491..688a42e3 100644--- a/i18n/locales/en.json+++ b/i18n/locales/en.json@@ -989,6 +989,11 @@"totalProcessed": "Processed","totalFailed": "Failure count","avgSuccessRate": "Success rate"+ },+ "recentActivity": {+ "title": "Recent Activity",+ "viewQueue": "Queue",+ "empty": "No recent tasks yet."}},"shareSite": {diff --git a/i18n/locales/ja.json b/i18n/locales/ja.jsonindex bf2746b6..a6db4f05 100644--- a/i18n/locales/ja.json+++ b/i18n/locales/ja.json@@ -915,6 +915,11 @@"totalProcessed": "処理","totalFailed": "障害カウント","avgSuccessRate": "成功率"+ },+ "recentActivity": {+ "title": "最近のアクティビティ",+ "viewQueue": "キュー",+ "empty": "最近のタスクはまだありません。"}},"shareSite": {diff --git a/i18n/locales/ru.json b/i18n/locales/ru.jsonindex 35f41fb8..6935ccf6 100644--- a/i18n/locales/ru.json+++ b/i18n/locales/ru.json@@ -988,6 +988,11 @@"totalProcessed": "Обработано","totalFailed": "Ошибок","avgSuccessRate": "Доля успешных задач"+ },+ "recentActivity": {+ "title": "Недавняя активность",+ "viewQueue": "Очередь",+ "empty": "Пока нет недавних задач."}},"shareSite": {diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.jsonindex 75e12fdc..fa33d794 100644--- a/i18n/locales/zh-Hans.json+++ b/i18n/locales/zh-Hans.json@@ -989,6 +989,11 @@"totalProcessed": "已处理","totalFailed": "失败计数","avgSuccessRate": "成功率"+ },+ "recentActivity": {+ "title": "最近动态",+ "viewQueue": "队列",+ "empty": "暂无最近的任务。"}},"shareSite": {diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.jsonindex 5fb2c0cc..3153e357 100644--- a/i18n/locales/zh-Hant-HK.json+++ b/i18n/locales/zh-Hant-HK.json@@ -915,6 +915,11 @@"totalProcessed": "已處理","totalFailed": "失敗計數","avgSuccessRate": "成功率"+ },+ "recentActivity": {+ "title": "最近動態",+ "viewQueue": "隊列",+ "empty": "尚無最近的任務。"}},"shareSite": {diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.jsonindex 0619b9de..03d01ffd 100644--- a/i18n/locales/zh-Hant-TW.json+++ b/i18n/locales/zh-Hant-TW.json@@ -915,6 +915,11 @@"totalProcessed": "已處理","totalFailed": "失敗計數","avgSuccessRate": "成功率"+ },+ "recentActivity": {+ "title": "最近動態",+ "viewQueue": "隊列",+ "empty": "尚無最近的任務。"}},"shareSite": {diff --git a/server/api/queue/recent.get.ts b/server/api/queue/recent.get.tsnew file mode 100644index 00000000..ab5ecf98--- /dev/null+++ b/server/api/queue/recent.get.ts@@ -0,0 +1,131 @@+import { desc, inArray, or } from 'drizzle-orm'+import { photos, pipelineQueue } from '~~/server/database/schema'+import type { PipelineQueueItem } from '~~/server/utils/db'++const RECENT_TASK_LIMIT = 5++function getFileName(storageKey: string | null | undefined) {+ if (!storageKey) return null+ return storageKey.split(/[\\/]/).pop() || null+}++function getTaskPhotoRef(payload: PipelineQueueItem['payload']) {+ if (payload.type === 'photo' || payload.type === 'live-photo-video') {+ return {+ photoId: null as string | null,+ storageKey: payload.storageKey || null,+ }+ }++ if (+ payload.type === 'photo-reverse-geocoding' ||+ payload.type === 'photo-erase-location'+ ) {+ return {+ photoId: payload.photoId || null,+ storageKey: null as string | null,+ }+ }++ return { photoId: null, storageKey: null }+}++export default defineEventHandler(async (event) => {+ await requireUserSession(event)++ const db = useDB()++ const recentTasks = await db+ .select({+ id: pipelineQueue.id,+ payload: pipelineQueue.payload,+ status: pipelineQueue.status,+ statusStage: pipelineQueue.statusStage,+ errorMessage: pipelineQueue.errorMessage,+ createdAt: pipelineQueue.createdAt,+ completedAt: pipelineQueue.completedAt,+ })+ .from(pipelineQueue)+ .orderBy(desc(pipelineQueue.createdAt))+ .limit(RECENT_TASK_LIMIT)+ .all()++ const photoIds = new Set<string>()+ const storageKeys = new Set<string>()++ for (const task of recentTasks) {+ const ref = getTaskPhotoRef(task.payload)+ if (ref.photoId) photoIds.add(ref.photoId)+ if (ref.storageKey) storageKeys.add(ref.storageKey)+ }++ const photoIdList = [...photoIds]+ const storageKeyList = [...storageKeys]+ const photoFilters = [+ ...(photoIdList.length > 0 ? [inArray(photos.id, photoIdList)] : []),+ ...(storageKeyList.length > 0+ ? [inArray(photos.storageKey, storageKeyList)]+ : []),+ ]++ const relatedPhotos =+ photoFilters.length > 0+ ? await db+ .select({+ id: photos.id,+ title: photos.title,+ thumbnailUrl: photos.thumbnailUrl,+ thumbnailHash: photos.thumbnailHash,+ originalUrl: photos.originalUrl,+ storageKey: photos.storageKey,+ })+ .from(photos)+ .where(or(...photoFilters))+ .all()+ : []++ const photosById = new Map(relatedPhotos.map((photo) => [photo.id, photo]))+ const photosByStorageKey = new Map(+ relatedPhotos+ .filter((photo) => photo.storageKey)+ .map((photo) => [photo.storageKey!, photo]),+ )++ const tasks = recentTasks.map((task) => {+ const ref = getTaskPhotoRef(task.payload)+ const photo =+ (ref.photoId && photosById.get(ref.photoId)) ||+ (ref.storageKey && photosByStorageKey.get(ref.storageKey)) ||+ null++ const storageKey =+ photo?.storageKey ||+ ('storageKey' in task.payload ? task.payload.storageKey : null) ||+ null++ return {+ id: task.id,+ type: task.payload.type,+ status: task.status,+ statusStage: task.statusStage,+ errorMessage: task.errorMessage,+ createdAt: task.createdAt,+ completedAt: task.completedAt,+ fileName: getFileName(storageKey),+ photo: photo+ ? {+ id: photo.id,+ title: photo.title,+ thumbnailUrl: photo.thumbnailUrl,+ thumbnailHash: photo.thumbnailHash,+ originalUrl: photo.originalUrl,+ }+ : null,+ }+ })++ return {+ tasks,+ timestamp: new Date().toISOString(),+ }+})