"use client"; import { useCallback, useMemo, useState, memo } from "react"; import { IconMessageCircle, IconPlus } from "@tabler/icons-react"; import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@kandev/ui/sheet"; import { Drawer, DrawerContent, DrawerHeader, DrawerTitle } from "@kandev/ui/drawer"; import { Button } from "@kandev/ui/button"; import { TaskSwitcher } from "../task-switcher"; import type { TaskSwitcherItem } from "../task-switcher"; import { SidebarFilterBar } from "../sidebar-filter/sidebar-filter-bar"; import type { StepDef } from "../task-switcher-context-menu"; import type { TaskMoveWorkflow } from "../task-move-context-menu"; import { applyView } from "@/lib/sidebar/apply-view"; import { useAppStore, useAppStoreApi } from "@/components/state-provider"; import { useEffectiveSidebarView } from "@/hooks/domains/sidebar/use-effective-sidebar-view"; import { useSidebarTaskPrefs } from "@/hooks/domains/sidebar/use-sidebar-task-prefs"; import { useRepositories } from "@/hooks/domains/workspace/use-repositories"; import { WorkspaceSwitcher } from "../workspace-switcher"; import { TaskCreateDialog } from "@/components/task-create-dialog"; import { TaskArchiveConfirmDialog } from "../task-archive-confirm-dialog"; import { TaskDeleteConfirmDialog } from "../task-delete-confirm-dialog"; import { TaskDetachTargetConfirmDialog } from "../task-detach-confirm-dialog"; import { TaskRenameDialog } from "../task-rename-dialog"; import { SidebarLinkDialogs } from "../task-session-sidebar-dialogs"; import { useSidebarLinkActions } from "../task-session-sidebar-link-actions"; import { useSidebarTaskLinking } from "../task-session-sidebar-task-linking"; import { useSheetData, useSheetActions } from "./session-task-switcher-sheet-hooks"; import { useQuickChatLauncher } from "@/hooks/use-quick-chat-launcher"; import { useMobileTaskRename } from "./use-mobile-task-rename"; type SessionTaskSwitcherSheetProps = { open: boolean; onOpenChange: (open: boolean) => void; workspaceId: string | null; workflowId: string | null; presentation?: "sheet" | "drawer"; }; function useMobileTaskLinking(workspaceId: string | null) { const store = useAppStoreApi(); const actions = useSidebarLinkActions(store); const taskListHandlers = useSidebarTaskLinking(workspaceId, actions); const { repositories } = useRepositories(workspaceId); return { actions, repositories, taskListHandlers, }; } export function MobileTaskList({ tasks, workflows, stepsByWorkflowId, activeTaskId, selectedTaskId, onSelectTask, onRenameTask, onArchiveTask, onDeleteTask, onDetachTask, onLinkPullRequest, onLinkIssue, onLinkMergeRequest, onLinkJiraTicket, onLinkLinearIssue, onLinkSentryIssue, deletingTaskId, isLoading, }: { tasks: TaskSwitcherItem[]; workflows: TaskMoveWorkflow[]; stepsByWorkflowId: Record; activeTaskId: string | null; selectedTaskId: string | null; onSelectTask: (taskId: string) => void; onRenameTask?: (taskId: string, currentTitle: string) => void; onArchiveTask: (taskId: string) => void; onDeleteTask: (taskId: string) => Promise | void; onDetachTask: (taskId: string) => Promise | void; onLinkPullRequest?: (taskId: string, taskTitle?: string) => void; onLinkIssue?: (taskId: string, taskTitle?: string) => void; onLinkMergeRequest?: (taskId: string, taskTitle?: string) => void; onLinkJiraTicket?: (taskId: string, taskTitle?: string) => void; onLinkLinearIssue?: (taskId: string, taskTitle?: string) => void; onLinkSentryIssue?: (taskId: string, taskTitle?: string) => void; deletingTaskId: string | null; isLoading?: boolean; }) { const view = useEffectiveSidebarView(); const { pinnedTaskIds, orderedTaskIds, subtaskOrderByParentId, togglePinnedTask, handleReorderGroup, handleReorderSubtasks, } = useSidebarTaskPrefs(); const toggleSidebarGroupCollapsed = useAppStore((s) => s.toggleSidebarGroupCollapsed); const collapsedSubtaskParents = useAppStore((s) => s.collapsedSubtaskParents); const toggleSubtaskCollapsed = useAppStore((s) => s.toggleSubtaskCollapsed); const handleToggleGroup = useCallback( (groupKey: string) => toggleSidebarGroupCollapsed(view.id, groupKey), [toggleSidebarGroupCollapsed, view.id], ); const grouped = useMemo( () => applyView(tasks, view, { pinnedTaskIds, orderedTaskIds, subtaskOrderByParentId, }), [tasks, view, pinnedTaskIds, orderedTaskIds, subtaskOrderByParentId], ); return ( ); } function TaskSwitcherSurfaceHeader({ workspaceId, workspaces, onWorkspaceChange, onQuickChat, onNewTask, presentation, }: { workspaceId: string | null; workspaces: Array<{ id: string; name: string }>; onWorkspaceChange: (workspaceId: string) => void; onQuickChat: () => void; onNewTask: () => void; presentation: "sheet" | "drawer"; }) { const content = ( <>
{presentation === "drawer" ? ( Tasks ) : ( Tasks )}
{workspaceId && ( )}
); if (presentation === "drawer") { return ( {content} ); } return {content}; } function surfaceAction( presentation: "sheet" | "drawer", onOpenChange: (open: boolean) => void, action: (...args: TArgs) => unknown, ): (...args: TArgs) => void; function surfaceAction( presentation: "sheet" | "drawer", onOpenChange: (open: boolean) => void, action: ((...args: TArgs) => unknown) | undefined, ): ((...args: TArgs) => void) | undefined; function surfaceAction( presentation: "sheet" | "drawer", onOpenChange: (open: boolean) => void, action: ((...args: TArgs) => unknown) | undefined, ): ((...args: TArgs) => void) | undefined { if (!action || presentation === "sheet") return action; return (...args) => { onOpenChange(false); action(...args); }; } type TaskSwitcherSurfaceContentProps = { presentation: "sheet" | "drawer"; workspaceId: string | null; onOpenChange: (open: boolean) => void; onQuickChat: () => void; onNewTask: () => void; data: ReturnType; actions: ReturnType; rename: ReturnType; linking: ReturnType; }; function TaskSwitcherSurfaceContent({ presentation, workspaceId, onOpenChange, onQuickChat, onNewTask, data, actions, rename, linking, }: TaskSwitcherSurfaceContentProps) { return ( <> ({ id: w.id, name: w.name }))} onWorkspaceChange={actions.handleWorkspaceChange} onQuickChat={onQuickChat} onNewTask={onNewTask} />
); } function TaskSwitcherDialogs({ dialogOpen, onDialogOpenChange, workspaceId, workflowId, data, actions, rename, linking, }: { dialogOpen: boolean; onDialogOpenChange: (open: boolean) => void; workspaceId: string | null; workflowId: string | null; data: ReturnType; actions: ReturnType; rename: ReturnType; linking: ReturnType; }) { return ( <> { if (!open) actions.setArchivingTask(null); }} taskTitle={actions.archivingTask?.title ?? ""} taskId={actions.archivingTask?.id} executorType={actions.archivingTask?.executorType} isArchiving={actions.isArchiving} onConfirm={({ cascade }) => actions.handleArchiveConfirm({ cascade })} /> { if (!open) rename.setRenamingTask(null); }} currentTitle={rename.renamingTask?.title ?? ""} onSubmit={rename.handleRenameSubmit} /> { if (!open) actions.setDeletingTask(null); }} taskTitle={actions.deletingTask?.title ?? ""} taskId={actions.deletingTask?.id} executorType={actions.deletingTask?.executorType} isDeleting={actions.isDeleting} onConfirm={({ cascade }) => actions.handleDeleteConfirm({ cascade })} /> actions.setDetachingTask(null)} onConfirm={actions.handleDetachConfirm} /> ); } export const SessionTaskSwitcherSheet = memo(function SessionTaskSwitcherSheet({ open, onOpenChange, workspaceId, workflowId, presentation = "sheet", }: SessionTaskSwitcherSheetProps) { const [dialogOpen, setDialogOpen] = useState(false); const data = useSheetData(workspaceId); const actions = useSheetActions(workspaceId, onOpenChange); const rename = useMobileTaskRename(); const linking = useMobileTaskLinking(workspaceId); const openQuickChat = useQuickChatLauncher(workspaceId); const handleQuickChat = useCallback(() => { onOpenChange(false); openQuickChat(); }, [onOpenChange, openQuickChat]); const surfaceContent = ( { if (presentation === "drawer") onOpenChange(false); setDialogOpen(true); }} data={data} actions={actions} rename={rename} linking={linking} /> ); const surface = presentation === "drawer" ? ( {surfaceContent} ) : ( {surfaceContent} ); return ( <> {surface} ); });