feat: add session-state sections to task switcher sidebar (#273)

This commit is contained in:
Carlos Florêncio
2026-02-24 09:18:42 +00:00
committed by GitHub
parent 32f7ceb319
commit 99d9c7730a
3 changed files with 192 additions and 70 deletions
+8 -2
View File
@@ -3,7 +3,7 @@
import { memo, useState } from "react";
import { IconLoader2 } from "@tabler/icons-react";
import { cn } from "@/lib/utils";
import type { TaskState } from "@/lib/types/http";
import type { TaskState, TaskSessionState } from "@/lib/types/http";
import { TaskItemMenu } from "./task-item-menu";
import { RemoteCloudTooltip } from "./remote-cloud-tooltip";
@@ -17,6 +17,7 @@ type TaskItemProps = {
description?: string;
stepName?: string;
state?: TaskState;
sessionState?: TaskSessionState;
isArchived?: boolean;
isSelected?: boolean;
onClick?: () => void;
@@ -111,6 +112,7 @@ export const TaskItem = memo(function TaskItem({
description,
stepName,
state,
sessionState,
isArchived,
isSelected = false,
onClick,
@@ -131,7 +133,11 @@ export const TaskItem = memo(function TaskItem({
const [menuOpen, setMenuOpen] = useState(false);
const effectiveMenuOpen = menuOpen || isDeleting === true;
const isInProgress = state === "IN_PROGRESS" || state === "SCHEDULING";
const isInProgress =
state === "IN_PROGRESS" ||
state === "SCHEDULING" ||
sessionState === "STARTING" ||
sessionState === "RUNNING";
const hasDiffStats = diffStats && (diffStats.additions > 0 || diffStats.deletions > 0);
return (
@@ -1,7 +1,7 @@
"use client";
import { useCallback, useMemo, useState, memo } from "react";
import type { TaskState, Repository, TaskSession, Task } from "@/lib/types/http";
import type { TaskState, TaskSessionState, Repository, TaskSession, Task } from "@/lib/types/http";
import type { KanbanState } from "@/lib/state/slices";
import { TaskSwitcher } from "./task-switcher";
import { Button } from "@kandev/ui/button";
@@ -85,6 +85,45 @@ type TaskSessionSidebarProps = {
workflowId: string | null;
};
type SessionInfo = {
diffStats: { additions: number; deletions: number } | undefined;
updatedAt: string | undefined;
sessionState: TaskSessionState | undefined;
};
type GitStatusMap = Record<
string,
{ files?: Record<string, { additions?: number; deletions?: number }> }
>;
function getSessionInfoForTask(
taskId: string,
sessionsByTaskId: Record<string, TaskSession[]>,
gitStatusBySessionId: GitStatusMap,
): SessionInfo {
const sessions = sessionsByTaskId[taskId] ?? [];
if (sessions.length === 0) {
return { diffStats: undefined, updatedAt: undefined, sessionState: undefined };
}
const primarySession = sessions.find((s: TaskSession) => s.is_primary);
const latestSession = primarySession ?? sessions[0];
if (!latestSession) {
return { diffStats: undefined, updatedAt: undefined, sessionState: undefined };
}
const updatedAt = latestSession.updated_at;
const sessionState = latestSession.state as TaskSessionState | undefined;
const gitStatus = gitStatusBySessionId[latestSession.id];
if (!gitStatus?.files) return { diffStats: undefined, updatedAt, sessionState };
let additions = 0;
let deletions = 0;
for (const file of Object.values(gitStatus.files)) {
additions += file.additions ?? 0;
deletions += file.deletions ?? 0;
}
const diffStats = additions === 0 && deletions === 0 ? undefined : { additions, deletions };
return { diffStats, updatedAt, sessionState };
}
function useSidebarData(workspaceId: string | null) {
const activeTaskId = useAppStore((state) => state.tasks.activeTaskId);
const activeSessionId = useAppStore((state) => state.tasks.activeSessionId);
@@ -119,39 +158,18 @@ function useSidebarData(workspaceId: string | null) {
return { allTasks: tasks, allSteps: sortedSteps };
}, [snapshots]);
const getSessionInfoForTask = useCallback(
(taskId: string) => {
const sessions = sessionsByTaskId[taskId] ?? [];
if (sessions.length === 0) return { diffStats: undefined, updatedAt: undefined };
const primarySession = sessions.find((s: TaskSession) => s.is_primary);
const latestSession = primarySession ?? sessions[0];
if (!latestSession) return { diffStats: undefined, updatedAt: undefined };
const updatedAt = latestSession.updated_at;
const gitStatus = gitStatusBySessionId[latestSession.id];
if (!gitStatus?.files) return { diffStats: undefined, updatedAt };
let additions = 0;
let deletions = 0;
for (const file of Object.values(gitStatus.files)) {
additions += file.additions ?? 0;
deletions += file.deletions ?? 0;
}
const diffStats = additions === 0 && deletions === 0 ? undefined : { additions, deletions };
return { diffStats, updatedAt };
},
[sessionsByTaskId, gitStatusBySessionId],
);
const tasksWithRepositories = useMemo(() => {
const repositories = workspaceId ? (repositoriesByWorkspace[workspaceId] ?? []) : [];
const repositoryPathsById = new Map(
repositories.map((repo: Repository) => [repo.id, repo.local_path]),
);
const items = allTasks.map((task: KanbanState["tasks"][number]) => {
const sessionInfo = getSessionInfoForTask(task.id);
const sessionInfo = getSessionInfoForTask(task.id, sessionsByTaskId, gitStatusBySessionId);
return {
id: task.id,
title: task.title,
state: task.state as TaskState | undefined,
sessionState: sessionInfo.sessionState,
description: task.description,
workflowStepId: task.workflowStepId as string | undefined,
repositoryPath: task.repositoryId ? repositoryPathsById.get(task.repositoryId) : undefined,
@@ -173,6 +191,7 @@ function useSidebarData(workspaceId: string | null) {
id: archivedState.archivedTaskId,
title: archivedState.archivedTaskTitle ?? "Archived task",
state: undefined,
sessionState: undefined,
description: undefined,
workflowStepId: undefined,
repositoryPath: archivedState.archivedTaskRepositoryPath,
@@ -186,7 +205,14 @@ function useSidebarData(workspaceId: string | null) {
});
}
return items;
}, [repositoriesByWorkspace, allTasks, workspaceId, getSessionInfoForTask, archivedState]);
}, [
repositoriesByWorkspace,
allTasks,
workspaceId,
sessionsByTaskId,
gitStatusBySessionId,
archivedState,
]);
return { activeTaskId, selectedTaskId, allSteps, isLoadingWorkflow, tasksWithRepositories };
}
+133 -43
View File
@@ -1,7 +1,7 @@
"use client";
import { memo, useMemo } from "react";
import type { TaskState } from "@/lib/types/http";
import type { TaskState, TaskSessionState } from "@/lib/types/http";
import { truncateRepoPath } from "@/lib/utils";
import { TaskItem } from "./task-item";
@@ -14,6 +14,7 @@ type TaskSwitcherItem = {
id: string;
title: string;
state?: TaskState;
sessionState?: TaskSessionState;
description?: string;
workflowStepId?: string;
repositoryPath?: string;
@@ -38,6 +39,30 @@ type TaskSwitcherProps = {
isLoading?: boolean;
};
type Section = {
label: string;
tasks: TaskSwitcherItem[];
};
const REVIEW_STATES = new Set<TaskSessionState>([
"WAITING_FOR_INPUT",
"COMPLETED",
"FAILED",
"CANCELLED",
]);
const IN_PROGRESS_STATES = new Set<TaskSessionState>(["RUNNING"]);
const BACKLOG_STATES = new Set<TaskSessionState>(["CREATED", "STARTING"]);
function classifyTask(
sessionState: TaskSessionState | undefined,
): "review" | "in_progress" | "backlog" {
if (!sessionState) return "backlog";
if (REVIEW_STATES.has(sessionState)) return "review";
if (IN_PROGRESS_STATES.has(sessionState)) return "in_progress";
if (BACKLOG_STATES.has(sessionState)) return "backlog";
return "backlog";
}
function TaskSwitcherSkeleton() {
return (
<div className="animate-pulse">
@@ -49,6 +74,73 @@ function TaskSwitcherSkeleton() {
);
}
function SectionHeader({ label, count }: { label: string; count: number }) {
return (
<div className="flex items-center justify-between px-3 py-1.5 bg-foreground/[0.03]">
<span className="text-[11px] font-medium text-muted-foreground uppercase tracking-wide">
{label}
</span>
<span className="text-[11px] text-muted-foreground/60">{count}</span>
</div>
);
}
function TaskSwitcherSection({
section,
stepNameById,
activeTaskId,
selectedTaskId,
onSelectTask,
onArchiveTask,
onDeleteTask,
deletingTaskId,
}: {
section: Section;
stepNameById: Map<string, string>;
activeTaskId: string | null;
selectedTaskId: string | null;
onSelectTask: (taskId: string) => void;
onArchiveTask?: (taskId: string) => void;
onDeleteTask?: (taskId: string) => void;
deletingTaskId?: string | null;
}) {
if (section.tasks.length === 0) return null;
return (
<div>
<SectionHeader label={section.label} count={section.tasks.length} />
{section.tasks.map((task) => {
const isActive = task.id === activeTaskId;
const isSelected = task.id === selectedTaskId || isActive;
const repoLabel = task.repositoryPath ? truncateRepoPath(task.repositoryPath) : undefined;
const stepName = task.workflowStepId ? stepNameById.get(task.workflowStepId) : undefined;
return (
<TaskItem
key={task.id}
title={task.title}
description={repoLabel}
stepName={stepName}
state={task.state}
sessionState={task.sessionState}
isArchived={task.isArchived}
isSelected={isSelected}
diffStats={task.diffStats}
isRemoteExecutor={task.isRemoteExecutor}
remoteExecutorType={task.remoteExecutorType}
remoteExecutorName={task.remoteExecutorName}
taskId={task.id}
primarySessionId={task.primarySessionId ?? null}
updatedAt={task.updatedAt}
onClick={() => onSelectTask(task.id)}
onArchive={onArchiveTask ? () => onArchiveTask(task.id) : undefined}
onDelete={onDeleteTask ? () => onDeleteTask(task.id) : undefined}
isDeleting={deletingTaskId === task.id}
/>
);
})}
</div>
);
}
export const TaskSwitcher = memo(function TaskSwitcher({
tasks,
steps,
@@ -68,55 +160,53 @@ export const TaskSwitcher = memo(function TaskSwitcher({
return map;
}, [steps]);
// Sort tasks: by step position first, then alphabetically
const sortedTasks = useMemo(() => {
const stepOrder = new Map(steps.map((col, i) => [col.id, i]));
return [...tasks].sort((a, b) => {
const aOrder = stepOrder.get(a.workflowStepId ?? "") ?? 999;
const bOrder = stepOrder.get(b.workflowStepId ?? "") ?? 999;
if (aOrder !== bOrder) return aOrder - bOrder;
return a.title.localeCompare(b.title);
});
}, [tasks, steps]);
const sections = useMemo(() => {
const review: TaskSwitcherItem[] = [];
const inProgress: TaskSwitcherItem[] = [];
const backlog: TaskSwitcherItem[] = [];
for (const task of tasks) {
const bucket = classifyTask(task.sessionState);
if (bucket === "review") review.push(task);
else if (bucket === "in_progress") inProgress.push(task);
else backlog.push(task);
}
const byTitle = (a: TaskSwitcherItem, b: TaskSwitcherItem) => a.title.localeCompare(b.title);
review.sort(byTitle);
inProgress.sort(byTitle);
backlog.sort(byTitle);
return [
{ label: "Review", tasks: review },
{ label: "In Progress", tasks: inProgress },
{ label: "Backlog", tasks: backlog },
] satisfies Section[];
}, [tasks]);
if (isLoading) {
return <TaskSwitcherSkeleton />;
}
if (tasks.length === 0) {
return <div className="px-3 py-3 text-xs text-muted-foreground">No tasks yet.</div>;
}
return (
<div>
{sortedTasks.length === 0 ? (
<div className="px-3 py-3 text-xs text-muted-foreground">No tasks yet.</div>
) : (
sortedTasks.map((task) => {
const isActive = task.id === activeTaskId;
const isSelected = task.id === selectedTaskId || isActive;
const repoLabel = task.repositoryPath ? truncateRepoPath(task.repositoryPath) : undefined;
const stepName = task.workflowStepId ? stepNameById.get(task.workflowStepId) : undefined;
return (
<TaskItem
key={task.id}
title={task.title}
description={repoLabel}
stepName={stepName}
state={task.state}
isArchived={task.isArchived}
isSelected={isSelected}
diffStats={task.diffStats}
isRemoteExecutor={task.isRemoteExecutor}
remoteExecutorType={task.remoteExecutorType}
remoteExecutorName={task.remoteExecutorName}
taskId={task.id}
primarySessionId={task.primarySessionId ?? null}
updatedAt={task.updatedAt}
onClick={() => onSelectTask(task.id)}
onArchive={onArchiveTask ? () => onArchiveTask(task.id) : undefined}
onDelete={onDeleteTask ? () => onDeleteTask(task.id) : undefined}
isDeleting={deletingTaskId === task.id}
/>
);
})
)}
{sections.map((section) => (
<TaskSwitcherSection
key={section.label}
section={section}
stepNameById={stepNameById}
activeTaskId={activeTaskId}
selectedTaskId={selectedTaskId}
onSelectTask={onSelectTask}
onArchiveTask={onArchiveTask}
onDeleteTask={onDeleteTask}
deletingTaskId={deletingTaskId}
/>
))}
</div>
);
});