| author | |
| committer | |
| log | 891eb02d49d4b511cc1b52ed4d675476484f2aa9 |
| tree | 79751a7d5c7f11c53c2f13bdf18ba6e19b61030e |
| parent | cc3e1db22a0a450396bf34650eebb7b8c56129de |
- Extract `DiffWorkerPoolProvider` into its own component and wrap `_chat` outlet with it
- Render/lazy-load `DiffPanel` from `/_chat/$threadId` so diff UI state and close behavior live with thread routing
- Keep responsive sheet vs inline diff presentation with route-level fallback loading UI4 files changed, 117 insertions(+), 128 deletions(-)
apps/web/src/components/DiffPanel.tsx+2-24| ... | ... | @@ -3,12 +3,11 @@ import { |
| 3 | 3 | FileDiff, |
| 4 | 4 | type FileDiffMetadata, |
| 5 | 5 | Virtualizer, |
| 6 | WorkerPoolContextProvider, | |
| 7 | 6 | } from "@pierre/diffs/react"; |
| 8 | 7 | import { useQuery } from "@tanstack/react-query"; |
| 9 | 8 | import { useNavigate, useParams, useSearch } from "@tanstack/react-router"; |
| 10 | 9 | import { Columns2Icon, Rows3Icon } from "lucide-react"; |
| 11 | import { type ReactNode, useEffect, useMemo, useRef, useState } from "react"; | |
| 10 | import { useEffect, useMemo, useRef, useState } from "react"; | |
| 12 | 11 | import { checkpointDiffQueryOptions } from "~/lib/providerReactQuery"; |
| 13 | 12 | import { cn } from "~/lib/utils"; |
| 14 | 13 | import { parseDiffRouteSearch } from "../diffRouteSearch"; |
| ... | ... | @@ -77,28 +76,7 @@ interface DiffPanelProps { |
| 77 | 76 | mode?: "inline" | "sheet"; |
| 78 | 77 | } |
| 79 | 78 | |
| 80 | export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) { | |
| 81 | const workerPoolSize = useMemo(() => { | |
| 82 | const cores = typeof navigator === "undefined" ? 4 : Math.max(1, navigator.hardwareConcurrency || 4); | |
| 83 | return Math.max(2, Math.min(6, Math.floor(cores / 2))); | |
| 84 | }, []); | |
| 85 | ||
| 86 | return ( | |
| 87 | <WorkerPoolContextProvider | |
| 88 | poolOptions={{ | |
| 89 | workerFactory: () => | |
| 90 | new Worker(new URL("../workers/diffs.worker.ts", import.meta.url), { type: "module" }), | |
| 91 | poolSize: workerPoolSize, | |
| 92 | totalASTLRUCacheSize: 240, | |
| 93 | }} | |
| 94 | highlighterOptions={{ | |
| 95 | tokenizeMaxLineLength: 1_000, | |
| 96 | }} | |
| 97 | > | |
| 98 | {children} | |
| 99 | </WorkerPoolContextProvider> | |
| 100 | ); | |
| 101 | } | |
| 79 | export { DiffWorkerPoolProvider } from "./DiffWorkerPoolProvider"; | |
| 102 | 80 | |
| 103 | 81 | export default function DiffPanel({ mode = "inline" }: DiffPanelProps) { |
| 104 | 82 | const api = useNativeApi(); |
apps/web/src/components/DiffWorkerPoolProvider.tsx created+26| ... | ... | @@ -0,0 +1,26 @@ |
| 1 | import { WorkerPoolContextProvider } from "@pierre/diffs/react"; | |
| 2 | import { useMemo, type ReactNode } from "react"; | |
| 3 | ||
| 4 | export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) { | |
| 5 | const workerPoolSize = useMemo(() => { | |
| 6 | const cores = typeof navigator === "undefined" ? 4 : Math.max(1, navigator.hardwareConcurrency || 4); | |
| 7 | return Math.max(2, Math.min(6, Math.floor(cores / 2))); | |
| 8 | }, []); | |
| 9 | ||
| 10 | return ( | |
| 11 | <WorkerPoolContextProvider | |
| 12 | poolOptions={{ | |
| 13 | workerFactory: () => | |
| 14 | new Worker(new URL("../workers/diffs.worker.ts", import.meta.url), { type: "module" }), | |
| 15 | poolSize: workerPoolSize, | |
| 16 | totalASTLRUCacheSize: 240, | |
| 17 | }} | |
| 18 | highlighterOptions={{ | |
| 19 | tokenizeMaxLineLength: 1_000, | |
| 20 | }} | |
| 21 | > | |
| 22 | {children} | |
| 23 | </WorkerPoolContextProvider> | |
| 24 | ); | |
| 25 | } | |
| 26 |
apps/web/src/routes/_chat.$threadId.tsx+83-2| ... | ... | @@ -1,15 +1,73 @@ |
| 1 | 1 | import { createFileRoute, useNavigate } from "@tanstack/react-router"; |
| 2 | import { useEffect } from "react"; | |
| 2 | import { Activity, Suspense, lazy, type ReactNode, useCallback, useEffect } from "react"; | |
| 3 | 3 | |
| 4 | 4 | import ChatView from "../components/ChatView"; |
| 5 | 5 | import { parseDiffRouteSearch } from "../diffRouteSearch"; |
| 6 | import { useMediaQuery } from "../hooks/useMediaQuery"; | |
| 6 | 7 | import { useStore } from "../store"; |
| 8 | import { Sheet, SheetPopup } from "../components/ui/sheet"; | |
| 9 | ||
| 10 | const DiffPanel = lazy(() => import("../components/DiffPanel")); | |
| 11 | const DIFF_INLINE_LAYOUT_MEDIA_QUERY = "(max-width: 1180px)"; | |
| 12 | ||
| 13 | const DiffPanelWrapper = (props: { | |
| 14 | children: ReactNode; | |
| 15 | sheet: boolean; | |
| 16 | diffOpen: boolean; | |
| 17 | onCloseDiff: () => void; | |
| 18 | }) => { | |
| 19 | if (props.sheet) { | |
| 20 | return ( | |
| 21 | <Sheet | |
| 22 | open={props.diffOpen} | |
| 23 | onOpenChange={(open) => { | |
| 24 | if (!open) { | |
| 25 | props.onCloseDiff(); | |
| 26 | } | |
| 27 | }} | |
| 28 | > | |
| 29 | <SheetPopup | |
| 30 | side="right" | |
| 31 | showCloseButton={false} | |
| 32 | keepMounted | |
| 33 | className="w-[min(88vw,820px)] max-w-[820px] p-0" | |
| 34 | > | |
| 35 | {props.children} | |
| 36 | </SheetPopup> | |
| 37 | </Sheet> | |
| 38 | ); | |
| 39 | } | |
| 40 | ||
| 41 | return ( | |
| 42 | <aside className={props.diffOpen ? undefined : "hidden"} aria-hidden={!props.diffOpen}> | |
| 43 | {props.children} | |
| 44 | </aside> | |
| 45 | ); | |
| 46 | }; | |
| 7 | 47 | |
| 8 | 48 | function ChatThreadRouteView() { |
| 9 | 49 | const { state } = useStore(); |
| 10 | 50 | const navigate = useNavigate(); |
| 11 | 51 | const { threadId } = Route.useParams(); |
| 52 | const search = Route.useSearch(); | |
| 12 | 53 | const threadExists = threadId ? state.threads.some((thread) => thread.id === threadId) : false; |
| 54 | const diffOpen = search.diff === "1"; | |
| 55 | const shouldUseDiffSheet = useMediaQuery(DIFF_INLINE_LAYOUT_MEDIA_QUERY); | |
| 56 | const closeDiff = useCallback(() => { | |
| 57 | void navigate({ | |
| 58 | to: "/$threadId", | |
| 59 | params: { threadId }, | |
| 60 | search: (previous) => { | |
| 61 | const { | |
| 62 | diff: _diff, | |
| 63 | diffTurnId: _diffTurnId, | |
| 64 | diffFilePath: _diffFilePath, | |
| 65 | ...rest | |
| 66 | } = previous; | |
| 67 | return rest; | |
| 68 | }, | |
| 69 | }); | |
| 70 | }, [navigate, threadId]); | |
| 13 | 71 | |
| 14 | 72 | useEffect(() => { |
| 15 | 73 | if (!threadId) { |
| ... | ... | @@ -31,10 +89,33 @@ function ChatThreadRouteView() { |
| 31 | 89 | return null; |
| 32 | 90 | } |
| 33 | 91 | |
| 34 | return <ChatView threadId={threadId} />; | |
| 92 | const diffLoadingFallback = | |
| 93 | !diffOpen || shouldUseDiffSheet ? ( | |
| 94 | <div className="flex h-full min-h-0 items-center justify-center px-4 text-center text-xs text-muted-foreground/70"> | |
| 95 | Loading diff viewer... | |
| 96 | </div> | |
| 97 | ) : ( | |
| 98 | <aside className="flex h-full w-[560px] shrink-0 items-center justify-center border-l border-border bg-card px-4 text-center text-xs text-muted-foreground/70"> | |
| 99 | Loading diff viewer... | |
| 100 | </aside> | |
| 101 | ); | |
| 102 | ||
| 103 | return ( | |
| 104 | <> | |
| 105 | <ChatView threadId={threadId} /> | |
| 106 | <Activity mode={diffOpen ? "visible" : "hidden"}> | |
| 107 | <DiffPanelWrapper sheet={shouldUseDiffSheet} diffOpen={diffOpen} onCloseDiff={closeDiff}> | |
| 108 | <Suspense fallback={diffLoadingFallback}> | |
| 109 | <DiffPanel mode={shouldUseDiffSheet ? "sheet" : "inline"} /> | |
| 110 | </Suspense> | |
| 111 | </DiffPanelWrapper> | |
| 112 | </Activity> | |
| 113 | </> | |
| 114 | ); | |
| 35 | 115 | } |
| 36 | 116 | |
| 37 | 117 | export const Route = createFileRoute("/_chat/$threadId")({ |
| 38 | 118 | validateSearch: (search) => parseDiffRouteSearch(search), |
| 39 | 119 | component: ChatThreadRouteView, |
| 40 | 120 | }); |
| 121 |
apps/web/src/routes/_chat.tsx+6-102| ... | ... | @@ -1,112 +1,15 @@ |
| 1 | import { | |
| 2 | Outlet, | |
| 3 | createFileRoute, | |
| 4 | useNavigate, | |
| 5 | useParams, | |
| 6 | useSearch, | |
| 7 | } from "@tanstack/react-router"; | |
| 8 | import { Activity, Suspense, lazy, type ReactNode, useCallback, useMemo } from "react"; | |
| 1 | import { Outlet, createFileRoute } from "@tanstack/react-router"; | |
| 9 | 2 | |
| 3 | import { DiffWorkerPoolProvider } from "../components/DiffWorkerPoolProvider"; | |
| 10 | 4 | import Sidebar from "../components/Sidebar"; |
| 11 | import { parseDiffRouteSearch } from "../diffRouteSearch"; | |
| 12 | import { useMediaQuery } from "../hooks/useMediaQuery"; | |
| 13 | import { Sheet, SheetPopup } from "../components/ui/sheet"; | |
| 14 | ||
| 15 | const DiffPanel = lazy(() => import("../components/DiffPanel")); | |
| 16 | const DiffWorkerPoolProvider = lazy(() => | |
| 17 | import("../components/DiffPanel").then((module) => ({ | |
| 18 | default: module.DiffWorkerPoolProvider, | |
| 19 | })), | |
| 20 | ); | |
| 21 | const DIFF_INLINE_LAYOUT_MEDIA_QUERY = "(max-width: 1180px)"; | |
| 22 | ||
| 23 | const DiffPanelWrapper = (props: { | |
| 24 | children: ReactNode; | |
| 25 | sheet: boolean; | |
| 26 | diffOpen: boolean; | |
| 27 | onCloseDiff: () => void; | |
| 28 | }) => { | |
| 29 | if (props.sheet) { | |
| 30 | return ( | |
| 31 | <Sheet | |
| 32 | open={props.diffOpen} | |
| 33 | onOpenChange={(open) => { | |
| 34 | if (!open) { | |
| 35 | props.onCloseDiff(); | |
| 36 | } | |
| 37 | }} | |
| 38 | > | |
| 39 | <SheetPopup | |
| 40 | side="right" | |
| 41 | showCloseButton={false} | |
| 42 | keepMounted | |
| 43 | className="w-[min(88vw,820px)] max-w-[820px] p-0" | |
| 44 | > | |
| 45 | {props.children} | |
| 46 | </SheetPopup> | |
| 47 | </Sheet> | |
| 48 | ); | |
| 49 | } | |
| 50 | ||
| 51 | return ( | |
| 52 | <aside className={props.diffOpen ? undefined : "hidden"} aria-hidden={!props.diffOpen}> | |
| 53 | {props.children} | |
| 54 | </aside> | |
| 55 | ); | |
| 56 | }; | |
| 57 | 5 | |
| 58 | 6 | function ChatRouteLayout() { |
| 59 | const navigate = useNavigate(); | |
| 60 | const params = useParams({ strict: false }); | |
| 61 | const routeThreadId = typeof params.threadId === "string" ? params.threadId : null; | |
| 62 | const rawSearch = useSearch({ strict: false }); | |
| 63 | const diffSearch = useMemo( | |
| 64 | () => parseDiffRouteSearch(rawSearch as Record<string, unknown>), | |
| 65 | [rawSearch], | |
| 66 | ); | |
| 67 | const diffOpen = routeThreadId !== null && diffSearch.diff === "1"; | |
| 68 | const shouldUseDiffSheet = useMediaQuery(DIFF_INLINE_LAYOUT_MEDIA_QUERY); | |
| 69 | const closeDiff = useCallback(() => { | |
| 70 | if (!routeThreadId) return; | |
| 71 | void navigate({ | |
| 72 | to: "/$threadId", | |
| 73 | params: { threadId: routeThreadId }, | |
| 74 | search: (previous) => { | |
| 75 | const { | |
| 76 | diff: _diff, | |
| 77 | diffTurnId: _diffTurnId, | |
| 78 | diffFilePath: _diffFilePath, | |
| 79 | ...rest | |
| 80 | } = previous; | |
| 81 | return rest; | |
| 82 | }, | |
| 83 | }); | |
| 84 | }, [navigate, routeThreadId]); | |
| 85 | ||
| 86 | const diffLoadingFallback = | |
| 87 | !diffOpen || shouldUseDiffSheet ? ( | |
| 88 | <div className="flex h-full min-h-0 items-center justify-center px-4 text-center text-xs text-muted-foreground/70"> | |
| 89 | Loading diff viewer... | |
| 90 | </div> | |
| 91 | ) : ( | |
| 92 | <aside className="flex h-full w-[560px] shrink-0 items-center justify-center border-l border-border bg-card px-4 text-center text-xs text-muted-foreground/70"> | |
| 93 | Loading diff viewer... | |
| 94 | </aside> | |
| 95 | ); | |
| 96 | ||
| 97 | 7 | return ( |
| 98 | 8 | <div className="flex h-screen overflow-hidden bg-background text-foreground isolate"> |
| 99 | 9 | <Sidebar /> |
| 100 | <Outlet /> | |
| 101 | <Activity mode={diffOpen ? "visible" : "hidden"}> | |
| 102 | <DiffPanelWrapper sheet={shouldUseDiffSheet} diffOpen={diffOpen} onCloseDiff={closeDiff}> | |
| 103 | <Suspense fallback={diffLoadingFallback}> | |
| 104 | <DiffWorkerPoolProvider> | |
| 105 | <DiffPanel mode={shouldUseDiffSheet ? "sheet" : "inline"} /> | |
| 106 | </DiffWorkerPoolProvider> | |
| 107 | </Suspense> | |
| 108 | </DiffPanelWrapper> | |
| 109 | </Activity> | |
| 10 | <DiffWorkerPoolProvider> | |
| 11 | <Outlet /> | |
| 12 | </DiffWorkerPoolProvider> | |
| 110 | 13 | </div> |
| 111 | 14 | ); |
| 112 | 15 | } |
| ... | ... | @@ -114,3 +17,4 @@ function ChatRouteLayout() { |
| 114 | 17 | export const Route = createFileRoute("/_chat")({ |
| 115 | 18 | component: ChatRouteLayout, |
| 116 | 19 | }); |
| 20 |