WebRemoteControlTaskIndexRow.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import {
  2. Archive,
  3. ArchiveX,
  4. CloudDownload,
  5. LoaderIcon,
  6. Pin,
  7. Trash2,
  8. } from "lucide-react";
  9. import {
  10. TID_TASK_ARCHIVE,
  11. TID_TASK_ITEM,
  12. resolveWebRemoteControlWorkspaceKey,
  13. testId,
  14. type WebRemoteControlTaskTarget,
  15. } from "@zcode/shared";
  16. import {
  17. useCallback,
  18. useState,
  19. type MouseEvent as ReactMouseEvent,
  20. } from "react";
  21. import { cn } from "@/components/lib/utils.js";
  22. import { Button } from "@/components/ui/button.js";
  23. import { ControlHintTooltip } from "@/ControlHintTooltip.js";
  24. import { formatTaskRelativeTime } from "@/lib/taskListItemPresentation.js";
  25. import { buildWebRemoteControlTaskRowKey } from "@/hooks/useWebRemoteControlTaskOpen.js";
  26. import { TaskWorkflowRunLines } from "@/components/workflow-run-line/TaskWorkflowRunLines.js";
  27. import type { WebRemoteControlTaskIndexSortBy } from "@/lib/webRemoteControlTaskIndex.js";
  28. interface WebRemoteControlTaskIndexRowIntl {
  29. formatMessage: (desc: { id: string }, values?: Record<string, string>) => string;
  30. }
  31. export function WebRemoteControlTaskIndexRow({
  32. activeTaskId,
  33. activeWorkspaceKey,
  34. canMutateTask,
  35. intl,
  36. mutatingTaskKey,
  37. onArchiveTask,
  38. onDeleteArchivedTask,
  39. onOpenTask,
  40. onToggleTaskPin,
  41. onUnarchiveTask,
  42. pendingArchiveTaskKey,
  43. showWorkspaceLabel = false,
  44. switchingTaskKey,
  45. task,
  46. taskSortBy,
  47. }: {
  48. activeTaskId: string | null;
  49. activeWorkspaceKey: string;
  50. canMutateTask: boolean;
  51. intl: WebRemoteControlTaskIndexRowIntl;
  52. mutatingTaskKey: string | null;
  53. onArchiveTask: (event: ReactMouseEvent, task: WebRemoteControlTaskTarget) => void;
  54. onDeleteArchivedTask: (event: ReactMouseEvent, task: WebRemoteControlTaskTarget) => void;
  55. onOpenTask: (task: WebRemoteControlTaskTarget) => void;
  56. onToggleTaskPin: (event: ReactMouseEvent, task: WebRemoteControlTaskTarget) => void;
  57. onUnarchiveTask: (event: ReactMouseEvent, task: WebRemoteControlTaskTarget) => void;
  58. pendingArchiveTaskKey: string | null;
  59. showWorkspaceLabel?: boolean;
  60. switchingTaskKey: string | null;
  61. task: WebRemoteControlTaskTarget;
  62. taskSortBy: WebRemoteControlTaskIndexSortBy;
  63. }) {
  64. const [hoverActionsVisible, setHoverActionsVisible] = useState(false);
  65. const rowKey = buildWebRemoteControlTaskRowKey(task);
  66. const taskWorkspaceKey = resolveWebRemoteControlWorkspaceKey(task);
  67. const isActive = taskWorkspaceKey === activeWorkspaceKey && task.taskId === activeTaskId;
  68. const isSwitching = switchingTaskKey === rowKey;
  69. const isMutating = mutatingTaskKey === rowKey;
  70. const isArchiveConfirming = pendingArchiveTaskKey === rowKey;
  71. const canPinTask = canMutateTask && !task.archived;
  72. const canArchiveTask = canMutateTask && !task.archived;
  73. const canUnarchiveTask = canMutateTask && Boolean(task.archived);
  74. const canDeleteArchivedTask = canMutateTask && Boolean(task.archived);
  75. const showPinnedState = Boolean(task.pinned && !task.archived);
  76. const rowDisabled = Boolean(switchingTaskKey);
  77. // Bugfix: 宽屏 Web 远控任务行的打开按钮覆盖整行主体,纯 CSS group-hover
  78. // 会让左侧 pin 操作被主按钮压住且不可命中;这里和桌面 TaskListItem 一样用行级 hover state 挂载动作。
  79. const showPinAction = canPinTask && hoverActionsVisible;
  80. const showArchiveAction = canArchiveTask && (hoverActionsVisible || isArchiveConfirming);
  81. const showArchivedActions =
  82. (canUnarchiveTask || canDeleteArchivedTask) && hoverActionsVisible;
  83. const taskTitle = task.title || intl.formatMessage({ id: "taskList.untitled" });
  84. const timeLabel = formatTaskRelativeTime(
  85. taskSortBy === "created" ? task.createdAt : task.updatedAt,
  86. intl,
  87. );
  88. const pinLabel = intl.formatMessage({
  89. id: task.pinned ? "taskList.unpin" : "taskList.pin",
  90. });
  91. const archiveLabel = intl.formatMessage({
  92. id: isArchiveConfirming ? "common.confirm" : "taskList.archive",
  93. });
  94. const unarchiveLabel = intl.formatMessage({ id: "taskList.unarchive" });
  95. const deleteLabel = intl.formatMessage({ id: "taskList.delete" });
  96. const isRemoteTask = Boolean(task.workspaceIdentity?.trim() || task.workspaceKind === "remote");
  97. const handleMouseEnter = useCallback(() => {
  98. setHoverActionsVisible(true);
  99. }, []);
  100. const handleMouseLeave = useCallback(() => {
  101. setHoverActionsVisible(false);
  102. }, []);
  103. return (
  104. <li
  105. data-web-remote-task-row={rowKey}
  106. onMouseEnter={handleMouseEnter}
  107. onMouseLeave={handleMouseLeave}
  108. className={cn(
  109. "group/task-item relative flex w-full cursor-pointer items-center gap-2 rounded-lg text-left transition-[background-color,border-color,box-shadow]",
  110. rowDisabled && "cursor-wait opacity-80",
  111. isActive ? "bg-selected" : "hover:bg-surface-hover",
  112. )}
  113. >
  114. {/* Bugfix: 宽屏 Web 远控任务行现在统一由 listWorkspaces() 事实源渲染。
  115. 之前 workspace / timeline / archived 分别落到不同桌面数据源,导致排序、搜索和跨 workspace 视图不一致。 */}
  116. <button
  117. type="button"
  118. aria-label={intl.formatMessage({ id: "webRemoteControl.openTask" }, { title: taskTitle })}
  119. data-testid={testId(TID_TASK_ITEM, task.taskId)}
  120. disabled={rowDisabled}
  121. onClick={() => onOpenTask(task)}
  122. className="flex min-w-0 flex-1 items-center gap-2 rounded-lg py-1 pl-2.5 pr-1 text-left disabled:cursor-wait disabled:opacity-80"
  123. >
  124. <span className="relative flex size-4 shrink-0 items-center justify-center">
  125. <span
  126. aria-hidden="true"
  127. className={cn(
  128. "flex size-4 items-center justify-center transition-opacity",
  129. showPinAction && "hidden",
  130. )}
  131. >
  132. {isSwitching || isMutating ? (
  133. <LoaderIcon className="size-4 animate-spin text-foreground-subtle" />
  134. ) : task.unreadAt ? (
  135. <span
  136. data-unread-indicator="true"
  137. className="h-1.5 w-1.5 rounded-full bg-sky-500 dark:bg-sky-400"
  138. />
  139. ) : showPinnedState ? (
  140. <Pin className="size-4 text-foreground-subtle" />
  141. ) : null}
  142. </span>
  143. </span>
  144. <span className="flex min-w-0 flex-1 flex-col">
  145. <span className="min-w-0 flex h-6 flex-wrap items-center gap-1.5">
  146. <span className="min-w-0 flex-1 truncate text-ui-base text-foreground" title={taskTitle}>
  147. {taskTitle}
  148. </span>
  149. {showWorkspaceLabel ? (
  150. <span
  151. className="max-w-28 shrink truncate text-ui-base text-foreground-subtlest"
  152. title={task.workspaceLabel}
  153. >
  154. {task.workspaceLabel}
  155. </span>
  156. ) : null}
  157. </span>
  158. {/* 工作流运行行(docs/dynamic-workflow/presentation.md「The sidebar run line」):行整体已是按钮,
  159. 这里只绘制;已结束的行由远控端自己的确认集合折叠。 */}
  160. {task.workflowActivity ? (
  161. <TaskWorkflowRunLines
  162. activity={task.workflowActivity}
  163. isActive={isActive}
  164. intl={intl}
  165. density="compact"
  166. />
  167. ) : null}
  168. </span>
  169. <span
  170. className={cn(
  171. "mr-0.5 shrink-0 text-ui-base text-foreground-subtle",
  172. showArchiveAction && "hidden",
  173. )}
  174. >
  175. {timeLabel}
  176. </span>
  177. </button>
  178. {showPinAction ? (
  179. <ControlHintTooltip title={pinLabel} side="right" align="center">
  180. {/* Bugfix: archived 行按桌面归档列表语义不显示置顶入口;否则 setTaskPinned 只改 pinned,
  181. Web 乐观更新却会把 archived 清掉,导致远控列表和服务端 membership 分叉。 */}
  182. <Button
  183. type="button"
  184. variant="ghost"
  185. size="icon-xs"
  186. disabled={rowDisabled || isMutating}
  187. onMouseDown={(event) => {
  188. event.preventDefault();
  189. event.stopPropagation();
  190. }}
  191. onClick={(event) => onToggleTaskPin(event, task)}
  192. className={cn(
  193. "absolute left-2.5 top-1/2 z-10 inline-flex size-4 min-w-0 -translate-y-1/2 rounded-sm !bg-transparent p-0 text-foreground-subtle hover:text-foreground",
  194. )}
  195. aria-label={pinLabel}
  196. >
  197. <Pin className="size-4" />
  198. </Button>
  199. </ControlHintTooltip>
  200. ) : null}
  201. {showArchiveAction ? (
  202. <ControlHintTooltip title={archiveLabel} side="right" align="center">
  203. <Button
  204. type="button"
  205. variant={isArchiveConfirming ? "destructive" : "ghost"}
  206. size={isArchiveConfirming ? "sm" : "icon-sm"}
  207. disabled={rowDisabled || isMutating}
  208. onMouseDown={(event) => {
  209. event.preventDefault();
  210. event.stopPropagation();
  211. }}
  212. onClick={(event) => onArchiveTask(event, task)}
  213. data-testid={testId(TID_TASK_ARCHIVE, task.taskId)}
  214. className={cn(
  215. "shrink-0",
  216. isArchiveConfirming ? "flex border-destructive/20 px-2" : "flex",
  217. )}
  218. aria-label={archiveLabel}
  219. >
  220. {isArchiveConfirming ? (
  221. <span>{intl.formatMessage({ id: "common.confirm" })}</span>
  222. ) : (
  223. <Archive className="size-3.5" />
  224. )}
  225. </Button>
  226. </ControlHintTooltip>
  227. ) : null}
  228. {showArchivedActions ? (
  229. <>
  230. {canUnarchiveTask ? (
  231. <ControlHintTooltip title={unarchiveLabel} side="right" align="center">
  232. <Button
  233. type="button"
  234. variant="ghost"
  235. size="icon-sm"
  236. disabled={rowDisabled || isMutating}
  237. onMouseDown={(event) => {
  238. event.preventDefault();
  239. event.stopPropagation();
  240. }}
  241. onClick={(event) => onUnarchiveTask(event, task)}
  242. className="shrink-0 text-foreground-subtle hover:text-foreground"
  243. aria-label={unarchiveLabel}
  244. >
  245. {/* Bugfix: Web 远控归档列表复用普通 task row,之前 archived 行没有桌面端的恢复入口。
  246. 这里按桌面归档列表语义补齐取消归档,并用远端图标标明操作会落到远端 host。 */}
  247. {isRemoteTask ? (
  248. <CloudDownload className="size-3.5" />
  249. ) : (
  250. <ArchiveX className="size-3.5" />
  251. )}
  252. </Button>
  253. </ControlHintTooltip>
  254. ) : null}
  255. {canDeleteArchivedTask ? (
  256. <ControlHintTooltip title={deleteLabel} side="right" align="center">
  257. <Button
  258. type="button"
  259. variant="ghost"
  260. size="icon-sm"
  261. disabled={rowDisabled || isMutating}
  262. onMouseDown={(event) => {
  263. event.preventDefault();
  264. event.stopPropagation();
  265. }}
  266. onClick={(event) => onDeleteArchivedTask(event, task)}
  267. className="shrink-0 text-destructive hover:text-destructive"
  268. aria-label={deleteLabel}
  269. >
  270. <Trash2 className="size-3.5" />
  271. </Button>
  272. </ControlHintTooltip>
  273. ) : null}
  274. </>
  275. ) : null}
  276. </li>
  277. );
  278. }