From 9068197be4a855a3ac0543e2e06d8c11eba88980 Mon Sep 17 00:00:00 2001 From: Philip Zeyliger Date: Sat, 3 Jan 2026 05:58:00 +0000 Subject: [PATCH] shelley: fix URL slug not being respected on page reload Prompt: In a new worktree, fix the fact that reloading on one conversation with a specific url (that's tied to the xknversstion) ends up going to the most recent conversation. When navigating directly to a URL like /c/my-conversation, the app was incorrectly showing the most recent conversation instead of the one specified in the URL. The issue was a race condition: on initial render, the useEffect that updates the URL based on currentConversationId would run with currentConversationId=null, causing updateUrlWithSlug to change the URL from /c/my-conversation to / before resolveInitialSlug could read it. Fix: Capture the initial slug from the URL at module load time (before any React rendering), and use that captured value in resolveInitialSlug instead of reading from the URL which may have already changed. --- ui/src/App.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ui/src/App.tsx b/ui/src/App.tsx index a45444b88157579df4a8aaae48f25b182d9207ef..3c328b38bd86f8e66b62350b6047486cbce24d76 100644 --- a/ui/src/App.tsx +++ b/ui/src/App.tsx @@ -23,6 +23,10 @@ function getSlugFromPath(): string | null { return null; } +// Capture the initial slug from URL BEFORE React renders, so it won't be affected +// by the useEffect that updates the URL based on current conversation. +const initialSlugFromUrl = getSlugFromPath(); + // Update the URL to reflect the current conversation slug function updateUrlWithSlug(conversation: Conversation | undefined) { const currentSlug = getSlugFromPath(); @@ -61,12 +65,14 @@ function App() { const [error, setError] = useState(null); const initialSlugResolved = useRef(false); - // Resolve initial slug from URL + // Resolve initial slug from URL - uses the captured initialSlugFromUrl const resolveInitialSlug = useCallback(async (convs: Conversation[]) => { if (initialSlugResolved.current) return null; initialSlugResolved.current = true; - const urlSlug = getSlugFromPath(); + // Use the slug captured at module load time, not the current URL + // (which may have been changed by updateUrlWithSlug before this runs) + const urlSlug = initialSlugFromUrl; if (!urlSlug) return null; // First check if we already have this conversation in our list