import { client } from '@/ui/api/client' import { FolderTree } from '@/ui/components/folder-tree' import { ResizeHandle } from '@/ui/components/resize-handle' import { Switch } from '@/ui/components/ui/switch' import { Button } from '@/ui/components/ui/button' 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 { useMutation, useInfiniteQuery, useQueryClient, useSuspenseQuery, } from '@tanstack/react-query' import { createFileRoute, useNavigate } from '@tanstack/react-router' import { useEffect, useMemo, useState } from 'react' import { Copy, ExternalLink, Calendar, Link2, Share2, Loader2 } from 'lucide-react' import { useInView } from 'react-intersection-observer' import { toast } from 'sonner' import type { ShareLinkInfo } 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 SharesPage() { const { projectId } = Route.useParams() const { teamId, ensureTeamIdForProject } = useTeamContextStore() const queryClient = useQueryClient() 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: sharesData, isLoading: isSharesLoading, fetchNextPage, hasNextPage, isFetchingNextPage, } = useInfiniteQuery({ queryKey: ['shares', projectId], queryFn: async ({ pageParam }) => { const res = await client.api.projects[':projectId'].shares.$get({ param: { projectId }, query: { first: '20', after: pageParam as string }, }) if (!res.ok) throw new Error('Failed to fetch share links') return (await res.json()) as unknown as { data: ShareLinkInfo[] 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 shares = useMemo(() => sharesData?.pages.flatMap((p) => p.data) || [], [sharesData]) const { mutate: toggleVisibility } = useMutation({ mutationFn: async ({ id, isDisabled }: { id: string; isDisabled: boolean }) => { const res = await client.api.shares[':shareId'].$put({ param: { shareId: id }, json: { isDisabled }, }) if (!res.ok) throw new Error('Failed to update share link') return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['shares', projectId] }) toast.success('Share link status updated') }, onError: (err) => { toast.error(`Error: ${err.message}`) }, }) if (!teamId || !rootFolderId || !projectInfo) { return } const handleCopyLink = (shareId: string) => { const url = `${window.location.origin}/share/${shareId}` navigator.clipboard.writeText(url) toast.success('Link copied to clipboard') } return (
{!isLeftSidebarCollapsed && (
setIsLeftSidebarCollapsed(true)} /> )} {!isLeftSidebarCollapsed && ( <>
{ setLeftSidebarWidth((prev) => Math.max(180, Math.min(400, prev + delta))) }} className="hidden md:block" /> )} {/* Main Content Area */}

All Share Links

{isSharesLoading ? (
) : shares.length === 0 ? (

No share links created yet.

) : (
Name Public Link Creator Created Date Active {shares.map((share) => { const shareUrl = `${window.location.origin}/share/${share.id}` return ( navigate({ to: '/projects/$projectId/shares/$shareId', params: { projectId, shareId: share.id }, }) } > {share.name}
handleCopyLink(share.id)} title="Click to copy link" > {shareUrl}
{share.creator?.image && ( )} {getInitials(share.creator?.name)} {share.creator?.name || 'Unknown'}
{new Date(share.createdAt).toLocaleDateString()}
toggleVisibility({ id: share.id, isDisabled: !checked }) } />
) })}
{isFetchingNextPage && }
)}
) } export const Route = createFileRoute('/projects/$projectId/shares/')({ component: SharesPage, loader: ({ context: { queryClient }, params: { projectId } }) => queryClient.ensureQueryData(projectInfoQueryOptions(projectId)), pendingComponent: () => , })