"use client"; import { useParams, usePathname, useSearchParams } from "next/navigation"; import { useEffect, useState } from "react"; import { uuid } from "@/core/utils/uuid"; export function useThreadChat() { const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>(); const pathname = usePathname(); const searchParams = useSearchParams(); const xClawUsedFromQuery = searchParams.get("xclaw_used"); const [threadId, setThreadId] = useState(() => { if (threadIdFromPath === "new") { const shouldUseQueryThreadId = pathname.startsWith("/workspace/chats/"); const queryThreadId = shouldUseQueryThreadId && xClawUsedFromQuery === "true" ? searchParams.get("thread_id")?.trim() : undefined; return queryThreadId ?? uuid(); } return threadIdFromPath; }); const [isNewThread, setIsNewThread] = useState( () => threadIdFromPath === "new", ); useEffect(() => { if (pathname.endsWith("/new")) { setIsNewThread(true); const shouldUseQueryThreadId = pathname.startsWith("/workspace/chats/"); const queryThreadId = shouldUseQueryThreadId && xClawUsedFromQuery === "true" ? searchParams.get("thread_id")?.trim() : undefined; setThreadId(queryThreadId ?? uuid()); return; } setIsNewThread(false); setThreadId(threadIdFromPath); }, [pathname, searchParams, threadIdFromPath]); const isMock = searchParams.get("mock") === "true"; return { threadId, isNewThread, setIsNewThread, isMock }; }