mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-28 18:36:23 +02:00
Components were calling useSearchParams() at the top level but only reading the value inside useEffect or callbacks, never in JSX. This subscribed the entire component tree to every URL query change. Fix: read from window.location.search directly inside the effect so no React subscription is created. Changes: - new-chat/page.tsx: read commentId inside effect + popstate listener for SPA back/forward support; removes subscription from 1500+ line tree - dashboard/page.tsx: read window.location.search at redirect time; removes searchParams from dep array - public-chat-footer.tsx: one-shot mount read for action=clone param - TokenHandler.tsx: one-shot mount read for token + refresh_token params Implements Vercel React Best Practices Rule: rerender-defer-reads (5.2)
78 lines
2.5 KiB
TypeScript
78 lines
2.5 KiB
TypeScript
"use client";
|
|
|
|
import { Copy } from "lucide-react";
|
|
import { useRouter } from "next/navigation";
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Spinner } from "@/components/ui/spinner";
|
|
import { publicChatApiService } from "@/lib/apis/public-chat-api.service";
|
|
import { getBearerToken } from "@/lib/auth-utils";
|
|
|
|
interface PublicChatFooterProps {
|
|
shareToken: string;
|
|
}
|
|
|
|
export function PublicChatFooter({ shareToken }: PublicChatFooterProps) {
|
|
const router = useRouter();
|
|
const [isCloning, setIsCloning] = useState(false);
|
|
const hasAutoCloned = useRef(false);
|
|
|
|
const triggerClone = useCallback(async () => {
|
|
setIsCloning(true);
|
|
|
|
try {
|
|
const response = await publicChatApiService.clonePublicChat({
|
|
share_token: shareToken,
|
|
});
|
|
|
|
// Redirect to the new chat page with cloned content
|
|
router.push(`/dashboard/${response.search_space_id}/new-chat/${response.thread_id}`);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "Failed to copy chat";
|
|
toast.error(message);
|
|
setIsCloning(false);
|
|
}
|
|
}, [shareToken, router]);
|
|
|
|
// Auto-trigger clone if user just logged in with action=clone.
|
|
// Read from window.location.search on mount — no subscription needed since
|
|
// this is a one-time post-login check. (Vercel Best Practice: rerender-defer-reads 5.2)
|
|
useEffect(() => {
|
|
const action = new URLSearchParams(window.location.search).get("action");
|
|
const token = getBearerToken();
|
|
|
|
// Only auto-clone once, if authenticated and action=clone is present
|
|
if (action === "clone" && token && !hasAutoCloned.current && !isCloning) {
|
|
hasAutoCloned.current = true;
|
|
triggerClone();
|
|
}
|
|
}, [isCloning, triggerClone]);
|
|
|
|
const handleCopyAndContinue = async () => {
|
|
const token = getBearerToken();
|
|
|
|
if (!token) {
|
|
// Include action=clone in the returnUrl so it persists after login
|
|
const returnUrl = encodeURIComponent(`/public/${shareToken}?action=clone`);
|
|
router.push(`/login?returnUrl=${returnUrl}`);
|
|
return;
|
|
}
|
|
|
|
await triggerClone();
|
|
};
|
|
|
|
return (
|
|
<div className="fixed bottom-6 left-1/2 z-50 -translate-x-1/2">
|
|
<Button
|
|
size="lg"
|
|
onClick={handleCopyAndContinue}
|
|
disabled={isCloning}
|
|
className="gap-2 rounded-full px-6 shadow-lg transition-all duration-200 hover:scale-[1.02] hover:shadow-xl hover:brightness-110 hover:bg-primary"
|
|
>
|
|
{isCloning ? <Spinner size="sm" /> : <Copy className="size-4" />}
|
|
Copy and continue this chat
|
|
</Button>
|
|
</div>
|
|
);
|
|
}
|