Skip to content

Commit c0db9fb

Browse files
ice201508jiuling
andauthored
fix-update-profile-likes-status (#1272)
* Draft MR * fix-update-profile-likes-status --------- Co-authored-by: jiuling <[email protected]>
1 parent a4055d9 commit c0db9fb

File tree

2 files changed

+102
-21
lines changed

2 files changed

+102
-21
lines changed

frontend/src/components/shared/ProfileRepoList.vue

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@
114114
</template>
115115

116116
<script setup>
117-
import { computed, ref, onMounted } from "vue"
117+
import { computed, ref, onMounted, watch, onUnmounted } from "vue"
118+
import { useRoute } from 'vue-router'
118119
import RepoItem from "./RepoItem.vue"
119120
import CollectionCards from "../collections/CollectionCards.vue"
120121
import ApplicationSpaceItem from "../application_spaces/ApplicationSpaceItem.vue"
@@ -128,6 +129,7 @@
128129
initiator: String
129130
})
130131
132+
const route = useRoute()
131133
const collections = ref({})
132134
const models = ref({})
133135
const datasets = ref({})
@@ -161,23 +163,43 @@
161163
const prefixPath =
162164
document.location.pathname.split("/")[1] === "organizations" ? "organization" : "user"
163165
166+
const isLoading = ref(false)
167+
const lastFetchTime = ref(0)
168+
const FETCH_DEBOUNCE_TIME = 1000
169+
164170
const getProfileRepoData = async () => {
165-
const collectionsUrl = reposUrl("collections")
166-
const modelsUrl = reposUrl("models")
167-
const datasetsUrl = reposUrl("datasets")
168-
const spacesUrl = reposUrl("spaces")
169-
const codesUrl = reposUrl("codes")
170-
const mcpsUrl = reposUrl("mcps")
171-
172-
const promises = [
173-
fetchData(collectionsUrl, collections, INITIAL_PER_PAGE, 1),
174-
fetchData(modelsUrl, models, INITIAL_PER_PAGE, 1),
175-
fetchData(datasetsUrl, datasets, INITIAL_PER_PAGE, 1),
176-
fetchData(spacesUrl, spaces, INITIAL_PER_PAGE, 1),
177-
fetchData(codesUrl, codes, INITIAL_PER_PAGE, 1),
178-
fetchData(mcpsUrl, mcps, INITIAL_PER_PAGE, 1),
179-
]
180-
await Promise.all(promises)
171+
// 防止重复请求
172+
if (isLoading.value) {
173+
return
174+
}
175+
176+
isLoading.value = true
177+
lastFetchTime.value = Date.now()
178+
179+
try {
180+
const collectionsUrl = reposUrl("collections")
181+
const modelsUrl = reposUrl("models")
182+
const datasetsUrl = reposUrl("datasets")
183+
const spacesUrl = reposUrl("spaces")
184+
const codesUrl = reposUrl("codes")
185+
const promptsUrl = reposUrl("prompts")
186+
const mcpsUrl = reposUrl("mcps")
187+
188+
const promises = [
189+
fetchData(collectionsUrl, collections, INITIAL_PER_PAGE, 1),
190+
fetchData(modelsUrl, models, INITIAL_PER_PAGE, 1),
191+
fetchData(datasetsUrl, datasets, INITIAL_PER_PAGE, 1),
192+
fetchData(spacesUrl, spaces, INITIAL_PER_PAGE, 1),
193+
fetchData(codesUrl, codes, INITIAL_PER_PAGE, 1),
194+
fetchData(mcpsUrl, mcps, INITIAL_PER_PAGE, 1),
195+
]
196+
if (props.initiator === "profile") {
197+
promises.push(fetchData(promptsUrl, prompts, INITIAL_PER_PAGE, 1))
198+
}
199+
await Promise.all(promises)
200+
} finally {
201+
isLoading.value = false
202+
}
181203
}
182204
183205
const viewMoreTargets = async (target) => {
@@ -304,5 +326,27 @@
304326
305327
onMounted(() => {
306328
getProfileRepoData()
329+
330+
document.addEventListener('visibilitychange', handleVisibilityChange)
307331
})
332+
333+
onUnmounted(() => {
334+
document.removeEventListener('visibilitychange', handleVisibilityChange)
335+
})
336+
337+
const handleVisibilityChange = () => {
338+
if (!document.hidden) {
339+
if (isLoading.value) {
340+
return
341+
}
342+
343+
const now = Date.now()
344+
if (now - lastFetchTime.value < FETCH_DEBOUNCE_TIME) {
345+
return
346+
}
347+
348+
// 重新获取数据
349+
getProfileRepoData()
350+
}
351+
}
308352
</script>

frontend/src/components/shared/RepoDetail.vue

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</template>
3737

3838
<script setup>
39-
import { onMounted, computed, provide, ref, watch } from 'vue'
39+
import { onMounted, computed, provide, ref, watch, onUnmounted } from 'vue'
4040
import RepoHeader from '../shared/RepoHeader.vue'
4141
import RepoTabs from '../shared/RepoTabs.vue'
4242
import useRepoDetailStore from '../../stores/RepoDetailStore'
@@ -67,6 +67,11 @@
6767
const { isInitialized } = storeToRefs(repoDetailStore)
6868
const lastCommit = ref({})
6969
70+
// 添加防抖相关状态
71+
const isLoading = ref(false)
72+
const lastFetchTime = ref(0)
73+
const FETCH_DEBOUNCE_TIME = 1000 // 1秒防抖
74+
7075
// const repo = ref({})
7176
// const tags = ref({
7277
// task_tags: [],
@@ -101,6 +106,14 @@
101106
})
102107
103108
const fetchRepoDetail = async () => {
109+
// 防止重复请求
110+
if (isLoading.value) {
111+
return
112+
}
113+
114+
isLoading.value = true
115+
lastFetchTime.value = Date.now()
116+
104117
const url = `/${props.repoType}s/${props.namespace}/${props.repoName}`
105118
106119
try {
@@ -124,6 +137,8 @@
124137
// ownerUrl.value = getOwnerUrl(repoData)
125138
} catch (error) {
126139
console.error('Failed to fetch repo detail:', error)
140+
} finally {
141+
isLoading.value = false
127142
}
128143
}
129144
@@ -169,11 +184,28 @@
169184
}
170185
}
171186
187+
const handleVisibilityChange = () => {
188+
if (!document.hidden) {
189+
// 检查是否正在加载
190+
if (isLoading.value) {
191+
return
192+
}
193+
194+
// 检查距离上次请求的时间
195+
const now = Date.now()
196+
if (now - lastFetchTime.value < FETCH_DEBOUNCE_TIME) {
197+
return
198+
}
199+
200+
// 重新获取数据
201+
fetchRepoDetail()
202+
fetchLastCommit()
203+
}
204+
}
205+
172206
onMounted(() => {
173-
// 1. 从URL参数获取状态
174207
const urlParams = getUrlParams()
175208
176-
// 2. 初始化store,优先使用URL参数
177209
const initialData = {
178210
repoType: props.repoType,
179211
namespace: props.namespace,
@@ -186,9 +218,14 @@
186218
187219
setRepoTab(initialData)
188220
189-
// 3. 获取数据
190221
fetchRepoDetail()
191222
fetchLastCommit()
223+
224+
document.addEventListener('visibilitychange', handleVisibilityChange)
225+
})
226+
227+
onUnmounted(() => {
228+
document.removeEventListener('visibilitychange', handleVisibilityChange)
192229
})
193230
194231
provide('fetchRepoDetail', fetchRepoDetail)

0 commit comments

Comments
 (0)