fix: avoid dashboard preview crash after registration

This commit is contained in:
yun-zhi-ztl 2026-03-17 14:53:26 +08:00
parent 4938bc3b82
commit beff78512a
3 changed files with 14 additions and 4 deletions

View file

@ -25,4 +25,12 @@ describe('limitPreviewItems', () => {
remainingCount: 1,
})
})
it('returns an empty preview instead of throwing when input is not an array', () => {
expect(limitPreviewItems({ items: ['a', 'b'] } as never, 3)).toEqual({
items: [],
hasMore: false,
remainingCount: 0,
})
})
})

View file

@ -1,10 +1,11 @@
export function limitPreviewItems<T>(items: T[], limit: number): {
export function limitPreviewItems<T>(items: T[] | null | undefined | unknown, limit: number): {
items: T[]
hasMore: boolean
remainingCount: number
} {
const visibleItems = items.slice(0, limit)
const remainingCount = Math.max(items.length - visibleItems.length, 0)
const normalizedItems: T[] = Array.isArray(items) ? (items as T[]) : []
const visibleItems = normalizedItems.slice(0, limit)
const remainingCount = Math.max(normalizedItems.length - visibleItems.length, 0)
return {
items: visibleItems,

View file

@ -1,6 +1,7 @@
import { Link } from '@tanstack/react-router'
import { useTranslation } from 'react-i18next'
import { useAuth } from '@/features/auth/use-auth'
import type { SkillSummary } from '@/api/types'
import { useMySkills } from '@/shared/hooks/use-skill-queries'
import { TokenList } from '@/features/token/token-list'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/shared/ui/card'
@ -14,7 +15,7 @@ export function DashboardPage() {
const { user, hasRole } = useAuth()
const governanceVisible = hasRole('SKILL_ADMIN') || hasRole('SUPER_ADMIN')
const { data: skillPage, isLoading: isLoadingSkills } = useMySkills({ page: 0, size: skillPreviewPageSize })
const skillPreview = limitPreviewItems(skillPage?.items ?? [], DASHBOARD_PREVIEW_LIMIT)
const skillPreview = limitPreviewItems<SkillSummary>(skillPage?.items ?? [], DASHBOARD_PREVIEW_LIMIT)
return (
<div className="space-y-8 animate-fade-up">