import { companyProjectsQuery } from "@/lib/company-projects-query"; import { useEffect } from "react"; import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { ListChecks, SearchX } from "lucide-react"; import { accessApi } from "@/api/access"; import { agentsApi } from "@/api/agents"; import { useAccountIdentity } from "@/api/companies-query"; import { issuesApi } from "@/api/issues"; import { EmptyState } from "@/components/EmptyState"; import { KanbanBoard } from "@/components/KanbanBoard"; import { TaskList, type TaskListItem } from "@/components/TaskList"; import { TaskListToolbar } from "@/components/TaskListToolbar"; import { TruncatedTextMarqueeProvider } from "@/components/TruncatedText"; import { Button } from "@/components/ui/button"; import { useBreadcrumbs } from "@/context/BreadcrumbContext"; import { useCompany } from "@/context/CompanyContext"; import { useDialogActions } from "@/context/DialogContext"; import { useTaskListCollection } from "@/hooks/useTaskListCollection"; import { formatUserLabel } from "@/lib/assignees"; import { buildCompanyUserProfileMap } from "@/lib/company-members"; import { queryKeys } from "@/lib/queryKeys"; import { taskListViewStorageKey } from "@/lib/task-list-views"; import type { Issue } from "@paperclipai/shared"; const pageSize = 200; /** Real API adapter for the Design proposals / Pages / Tasks composition. * All visual and collection behavior comes from the reviewed shared subjects. */ export function DesignSandboxTasks() { const { selectedCompanyId: companyId } = useCompany(); const { userId } = useAccountIdentity(); const { openNewIssue } = useDialogActions(); const { setBreadcrumbs } = useBreadcrumbs(); const client = useQueryClient(); useEffect(() => { setBreadcrumbs([{ label: "Tasks" }]); }, [setBreadcrumbs]); const tasks = useInfiniteQuery({ queryKey: [...queryKeys.issues.list(companyId!), "design-sandbox", userId], queryFn: ({ pageParam, signal }) => issuesApi.list(companyId!, { limit: pageSize, offset: pageParam, includeAgentCreatedTasks: true, includeRoutineExecutions: true, sortField: "id", sortDir: "asc", }, { signal }), initialPageParam: 0, getNextPageParam: (last, _pages, offset) => last.length === pageSize ? offset + pageSize : undefined, enabled: !!companyId, }); const agents = useQuery({ queryKey: queryKeys.agents.list(companyId!), queryFn: () => agentsApi.list(companyId!), enabled: !!companyId }); const projects = useQuery({ ...companyProjectsQuery(companyId!, { includeArchived: true }), enabled: !!companyId }); const directory = useQuery({ queryKey: queryKeys.access.companyUserDirectory(companyId!), queryFn: () => accessApi.listUserDirectory(companyId!), enabled: !!companyId }); const labels = useQuery({ queryKey: queryKeys.issues.labels(companyId!), queryFn: () => issuesApi.listLabels(companyId!), enabled: !!companyId }); // The ordinary organization list does not include viewer read context. // Ask the server for the signed-in user's unread set instead of guessing // unread state from dates or reusing Storybook fixture flags. const unread = useQuery({ queryKey: [...queryKeys.issues.list(companyId!), "design-sandbox-unread", userId], queryFn: async ({ signal }) => { const ids = new Set(); let page: Issue[]; let offset = 0; do { page = await issuesApi.list(companyId!, { unreadForUserId: "me", includeAgentCreatedTasks: true, includeRoutineExecutions: true, limit: pageSize, offset, sortField: "id", sortDir: "asc" }, { signal }); for (const issue of page) ids.add(issue.id); offset += pageSize; } while (page.length === pageSize); return ids; }, enabled: !!companyId && !!userId, }); const users = buildCompanyUserProfileMap(directory.data?.users); const agentById = new Map(agents.data?.map(agent => [agent.id, agent])); const projectById = new Map(projects.data?.map(project => [project.id, project])); const items: TaskListItem[] = [...new Map((tasks.data?.pages.flat() ?? []).map(issue => [issue.id, issue])).values()].map(issue => ({ issue: { ...issue, project: issue.project ?? projectById.get(issue.projectId ?? "") }, unread: unread.data?.has(issue.id) ?? false, createdBy: issue.createdByAgentId ? agentById.get(issue.createdByAgentId)?.name ?? "Unknown agent" : users.get(issue.createdByUserId ?? "")?.label ?? (issue.createdByUserId === "local-board" ? "Board" : "Unknown user"), assignee: issue.assigneeAgentId ? agentById.get(issue.assigneeAgentId)?.name ?? "Unknown agent" : users.get(issue.assigneeUserId ?? "")?.label ?? formatUserLabel(issue.assigneeUserId), assigneeAgent: agentById.get(issue.assigneeAgentId ?? ""), assigneeUserAvatarUrl: users.get(issue.assigneeUserId ?? "")?.image, })); const collection = useTaskListCollection({ items, companyId, userId, viewStorageKey: taskListViewStorageKey("design-integration-sandbox", userId ?? "local-board"), }); const update = useMutation({ mutationFn: ({ id, data }: { id: string; data: Record }) => issuesApi.update(id, data), onSuccess: () => { void client.invalidateQueries({ queryKey: queryKeys.issues.list(companyId!) }); }, }); const error = tasks.error ?? agents.error ?? projects.error ?? directory.error ?? labels.error ?? unread.error ?? update.error; const empty = collection.companyItems.length === 0 ? openNewIssue()} /> : ; if (!companyId) return ; return
openNewIssue()} filters={{ state: collection.filters, onChange: collection.updateFilters, activeFilterCount: collection.activeFilterCount, enableLiveRunsFilter: false, agents: agents.data ?? [], projects: projects.data ?? [], labels: labels.data ?? [], currentUserId: userId, creators: collection.creators }} /> {error ?
{error instanceof Error ? error.message : "Could not load tasks"}
: null} {tasks.isPending ?

Loading tasks…

: <> {collection.narrowed ?

{collection.matchingItems.length} matching tasks{collection.layout === "list" && collection.visibleItems.length > collection.matchingItems.length ? " · Parent tasks included for context" : ""}

: null} {collection.layout === "board" ? collection.matchingItems.length ? item.issue)} agents={agents.data} onUpdateIssue={(id, data) => update.mutate({ id, data })} /> : empty : null} {tasks.hasNextPage ?
Search, views and filters cover the {items.length} loaded tasks.
: null} }
; }