feat: fix kanban ssr
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { KanbanBoard } from '@/components/kanban-board';
|
||||
|
||||
export default function Page() {
|
||||
return <KanbanBoard />;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import {
|
||||
DndContext,
|
||||
DragEndEvent,
|
||||
DragOverlay,
|
||||
DragStartEvent,
|
||||
PointerSensor,
|
||||
useSensor,
|
||||
useSensors,
|
||||
} from '@dnd-kit/core';
|
||||
import { KanbanColumn, Column } from './kanban-column';
|
||||
import { Task } from './kanban-card';
|
||||
import { KanbanCardPreview, Task } from './kanban-card';
|
||||
import { ThemeToggle } from './theme-toggle';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { IconPlus } from '@tabler/icons-react';
|
||||
import { TaskCreateDialog } from './task-create-dialog';
|
||||
|
||||
type ColumnId = 'todo' | 'in-progress' | 'in-review' | 'done';
|
||||
|
||||
@@ -29,6 +34,13 @@ const initialTasks: Task[] = [
|
||||
|
||||
export function KanbanBoard() {
|
||||
const [tasks, setTasks] = useState<Task[]>(initialTasks);
|
||||
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
||||
const [activeTaskId, setActiveTaskId] = useState<string | null>(null);
|
||||
const [isMounted, setIsMounted] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setIsMounted(true);
|
||||
}, []);
|
||||
|
||||
const sensors = useSensors(
|
||||
useSensor(PointerSensor, {
|
||||
@@ -38,9 +50,19 @@ export function KanbanBoard() {
|
||||
})
|
||||
);
|
||||
|
||||
const activeTask = useMemo(
|
||||
() => tasks.find((task) => task.id === activeTaskId) ?? null,
|
||||
[tasks, activeTaskId]
|
||||
);
|
||||
|
||||
const handleDragStart = (event: DragStartEvent) => {
|
||||
setActiveTaskId(event.active.id as string);
|
||||
};
|
||||
|
||||
const handleDragEnd = (event: DragEndEvent) => {
|
||||
const { active, over } = event;
|
||||
|
||||
setActiveTaskId(null);
|
||||
if (!over) return;
|
||||
|
||||
const taskId = active.id as string;
|
||||
@@ -53,6 +75,10 @@ export function KanbanBoard() {
|
||||
);
|
||||
};
|
||||
|
||||
const handleDragCancel = () => {
|
||||
setActiveTaskId(null);
|
||||
};
|
||||
|
||||
const handleAddTask = (columnId: string, title: string, description?: string) => {
|
||||
const newTask: Task = {
|
||||
id: crypto.randomUUID(),
|
||||
@@ -67,13 +93,33 @@ export function KanbanBoard() {
|
||||
return tasks.filter((task) => task.status === columnId);
|
||||
};
|
||||
|
||||
if (!isMounted) {
|
||||
return <div className="h-screen w-full bg-background" />;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="h-screen w-full flex flex-col bg-background">
|
||||
<header className="flex items-center justify-between p-6 pb-4">
|
||||
<h1 className="text-2xl font-bold">Kanban Board</h1>
|
||||
<ThemeToggle />
|
||||
<div className="flex items-center gap-2">
|
||||
<Button onClick={() => setIsDialogOpen(true)}>
|
||||
<IconPlus className="h-4 w-4" />
|
||||
Add task
|
||||
</Button>
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
</header>
|
||||
<DndContext sensors={sensors} onDragEnd={handleDragEnd}>
|
||||
<TaskCreateDialog
|
||||
open={isDialogOpen}
|
||||
onOpenChange={setIsDialogOpen}
|
||||
onSubmit={(title, description) => handleAddTask('todo', title, description)}
|
||||
/>
|
||||
<DndContext
|
||||
sensors={sensors}
|
||||
onDragStart={handleDragStart}
|
||||
onDragEnd={handleDragEnd}
|
||||
onDragCancel={handleDragCancel}
|
||||
>
|
||||
<div className="flex-1 overflow-x-auto px-6 pb-6">
|
||||
<div className="inline-grid grid-flow-col auto-cols-[minmax(280px,360px)] gap-4">
|
||||
{COLUMNS.map((column) => (
|
||||
@@ -81,11 +127,13 @@ export function KanbanBoard() {
|
||||
key={column.id}
|
||||
column={column}
|
||||
tasks={getTasksForColumn(column.id)}
|
||||
onAddTask={handleAddTask}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<DragOverlay dropAnimation={null}>
|
||||
{activeTask ? <KanbanCardPreview task={activeTask} /> : null}
|
||||
</DragOverlay>
|
||||
</DndContext>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -16,27 +16,10 @@ interface KanbanCardProps {
|
||||
task: Task;
|
||||
}
|
||||
|
||||
export function KanbanCard({ task }: KanbanCardProps) {
|
||||
const { attributes, listeners, setNodeRef, transform, isDragging } = useDraggable({
|
||||
id: task.id,
|
||||
});
|
||||
|
||||
const style = {
|
||||
transform: CSS.Translate.toString(transform),
|
||||
};
|
||||
|
||||
function KanbanCardLayout({ task, className }: KanbanCardProps & { className?: string }) {
|
||||
return (
|
||||
<Card
|
||||
ref={setNodeRef}
|
||||
style={style}
|
||||
className={cn(
|
||||
'cursor-grab active:cursor-grabbing mb-2 transition-opacity',
|
||||
isDragging && 'opacity-50 z-50'
|
||||
)}
|
||||
{...listeners}
|
||||
{...attributes}
|
||||
>
|
||||
<CardContent className="p-3">
|
||||
<Card size="sm" className={cn('w-full py-0', className)}>
|
||||
<CardContent className="px-3 py-2">
|
||||
<p className="text-sm font-medium">{task.title}</p>
|
||||
{task.description && (
|
||||
<p className="text-xs text-muted-foreground mt-1">{task.description}</p>
|
||||
@@ -45,3 +28,45 @@ export function KanbanCard({ task }: KanbanCardProps) {
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export function KanbanCard({ task }: KanbanCardProps) {
|
||||
const { attributes, listeners, setNodeRef, transform, isDragging } = useDraggable({
|
||||
id: task.id,
|
||||
});
|
||||
|
||||
const style = {
|
||||
transform: CSS.Translate.toString(transform),
|
||||
transition: 'none',
|
||||
willChange: isDragging ? 'transform' : undefined,
|
||||
};
|
||||
|
||||
return (
|
||||
<Card
|
||||
size="sm"
|
||||
ref={setNodeRef}
|
||||
style={style}
|
||||
className={cn(
|
||||
'cursor-grab active:cursor-grabbing mb-2 w-full py-0',
|
||||
isDragging && 'opacity-50 z-50'
|
||||
)}
|
||||
{...listeners}
|
||||
{...attributes}
|
||||
>
|
||||
<CardContent className="px-3 py-2">
|
||||
<p className="text-sm font-medium">{task.title}</p>
|
||||
{task.description && (
|
||||
<p className="text-xs text-muted-foreground mt-1">{task.description}</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export function KanbanCardPreview({ task }: KanbanCardProps) {
|
||||
return (
|
||||
<KanbanCardLayout
|
||||
task={task}
|
||||
className="cursor-grabbing shadow-lg ring-1 ring-primary/30 pointer-events-none"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,11 +3,7 @@
|
||||
import { useDroppable } from '@dnd-kit/core';
|
||||
import { KanbanCard, Task } from './kanban-card';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { IconPlus } from '@tabler/icons-react';
|
||||
import { useState } from 'react';
|
||||
import { TaskCreateDialog } from './task-create-dialog';
|
||||
|
||||
export interface Column {
|
||||
id: string;
|
||||
@@ -18,25 +14,18 @@ export interface Column {
|
||||
interface KanbanColumnProps {
|
||||
column: Column;
|
||||
tasks: Task[];
|
||||
onAddTask: (columnId: string, title: string, description?: string) => void;
|
||||
}
|
||||
|
||||
export function KanbanColumn({ column, tasks, onAddTask }: KanbanColumnProps) {
|
||||
export function KanbanColumn({ column, tasks }: KanbanColumnProps) {
|
||||
const { setNodeRef, isOver } = useDroppable({
|
||||
id: column.id,
|
||||
});
|
||||
|
||||
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
||||
|
||||
const handleDialogSubmit = (title: string, description: string) => {
|
||||
onAddTask(column.id, title, description);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={setNodeRef}
|
||||
className={cn(
|
||||
'flex flex-col h-full bg-card rounded-lg border border-border p-4 min-h-[600px]',
|
||||
'flex flex-col h-full min-w-0 bg-card rounded-lg border border-border p-4 min-h-[600px]',
|
||||
isOver && 'ring-2 ring-primary'
|
||||
)}
|
||||
>
|
||||
@@ -49,24 +38,10 @@ export function KanbanColumn({ column, tasks, onAddTask }: KanbanColumnProps) {
|
||||
{tasks.length}
|
||||
</Badge>
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-8 w-8 p-0"
|
||||
onClick={() => setIsDialogOpen(true)}
|
||||
>
|
||||
<IconPlus className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<TaskCreateDialog
|
||||
open={isDialogOpen}
|
||||
onOpenChange={setIsDialogOpen}
|
||||
onSubmit={handleDialogSubmit}
|
||||
/>
|
||||
|
||||
{/* Tasks */}
|
||||
<div className="flex-1 overflow-y-auto space-y-0">
|
||||
<div className="flex-1 overflow-y-auto overflow-x-hidden space-y-0">
|
||||
{tasks.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground text-center mt-8">No tasks yet</p>
|
||||
) : (
|
||||
|
||||
Reference in New Issue
Block a user