import { client } from '@/ui/api/client'
import { FolderTree } from '@/ui/components/folder-tree'
import { ResizeHandle } from '@/ui/components/resize-handle'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/ui/components/ui/table'
import { Avatar, AvatarImage, AvatarFallback } from '@/ui/components/ui/avatar'
import { useUiStore } from '@/ui/stores/ui'
import { useTeamContextStore } from '@/ui/stores/team-context'
import { useInfiniteQuery, useSuspenseQuery } from '@tanstack/react-query'
import { createFileRoute, useNavigate } from '@tanstack/react-router'
import { useEffect, useMemo, useState } from 'react'
import { Bookmark, Calendar, LayoutGrid, Loader2 } from 'lucide-react'
import { useInView } from 'react-intersection-observer'
import type { CollectionInfo } from '@shumai/dtos'
import { ProjectFolderSkeleton } from '@/ui/components/loading-skeletons'
const getInitials = (name?: string) => {
if (!name) return '??'
return name
.split(' ')
.map((n) => n[0])
.slice(0, 2)
.join('')
.toUpperCase()
}
const projectInfoQueryOptions = (projectId: string) => ({
queryKey: ['projects', projectId],
queryFn: async () => {
const res = await client.api.projects[':projectId'].$get({
param: { projectId },
})
if (!res.ok) throw new Error('Failed to fetch project')
return await res.json()
},
})
function CollectionsPage() {
const { projectId } = Route.useParams()
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
const navigate = useNavigate()
useEffect(() => {
ensureTeamIdForProject(projectId)
}, [projectId, ensureTeamIdForProject])
const { data: projectInfo } = useSuspenseQuery(projectInfoQueryOptions(projectId))
const rootFolderId = projectInfo?.rootFolder
const [leftSidebarWidth, setLeftSidebarWidth] = useState(240)
const {
fileListLeftSidebarCollapsed: isLeftSidebarCollapsed,
setFileListLeftSidebarCollapsed: setIsLeftSidebarCollapsed,
} = useUiStore()
const {
data: collectionsData,
isLoading: isCollectionsLoading,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
} = useInfiniteQuery({
queryKey: ['collections', projectId],
queryFn: async ({ pageParam }) => {
const res = await client.api.projects[':projectId'].collections.$get({
param: { projectId },
query: { first: '20', after: pageParam as string },
})
if (!res.ok) throw new Error('Failed to fetch collections')
return (await res.json()) as unknown as {
data: CollectionInfo[]
pageInfo: { cursor?: string; total?: number }
}
},
initialPageParam: '',
getNextPageParam: (lastPage) => lastPage.pageInfo?.cursor || undefined,
enabled: !!projectId,
})
const { ref: inViewRef, inView } = useInView()
useEffect(() => {
if (inView && hasNextPage && !isFetchingNextPage) {
fetchNextPage()
}
}, [inView, hasNextPage, isFetchingNextPage, fetchNextPage])
const collections = useMemo(
() => collectionsData?.pages.flatMap((p) => p.data) || [],
[collectionsData],
)
if (!teamId || !rootFolderId || !projectInfo) {
return
No collections created yet.