Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(photos): PhotosProvider 中新增刷新和状态

Timothy Yina1155002190a
diff --git a/app/app.vue b/app/app.vue
index 4c727267..d5934281 100644
--- a/app/app.vue
+++ b/app/app.vue
@@ -9,7 +9,7 @@ useHead({
titleTemplate: (title) => `${title} :: ${config.public.APP_TITLE}`,
})
-const { data } = useFetch('/api/photos')
+const { data, refresh, status } = useFetch('/api/photos')
const photos = computed(() => (data.value as Photo[]) || [])
watchEffect(() => {
@@ -35,7 +35,11 @@ provide(
<template>
<UApp>
- <PhotosProvider :photos="photos">
+ <PhotosProvider
+ :photos="photos"
+ :refresh="refresh"
+ :status="status"
+ >
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
diff --git a/app/components/PhotosProvider.vue b/app/components/PhotosProvider.vue
index 1f176fc6..3107474c 100644
--- a/app/components/PhotosProvider.vue
+++ b/app/components/PhotosProvider.vue
@@ -1,16 +1,20 @@
<script lang="ts" setup>
+import type { AsyncDataRequestStatus } from '#app'
+
const props = defineProps<{
photos: Photo[]
+ refresh: () => Promise<void>
+ status: AsyncDataRequestStatus
}>()
const photosRef = toRef(props, 'photos')
-providePhotos(photosRef)
+const status = toRef(props, 'status')
+const refresh = props.refresh
+providePhotos(photosRef, status, refresh)
</script>
<template>
<slot />
</template>
-<style scoped>
-
-</style>
+<style scoped></style>
diff --git a/app/composables/usePhotos.ts b/app/composables/usePhotos.ts
index 6979de5b..58191014 100644
--- a/app/composables/usePhotos.ts
+++ b/app/composables/usePhotos.ts
@@ -1,5 +1,9 @@
+import type { AsyncDataRequestStatus } from "#app"
+
interface PhotosContext {
photos: Ref<Photo[]>
+ status: Ref<AsyncDataRequestStatus>
+ refresh: () => Promise<void>
getPhotoById: (id: string) => Photo | undefined
filterPhotos: (predicate: (photo: Photo) => boolean) => Photo[]
totalCount: ComputedRef<number>
@@ -7,29 +11,35 @@ interface PhotosContext {
const PhotosContextKey = Symbol('PhotosContext') as InjectionKey<PhotosContext>
-export function providePhotos(photos: Ref<Photo[]>) {
+export function providePhotos(
+ photos: Ref<Photo[]>,
+ status: Ref<AsyncDataRequestStatus>,
+ refresh: () => Promise<void>,
+) {
const context: PhotosContext = {
photos,
+ status,
+ refresh,
getPhotoById: (id: string) => {
- return photos.value.find(photo => photo.id === id)
+ return photos.value.find((photo) => photo.id === id)
},
filterPhotos: (predicate: (photo: Photo) => boolean) => {
return photos.value.filter(predicate)
},
- totalCount: computed(() => photos.value.length)
+ totalCount: computed(() => photos.value.length),
}
provide(PhotosContextKey, context)
-
+
return context
}
export function usePhotos(): PhotosContext {
const context = inject(PhotosContextKey)
-
+
if (!context) {
throw new Error('usePhotos must be used within a PhotosProvider')
}
-
+
return context
}
diff --git a/app/pages/dashboard/photos.vue b/app/pages/dashboard/photos.vue
index 03916f42..a1af0631 100644
--- a/app/pages/dashboard/photos.vue
+++ b/app/pages/dashboard/photos.vue
@@ -18,7 +18,7 @@ useHead({
const dayjs = useDayjs()
const { localizeExif } = useExifLocalization()
-const { data, status, refresh } = useFetch('/api/photos')
+const { photos: data, status, refresh } = usePhotos()
// 当前上传的文件信息
interface UploadingFile {