From 29e2eb536f616011b2c1b3574eca21807640d9cb Mon Sep 17 00:00:00 2001 From: keven1024 Date: Fri, 16 May 2025 08:10:42 +0800 Subject: [PATCH] refactor(front): update FileShareView and [id].vue to utilize @tanstack/vue-query for data fetching and state management --- front/components/Share/FileShareView.vue | 5 +++- front/pages/s/[id].vue | 33 +++++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/front/components/Share/FileShareView.vue b/front/components/Share/FileShareView.vue index bd33524..01c1a2f 100644 --- a/front/components/Share/FileShareView.vue +++ b/front/components/Share/FileShareView.vue @@ -5,7 +5,7 @@ import duration from 'dayjs/plugin/duration'; import relativeTime from 'dayjs/plugin/relativeTime'; import { isBoolean } from 'lodash-es'; import { LucideCheck, LucideX } from 'lucide-vue-next'; - +import { useQueryClient } from '@tanstack/vue-query'; dayjs.extend(duration) dayjs.extend(relativeTime) @@ -13,6 +13,8 @@ const props = defineProps<{ data: any }>() +const queryClient = useQueryClient() + const handleDownload = async () => { const { id } = props?.data || {} const data = await $fetch<{ @@ -31,6 +33,7 @@ const handleDownload = async () => { return } (window as any)?.open(`/api/download?token=${token}`) + queryClient.invalidateQueries({ queryKey: ['share', id] }) } const expireSeconds = computed(() => { diff --git a/front/pages/s/[id].vue b/front/pages/s/[id].vue index bb3dae3..86c2370 100644 --- a/front/pages/s/[id].vue +++ b/front/pages/s/[id].vue @@ -5,25 +5,28 @@ import { Skeleton } from '@/components/ui/skeleton' import dayjs from 'dayjs' import FileShareView from '@/components/Share/FileShareView.vue' import TextShareView from '@/components/Share/TextShareView.vue' +import { useQuery } from '@tanstack/vue-query' const route = useRoute() const router = useRouter() - const id = computed(() => route.params.id) -const { state, isLoading } = useAsyncState(async () => { - const data = await $fetch<{ - code: number - data: { - id?: string - expire_at?: number - } - }>(`/api/share/${id.value}`) - return data?.data -}, null) +const { data, isLoading } = useQuery({ + queryKey: ['share', id.value], + queryFn: async () => { + const data = await $fetch<{ + code: number + data: { + id?: string + expire_at?: number + } + }>(`/api/share/${id.value}`) + return data?.data + } +}) const isExpired = computed(() => { - const { expire_at } = state.value || {} - return !state || !expire_at || dayjs(expire_at * 10e2).isBefore(dayjs()) + const { expire_at } = data.value || {} + return !data || !expire_at || dayjs(expire_at * 10e2).isBefore(dayjs()) }) @@ -48,8 +51,8 @@ const isExpired = computed(() => { }">返回首页