Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
Address PR feedback on router and thread hydration
Julius Marmingefe69e9694252
diff --git a/apps/web/src/components/BranchToolbar.tsx b/apps/web/src/components/BranchToolbar.tsxindex f13490a3f..21113658f 100644--- a/apps/web/src/components/BranchToolbar.tsx+++ b/apps/web/src/components/BranchToolbar.tsx@@ -23,6 +23,7 @@ import {import { ChevronDownIcon } from "lucide-react";interface BranchToolbarProps {+ threadId: string;envMode: "local" | "worktree";onEnvModeChange: (mode: "local" | "worktree") => void;envLocked: boolean;@@ -30,6 +31,7 @@ interface BranchToolbarProps {}export default function BranchToolbar({+ threadId,envMode,onEnvModeChange,envLocked,@@ -42,7 +44,7 @@ export default function BranchToolbar({const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false);const [branchQuery, setBranchQuery] = useState("");- const activeThread = state.threads.find((thread) => thread.id === state.activeThreadId);+ const activeThread = state.threads.find((thread) => thread.id === threadId);const activeProject = state.projects.find((project) => project.id === activeThread?.projectId);const activeThreadId = activeThread?.id;const activeThreadBranch = activeThread?.branch ?? null;diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex b51308274..7169e1796 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -63,6 +63,7 @@ import {} from "../session-logic";import { isScrollContainerNearBottom } from "../chat-scroll";import { useStore } from "../store";+import { truncateTitle } from "../truncateTitle";import {DEFAULT_THREAD_TERMINAL_ID,MAX_THREAD_TERMINAL_COUNT,@@ -1129,6 +1130,9 @@ export default function ChatView({ threadId }: ChatViewProps) {revokePreviewUrls(existing);return [];});+ setPrompt("");+ promptRef.current = "";+ setIsSending(false);setComposerCursor(0);setComposerHighlightedItemId(null);dragDepthRef.current = 0;@@ -1650,7 +1654,7 @@ export default function ChatView({ threadId }: ChatViewProps) {(composerImagesSnapshot.length > 0? `Image: ${composerImagesSnapshot[0]?.name ?? "attachment"}`: "New thread");- const title = titleSeed.length > 50 ? `${titleSeed.slice(0, 50)}...` : titleSeed;+ const title = truncateTitle(titleSeed);dispatch({type: "SET_THREAD_TITLE",threadId: activeThread.id,@@ -1944,6 +1948,7 @@ export default function ChatView({ threadId }: ChatViewProps) {className={`flex items-center justify-between border-b border-border px-5 ${isElectron ? "drag-region h-[52px]" : "py-3"}`}><ChatHeader+ activeThreadId={activeThread.id}activeThreadTitle={activeThread.title}activeProjectName={activeProject?.name}activeProjectScripts={activeProject?.scripts}@@ -2210,6 +2215,7 @@ export default function ChatView({ threadId }: ChatViewProps) {{isGitRepo && (<BranchToolbar+ threadId={activeThread.id}envMode={envMode}onEnvModeChange={onEnvModeChange}envLocked={envLocked}@@ -2286,6 +2292,7 @@ export default function ChatView({ threadId }: ChatViewProps) {}interface ChatHeaderProps {+ activeThreadId: string;activeThreadTitle: string;activeProjectName: string | undefined;activeProjectScripts: ProjectScript[] | undefined;@@ -2301,6 +2308,7 @@ interface ChatHeaderProps {}const ChatHeader = memo(function ChatHeader({+ activeThreadId,activeThreadTitle,activeProjectName,activeProjectScripts,@@ -2333,8 +2341,12 @@ const ChatHeader = memo(function ChatHeader({onUpdateScript={onUpdateProjectScript}/>)}- {activeProjectName && <OpenInPicker keybindings={keybindings} />}- {activeProjectName && <GitActionsControl api={api} gitCwd={gitCwd} />}+ {activeProjectName && (+ <OpenInPicker keybindings={keybindings} activeThreadId={activeThreadId} />+ )}+ {activeProjectName && (+ <GitActionsControl api={api} gitCwd={gitCwd} activeThreadId={activeThreadId} />+ )}<Togglepressed={diffOpen}onPressedChange={onToggleDiff}@@ -2827,8 +2839,10 @@ const ReasoningEffortPicker = memo(function ReasoningEffortPicker(props: {const OpenInPicker = memo(function OpenInPicker({keybindings,+ activeThreadId,}: {keybindings: ResolvedKeybindingsConfig;+ activeThreadId: string | null;}) {const [lastEditor, setLastEditor] = useState<EditorId>(() => {const stored = localStorage.getItem(LAST_EDITOR_KEY);@@ -2855,7 +2869,7 @@ const OpenInPicker = memo(function OpenInPicker({const api = useNativeApi();const { state } = useStore();- const activeThread = state.threads.find((t) => t.id === state.activeThreadId);+ const activeThread = state.threads.find((t) => t.id === activeThreadId);const activeProject = state.projects.find((p) => p.id === activeThread?.projectId);const openInEditor = useCallback(diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsxindex 7890665a9..11b5fc452 100644--- a/apps/web/src/components/GitActionsControl.tsx+++ b/apps/web/src/components/GitActionsControl.tsx@@ -38,12 +38,12 @@ import {gitStatusQueryOptions,invalidateGitQueries,} from "~/lib/gitReactQuery";-import { useStore } from "~/store";import { preferredTerminalEditor, resolvePathLinkTarget } from "~/terminal-links";interface GitActionsControlProps {api: NativeApi | undefined;gitCwd: string | null;+ activeThreadId: string | null;}function getMenuActionDisabledReason(@@ -136,9 +136,11 @@ function GitQuickActionIcon({ quickAction }: { quickAction: GitQuickAction }) {return <InfoIcon className={iconClassName} />;}-export default function GitActionsControl({ api, gitCwd }: GitActionsControlProps) {- const { state } = useStore();- const activeThreadId = state.activeThreadId;+export default function GitActionsControl({+ api,+ gitCwd,+ activeThreadId,+}: GitActionsControlProps) {const threadToastData = useMemo(() => (activeThreadId ? { threadId: activeThreadId } : undefined),[activeThreadId],diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsxindex 35ae22e1a..9f9b5f15b 100644--- a/apps/web/src/components/Sidebar.tsx+++ b/apps/web/src/components/Sidebar.tsx@@ -397,7 +397,9 @@ export default function Sidebar() {useEffect(() => {const onWindowKeyDown = (event: KeyboardEvent) => {- const activeThread = state.threads.find((t) => t.id === state.activeThreadId);+ const activeThread = routeThreadId+ ? state.threads.find((thread) => thread.id === routeThreadId)+ : undefined;if (isChatNewLocalShortcut(event, keybindings)) {const projectId = activeThread?.projectId ?? state.projects[0]?.id;if (!projectId) return;@@ -420,7 +422,7 @@ export default function Sidebar() {return () => {window.removeEventListener("keydown", onWindowKeyDown);};- }, [handleNewThread, keybindings, state.activeThreadId, state.projects, state.threads]);+ }, [handleNewThread, keybindings, routeThreadId, state.projects, state.threads]);return (<aside className="sidebar flex h-full w-[260px] shrink-0 flex-col border-r border-border bg-card">diff --git a/apps/web/src/router.tsx b/apps/web/src/router.tsxindex 2cedab6cc..84438e650 100644--- a/apps/web/src/router.tsx+++ b/apps/web/src/router.tsx@@ -1,8 +1,15 @@-import { createRouter } from "@tanstack/react-router";+import { createHashHistory, createRouter } from "@tanstack/react-router";+import { isElectron } from "./env";import { routeTree } from "./routeTree.gen";-export const router = createRouter({ routeTree });+const useHashHistory =+ isElectron && typeof window !== "undefined" && window.location.protocol === "file:";++export const router = createRouter({+ routeTree,+ ...(useHashHistory ? { history: createHashHistory() } : {}),+});declare module "@tanstack/react-router" {interface Register {diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsxindex 5306dd4e8..ef8760276 100644--- a/apps/web/src/routes/__root.tsx+++ b/apps/web/src/routes/__root.tsx@@ -67,6 +67,7 @@ function AutoProjectBootstrap() {threadId: existingThread.id,});}+ dispatch({ type: "SET_THREADS_HYDRATED", hydrated: true });return;}@@ -82,12 +83,14 @@ function AutoProjectBootstrap() {cwd: payload.cwd,model: DEFAULT_MODEL,expanded: true,+ scripts: [],},});dispatch({type: "ADD_THREAD",thread: createThread(projectId),});+ dispatch({ type: "SET_THREADS_HYDRATED", hydrated: true });});}, [state.projects, state.threads, state.activeThreadId, dispatch]);@@ -118,8 +121,10 @@ function DesktopProjectBootstrap() {cwd: project.cwd,model: DEFAULT_MODEL,expanded: true,+ scripts: project.scripts,})),});+ dispatch({ type: "SET_THREADS_HYDRATED", hydrated: true });bootstrappedRef.current = true;} catch {if (disposed) return;diff --git a/apps/web/src/routes/_chat.$threadId.tsx b/apps/web/src/routes/_chat.$threadId.tsxindex 7ca462e8b..042fa0d37 100644--- a/apps/web/src/routes/_chat.$threadId.tsx+++ b/apps/web/src/routes/_chat.$threadId.tsx@@ -16,6 +16,10 @@ function ChatThreadRouteView() {return;}+ if (!state.threadsHydrated) {+ return;+ }+if (!threadExists) {void navigate({ to: "/", replace: true });return;@@ -26,9 +30,9 @@ function ChatThreadRouteView() {}dispatch({ type: "SET_ACTIVE_THREAD", threadId });- }, [dispatch, navigate, state.activeThreadId, threadExists, threadId]);+ }, [dispatch, navigate, state.activeThreadId, state.threadsHydrated, threadExists, threadId]);- if (!threadId || !threadExists) {+ if (!threadId || !state.threadsHydrated || !threadExists) {return null;}diff --git a/apps/web/src/routes/_chat.index.tsx b/apps/web/src/routes/_chat.index.tsxindex 5e973a956..d8db92c20 100644--- a/apps/web/src/routes/_chat.index.tsx+++ b/apps/web/src/routes/_chat.index.tsx@@ -4,6 +4,7 @@ import { type FormEvent, useMemo, useState } from "react";import { Button } from "../components/ui/button";import { Textarea } from "../components/ui/textarea";import { createThread } from "../threadFactory";+import { truncateTitle } from "../truncateTitle";import { useStore } from "../store";function ChatIndexRouteView() {@@ -24,11 +25,7 @@ function ChatIndexRouteView() {event.preventDefault();if (!defaultProject) return;- const titleSeed = draft.trim();- const threadTitle =- titleSeed.length > 0- ? (titleSeed.length > 50 ? `${titleSeed.slice(0, 50)}...` : titleSeed)- : undefined;+ const threadTitle = truncateTitle(draft);const thread = createThread(defaultProject.id,threadTitlediff --git a/apps/web/src/store.test.ts b/apps/web/src/store.test.tsindex cbdd5617b..90a3a735e 100644--- a/apps/web/src/store.test.ts+++ b/apps/web/src/store.test.ts@@ -80,6 +80,7 @@ function makeState(thread: Thread): AppState {],threads: [thread],activeThreadId: thread.id,+ threadsHydrated: true,runtimeMode: "full-access",diffOpen: false,diffThreadId: null,@@ -617,6 +618,7 @@ describe("store reducer thread continuity", () => {}),],activeThreadId: "thread-b",+ threadsHydrated: true,runtimeMode: "full-access",diffOpen: true,diffThreadId: "thread-b",@@ -674,6 +676,7 @@ describe("store reducer thread continuity", () => {],threads: [makeThread({ id: "thread-a", projectId: "project-old-a" })],activeThreadId: "thread-a",+ threadsHydrated: true,runtimeMode: "full-access",diffOpen: false,diffThreadId: null,@@ -725,6 +728,16 @@ describe("store reducer thread continuity", () => {]);});+ it("marks threads hydration state", () => {+ const state = makeState(makeThread());+ const next = reducer(state, {+ type: "SET_THREADS_HYDRATED",+ hydrated: false,+ });++ expect(next.threadsHydrated).toBe(false);+ });+it("deletes a project and all of its threads", () => {const state: AppState = {projects: [@@ -756,6 +769,7 @@ describe("store reducer thread continuity", () => {}),],activeThreadId: "thread-a",+ threadsHydrated: true,runtimeMode: "full-access",diffOpen: true,diffThreadId: "thread-a",diff --git a/apps/web/src/store.ts b/apps/web/src/store.tsindex 439368cf9..aa269c058 100644--- a/apps/web/src/store.ts+++ b/apps/web/src/store.ts@@ -38,6 +38,7 @@ type Action =| { type: "ADD_PROJECT"; project: Project }| { type: "SET_PROJECT_SCRIPTS"; projectId: string; scripts: ProjectScript[] }| { type: "SYNC_PROJECTS"; projects: Project[] }+ | { type: "SET_THREADS_HYDRATED"; hydrated: boolean }| { type: "TOGGLE_PROJECT"; projectId: string }| { type: "DELETE_PROJECT"; projectId: string }| { type: "ADD_THREAD"; thread: Thread }@@ -113,6 +114,7 @@ export interface AppState {projects: Project[];threads: Thread[];activeThreadId: string | null;+ threadsHydrated: boolean;runtimeMode: RuntimeMode;diffOpen: boolean;diffThreadId: string | null;@@ -136,6 +138,7 @@ const initialState: AppState = {projects: [],threads: [],activeThreadId: null,+ threadsHydrated: false,runtimeMode: DEFAULT_RUNTIME_MODE,diffOpen: false,diffThreadId: null,@@ -167,6 +170,7 @@ function readPersistedState(): AppState {...hydrated,threads,activeThreadId,+ threadsHydrated: threads.length > 0,diffOpen: false,diffThreadId: null,diffTurnId: null,@@ -593,6 +597,15 @@ export function reducer(state: AppState, action: Action): AppState {),};+ case "SET_THREADS_HYDRATED":+ if (state.threadsHydrated === action.hydrated) {+ return state;+ }+ return {+ ...state,+ threadsHydrated: action.hydrated,+ };+case "SYNC_PROJECTS": {const previousByCwd = new Map(state.projects.map((project) => [project.cwd, project] as const),diff --git a/apps/web/src/truncateTitle.test.ts b/apps/web/src/truncateTitle.test.tsnew file mode 100644index 000000000..d7d61c5da--- /dev/null+++ b/apps/web/src/truncateTitle.test.ts@@ -0,0 +1,17 @@+import { describe, expect, it } from "vitest";++import { truncateTitle } from "./truncateTitle";++describe("truncateTitle", () => {+ it("trims surrounding whitespace", () => {+ expect(truncateTitle(" hello world ")).toBe("hello world");+ });++ it("returns trimmed text when within max length", () => {+ expect(truncateTitle("alpha", 10)).toBe("alpha");+ });++ it("appends ellipsis when text exceeds max length", () => {+ expect(truncateTitle("abcdefghij", 5)).toBe("abcde...");+ });+});diff --git a/apps/web/src/truncateTitle.ts b/apps/web/src/truncateTitle.tsnew file mode 100644index 000000000..bce554528--- /dev/null+++ b/apps/web/src/truncateTitle.ts@@ -0,0 +1,7 @@+export function truncateTitle(text: string, maxLength = 50): string {+ const trimmed = text.trim();+ if (trimmed.length <= maxLength) {+ return trimmed;+ }+ return `${trimmed.slice(0, maxLength)}...`;+}