|
232 | 232 | const hasSpaces = computed(() => spaces.value?.data?.length > 0) |
233 | 233 | const hasMcps = computed(() => mcps.value?.data?.length > 0) |
234 | 234 |
|
235 | | - // 根据activeTab和是否有更多数据来决定显示数量 |
236 | 235 | const displayedCollections = computed(() => { |
237 | 236 | const data = collections.value?.data || [] |
238 | | - // 在"全部"tab下,如果已经展开则显示所有数据,否则只显示前6个 |
239 | | - if (activeTab.value === '') { |
240 | | - return expandedSections.value.collections ? data : data.slice(0, INITIAL_PER_PAGE) |
241 | | - } |
242 | | - // 在特定tab下,如果已经展开则显示所有数据,否则只显示前6个 |
243 | | - if (activeTab.value === 'collections' && expandedSections.value.collections) { |
| 237 | + if (data.length > INITIAL_PER_PAGE) { |
244 | 238 | return data |
245 | 239 | } |
246 | 240 | return data.slice(0, INITIAL_PER_PAGE) |
247 | 241 | }) |
248 | 242 |
|
249 | 243 | const displayedModels = computed(() => { |
250 | 244 | const data = models.value?.data || [] |
251 | | - if (activeTab.value === '') { |
252 | | - return expandedSections.value.models ? data : data.slice(0, INITIAL_PER_PAGE) |
253 | | - } |
254 | | - if (activeTab.value === 'models' && expandedSections.value.models) { |
| 245 | + if (data.length > INITIAL_PER_PAGE) { |
255 | 246 | return data |
256 | 247 | } |
257 | 248 | return data.slice(0, INITIAL_PER_PAGE) |
258 | 249 | }) |
259 | 250 |
|
260 | 251 | const displayedDatasets = computed(() => { |
261 | 252 | const data = datasets.value?.data || [] |
262 | | - if (activeTab.value === '') { |
263 | | - return expandedSections.value.datasets ? data : data.slice(0, INITIAL_PER_PAGE) |
264 | | - } |
265 | | - if (activeTab.value === 'datasets' && expandedSections.value.datasets) { |
| 253 | + if (data.length > INITIAL_PER_PAGE) { |
266 | 254 | return data |
267 | 255 | } |
268 | 256 | return data.slice(0, INITIAL_PER_PAGE) |
269 | 257 | }) |
270 | 258 |
|
271 | 259 | const displayedCodes = computed(() => { |
272 | 260 | const data = codes.value?.data || [] |
273 | | - if (activeTab.value === '') { |
274 | | - return expandedSections.value.codes ? data : data.slice(0, INITIAL_PER_PAGE) |
275 | | - } |
276 | | - if (activeTab.value === 'codes' && expandedSections.value.codes) { |
| 261 | + if (data.length > INITIAL_PER_PAGE) { |
277 | 262 | return data |
278 | 263 | } |
279 | 264 | return data.slice(0, INITIAL_PER_PAGE) |
280 | 265 | }) |
281 | 266 |
|
282 | 267 | const displayedPrompts = computed(() => { |
283 | 268 | const data = prompts.value?.data || [] |
284 | | - if (activeTab.value === '') { |
285 | | - return expandedSections.value.prompts ? data : data.slice(0, INITIAL_PER_PAGE) |
286 | | - } |
287 | | - if (activeTab.value === 'prompts' && expandedSections.value.prompts) { |
| 269 | + if (data.length > INITIAL_PER_PAGE) { |
288 | 270 | return data |
289 | 271 | } |
290 | 272 | return data.slice(0, INITIAL_PER_PAGE) |
291 | 273 | }) |
292 | 274 |
|
293 | 275 | const displayedSpaces = computed(() => { |
294 | 276 | const data = spaces.value?.data || [] |
295 | | - if (activeTab.value === '') { |
296 | | - return expandedSections.value.spaces ? data : data.slice(0, INITIAL_PER_PAGE) |
297 | | - } |
298 | | - if (activeTab.value === 'spaces' && expandedSections.value.spaces) { |
| 277 | + if (data.length > INITIAL_PER_PAGE) { |
299 | 278 | return data |
300 | 279 | } |
301 | 280 | return data.slice(0, INITIAL_PER_PAGE) |
302 | 281 | }) |
303 | 282 |
|
304 | 283 | const displayedMcps = computed(() => { |
305 | 284 | const data = mcps.value?.data || [] |
306 | | - if (activeTab.value === '') { |
307 | | - return expandedSections.value.mcps ? data : data.slice(0, INITIAL_PER_PAGE) |
308 | | - } |
309 | | - if (activeTab.value === 'mcps' && expandedSections.value.mcps) { |
| 285 | + if (data.length > INITIAL_PER_PAGE) { |
310 | 286 | return data |
311 | 287 | } |
312 | 288 | return data.slice(0, INITIAL_PER_PAGE) |
|
328 | 304 | lastFetchTime.value = Date.now() |
329 | 305 | |
330 | 306 | try { |
331 | | - const timestamp = Date.now() |
332 | | - const collectionsUrl = `${reposUrl("collections")}?_t=${timestamp}` |
333 | | - const modelsUrl = `${reposUrl("models")}?_t=${timestamp}` |
334 | | - const datasetsUrl = `${reposUrl("datasets")}?_t=${timestamp}` |
335 | | - const spacesUrl = `${reposUrl("spaces")}?_t=${timestamp}` |
336 | | - const codesUrl = `${reposUrl("codes")}?_t=${timestamp}` |
337 | | - const promptsUrl = `${reposUrl("prompts")}?_t=${timestamp}` |
338 | | - const mcpsUrl = `${reposUrl("mcps")}?_t=${timestamp}` |
| 307 | + const collectionsUrl = `${reposUrl("collections")}` |
| 308 | + const modelsUrl = `${reposUrl("models")}` |
| 309 | + const datasetsUrl = `${reposUrl("datasets")}` |
| 310 | + const spacesUrl = `${reposUrl("spaces")}` |
| 311 | + const codesUrl = `${reposUrl("codes")}` |
| 312 | + const promptsUrl = `${reposUrl("prompts")}` |
| 313 | + const mcpsUrl = `${reposUrl("mcps")}` |
339 | 314 |
|
340 | 315 | const promises = [ |
341 | 316 | fetchData(collectionsUrl, collections, INITIAL_PER_PAGE, 1), |
|
355 | 330 | } |
356 | 331 |
|
357 | 332 | const viewMoreTargets = async (target) => { |
358 | | - // 标记该类型已展开 |
359 | | - expandedSections.value[target] = true |
360 | | -
|
361 | 333 | switch (target) { |
362 | 334 | case "collections": |
363 | 335 | collectionsLoading.value = true |
364 | 336 | await fetchMoreCollections() |
365 | 337 | collectionsLoading.value = false |
| 338 | + // 只有当没有更多数据时才标记为已展开 |
| 339 | + if (!collections.value.more) { |
| 340 | + expandedSections.value.collections = true |
| 341 | + } |
366 | 342 | break |
367 | 343 | case "models": |
368 | 344 | modelsLoading.value = true |
369 | 345 | await fetchMoreModels() |
370 | 346 | modelsLoading.value = false |
| 347 | + if (!models.value.more) { |
| 348 | + expandedSections.value.models = true |
| 349 | + } |
371 | 350 | break |
372 | 351 | case "datasets": |
373 | 352 | datasetsLoading.value = true |
374 | 353 | await fetchMoreDatasets() |
375 | 354 | datasetsLoading.value = false |
| 355 | + if (!datasets.value.more) { |
| 356 | + expandedSections.value.datasets = true |
| 357 | + } |
376 | 358 | break |
377 | 359 | case "spaces": |
378 | 360 | spacesLoading.value = true |
379 | 361 | await fetchMoreSpaces() |
380 | 362 | spacesLoading.value = false |
| 363 | + if (!spaces.value.more) { |
| 364 | + expandedSections.value.spaces = true |
| 365 | + } |
381 | 366 | break |
382 | 367 | case "codes": |
383 | 368 | codeLoading.value = true |
384 | 369 | await fetchMoreCodes() |
385 | 370 | codeLoading.value = false |
| 371 | + if (!codes.value.more) { |
| 372 | + expandedSections.value.codes = true |
| 373 | + } |
386 | 374 | break |
387 | 375 | case "prompts": |
388 | 376 | promptsLoading.value = true |
389 | 377 | await fetchMorePrompts() |
390 | 378 | promptsLoading.value = false |
| 379 | + if (!prompts.value.more) { |
| 380 | + expandedSections.value.prompts = true |
| 381 | + } |
391 | 382 | break |
392 | 383 | case "mcps": |
393 | 384 | mcpsLoading.value = true |
394 | 385 | await fetchMoreMcp() |
395 | 386 | mcpsLoading.value = false |
| 387 | + if (!mcps.value.more) { |
| 388 | + expandedSections.value.mcps = true |
| 389 | + } |
396 | 390 | break |
397 | 391 | } |
398 | 392 | } |
|
407 | 401 | } |
408 | 402 | } |
409 | 403 |
|
410 | | - const updatePageAndData = (response, targetRef, pageRef) => { |
411 | | - if (!targetRef.value.data || pageRef.value === 1) { |
412 | | - targetRef.value.data = [] |
413 | | - } |
| 404 | + const updatePageAndData = (response, targetRef, pageRef, isFirstExpand = false) => { |
414 | 405 | const addData = targetRef === mcps ? response.data.data : response.data |
415 | | - targetRef.value.data = [...targetRef.value.data, ...addData] |
416 | 406 | const repoTotal = targetRef === mcps ? response.data.total : response.total |
| 407 | + |
| 408 | + if (isFirstExpand) { |
| 409 | + targetRef.value.data = addData |
| 410 | + pageRef.value = 1 |
| 411 | + } else { |
| 412 | + targetRef.value.data = [...targetRef.value.data, ...addData] |
| 413 | + } |
| 414 | + |
417 | 415 | targetRef.value.total = repoTotal |
418 | | -
|
419 | 416 | const loadedCount = targetRef.value.data.length |
420 | 417 | const hasMore = loadedCount < repoTotal |
421 | 418 | targetRef.value.more = hasMore |
|
430 | 427 | params.append("per", perPage) |
431 | 428 | params.append("page", page) |
432 | 429 |
|
| 430 | + const timestamp = Date.now() |
| 431 | +
|
433 | 432 | try { |
434 | | - const { data, error } = await useFetchApi(`${url}?${params}`).json() |
| 433 | + const { data, error } = await useFetchApi(`${url}?${params}&_t=${timestamp}`).json() |
435 | 434 |
|
436 | 435 | if (error.value) { |
437 | 436 | ElMessage({ |
|
452 | 451 | targetRef.value.more = data.value.total > perPage |
453 | 452 | } |
454 | 453 | } else { |
455 | | - updatePageAndData(data.value, targetRef, getPageRef(targetRef)) |
| 454 | + const pageRef = getPageRef(targetRef) |
| 455 | + const isFirstExpand = pageRef.value === 1 && perPage === PER_PAGE |
| 456 | + updatePageAndData(data.value, targetRef, pageRef, isFirstExpand) |
456 | 457 | } |
457 | 458 |
|
458 | 459 | return data.value |
|
475 | 476 | const fetchMore = async (targetRef, type) => { |
476 | 477 | const pageRef = getPageRef(targetRef) |
477 | 478 | const url = reposUrl(type) |
478 | | - await fetchData(url, targetRef, PER_PAGE, pageRef.value) |
| 479 | + const currentLoadedCount = targetRef.value.data?.length || 0 |
| 480 | +
|
| 481 | + if (currentLoadedCount <= INITIAL_PER_PAGE) { |
| 482 | + await fetchData(url, targetRef, PER_PAGE, 1) |
| 483 | + } else { |
| 484 | + await fetchData(url, targetRef, PER_PAGE, pageRef.value) |
| 485 | + } |
479 | 486 | } |
480 | 487 |
|
481 | 488 | const fetchMoreCollections = () => fetchMore(collections, "collections") |
|
487 | 494 | const fetchMoreMcp = () => fetchMore(mcps, "mcps") |
488 | 495 |
|
489 | 496 |
|
490 | | - // 添加处理浏览器前进后退的函数 |
491 | 497 | const handlePopState = () => { |
492 | 498 | if (isLoading.value) { |
493 | 499 | return |
|
0 commit comments