import { useEffect, useMemo } from 'react' import { useInfiniteQuery } from '@tanstack/react-query' import { useDroppable, useDragOperation } from '@dnd-kit/react' import { useInView } from 'react-intersection-observer' import { client } from '@/ui/api/client' import { cn } from '@/ui/lib/utils' import { m } from '@/ui/paraglide/messages.js' import { Button } from '@/ui/components/ui/button' import { ScrollArea } from '@/ui/components/ui/scroll-area' import { KanbanTaskStatus, type KanbanTaskInfo, type ListKanbanTasksResponse } from '@shumai/dtos' import { getStatusColor, getStatusLabel } from './kanban-types' import { KanbanCard } from './kanban-card' import { Plus, Loader2 } from 'lucide-react' interface KanbanColumnProps { teamId: string status: KanbanTaskStatus selectedGoalId: string | null scope: 'team' | 'my' currentUserId?: string currentUserRole?: string onTaskClick: (task: KanbanTaskInfo) => void onDeleteTask?: (task: KanbanTaskInfo) => void onCreateTaskInColumn: (status: KanbanTaskStatus) => void } export function KanbanColumn({ teamId, status, selectedGoalId, scope, currentUserId, currentUserRole, onTaskClick, onDeleteTask, onCreateTaskInColumn, }: KanbanColumnProps) { const isOwnerOrEditor = currentUserRole?.toLowerCase() === 'owner' || currentUserRole?.toLowerCase() === 'editor' const assigneeId = scope === 'my' ? currentUserId : undefined const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = useInfiniteQuery({ queryKey: ['teams', teamId, 'kanban', 'tasks', { status, goalId: selectedGoalId, assigneeId }], queryFn: async ({ pageParam = '' }) => { const res = await client.api.teams[':teamId'].kanban.tasks.$get({ param: { teamId }, query: { status, goalId: selectedGoalId || undefined, assigneeId: assigneeId || undefined, first: '20', after: (pageParam as string) || undefined, }, }) if (!res.ok) throw new Error('Failed to fetch column tasks') return (await res.json()) as unknown as ListKanbanTasksResponse }, initialPageParam: '', getNextPageParam: (lastPage) => lastPage.pageInfo?.cursor || undefined, refetchInterval: 5000, enabled: !!teamId, }) const { ref: loadMoreRef, inView } = useInView({ threshold: 0.1, }) useEffect(() => { if (inView && hasNextPage && !isFetchingNextPage) { fetchNextPage() } }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]) const allTasks = useMemo(() => { return data?.pages?.flatMap((page) => (Array.isArray(page?.data) ? page.data : [])) || [] }, [data]) const { source, target } = useDragOperation() const lastVisibleTask = allTasks.filter((t) => t.id !== source?.id).at(-1) const { ref: setDroppableRef, isDropTarget } = useDroppable({ id: status, data: { type: 'kanban_column', status, lastTask: lastVisibleTask, }, }) const isDraggingTask = source?.data?.type === 'kanban_task' const isOverThisColumn = isDraggingTask && (isDropTarget || (target?.data?.type === 'kanban_column' && target.data.status === status) || (target?.data?.type === 'reorder' && (target.data.task as KanbanTaskInfo | undefined)?.status === status)) const isOverColumnEmptySpace = isDraggingTask && target?.data?.type === 'kanban_column' && target.data.status === status const totalCount = data?.pages?.[0]?.pageInfo?.total ?? allTasks.length const statusColor = getStatusColor(status) return (
{m.no_tasks_in_column()}