mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-18 23:11:12 +02:00
- Added the ensure_publication function to create and verify the zero_publication if it is missing, ensuring idempotency during database initialization. - Integrated ensure_publication into the create_db_and_tables function to prevent zero-cache crash loops on startup. - Introduced a self-check script to validate the ensure_publication functionality on a create_all-bootstrapped database. - Updated various components to reflect the transition from search space to workspace, including adjustments in imports and routing paths.
76 lines
2.5 KiB
TypeScript
76 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 { useSession } from "@/hooks/use-session";
|
|
import { publicChatApiService } from "@/lib/apis/public-chat-api.service";
|
|
|
|
interface PublicChatFooterProps {
|
|
shareToken: string;
|
|
}
|
|
|
|
export function PublicChatFooter({ shareToken }: PublicChatFooterProps) {
|
|
const router = useRouter();
|
|
const session = useSession();
|
|
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.workspace_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");
|
|
|
|
// Only auto-clone once, if authenticated and action=clone is present
|
|
if (action === "clone" && session.authenticated && !hasAutoCloned.current && !isCloning) {
|
|
hasAutoCloned.current = true;
|
|
triggerClone();
|
|
}
|
|
}, [isCloning, session.authenticated, triggerClone]);
|
|
|
|
const handleCopyAndContinue = async () => {
|
|
if (!session.authenticated) {
|
|
// 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-al select-none 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>
|
|
);
|
|
}
|