diff --git a/app/api/leantime/status-labels/route.ts b/app/api/leantime/status-labels/route.ts index 15809945..b00f5780 100644 --- a/app/api/leantime/status-labels/route.ts +++ b/app/api/leantime/status-labels/route.ts @@ -8,7 +8,7 @@ const userCache = new Map(); export async function GET() { const session = await getServerSession(authOptions); - if (!session) { + if (!session || !session.user?.email) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } @@ -51,10 +51,12 @@ export async function GET() { throw new Error('Could not find Leantime user ID'); } - leantimeUserId = userData.result.id; + leantimeUserId = userData.result.id as number; // Cache the user ID for 5 minutes - userCache.set(session.user.email, leantimeUserId); - setTimeout(() => userCache.delete(session.user.email), 5 * 60 * 1000); + if (session.user.email) { + userCache.set(session.user.email, leantimeUserId); + setTimeout(() => userCache.delete(session.user.email!), 5 * 60 * 1000); + } } // Now fetch the status labels @@ -108,19 +110,34 @@ export async function GET() { return NextResponse.json({ statusLabels: [] }); } - // Transform the nested status labels into a flat array - const transformedLabels = Object.entries(data.result).flatMap(([projectId, labels]) => { - return Object.entries(labels).map(([id, label]: [string, any]) => ({ - id, - name: label.name, - projectId, - projectName: projectId, // You might want to fetch project names separately - statusType: label.statusType, - class: label.class, - sortKey: label.sortKey - })); + // Create a map to store unique status labels + const uniqueLabels = new Map(); + + // Transform and deduplicate status labels + Object.entries(data.result as Record>).forEach(([projectId, labels]) => { + Object.entries(labels).forEach(([id, label]) => { + const key = label.name; + if (!uniqueLabels.has(key)) { + uniqueLabels.set(key, { + id, + name: label.name, + statusType: label.statusType, + class: label.class, + sortKey: label.sortKey, + projects: [] + }); + } + uniqueLabels.get(key).projects.push({ + id: projectId, + name: projectId // You might want to fetch project names separately + }); + }); }); + // Convert the map to an array and sort by sortKey + const transformedLabels = Array.from(uniqueLabels.values()) + .sort((a, b) => (a.sortKey || '').localeCompare(b.sortKey || '')); + return NextResponse.json({ statusLabels: transformedLabels }); } catch (error) { console.error('Detailed error in status labels fetch:', error); diff --git a/components/flow.tsx b/components/flow.tsx index a25a7936..bfc886fc 100644 --- a/components/flow.tsx +++ b/components/flow.tsx @@ -7,14 +7,18 @@ import { RefreshCw } from "lucide-react"; import { useRouter } from "next/navigation"; import { useSession } from "next-auth/react"; +interface Project { + id: string; + name: string; +} + interface StatusLabel { id: string; name: string; - projectId: string; - projectName: string; statusType: string; class: string; sortKey: string; + projects: Project[]; } export function Flow() { @@ -67,15 +71,6 @@ export function Flow() { } }, [session]); - // Group status labels by project - const groupedLabels = statusLabels.reduce((acc, label) => { - if (!acc[label.projectId]) { - acc[label.projectId] = []; - } - acc[label.projectId].push(label); - return acc; - }, {} as Record); - return ( - {Object.entries(groupedLabels).length === 0 ? ( + {statusLabels.length === 0 ? (

No status labels found

) : ( - Object.entries(groupedLabels).map(([projectId, labels]) => ( -
-

{projectId}

-
- {labels.map((label) => ( -
-
-
-

{label.name}

- - {label.statusType} - -
-
+
+ {statusLabels.map((label) => ( +
+
+
+

{label.name}

+ + {label.statusType} +
- ))} + {label.projects.length > 0 && ( +
+ {label.projects.map((project) => ( + + {project.name} + + ))} +
+ )} +
-
- )) + ))} +
)}
)}