| ... | @@ -1,7 +1,7 @@ | ... | @@ -1,7 +1,7 @@ |
| 1 | import { MonitorIcon, MoonIcon, SunIcon, TerminalIcon } from "lucide-react"; | 1 | import { MonitorIcon, MoonIcon, SunIcon, TerminalIcon } from "lucide-react"; |
| 2 | import { useCallback, useEffect, useMemo, useState } from "react"; | 2 | import { useCallback, useEffect, useMemo, useState } from "react"; |
| 3 | import { useMutation, useQueries, useQueryClient } from "@tanstack/react-query"; | 3 | import { useMutation, useQueries, useQueryClient } from "@tanstack/react-query"; |
| 4 | import type { GitStatusResult } from "@t3tools/contracts"; | 4 | import { gitStatusResultSchema, type GitStatusResult } from "@t3tools/contracts"; |
| 5 | import { isElectron } from "../env"; | 5 | import { isElectron } from "../env"; |
| 6 | import { useTheme } from "../hooks/useTheme"; | 6 | import { useTheme } from "../hooks/useTheme"; |
| 7 | import { DEFAULT_MODEL } from "../model-logic"; | 7 | import { DEFAULT_MODEL } from "../model-logic"; |
| ... | @@ -153,29 +153,49 @@ export default function Sidebar() { | ... | @@ -153,29 +153,49 @@ export default function Sidebar() { |
| 153 | })), | 153 | })), |
| 154 | [projectCwdById, state.threads], | 154 | [projectCwdById, state.threads], |
| 155 | ); | 155 | ); |
| | 156 | const threadGitStatusCwds = useMemo( |
| | 157 | () => |
| | 158 | [ |
| | 159 | ...new Set( |
| | 160 | threadGitTargets |
| | 161 | .filter((target) => target.branch !== null) |
| | 162 | .map((target) => target.cwd) |
| | 163 | .filter((cwd): cwd is string => cwd !== null), |
| | 164 | ), |
| | 165 | ], |
| | 166 | [threadGitTargets], |
| | 167 | ); |
| 156 | const threadGitStatusQueries = useQueries({ | 168 | const threadGitStatusQueries = useQueries({ |
| 157 | queries: threadGitTargets.map((target) => { | 169 | queries: threadGitStatusCwds.map((cwd) => { |
| 158 | const base = gitStatusQueryOptions(api, target.cwd); | 170 | const base = gitStatusQueryOptions(api, cwd); |
| 159 | return { | 171 | return { |
| 160 | ...base, | 172 | ...base, |
| 161 | enabled: Boolean(base.enabled && target.branch !== null), | 173 | enabled: Boolean(base.enabled), |
| 162 | staleTime: 30_000, | 174 | staleTime: 30_000, |
| 163 | refetchInterval: 60_000, | 175 | refetchInterval: 60_000, |
| 164 | }; | 176 | }; |
| 165 | }), | 177 | }), |
| 166 | }); | 178 | }); |
| 167 | const mergedPrByThreadId = useMemo(() => { | 179 | const mergedPrByThreadId = useMemo(() => { |
| | 180 | const statusByCwd = new Map<string, GitStatusResult>(); |
| | 181 | for (let index = 0; index < threadGitStatusCwds.length; index += 1) { |
| | 182 | const cwd = threadGitStatusCwds[index]; |
| | 183 | if (!cwd) continue; |
| | 184 | const parsed = gitStatusResultSchema.safeParse(threadGitStatusQueries[index]?.data); |
| | 185 | if (parsed.success) { |
| | 186 | statusByCwd.set(cwd, parsed.data); |
| | 187 | } |
| | 188 | } |
| | 189 | |
| 168 | const map = new Map<string, boolean>(); | 190 | const map = new Map<string, boolean>(); |
| 169 | for (let index = 0; index < threadGitTargets.length; index += 1) { | 191 | for (const target of threadGitTargets) { |
| 170 | const target = threadGitTargets[index]; | 192 | const status = target.cwd ? statusByCwd.get(target.cwd) : undefined; |
| 171 | if (!target) continue; | | |
| 172 | const status = threadGitStatusQueries[index]?.data as GitStatusResult | undefined; | | |
| 173 | const branchMatches = | 193 | const branchMatches = |
| 174 | target.branch !== null && status?.branch !== null && status?.branch === target.branch; | 194 | target.branch !== null && status?.branch !== null && status?.branch === target.branch; |
| 175 | map.set(target.threadId, branchMatches && status?.openPr === null && status?.mergedPr !== null); | 195 | map.set(target.threadId, branchMatches && status?.openPr === null && status?.mergedPr !== null); |
| 176 | } | 196 | } |
| 177 | return map; | 197 | return map; |
| 178 | }, [threadGitStatusQueries, threadGitTargets]); | 198 | }, [threadGitStatusCwds, threadGitStatusQueries, threadGitTargets]); |
| 179 | | 199 | |
| 180 | const handleNewThread = useCallback( | 200 | const handleNewThread = useCallback( |
| 181 | (projectId: string) => { | 201 | (projectId: string) => { |