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 { scopedThreadKey } from "@t3tools/client-runtime/environment"; |
| 2 | import { useMatches } from "@tanstack/react-router"; |
| 3 | import { useState } from "react"; |
| 4 | |
| 5 | import { |
| 6 | getAppModalReturnThreadTarget, |
| 7 | isAppModalPage, |
| 8 | } from "../components/sidebar/mainAppLocation"; |
| 9 | import type { ThreadRouteTarget } from "../threadRoutes"; |
| 10 | import { useThreadRouteTarget } from "./useThreadRouteTarget"; |
| 11 | |
| 12 | function threadRouteTargetKey(target: ThreadRouteTarget | null) { |
| 13 | if (!target) return null; |
| 14 | return target.kind === "server" ? scopedThreadKey(target.threadRef) : target.draftId; |
| 15 | } |
| 16 | |
| 17 | /** Settings and Usage render over the thread that opened them; that thread stays the shown one. */ |
| 18 | export function useShownThreadRouteTarget() { |
| 19 | const routeTarget = useThreadRouteTarget(); |
| 20 | // Read matches, not location, so this agrees with the params useThreadRouteTarget reads. |
| 21 | const modalOpen = useMatches({ |
| 22 | select: (matches) => matches.some((match) => isAppModalPage(match.pathname)), |
| 23 | }); |
| 24 | const [backgroundTarget, setBackgroundTarget] = useState(() => |
| 25 | modalOpen ? getAppModalReturnThreadTarget() : routeTarget, |
| 26 | ); |
| 27 | if (!modalOpen && threadRouteTargetKey(backgroundTarget) !== threadRouteTargetKey(routeTarget)) { |
| 28 | setBackgroundTarget(routeTarget); |
| 29 | } |
| 30 | return { shownTarget: modalOpen ? backgroundTarget : routeTarget, modalOpen }; |
| 31 | } |