'use client' import { useState } from 'react' import Link from 'next/link' import { X } from 'lucide-react' import { useTranslation } from 'react-i18next' import { linkSessionTasks, sessionTaskLinks, updateTaskCard, type TaskCard, type TaskStatus, } from '@/lib/task-board-api' import { useTaskBoard } from '@/lib/task-board-store' import { activeWorkspaceId } from '@/lib/workspace-scope' export default function TaskPanelSection({ sessionId, draftTaskIds = [], onDraftChange, }: { sessionId: string | null draftTaskIds?: string[] onDraftChange?: (ids: string[]) => void }) { const { t } = useTranslation() const { board } = useTaskBoard() const [busy, setBusy] = useState(false) const [error, setError] = useState(false) const workspaceId = activeWorkspaceId() const ids = sessionId ? (sessionTaskLinks(board, sessionId, workspaceId)?.task_ids ?? []) : draftTaskIds const linked = [...ids].reverse().flatMap(id => board?.cards.find(card => card.id === id) ?? []) const assigned = board?.cards.filter( // The board is already scoped by the server, including legacy General tasks. card => !card.archived && !ids.includes(card.id) ) ?? [] if (!linked.length && !assigned.length) return null async function save(operation: () => Promise) { setBusy(true) setError(false) try { await operation() } catch { setError(true) } finally { setBusy(false) } } function rows(cards: TaskCard[], allowUnlink: boolean) { return cards.map(card => (
{card.title} {allowUnlink && (sessionId || onDraftChange) && ( )}
{card.note && (

{card.note}

)} {card.archived && ( {t('Archive')} )}
)) } return (
{error && (

{t('tasks.saveError')}

)} {linked.length > 0 && (

{t('tasks.linked')}

{rows(linked, true)}
)} {assigned.length > 0 && (

{t('tasks.workspace')}

{rows(assigned, false)}
)} {t('tasks.openBoard')}
) }