shelley: fix URL slug not being respected on page reload

Philip Zeyliger created

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.

Change summary

ui/src/App.tsx | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

Detailed changes

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<string | null>(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