NeahFront7/components/flow.tsx
2025-04-12 19:40:31 +02:00

210 lines
7.0 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { RefreshCw } from "lucide-react";
interface Task {
id: number;
headline: string;
description: string;
projectName: string;
status: number;
dateToFinish: string;
editorId: string;
editorFirstname: string | null;
editorLastname: string | null;
}
interface ProjectSummary {
name: string;
tasks: {
status: number;
count: number;
}[];
}
export function Flow() {
const [tasks, setTasks] = useState<Task[]>([]);
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const [refreshing, setRefreshing] = useState(false);
const getStatusLabel = (status: number): string => {
switch (status) {
case 1: return 'New';
case 2: return 'Ready';
case 3: return 'In Progress';
case 4: return 'Review';
case 5: return 'Done';
default: return 'Unknown';
}
};
const getStatusColor = (status: number): string => {
switch (status) {
case 1: return 'bg-blue-500';
case 2: return 'bg-green-500';
case 3: return 'bg-yellow-500';
case 4: return 'bg-purple-500';
case 5: return 'bg-gray-500';
default: return 'bg-gray-300';
}
};
const formatDate = (dateStr: string): string => {
if (!dateStr || dateStr === '0000-00-00 00:00:00') return '';
try {
const date = new Date(dateStr);
if (isNaN(date.getTime())) return '';
return date.toLocaleDateString('fr-FR', {
day: '2-digit',
month: '2-digit',
year: 'numeric'
});
} catch {
return '';
}
};
const fetchTasks = async (isRefresh = false) => {
try {
if (isRefresh) setRefreshing(true);
const response = await fetch('/api/leantime/tasks');
if (!response.ok) throw new Error('Failed to fetch tasks');
const data = await response.json();
console.log('Tasks API response:', data);
if (!data.tasks || !Array.isArray(data.tasks)) {
console.warn('No tasks found in response:', data);
setTasks([]);
return;
}
// Sort tasks by status and date
const sortedTasks = data.tasks
.filter((task: Task) =>
task.headline &&
typeof task.headline === 'string' &&
task.status !== 3 // Not completed
)
.sort((a: Task, b: Task) => {
// Define priority order for tasks
const priorityOrder = [
'Mettre en ligne site wix SLM-SA',
'Bi-Dimension Investment',
'Draft Pax Index Mvt 4 CARE',
'Finaliser Pax Index Mvt 5 Data Intelligence Governance DIG',
'Continuous Improving Cycle : Risk Monitoring + Model Assessments + Learning Machine',
'Rediger pétition ODD18 et intégration au site CLM',
'complete ressources background and quote'
];
const aIndex = priorityOrder.indexOf(a.headline);
const bIndex = priorityOrder.indexOf(b.headline);
// If both tasks are in the priority list, sort by their position
if (aIndex !== -1 && bIndex !== -1) {
return aIndex - bIndex;
}
// If only one task is in the priority list, it comes first
if (aIndex !== -1) return -1;
if (bIndex !== -1) return 1;
// For tasks not in the priority list, sort by status and date
if (a.status !== b.status) {
return a.status - b.status;
}
const dateA = a.dateToFinish && a.dateToFinish !== '0000-00-00 00:00:00'
? new Date(a.dateToFinish).getTime()
: Number.MAX_SAFE_INTEGER;
const dateB = b.dateToFinish && b.dateToFinish !== '0000-00-00 00:00:00'
? new Date(b.dateToFinish).getTime()
: Number.MAX_SAFE_INTEGER;
return dateA - dateB;
});
console.log('Filtered and sorted tasks:', sortedTasks);
setTasks(sortedTasks); // Remove the slice(0, 6) to show all tasks
setError(null);
} catch (err) {
console.error('Error fetching tasks:', err);
setError('Failed to fetch tasks');
} finally {
setLoading(false);
setRefreshing(false);
}
};
useEffect(() => {
fetchTasks();
}, []);
return (
<Card className="transition-transform duration-500 ease-in-out transform hover:scale-105">
<CardHeader className="flex flex-row items-center justify-between pb-2">
<CardTitle className="text-lg font-medium">My ToDos</CardTitle>
<Button
variant="ghost"
size="icon"
onClick={() => fetchTasks(true)}
disabled={refreshing}
className={refreshing ? 'animate-spin' : ''}
>
<RefreshCw className="h-4 w-4" />
</Button>
</CardHeader>
<CardContent>
{loading ? (
<div className="flex items-center justify-center py-4">
<div className="h-4 w-4 animate-spin rounded-full border-2 border-primary border-t-transparent" />
</div>
) : error ? (
<div className="text-center text-sm text-red-500">{error}</div>
) : tasks.length === 0 ? (
<div className="text-center text-sm text-gray-500">No tasks assigned to you</div>
) : (
<div className="space-y-2">
{tasks.map((task) => (
<div
key={task.id}
className="p-3 rounded-lg bg-white/50 backdrop-blur-sm hover:bg-white/60 transition-colors border border-gray-200"
>
<div className="flex items-start gap-3">
<div className="flex-1">
<div className="flex items-center gap-2">
<h3 className="text-sm font-medium text-blue-600 hover:text-blue-800 truncate" title={task.headline}>
{task.headline}
</h3>
<span className={`px-2 py-0.5 text-xs rounded-full ${getStatusColor(task.status)} text-white`}>
{getStatusLabel(task.status)}
</span>
</div>
<div className="mt-1 flex items-center gap-2 text-xs text-gray-500">
{task.projectName && (
<span className="inline-flex items-center">
<span className="mr-1">📁</span> {task.projectName}
</span>
)}
{task.dateToFinish && task.dateToFinish !== '0000-00-00 00:00:00' && (
<span className="inline-flex items-center">
<span className="mr-1">📅</span> {formatDate(task.dateToFinish)}
</span>
)}
</div>
</div>
</div>
</div>
))}
</div>
)}
</CardContent>
</Card>
);
}