WorkspacePinnedTasksSection.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* eslint-disable max-lines -- pinned 列表现在同时承载本地查询、远端主动注入结果和任务操作分发,先集中保持交互一致。 */
  2. import { useCallback, useEffect, useMemo, useRef, useState } from "react";
  3. import type { MouseEvent as ReactMouseEvent } from "react";
  4. import type { ZCodeTaskMeta } from "@zcode/shared";
  5. import { toast } from "@/components/ui/toast.js";
  6. import { ContextMenu, ContextMenuTrigger } from "@/components/ui/context-menu.js";
  7. import { useGlobalTaskList } from "@/hooks/useGlobalTaskList.js";
  8. import { useLocalWorkspaceScopes } from "@/hooks/useLocalWorkspaceScopes.js";
  9. import { useBaseWorkspaceServices } from "@/hooks/useWorkspaceServices.js";
  10. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  11. import { buildTaskWorkspaceKey } from "@/lib/taskQueryCache.js";
  12. import { compareZCodeTaskListItems } from "@/lib/taskListOrdering.js";
  13. import { resolveTaskFileTreeTargetFromTabs } from "@/lib/taskFileTreeTarget.js";
  14. import { MemoTaskItem, TaskListItemContextMenuContent } from "@/TaskListItem.js";
  15. import { TaskRenameDialog } from "@/TaskRenameDialog.js";
  16. import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  17. import type { WorkspaceTabState } from "@/store/tabStore.js";
  18. import { applyTaskQueryCacheMutation } from "@/store/taskQueryCacheStore.js";
  19. import { TaskListRemoteSyncHint } from "@/TaskListRemoteSyncHint.js";
  20. import { useRemotePinnedTaskStore } from "@/store/remotePinnedTaskStore.js";
  21. import { useRemoteTimelineTaskStore } from "@/store/remoteTimelineTaskStore.js";
  22. import {
  23. getRemoteWorkspaceServicesForIdentity,
  24. useRemoteWorkspaceSessionStore,
  25. } from "@/store/remoteWorkspaceSessionStore.js";
  26. function buildPinnedItemKey(workspacePath: string, taskId: string, workspaceIdentity?: string) {
  27. return `${buildTaskWorkspaceKey(workspacePath, workspaceIdentity)}:${taskId}`;
  28. }
  29. interface PinnedTaskItemHandlers {
  30. onSelectTask: (taskId: string) => void;
  31. onArchiveTaskInline: (event: ReactMouseEvent, taskId: string) => void;
  32. onTogglePinTask: (taskId: string, pinned: boolean) => void;
  33. onStartRenameTask: (taskId: string, currentTitle: string) => void;
  34. onArchiveTask: (taskId: string) => void;
  35. onMarkTaskAsUnread: (taskId: string) => void;
  36. onOpenTaskContextMenu: (taskId: string) => void;
  37. onOpenFileTree: (task: ZCodeTaskMeta) => void;
  38. }
  39. function PinnedTasksSectionTitle({ title }: { title: string }) {
  40. return <h3 className="px-2.5 py-1 text-ui-base font-medium text-foreground-subtlest">{title}</h3>;
  41. }
  42. export function WorkspacePinnedTasksSection({
  43. workspaceTabs,
  44. activeWorkspacePath,
  45. activeWorkspaceIdentity,
  46. activeTaskId,
  47. taskSortBy,
  48. mobileActiveTaskKey,
  49. onSelectTask,
  50. onOpenFileTree,
  51. }: {
  52. workspaceTabs: WorkspaceTabState[];
  53. activeWorkspacePath: string;
  54. activeWorkspaceIdentity?: string;
  55. activeTaskId: string | null;
  56. taskSortBy: "created" | "updated";
  57. mobileActiveTaskKey?: string | null;
  58. onSelectTask: (
  59. targetWorkspacePath: string,
  60. taskId: string,
  61. targetWorkspaceIdentity?: string,
  62. expectedUnreadAt?: number,
  63. ) => void;
  64. onOpenFileTree?: (target: {
  65. workspacePath: string;
  66. workspaceName: string;
  67. workspaceIdentity?: string;
  68. workspaceRemoteSessionId?: string;
  69. }) => void;
  70. }) {
  71. const { intl } = useZCodeIntl();
  72. const baseServices = useBaseWorkspaceServices();
  73. const scopedWorkspaceTabs = useLocalWorkspaceScopes({
  74. workspaceTabs,
  75. });
  76. const remoteSessionIdByWorkspaceIdentity = useRemoteWorkspaceSessionStore(
  77. (state) => state.sessionIdByWorkspaceIdentity,
  78. );
  79. const removeTaskState = useZCodeSessionStore((state) => state.removeTaskState);
  80. const upsertOptimisticTaskListItem = useZCodeSessionStore(
  81. (state) => state.upsertOptimisticTaskListItem,
  82. );
  83. const removeOptimisticTaskListItem = useZCodeSessionStore(
  84. (state) => state.removeOptimisticTaskListItem,
  85. );
  86. const setTaskUnreadIndicator = useZCodeSessionStore((state) => state.setTaskUnreadIndicator);
  87. const [pendingArchiveItemKey, setPendingArchiveItemKey] = useState<string | null>(null);
  88. const [renamingItemKey, setRenamingItemKey] = useState<string | null>(null);
  89. const [contextMenuItemKey, setContextMenuItemKey] = useState<string | null>(null);
  90. const [renameDraft, setRenameDraft] = useState("");
  91. const [showAllTasks, setShowAllTasks] = useState(false);
  92. const collapsedLimit = 20;
  93. const renameInputRef = useRef<HTMLInputElement | null>(null);
  94. const pendingArchiveItemKeyRef = useRef<string | null>(pendingArchiveItemKey);
  95. const taskSortByRef = useRef(taskSortBy);
  96. const intlRef = useRef(intl);
  97. const onSelectTaskRef = useRef(onSelectTask);
  98. const removeTaskStateRef = useRef(removeTaskState);
  99. const upsertOptimisticTaskListItemRef = useRef(upsertOptimisticTaskListItem);
  100. const removeOptimisticTaskListItemRef = useRef(removeOptimisticTaskListItem);
  101. const setTaskUnreadIndicatorRef = useRef(setTaskUnreadIndicator);
  102. const taskItemHandlersByKeyRef = useRef(new Map<string, PinnedTaskItemHandlers>());
  103. pendingArchiveItemKeyRef.current = pendingArchiveItemKey;
  104. taskSortByRef.current = taskSortBy;
  105. intlRef.current = intl;
  106. onSelectTaskRef.current = onSelectTask;
  107. removeTaskStateRef.current = removeTaskState;
  108. upsertOptimisticTaskListItemRef.current = upsertOptimisticTaskListItem;
  109. removeOptimisticTaskListItemRef.current = removeOptimisticTaskListItem;
  110. setTaskUnreadIndicatorRef.current = setTaskUnreadIndicator;
  111. const { items: localItems } = useGlobalTaskList({
  112. kind: "pinned",
  113. workspaceTabs: scopedWorkspaceTabs,
  114. sortBy: taskSortBy,
  115. searchQuery: "",
  116. expanded: true,
  117. collapsedLimit,
  118. });
  119. const remotePinnedItemsByWorkspaceKey = useRemotePinnedTaskStore(
  120. (state) => state.itemsByWorkspaceKey,
  121. );
  122. const remotePinnedLoadingByWorkspaceKey = useRemotePinnedTaskStore(
  123. (state) => state.loadingByWorkspaceKey,
  124. );
  125. const remoteItems = useMemo(() => {
  126. const remoteWorkspaceKeys = new Set(
  127. workspaceTabs
  128. .filter((tab) => tab.workspaceIdentity || tab.remoteTarget || tab.remoteSessionId)
  129. .map((tab) => buildTaskWorkspaceKey(tab.workspacePath, tab.workspaceIdentity)),
  130. );
  131. return [...remoteWorkspaceKeys].flatMap(
  132. (workspaceKey) => remotePinnedItemsByWorkspaceKey[workspaceKey] ?? [],
  133. );
  134. }, [remotePinnedItemsByWorkspaceKey, workspaceTabs]);
  135. const sortedItems = useMemo(() => {
  136. return [...localItems, ...remoteItems].sort((left, right) =>
  137. compareZCodeTaskListItems(left, right, taskSortBy),
  138. );
  139. }, [localItems, remoteItems, taskSortBy]);
  140. const items = showAllTasks ? sortedItems : sortedItems.slice(0, collapsedLimit);
  141. const total = sortedItems.length;
  142. const syncingRemoteWorkspaces = workspaceTabs.some((tab) => {
  143. if (!tab.workspaceIdentity && !tab.remoteTarget && !tab.remoteSessionId) {
  144. return false;
  145. }
  146. return Boolean(
  147. remotePinnedLoadingByWorkspaceKey[
  148. buildTaskWorkspaceKey(tab.workspacePath, tab.workspaceIdentity)
  149. ],
  150. );
  151. });
  152. const canToggleExpanded = total > collapsedLimit;
  153. const sectionTitle = intl.formatMessage({ id: "taskList.pinnedSection" });
  154. const activeWorkspaceKey = buildTaskWorkspaceKey(activeWorkspacePath, activeWorkspaceIdentity);
  155. const itemByKey = useMemo(() => {
  156. const nextItemByKey = new Map<string, ZCodeTaskMeta>();
  157. for (const item of items) {
  158. nextItemByKey.set(
  159. buildPinnedItemKey(item.workspacePath, item.taskId, item.workspaceIdentity),
  160. item,
  161. );
  162. }
  163. return nextItemByKey;
  164. }, [items]);
  165. const itemByKeyRef = useRef(itemByKey);
  166. itemByKeyRef.current = itemByKey;
  167. const workspaceTabsRef = useRef(workspaceTabs);
  168. workspaceTabsRef.current = workspaceTabs;
  169. const onOpenFileTreeRef = useRef(onOpenFileTree);
  170. onOpenFileTreeRef.current = onOpenFileTree;
  171. const resolveTaskServices = useCallback(
  172. (workspaceIdentity?: string) => {
  173. if (!workspaceIdentity) {
  174. return baseServices;
  175. }
  176. return getRemoteWorkspaceServicesForIdentity(workspaceIdentity);
  177. },
  178. [baseServices],
  179. );
  180. const resolveTaskServicesRef = useRef(resolveTaskServices);
  181. resolveTaskServicesRef.current = resolveTaskServices;
  182. const handleCancelArchiveConfirm = useCallback(() => {
  183. setPendingArchiveItemKey(null);
  184. }, []);
  185. const handleCancelRenameTask = useCallback(() => {
  186. setRenamingItemKey(null);
  187. setRenameDraft("");
  188. }, []);
  189. const findItemByKey = useCallback(
  190. (itemKey: string) => itemByKey.get(itemKey) ?? null,
  191. [itemByKey],
  192. );
  193. const getCurrentPinnedItemContext = useCallback((itemKey: string) => {
  194. const item = itemByKeyRef.current.get(itemKey);
  195. if (!item) {
  196. return null;
  197. }
  198. const services = resolveTaskServicesRef.current(item.workspaceIdentity);
  199. if (!services) {
  200. return null;
  201. }
  202. return { item, services };
  203. }, []);
  204. const archivePinnedItem = useCallback(
  205. (itemKey: string) => {
  206. const current = getCurrentPinnedItemContext(itemKey);
  207. if (!current) {
  208. return;
  209. }
  210. const { item, services } = current;
  211. void services.zcodeTaskService
  212. .archiveTask({
  213. taskId: item.taskId,
  214. workspacePath: item.workspacePath,
  215. ...(item.workspaceIdentity ? { workspaceIdentity: item.workspaceIdentity } : {}),
  216. })
  217. .then((meta) => {
  218. removeTaskStateRef.current(item.workspacePath, item.taskId, item.workspaceIdentity);
  219. if (item.workspaceIdentity) {
  220. useRemotePinnedTaskStore
  221. .getState()
  222. .removeTask(item.workspacePath, item.taskId, item.workspaceIdentity);
  223. useRemoteTimelineTaskStore
  224. .getState()
  225. .removeTask(item.workspacePath, item.taskId, item.workspaceIdentity);
  226. }
  227. applyTaskQueryCacheMutation({
  228. previousTask: item,
  229. nextTask: meta,
  230. previousState: { pinned: true, archived: false },
  231. nextState: { pinned: false, archived: true },
  232. });
  233. });
  234. },
  235. [getCurrentPinnedItemContext],
  236. );
  237. const selectPinnedItem = useCallback((itemKey: string) => {
  238. const item = itemByKeyRef.current.get(itemKey);
  239. if (!item) {
  240. return;
  241. }
  242. onSelectTaskRef.current(
  243. item.workspacePath,
  244. item.taskId,
  245. item.workspaceIdentity,
  246. item.unreadAt,
  247. );
  248. }, []);
  249. const archivePinnedItemInline = useCallback(
  250. (event: ReactMouseEvent, itemKey: string) => {
  251. event.stopPropagation();
  252. if (pendingArchiveItemKeyRef.current !== itemKey) {
  253. setPendingArchiveItemKey(itemKey);
  254. return;
  255. }
  256. setPendingArchiveItemKey(null);
  257. archivePinnedItem(itemKey);
  258. },
  259. [archivePinnedItem],
  260. );
  261. const togglePinnedItemPin = useCallback(
  262. (itemKey: string, pinned: boolean) => {
  263. const current = getCurrentPinnedItemContext(itemKey);
  264. if (!current) {
  265. return;
  266. }
  267. const { item, services } = current;
  268. // unpin 以前等 RPC 返回后才把任务移出 pinned 区,重查期间会出现列表闪烁。
  269. // 这里先乐观移动,RPC 失败再把任务恢复为 pinned。
  270. if (item.workspaceIdentity) {
  271. useRemotePinnedTaskStore
  272. .getState()
  273. .removeTask(item.workspacePath, item.taskId, item.workspaceIdentity);
  274. if (!pinned) {
  275. useRemoteTimelineTaskStore.getState().upsertTask(item, taskSortByRef.current);
  276. }
  277. }
  278. applyTaskQueryCacheMutation({
  279. previousTask: item,
  280. nextTask: item,
  281. previousState: { pinned: true, archived: false },
  282. nextState: { pinned, archived: false },
  283. });
  284. void services.zcodeTaskService
  285. .setTaskPinned({
  286. taskId: item.taskId,
  287. workspacePath: item.workspacePath,
  288. ...(item.workspaceIdentity ? { workspaceIdentity: item.workspaceIdentity } : {}),
  289. pinned,
  290. })
  291. .then((meta) => {
  292. removeOptimisticTaskListItemRef.current(
  293. item.workspacePath,
  294. item.taskId,
  295. item.workspaceIdentity,
  296. );
  297. if (item.workspaceIdentity && pinned) {
  298. useRemotePinnedTaskStore.getState().upsertTask(meta);
  299. useRemoteTimelineTaskStore
  300. .getState()
  301. .removeTask(item.workspacePath, item.taskId, item.workspaceIdentity);
  302. }
  303. if (item.workspaceIdentity && !pinned) {
  304. useRemoteTimelineTaskStore.getState().upsertTask(meta, taskSortByRef.current);
  305. }
  306. applyTaskQueryCacheMutation({
  307. previousTask: item,
  308. nextTask: meta,
  309. previousState: { pinned, archived: false },
  310. nextState: { pinned, archived: false },
  311. });
  312. })
  313. .catch(() => {
  314. if (item.workspaceIdentity) {
  315. useRemotePinnedTaskStore.getState().upsertTask(item);
  316. useRemoteTimelineTaskStore
  317. .getState()
  318. .removeTask(item.workspacePath, item.taskId, item.workspaceIdentity);
  319. }
  320. applyTaskQueryCacheMutation({
  321. previousTask: item,
  322. nextTask: item,
  323. previousState: { pinned, archived: false },
  324. nextState: { pinned: true, archived: false },
  325. });
  326. toast(intlRef.current.formatMessage({ id: "taskList.pinFailed" }));
  327. });
  328. },
  329. [getCurrentPinnedItemContext],
  330. );
  331. const startPinnedItemRename = useCallback((itemKey: string, currentTitle: string) => {
  332. setPendingArchiveItemKey(null);
  333. setRenamingItemKey(itemKey);
  334. setRenameDraft(currentTitle);
  335. }, []);
  336. const markPinnedItemAsUnread = useCallback(
  337. (itemKey: string) => {
  338. const current = getCurrentPinnedItemContext(itemKey);
  339. if (!current) {
  340. return;
  341. }
  342. const { item, services } = current;
  343. void services.zcodeTaskService
  344. .setTaskUnread({
  345. taskId: item.taskId,
  346. workspacePath: item.workspacePath,
  347. ...(item.workspaceIdentity ? { workspaceIdentity: item.workspaceIdentity } : {}),
  348. unread: true,
  349. })
  350. .then((meta) => {
  351. setTaskUnreadIndicatorRef.current(
  352. item.workspacePath,
  353. item.taskId,
  354. true,
  355. item.workspaceIdentity,
  356. );
  357. upsertOptimisticTaskListItemRef.current(item.workspacePath, meta, item.workspaceIdentity);
  358. if (item.workspaceIdentity) {
  359. useRemotePinnedTaskStore.getState().upsertTask(meta);
  360. }
  361. applyTaskQueryCacheMutation({
  362. previousTask: item,
  363. nextTask: meta,
  364. previousState: { pinned: true, archived: false },
  365. nextState: { pinned: true, archived: false },
  366. });
  367. });
  368. },
  369. [getCurrentPinnedItemContext],
  370. );
  371. const openPinnedItemContextMenu = useCallback((itemKey: string) => {
  372. // pinned row handler 按 itemKey 缓存;打开菜单时从 ref 读取最新确认态,
  373. // 避免 pendingArchiveItemKey 变化时重建所有 TaskListItem callback。
  374. if (pendingArchiveItemKeyRef.current === itemKey) {
  375. setPendingArchiveItemKey(null);
  376. }
  377. setContextMenuItemKey(itemKey);
  378. }, []);
  379. const getPinnedTaskItemHandlers = useCallback(
  380. (itemKey: string) => {
  381. let handlers = taskItemHandlersByKeyRef.current.get(itemKey);
  382. if (!handlers) {
  383. // trace 显示 pinned row 的 action props 仍因 map 内联闭包变化。
  384. // 每个 itemKey 只创建一次 handler,实际执行时再通过 ref 读取最新 item/services/state。
  385. handlers = {
  386. onSelectTask: () => {
  387. selectPinnedItem(itemKey);
  388. },
  389. onArchiveTaskInline: (event) => {
  390. archivePinnedItemInline(event, itemKey);
  391. },
  392. onTogglePinTask: (_taskId, pinned) => {
  393. togglePinnedItemPin(itemKey, pinned);
  394. },
  395. onStartRenameTask: (_taskId, currentTitle) => {
  396. startPinnedItemRename(itemKey, currentTitle);
  397. },
  398. onArchiveTask: () => {
  399. archivePinnedItem(itemKey);
  400. },
  401. onMarkTaskAsUnread: () => {
  402. markPinnedItemAsUnread(itemKey);
  403. },
  404. onOpenTaskContextMenu: () => {
  405. openPinnedItemContextMenu(itemKey);
  406. },
  407. onOpenFileTree: (task) => {
  408. const target = resolveTaskFileTreeTargetFromTabs(task, workspaceTabsRef.current);
  409. if (target) {
  410. onOpenFileTreeRef.current?.(target);
  411. }
  412. },
  413. };
  414. taskItemHandlersByKeyRef.current.set(itemKey, handlers);
  415. }
  416. return handlers;
  417. },
  418. [
  419. archivePinnedItem,
  420. archivePinnedItemInline,
  421. markPinnedItemAsUnread,
  422. openPinnedItemContextMenu,
  423. selectPinnedItem,
  424. startPinnedItemRename,
  425. togglePinnedItemPin,
  426. ],
  427. );
  428. useEffect(() => {
  429. if (!renamingItemKey) {
  430. return;
  431. }
  432. renameInputRef.current?.focus();
  433. renameInputRef.current?.select();
  434. }, [renamingItemKey]);
  435. useEffect(() => {
  436. if (!contextMenuItemKey) {
  437. return;
  438. }
  439. if (!findItemByKey(contextMenuItemKey)) {
  440. setContextMenuItemKey(null);
  441. }
  442. }, [contextMenuItemKey, findItemByKey]);
  443. useEffect(() => {
  444. for (const itemKey of taskItemHandlersByKeyRef.current.keys()) {
  445. if (!itemByKey.has(itemKey)) {
  446. taskItemHandlersByKeyRef.current.delete(itemKey);
  447. }
  448. }
  449. }, [itemByKey]);
  450. const contextMenuItem = contextMenuItemKey ? findItemByKey(contextMenuItemKey) : null;
  451. const contextMenuServices = contextMenuItem
  452. ? resolveTaskServices(contextMenuItem.workspaceIdentity)
  453. : null;
  454. if (items.length === 0) {
  455. // 切换/加入工作区时 pinned 查询会先进入 loading,但此时没有可展示的数据。
  456. // 不能仍渲染“已置顶 + 正在获取任务”,否则侧栏每次切换都出现一次无实际帮助的 loading。
  457. // 有缓存数据时继续走下面的正常渲染路径,保持 stale-while-revalidate 的展示体验。
  458. return null;
  459. }
  460. return (
  461. <div className="flex flex-col gap-1 px-2 empty:hidden">
  462. {renamingItemKey !== null ? (
  463. <TaskRenameDialog
  464. open
  465. value={renameDraft}
  466. inputRef={renameInputRef}
  467. intl={intl}
  468. onOpenChange={(open) => {
  469. if (!open) {
  470. handleCancelRenameTask();
  471. }
  472. }}
  473. onChange={setRenameDraft}
  474. onCancel={handleCancelRenameTask}
  475. onConfirm={() => {
  476. if (!renamingItemKey) {
  477. return;
  478. }
  479. const item = findItemByKey(renamingItemKey);
  480. if (!item) {
  481. handleCancelRenameTask();
  482. return;
  483. }
  484. const services = resolveTaskServices(item.workspaceIdentity);
  485. if (!services) {
  486. handleCancelRenameTask();
  487. return;
  488. }
  489. void services.zcodeTaskService
  490. .renameTask({
  491. taskId: item.taskId,
  492. workspacePath: item.workspacePath,
  493. ...(item.workspaceIdentity ? { workspaceIdentity: item.workspaceIdentity } : {}),
  494. title: renameDraft.trim(),
  495. })
  496. .then((meta) => {
  497. upsertOptimisticTaskListItem(item.workspacePath, meta, item.workspaceIdentity);
  498. if (item.workspaceIdentity) {
  499. useRemotePinnedTaskStore.getState().upsertTask(meta);
  500. }
  501. applyTaskQueryCacheMutation({
  502. previousTask: item,
  503. nextTask: meta,
  504. previousState: { pinned: true, archived: false },
  505. nextState: { pinned: true, archived: false },
  506. });
  507. handleCancelRenameTask();
  508. })
  509. .catch(() => {
  510. toast(intl.formatMessage({ id: "taskList.renameFailed" }));
  511. });
  512. }}
  513. />
  514. ) : null}
  515. <PinnedTasksSectionTitle title={sectionTitle} />
  516. <ContextMenu
  517. onOpenChange={(open) => {
  518. if (!open) {
  519. setContextMenuItemKey(null);
  520. }
  521. }}
  522. >
  523. <ContextMenuTrigger asChild>
  524. <ul className="space-y-0.5">
  525. {items.map((item) => {
  526. const itemKey = buildPinnedItemKey(
  527. item.workspacePath,
  528. item.taskId,
  529. item.workspaceIdentity,
  530. );
  531. const services = resolveTaskServices(item.workspaceIdentity);
  532. if (!services) {
  533. return null;
  534. }
  535. const handlers = getPinnedTaskItemHandlers(itemKey);
  536. return (
  537. <MemoTaskItem
  538. key={itemKey}
  539. workspacePath={item.workspacePath}
  540. remoteSessionId={
  541. item.workspaceIdentity
  542. ? remoteSessionIdByWorkspaceIdentity[item.workspaceIdentity]
  543. : undefined
  544. }
  545. task={item}
  546. isPinned
  547. isActive={
  548. // 同路径远端 workspace 可能包含相同 taskId,选中态必须按 workspaceIdentity 隔离。
  549. buildTaskWorkspaceKey(item.workspacePath, item.workspaceIdentity) ===
  550. activeWorkspaceKey && item.taskId === activeTaskId
  551. }
  552. isMobileActive={mobileActiveTaskKey === itemKey}
  553. onSelectTask={handlers.onSelectTask}
  554. onArchiveTaskInline={handlers.onArchiveTaskInline}
  555. onCancelArchiveConfirm={handleCancelArchiveConfirm}
  556. isArchiveConfirming={pendingArchiveItemKey === itemKey}
  557. onTogglePinTask={handlers.onTogglePinTask}
  558. onStartRenameTask={handlers.onStartRenameTask}
  559. onArchiveTask={handlers.onArchiveTask}
  560. onMarkTaskAsUnread={handlers.onMarkTaskAsUnread}
  561. onOpenTaskContextMenu={handlers.onOpenTaskContextMenu}
  562. onOpenFileTree={onOpenFileTree ? handlers.onOpenFileTree : undefined}
  563. intl={intl}
  564. />
  565. );
  566. })}
  567. </ul>
  568. </ContextMenuTrigger>
  569. {contextMenuItem && contextMenuServices ? (
  570. <TaskListItemContextMenuContent
  571. workspacePath={contextMenuItem.workspacePath}
  572. remoteSessionId={
  573. contextMenuItem.workspaceIdentity
  574. ? remoteSessionIdByWorkspaceIdentity[contextMenuItem.workspaceIdentity]
  575. : undefined
  576. }
  577. task={contextMenuItem}
  578. isPinned
  579. intl={intl}
  580. onTogglePinTask={(_taskId, pinned) => {
  581. if (contextMenuItem.workspaceIdentity) {
  582. useRemotePinnedTaskStore
  583. .getState()
  584. .removeTask(
  585. contextMenuItem.workspacePath,
  586. contextMenuItem.taskId,
  587. contextMenuItem.workspaceIdentity,
  588. );
  589. if (!pinned) {
  590. useRemoteTimelineTaskStore.getState().upsertTask(contextMenuItem, taskSortBy);
  591. }
  592. }
  593. applyTaskQueryCacheMutation({
  594. previousTask: contextMenuItem,
  595. nextTask: contextMenuItem,
  596. previousState: { pinned: true, archived: false },
  597. nextState: { pinned, archived: false },
  598. });
  599. void contextMenuServices.zcodeTaskService
  600. .setTaskPinned({
  601. taskId: contextMenuItem.taskId,
  602. workspacePath: contextMenuItem.workspacePath,
  603. ...(contextMenuItem.workspaceIdentity
  604. ? { workspaceIdentity: contextMenuItem.workspaceIdentity }
  605. : {}),
  606. pinned,
  607. })
  608. .then((meta) => {
  609. removeOptimisticTaskListItem(
  610. contextMenuItem.workspacePath,
  611. contextMenuItem.taskId,
  612. contextMenuItem.workspaceIdentity,
  613. );
  614. if (contextMenuItem.workspaceIdentity && pinned) {
  615. useRemotePinnedTaskStore.getState().upsertTask(meta);
  616. useRemoteTimelineTaskStore
  617. .getState()
  618. .removeTask(
  619. contextMenuItem.workspacePath,
  620. contextMenuItem.taskId,
  621. contextMenuItem.workspaceIdentity,
  622. );
  623. }
  624. if (contextMenuItem.workspaceIdentity && !pinned) {
  625. useRemoteTimelineTaskStore.getState().upsertTask(meta, taskSortBy);
  626. }
  627. applyTaskQueryCacheMutation({
  628. previousTask: contextMenuItem,
  629. nextTask: meta,
  630. previousState: { pinned, archived: false },
  631. nextState: { pinned, archived: false },
  632. });
  633. })
  634. .catch(() => {
  635. if (contextMenuItem.workspaceIdentity) {
  636. useRemotePinnedTaskStore.getState().upsertTask(contextMenuItem);
  637. useRemoteTimelineTaskStore
  638. .getState()
  639. .removeTask(
  640. contextMenuItem.workspacePath,
  641. contextMenuItem.taskId,
  642. contextMenuItem.workspaceIdentity,
  643. );
  644. }
  645. applyTaskQueryCacheMutation({
  646. previousTask: contextMenuItem,
  647. nextTask: contextMenuItem,
  648. previousState: { pinned, archived: false },
  649. nextState: { pinned: true, archived: false },
  650. });
  651. toast(intl.formatMessage({ id: "taskList.pinFailed" }));
  652. });
  653. }}
  654. onStartRenameTask={(_taskId, currentTitle) => {
  655. setPendingArchiveItemKey(null);
  656. setRenamingItemKey(contextMenuItemKey);
  657. setRenameDraft(currentTitle);
  658. }}
  659. onArchiveTask={() => {
  660. void contextMenuServices.zcodeTaskService
  661. .archiveTask({
  662. taskId: contextMenuItem.taskId,
  663. workspacePath: contextMenuItem.workspacePath,
  664. ...(contextMenuItem.workspaceIdentity
  665. ? { workspaceIdentity: contextMenuItem.workspaceIdentity }
  666. : {}),
  667. })
  668. .then((meta) => {
  669. removeTaskState(
  670. contextMenuItem.workspacePath,
  671. contextMenuItem.taskId,
  672. contextMenuItem.workspaceIdentity,
  673. );
  674. if (contextMenuItem.workspaceIdentity) {
  675. useRemotePinnedTaskStore
  676. .getState()
  677. .removeTask(
  678. contextMenuItem.workspacePath,
  679. contextMenuItem.taskId,
  680. contextMenuItem.workspaceIdentity,
  681. );
  682. useRemoteTimelineTaskStore
  683. .getState()
  684. .removeTask(
  685. contextMenuItem.workspacePath,
  686. contextMenuItem.taskId,
  687. contextMenuItem.workspaceIdentity,
  688. );
  689. }
  690. applyTaskQueryCacheMutation({
  691. previousTask: contextMenuItem,
  692. nextTask: meta,
  693. previousState: { pinned: true, archived: false },
  694. nextState: { pinned: false, archived: true },
  695. });
  696. });
  697. }}
  698. onMarkTaskAsUnread={() => {
  699. void contextMenuServices.zcodeTaskService
  700. .setTaskUnread({
  701. taskId: contextMenuItem.taskId,
  702. workspacePath: contextMenuItem.workspacePath,
  703. ...(contextMenuItem.workspaceIdentity
  704. ? { workspaceIdentity: contextMenuItem.workspaceIdentity }
  705. : {}),
  706. unread: true,
  707. })
  708. .then((meta) => {
  709. setTaskUnreadIndicator(
  710. contextMenuItem.workspacePath,
  711. contextMenuItem.taskId,
  712. true,
  713. contextMenuItem.workspaceIdentity,
  714. );
  715. upsertOptimisticTaskListItem(
  716. contextMenuItem.workspacePath,
  717. meta,
  718. contextMenuItem.workspaceIdentity,
  719. );
  720. if (contextMenuItem.workspaceIdentity) {
  721. useRemotePinnedTaskStore.getState().upsertTask(meta);
  722. }
  723. applyTaskQueryCacheMutation({
  724. previousTask: contextMenuItem,
  725. nextTask: meta,
  726. previousState: { pinned: true, archived: false },
  727. nextState: { pinned: true, archived: false },
  728. });
  729. });
  730. }}
  731. />
  732. ) : null}
  733. </ContextMenu>
  734. {syncingRemoteWorkspaces ? <TaskListRemoteSyncHint /> : null}
  735. {canToggleExpanded ? (
  736. <div className="cursor-pointer pl-8.5">
  737. <span
  738. className="text-ui-base text-foreground-subtlest hover:text-foreground-subtle"
  739. onClick={() => {
  740. setShowAllTasks((current) => !current);
  741. }}
  742. >
  743. {intl.formatMessage({
  744. id: showAllTasks ? "taskList.showLess" : "taskList.showMore",
  745. })}
  746. </span>
  747. </div>
  748. ) : null}
  749. </div>
  750. );
  751. }