The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { OlderSettledScope } from "@t3tools/client-runtime/state/threads"; |
| 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { useMemo, useSyncExternalStore } from "react"; |
| 4 | |
| 5 | import { keepMarked } from "../../i18n/messages"; |
| 6 | import { scopedProjectKey } from "../../lib/scopedEntities"; |
| 7 | import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../../state/olderSettledShelf"; |
| 8 | import type { PendingNewTask } from "../../state/use-pending-new-tasks"; |
| 9 | import { |
| 10 | buildThreadListV2Items, |
| 11 | buildThreadListV2ListItems, |
| 12 | getThreadListV2OrderedSection, |
| 13 | threadListInboxReturns, |
| 14 | threadListV2LayoutTime, |
| 15 | } from "./threadListV2"; |
| 16 | import { computeThreadMoveAvailability } from "./threadOrder"; |
| 17 | |
| 18 | /** |
| 19 | * Screens' select handlers read the content-only key set; a fresh one per list rebuild gave |
| 20 | * every row a new handler whenever any thread shell changed. Keyed by a string, the set keeps |
| 21 | * its identity until its keys change. |
| 22 | */ |
| 23 | function useKeySet(newlineSeparatedKeys: string): ReadonlySet<string> { |
| 24 | return useMemo( |
| 25 | () => new Set(newlineSeparatedKeys.length === 0 ? [] : newlineSeparatedKeys.split("\n")), |
| 26 | [newlineSeparatedKeys], |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | type LayoutInput = Parameters<typeof buildThreadListV2Items>[0]; |
| 31 | |
| 32 | /** |
| 33 | * The React Compiler drops unread dependencies, so pass the expired-grace version through |
| 34 | * this call to keep inbox updates tied to its subscription. |
| 35 | */ |
| 36 | function observeInbox( |
| 37 | threads: Parameters<typeof threadListInboxReturns.observe>[0], |
| 38 | _expiredGraceVersion: number, |
| 39 | ) { |
| 40 | return threadListInboxReturns.observe(threads); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Shelf callbacks prevent compiler caching across the whole derivation, so each stage keeps |
| 45 | * its own memo boundary to preserve the list's identity during unrelated navigation renders. |
| 46 | */ |
| 47 | export function useThreadListV2(input: { |
| 48 | readonly threads: LayoutInput["threads"]; |
| 49 | readonly pendingTasks: ReadonlyArray<PendingNewTask>; |
| 50 | readonly environmentId: EnvironmentId | null; |
| 51 | readonly projectRefs: LayoutInput["projectRefs"]; |
| 52 | readonly searchQuery: string; |
| 53 | /** Threads that match the search by content, keyed like `threadSearchMatchKey`. */ |
| 54 | readonly searchMatches: ReadonlyMap<string, unknown>; |
| 55 | readonly settlementEnvironmentIds: ReadonlySet<EnvironmentId>; |
| 56 | readonly snoozeEnvironmentIds: ReadonlySet<EnvironmentId>; |
| 57 | readonly pinReorderEnvironmentIds: ReadonlySet<EnvironmentId>; |
| 58 | readonly activeReorderEnvironmentIds: ReadonlySet<EnvironmentId>; |
| 59 | readonly queuedThreadKeys: ReadonlySet<string>; |
| 60 | readonly settledLimit: number; |
| 61 | readonly now: string; |
| 62 | readonly pendingOrder: LayoutInput["pendingOrder"]; |
| 63 | readonly shelfPreferencesLoaded: boolean; |
| 64 | readonly workingShelfEnabled: boolean; |
| 65 | readonly workingShelfExpanded: boolean; |
| 66 | readonly snoozedShelfExpanded: boolean; |
| 67 | readonly settledShelfExpanded: boolean; |
| 68 | readonly selectedThreadKey: string | null; |
| 69 | }) { |
| 70 | const { threads, workingShelfEnabled } = input; |
| 71 | const layoutTime = useMemo( |
| 72 | () => threadListV2LayoutTime({ threads, now: input.now, searchQuery: input.searchQuery }), |
| 73 | [threads, input.now, input.searchQuery], |
| 74 | ); |
| 75 | // A thread can stop working when the threads change, or when its exit grace expires. |
| 76 | const inboxVersion = useSyncExternalStore( |
| 77 | threadListInboxReturns.subscribe, |
| 78 | threadListInboxReturns.getSnapshot, |
| 79 | ); |
| 80 | const inbox = useMemo( |
| 81 | () => observeInbox(workingShelfEnabled ? threads : null, inboxVersion), |
| 82 | [threads, workingShelfEnabled, inboxVersion], |
| 83 | ); |
| 84 | // Compute move availability per section; per-row planning is quadratic. |
| 85 | const moveAvailability = useMemo(() => { |
| 86 | const sectionAvailability = (section: "pinned" | "active") => |
| 87 | computeThreadMoveAvailability({ |
| 88 | allThreads: threads, |
| 89 | section, |
| 90 | pendingOrder: input.pendingOrder, |
| 91 | reorderableEnvironmentIds: |
| 92 | section === "pinned" ? input.pinReorderEnvironmentIds : input.activeReorderEnvironmentIds, |
| 93 | ordered: getThreadListV2OrderedSection({ |
| 94 | threads, |
| 95 | section, |
| 96 | pendingOrder: input.pendingOrder, |
| 97 | now: layoutTime, |
| 98 | settlementEnvironmentIds: input.settlementEnvironmentIds, |
| 99 | snoozeEnvironmentIds: input.snoozeEnvironmentIds, |
| 100 | queuedThreadKeys: input.queuedThreadKeys, |
| 101 | }), |
| 102 | }); |
| 103 | return new Map([ |
| 104 | ...sectionAvailability("pinned"), |
| 105 | ...(workingShelfEnabled ? [] : sectionAvailability("active")), |
| 106 | ]); |
| 107 | }, [ |
| 108 | threads, |
| 109 | input.pendingOrder, |
| 110 | input.pinReorderEnvironmentIds, |
| 111 | input.activeReorderEnvironmentIds, |
| 112 | layoutTime, |
| 113 | input.settlementEnvironmentIds, |
| 114 | input.snoozeEnvironmentIds, |
| 115 | input.queuedThreadKeys, |
| 116 | workingShelfEnabled, |
| 117 | ]); |
| 118 | // Older settled threads load in pages; the shelf lists them only as far as they are loaded. |
| 119 | const scopes = useMemo( |
| 120 | () => |
| 121 | olderSettledScopes({ |
| 122 | environmentId: input.environmentId, |
| 123 | projectRefs: input.projectRefs, |
| 124 | settlementEnvironmentIds: input.settlementEnvironmentIds, |
| 125 | }), |
| 126 | [input.environmentId, input.projectRefs, input.settlementEnvironmentIds], |
| 127 | ); |
| 128 | const olderSettledShelf = useOlderSettledShelf({ |
| 129 | scopes, |
| 130 | expanded: input.settledShelfExpanded, |
| 131 | // A search lists only matches, so the shelf would page forever; loaded pages stay. |
| 132 | visibleCount: input.searchQuery.trim().length === 0 ? input.settledLimit : 0, |
| 133 | }); |
| 134 | const { isListed, unloadedCount } = olderSettledShelf; |
| 135 | const layout = useMemo( |
| 136 | () => |
| 137 | buildThreadListV2Items({ |
| 138 | pendingOrder: input.pendingOrder, |
| 139 | threads: threads.filter((thread) => thread.archivedAt === null), |
| 140 | environmentId: input.environmentId, |
| 141 | projectRefs: input.projectRefs, |
| 142 | searchQuery: input.searchQuery, |
| 143 | matchedThreadKeys: new Set(input.searchMatches.keys()), |
| 144 | settlementEnvironmentIds: input.settlementEnvironmentIds, |
| 145 | snoozeEnvironmentIds: input.snoozeEnvironmentIds, |
| 146 | queuedThreadKeys: input.queuedThreadKeys, |
| 147 | settledLimit: input.settledLimit, |
| 148 | now: layoutTime, |
| 149 | workingShelfEnabled, |
| 150 | workingShelfExpanded: input.workingShelfExpanded, |
| 151 | inboxReturnAt: inbox.returnedAt, |
| 152 | isWorking: inbox.isWorking, |
| 153 | snoozedShelfExpanded: input.snoozedShelfExpanded, |
| 154 | settledShelfExpanded: input.settledShelfExpanded, |
| 155 | selectedThreadKey: input.selectedThreadKey, |
| 156 | isListedSettled: isListed, |
| 157 | unloadedSettledCount: unloadedCount( |
| 158 | threads.filter( |
| 159 | (thread) => |
| 160 | thread.archivedAt === null && |
| 161 | scopes.some( |
| 162 | (scope) => |
| 163 | scope.environmentId === thread.environmentId && |
| 164 | (scope.projectIds === null || scope.projectIds.includes(thread.projectId)), |
| 165 | ), |
| 166 | ), |
| 167 | ), |
| 168 | }), |
| 169 | [ |
| 170 | input.pendingOrder, |
| 171 | threads, |
| 172 | input.environmentId, |
| 173 | input.projectRefs, |
| 174 | input.searchQuery, |
| 175 | input.searchMatches, |
| 176 | input.settlementEnvironmentIds, |
| 177 | input.snoozeEnvironmentIds, |
| 178 | input.queuedThreadKeys, |
| 179 | input.settledLimit, |
| 180 | layoutTime, |
| 181 | workingShelfEnabled, |
| 182 | input.workingShelfExpanded, |
| 183 | inbox.returnedAt, |
| 184 | inbox.isWorking, |
| 185 | input.snoozedShelfExpanded, |
| 186 | input.settledShelfExpanded, |
| 187 | input.selectedThreadKey, |
| 188 | isListed, |
| 189 | unloadedCount, |
| 190 | scopes, |
| 191 | ], |
| 192 | ); |
| 193 | useOlderSettledShelfGrowth(olderSettledShelf, layout.listedSettledThreads); |
| 194 | const contentOnlyThreadKeys = useKeySet([...layout.contentOnlyThreadKeys].sort().join("\n")); |
| 195 | const pendingTasks = useMemo(() => { |
| 196 | const scopedProjectKeys = |
| 197 | input.projectRefs == null |
| 198 | ? null |
| 199 | : new Set( |
| 200 | input.projectRefs.map((ref) => scopedProjectKey(ref.environmentId, ref.projectId)), |
| 201 | ); |
| 202 | const searchQuery = input.searchQuery.trim().toLocaleLowerCase(); |
| 203 | return input.pendingTasks.filter( |
| 204 | (pendingTask) => |
| 205 | (input.environmentId === null || pendingTask.environmentId === input.environmentId) && |
| 206 | (scopedProjectKeys === null || |
| 207 | scopedProjectKeys.has( |
| 208 | scopedProjectKey(pendingTask.environmentId, pendingTask.projectId), |
| 209 | )) && |
| 210 | (searchQuery.length === 0 || pendingTask.title.toLocaleLowerCase().includes(searchQuery)), |
| 211 | ); |
| 212 | }, [input.projectRefs, input.searchQuery, input.pendingTasks, input.environmentId]); |
| 213 | const items = useMemo( |
| 214 | () => |
| 215 | buildThreadListV2ListItems({ |
| 216 | items: layout.items, |
| 217 | pendingTasks, |
| 218 | workingCount: layout.workingCount, |
| 219 | workingShelfExpanded: input.workingShelfExpanded, |
| 220 | workingShelfHeaderIndex: layout.workingShelfHeaderIndex, |
| 221 | snoozedCount: layout.snoozedCount, |
| 222 | snoozedShelfExpanded: input.snoozedShelfExpanded, |
| 223 | snoozedShelfHeaderIndex: layout.snoozedShelfHeaderIndex, |
| 224 | settledCount: layout.settledCount, |
| 225 | settledShelfExpanded: input.settledShelfExpanded, |
| 226 | settledShelfHeaderIndex: layout.settledShelfHeaderIndex, |
| 227 | snoozeLabelNow: `${input.now.slice(0, 16)}:00.000Z`, |
| 228 | snoozeEnvironmentIds: input.snoozeEnvironmentIds, |
| 229 | queuedThreadKeys: input.queuedThreadKeys, |
| 230 | moveAvailability, |
| 231 | shelfPreferencesLoading: !input.shelfPreferencesLoaded, |
| 232 | resolve: keepMarked, |
| 233 | }), |
| 234 | [ |
| 235 | layout, |
| 236 | pendingTasks, |
| 237 | input.workingShelfExpanded, |
| 238 | input.snoozedShelfExpanded, |
| 239 | input.settledShelfExpanded, |
| 240 | input.now, |
| 241 | input.snoozeEnvironmentIds, |
| 242 | input.queuedThreadKeys, |
| 243 | moveAvailability, |
| 244 | input.shelfPreferencesLoaded, |
| 245 | ], |
| 246 | ); |
| 247 | return { layout, items, contentOnlyThreadKeys, olderSettledShelf }; |
| 248 | } |
| 249 | |
| 250 | /** The list's scope per environment: its projects there, or every project. */ |
| 251 | function olderSettledScopes(input: { |
| 252 | readonly environmentId: EnvironmentId | null; |
| 253 | readonly projectRefs: LayoutInput["projectRefs"]; |
| 254 | readonly settlementEnvironmentIds: ReadonlySet<EnvironmentId>; |
| 255 | }): ReadonlyArray<OlderSettledScope> { |
| 256 | const environmentIds = |
| 257 | input.environmentId === null ? [...input.settlementEnvironmentIds] : [input.environmentId]; |
| 258 | return environmentIds.flatMap((environmentId): ReadonlyArray<OlderSettledScope> => { |
| 259 | if (input.projectRefs == null) return [{ environmentId, projectIds: null }]; |
| 260 | const projectIds = input.projectRefs.flatMap((ref) => |
| 261 | ref.environmentId === environmentId ? [ref.projectId] : [], |
| 262 | ); |
| 263 | return projectIds.length === 0 ? [] : [{ environmentId, projectIds }]; |
| 264 | }); |
| 265 | } |