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 { |
| 2 | buildProjectGroups, |
| 3 | pickableProjectGroups, |
| 4 | resolveScratchProjectScopeKey, |
| 5 | } from "@t3tools/client-runtime/state/project-grouping"; |
| 6 | import type { |
| 7 | EnvironmentProject, |
| 8 | EnvironmentThreadShell, |
| 9 | } from "@t3tools/client-runtime/state/shell"; |
| 10 | import { |
| 11 | getThreadSortTimestamp, |
| 12 | toSortableTimestamp, |
| 13 | } from "@t3tools/client-runtime/state/thread-sort"; |
| 14 | import type { |
| 15 | EnvironmentId, |
| 16 | ScopedProjectRef, |
| 17 | SidebarProjectGroupingMode, |
| 18 | SidebarProjectSortOrder, |
| 19 | } from "@t3tools/contracts"; |
| 20 | import * as Arr from "effect/Array"; |
| 21 | import * as Order from "effect/Order"; |
| 22 | |
| 23 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 24 | import { scopedProjectKey } from "../../lib/scopedEntities"; |
| 25 | import type { PendingNewTask } from "../../state/use-pending-new-tasks"; |
| 26 | |
| 27 | export type HomeProjectSortOrder = Exclude<SidebarProjectSortOrder, "manual">; |
| 28 | |
| 29 | export interface HomeProjectScope { |
| 30 | readonly key: string; |
| 31 | readonly title: string; |
| 32 | readonly representative: EnvironmentProject; |
| 33 | readonly projects: ReadonlyArray<EnvironmentProject>; |
| 34 | readonly projectRefs: ReadonlyArray<ScopedProjectRef>; |
| 35 | readonly pinned: boolean; |
| 36 | readonly archived: boolean; |
| 37 | } |
| 38 | |
| 39 | function getProjectSortTimestamp( |
| 40 | project: EnvironmentProject, |
| 41 | sortOrder: HomeProjectSortOrder, |
| 42 | ): number { |
| 43 | return sortOrder === "created_at" |
| 44 | ? (toSortableTimestamp(project.createdAt) ?? Number.NEGATIVE_INFINITY) |
| 45 | : (toSortableTimestamp(project.updatedAt) ?? |
| 46 | toSortableTimestamp(project.createdAt) ?? |
| 47 | Number.NEGATIVE_INFINITY); |
| 48 | } |
| 49 | |
| 50 | export function buildHomeProjectScopes( |
| 51 | input: { |
| 52 | readonly projects: ReadonlyArray<EnvironmentProject>; |
| 53 | readonly environmentId: EnvironmentId | null; |
| 54 | readonly projectGroupingMode: SidebarProjectGroupingMode; |
| 55 | }, |
| 56 | resolve: MessageResolver = m, |
| 57 | ): ReadonlyArray<HomeProjectScope> { |
| 58 | const projects = input.projects.filter( |
| 59 | (project) => input.environmentId === null || project.environmentId === input.environmentId, |
| 60 | ); |
| 61 | return buildProjectGroups( |
| 62 | { |
| 63 | projects, |
| 64 | groupScratchProjects: true, |
| 65 | settings: { |
| 66 | sidebarProjectGroupingMode: input.projectGroupingMode, |
| 67 | sidebarProjectGroupingOverrides: {}, |
| 68 | }, |
| 69 | }, |
| 70 | resolve, |
| 71 | ).map((group) => { |
| 72 | return { |
| 73 | key: group.key, |
| 74 | title: group.label, |
| 75 | representative: group.representative, |
| 76 | projects: group.members.map((member) => member.project), |
| 77 | projectRefs: group.memberProjectRefs, |
| 78 | pinned: group.pinned, |
| 79 | archived: group.archived, |
| 80 | }; |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | /** The scope the thread list's project filter shows; an archived or unknown key shows all projects. */ |
| 85 | export function resolveHomeProjectFilterScope( |
| 86 | scopes: ReadonlyArray<HomeProjectScope>, |
| 87 | projects: ReadonlyArray<EnvironmentProject>, |
| 88 | options: { |
| 89 | readonly selectedProjectKey: string | null; |
| 90 | readonly projectGroupingMode: SidebarProjectGroupingMode; |
| 91 | }, |
| 92 | ): HomeProjectScope | null { |
| 93 | // A legacy per-environment scratch key resolves here; the stored value is only |
| 94 | // rewritten when the user picks a filter. |
| 95 | const key = resolveScratchProjectScopeKey(options.selectedProjectKey, projects, { |
| 96 | sidebarProjectGroupingMode: options.projectGroupingMode, |
| 97 | sidebarProjectGroupingOverrides: {}, |
| 98 | }); |
| 99 | return pickableProjectGroups(scopes).find((scope) => scope.key === key) ?? null; |
| 100 | } |
| 101 | |
| 102 | export function findHomeProjectScope( |
| 103 | scopes: ReadonlyArray<HomeProjectScope>, |
| 104 | key: string | null, |
| 105 | ): HomeProjectScope | null { |
| 106 | if (key === null) { |
| 107 | return null; |
| 108 | } |
| 109 | return ( |
| 110 | scopes.find((scope) => scope.key === key) ?? |
| 111 | scopes.find((scope) => |
| 112 | scope.projectRefs.some( |
| 113 | (projectRef) => scopedProjectKey(projectRef.environmentId, projectRef.projectId) === key, |
| 114 | ), |
| 115 | ) ?? |
| 116 | null |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | export function sortHomeProjectScopes(input: { |
| 121 | readonly scopes: ReadonlyArray<HomeProjectScope>; |
| 122 | readonly threads: ReadonlyArray<EnvironmentThreadShell>; |
| 123 | readonly pendingTasks: ReadonlyArray<PendingNewTask>; |
| 124 | readonly projectSortOrder: HomeProjectSortOrder; |
| 125 | }): ReadonlyArray<HomeProjectScope> { |
| 126 | const scopeKeyByProjectRef = new Map( |
| 127 | input.scopes.flatMap((scope) => |
| 128 | scope.projectRefs.map( |
| 129 | (projectRef) => |
| 130 | [scopedProjectKey(projectRef.environmentId, projectRef.projectId), scope.key] as const, |
| 131 | ), |
| 132 | ), |
| 133 | ); |
| 134 | const latestActivityByScope = new Map<string, number>(); |
| 135 | const recordActivity = (scopeKey: string | undefined, timestamp: number) => { |
| 136 | if (!scopeKey || !Number.isFinite(timestamp)) return; |
| 137 | latestActivityByScope.set( |
| 138 | scopeKey, |
| 139 | Math.max(latestActivityByScope.get(scopeKey) ?? Number.NEGATIVE_INFINITY, timestamp), |
| 140 | ); |
| 141 | }; |
| 142 | |
| 143 | for (const thread of input.threads) { |
| 144 | if (thread.archivedAt !== null) continue; |
| 145 | recordActivity( |
| 146 | scopeKeyByProjectRef.get(scopedProjectKey(thread.environmentId, thread.projectId)), |
| 147 | getThreadSortTimestamp(thread, input.projectSortOrder), |
| 148 | ); |
| 149 | } |
| 150 | for (const pendingTask of input.pendingTasks) { |
| 151 | recordActivity( |
| 152 | scopeKeyByProjectRef.get(scopedProjectKey(pendingTask.environmentId, pendingTask.projectId)), |
| 153 | Date.parse(pendingTask.createdAt), |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | return Arr.sort( |
| 158 | input.scopes, |
| 159 | Order.mapInput( |
| 160 | Order.Struct({ |
| 161 | timestamp: Order.flip(Order.Number), |
| 162 | title: Order.String, |
| 163 | key: Order.String, |
| 164 | }), |
| 165 | (scope: HomeProjectScope) => ({ |
| 166 | timestamp: |
| 167 | latestActivityByScope.get(scope.key) ?? |
| 168 | Math.max( |
| 169 | ...scope.projects.map((project) => |
| 170 | getProjectSortTimestamp(project, input.projectSortOrder), |
| 171 | ), |
| 172 | ), |
| 173 | title: scope.title, |
| 174 | key: scope.key, |
| 175 | }), |
| 176 | ), |
| 177 | ); |
| 178 | } |