2026-01-13 00:17:12 -08:00
|
|
|
"use client";
|
|
|
|
|
|
2026-02-04 18:26:38 +02:00
|
|
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
2026-01-20 16:33:42 +05:30
|
|
|
import { useAtomValue, useSetAtom } from "jotai";
|
2026-02-20 02:31:10 +05:30
|
|
|
import { Earth, User, Users } from "lucide-react";
|
2026-02-04 18:26:38 +02:00
|
|
|
import { useParams, useRouter } from "next/navigation";
|
2026-02-02 14:24:59 +02:00
|
|
|
import { useCallback, useMemo, useState } from "react";
|
2026-01-13 00:17:12 -08:00
|
|
|
import { toast } from "sonner";
|
2026-01-20 16:33:42 +05:30
|
|
|
import { currentThreadAtom, setThreadVisibilityAtom } from "@/atoms/chat/current-thread.atom";
|
2026-02-02 14:24:59 +02:00
|
|
|
import { myAccessAtom } from "@/atoms/members/members-query.atoms";
|
2026-02-03 13:32:11 -08:00
|
|
|
import { createPublicChatSnapshotMutationAtom } from "@/atoms/public-chat-snapshots/public-chat-snapshots-mutation.atoms";
|
2026-01-13 00:17:12 -08:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
2026-01-20 17:35:08 +05:30
|
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
2026-02-04 18:26:38 +02:00
|
|
|
import { chatThreadsApiService } from "@/lib/apis/chat-threads-api.service";
|
2026-01-27 13:49:46 +02:00
|
|
|
import {
|
|
|
|
|
type ChatVisibility,
|
|
|
|
|
type ThreadRecord,
|
|
|
|
|
updateThreadVisibility,
|
|
|
|
|
} from "@/lib/chat/thread-persistence";
|
2026-01-13 00:17:12 -08:00
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
interface ChatShareButtonProps {
|
|
|
|
|
thread: ThreadRecord | null;
|
|
|
|
|
onVisibilityChange?: (visibility: ChatVisibility) => void;
|
|
|
|
|
className?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const visibilityOptions: {
|
|
|
|
|
value: ChatVisibility;
|
|
|
|
|
label: string;
|
|
|
|
|
description: string;
|
2026-01-20 16:57:15 +05:30
|
|
|
icon: typeof User;
|
2026-01-13 00:17:12 -08:00
|
|
|
}[] = [
|
|
|
|
|
{
|
|
|
|
|
value: "PRIVATE",
|
|
|
|
|
label: "Private",
|
|
|
|
|
description: "Only you can access this chat",
|
2026-01-20 16:57:15 +05:30
|
|
|
icon: User,
|
2026-01-13 00:17:12 -08:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: "SEARCH_SPACE",
|
|
|
|
|
label: "Search Space",
|
|
|
|
|
description: "All members of this search space can access",
|
|
|
|
|
icon: Users,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export function ChatShareButton({ thread, onVisibilityChange, className }: ChatShareButtonProps) {
|
|
|
|
|
const queryClient = useQueryClient();
|
2026-02-04 18:26:38 +02:00
|
|
|
const router = useRouter();
|
|
|
|
|
const params = useParams();
|
2026-01-13 00:17:12 -08:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
|
2026-01-20 16:33:42 +05:30
|
|
|
// Use Jotai atom for visibility (single source of truth)
|
|
|
|
|
const currentThreadState = useAtomValue(currentThreadAtom);
|
|
|
|
|
const setThreadVisibility = useSetAtom(setThreadVisibilityAtom);
|
|
|
|
|
|
2026-01-30 14:20:06 +02:00
|
|
|
// Snapshot creation mutation
|
|
|
|
|
const { mutateAsync: createSnapshot, isPending: isCreatingSnapshot } = useAtomValue(
|
2026-02-02 16:05:23 +02:00
|
|
|
createPublicChatSnapshotMutationAtom
|
2026-01-26 18:39:59 +02:00
|
|
|
);
|
|
|
|
|
|
2026-02-02 14:24:59 +02:00
|
|
|
// Permission check for public sharing
|
|
|
|
|
const { data: access } = useAtomValue(myAccessAtom);
|
|
|
|
|
const canCreatePublicLink = useMemo(() => {
|
|
|
|
|
if (!access) return false;
|
|
|
|
|
if (access.is_owner) return true;
|
|
|
|
|
return access.permissions?.includes("public_sharing:create") ?? false;
|
|
|
|
|
}, [access]);
|
|
|
|
|
|
2026-02-04 18:26:38 +02:00
|
|
|
// Query to check if thread has public snapshots
|
|
|
|
|
const { data: snapshotsData } = useQuery({
|
|
|
|
|
queryKey: ["thread-snapshots", thread?.id],
|
|
|
|
|
queryFn: () => chatThreadsApiService.listPublicChatSnapshots({ thread_id: thread!.id }),
|
|
|
|
|
enabled: !!thread?.id,
|
|
|
|
|
staleTime: 30000, // Cache for 30 seconds
|
|
|
|
|
});
|
|
|
|
|
const hasPublicSnapshots = (snapshotsData?.snapshots?.length ?? 0) > 0;
|
|
|
|
|
const snapshotCount = snapshotsData?.snapshots?.length ?? 0;
|
|
|
|
|
|
2026-01-20 16:33:42 +05:30
|
|
|
// Use Jotai visibility if available (synced from chat page), otherwise fall back to thread prop
|
|
|
|
|
const currentVisibility = currentThreadState.visibility ?? thread?.visibility ?? "PRIVATE";
|
2026-01-13 00:17:12 -08:00
|
|
|
|
|
|
|
|
const handleVisibilityChange = useCallback(
|
|
|
|
|
async (newVisibility: ChatVisibility) => {
|
|
|
|
|
if (!thread || newVisibility === currentVisibility) {
|
|
|
|
|
setOpen(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 16:33:42 +05:30
|
|
|
// Update Jotai atom immediately for instant UI feedback
|
|
|
|
|
setThreadVisibility(newVisibility);
|
|
|
|
|
|
2026-01-13 00:17:12 -08:00
|
|
|
try {
|
|
|
|
|
await updateThreadVisibility(thread.id, newVisibility);
|
|
|
|
|
|
2026-01-20 16:33:42 +05:30
|
|
|
// Refetch threads list to update sidebar
|
2026-01-13 00:17:12 -08:00
|
|
|
await queryClient.refetchQueries({
|
|
|
|
|
predicate: (query) => Array.isArray(query.queryKey) && query.queryKey[0] === "threads",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onVisibilityChange?.(newVisibility);
|
|
|
|
|
toast.success(
|
|
|
|
|
newVisibility === "SEARCH_SPACE" ? "Chat shared with search space" : "Chat is now private"
|
|
|
|
|
);
|
|
|
|
|
setOpen(false);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Failed to update visibility:", error);
|
2026-01-20 16:33:42 +05:30
|
|
|
// Revert Jotai state on error
|
|
|
|
|
setThreadVisibility(thread.visibility ?? "PRIVATE");
|
2026-01-13 00:17:12 -08:00
|
|
|
toast.error("Failed to update sharing settings");
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-01-20 16:33:42 +05:30
|
|
|
[thread, currentVisibility, onVisibilityChange, queryClient, setThreadVisibility]
|
2026-01-13 00:17:12 -08:00
|
|
|
);
|
|
|
|
|
|
2026-01-30 14:20:06 +02:00
|
|
|
const handleCreatePublicLink = useCallback(async () => {
|
2026-01-26 18:39:59 +02:00
|
|
|
if (!thread) return;
|
|
|
|
|
|
|
|
|
|
try {
|
2026-01-30 14:20:06 +02:00
|
|
|
await createSnapshot({ thread_id: thread.id });
|
2026-02-04 18:26:38 +02:00
|
|
|
// Refetch snapshots to show the globe indicator
|
|
|
|
|
await queryClient.invalidateQueries({ queryKey: ["thread-snapshots", thread.id] });
|
2026-01-30 14:20:06 +02:00
|
|
|
setOpen(false);
|
2026-01-27 13:49:46 +02:00
|
|
|
} catch (error) {
|
2026-01-30 14:20:06 +02:00
|
|
|
console.error("Failed to create public link:", error);
|
2026-01-26 18:39:59 +02:00
|
|
|
}
|
2026-02-04 18:26:38 +02:00
|
|
|
}, [thread, createSnapshot, queryClient]);
|
2026-01-26 18:39:59 +02:00
|
|
|
|
2026-01-13 00:17:12 -08:00
|
|
|
// Don't show if no thread (new chat that hasn't been created yet)
|
|
|
|
|
if (!thread) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 14:20:06 +02:00
|
|
|
const CurrentIcon = currentVisibility === "PRIVATE" ? User : Users;
|
|
|
|
|
const buttonLabel = currentVisibility === "PRIVATE" ? "Private" : "Shared";
|
2026-01-13 00:17:12 -08:00
|
|
|
|
|
|
|
|
return (
|
2026-02-04 18:26:38 +02:00
|
|
|
<div className={cn("flex items-center gap-1", className)}>
|
2026-02-11 22:14:59 +05:30
|
|
|
{/* Globe indicator when public snapshots exist - clicks to settings */}
|
|
|
|
|
{hasPublicSnapshots && (
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-02-22 04:41:56 +05:30
|
|
|
onClick={() =>
|
|
|
|
|
router.push(`/dashboard/${params.search_space_id}/settings?section=public-links`)
|
|
|
|
|
}
|
2026-02-11 22:14:59 +05:30
|
|
|
className="flex items-center justify-center h-8 w-8 rounded-md bg-muted/50 hover:bg-muted transition-colors"
|
|
|
|
|
>
|
2026-02-20 14:28:01 -08:00
|
|
|
<Earth className="h-4 w-4 text-muted-foreground" />
|
2026-02-11 22:14:59 +05:30
|
|
|
</button>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent>
|
|
|
|
|
{snapshotCount === 1
|
|
|
|
|
? "This chat has a public link"
|
|
|
|
|
: `This chat has ${snapshotCount} public links`}
|
|
|
|
|
</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
)}
|
|
|
|
|
|
2026-02-04 18:26:38 +02:00
|
|
|
<Popover open={open} onOpenChange={setOpen}>
|
|
|
|
|
<Tooltip>
|
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<PopoverTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
className="h-8 w-8 md:w-auto md:px-3 md:gap-2 relative bg-muted hover:bg-muted/80 border-0"
|
2026-01-13 00:17:12 -08:00
|
|
|
>
|
2026-02-04 18:26:38 +02:00
|
|
|
<CurrentIcon className="h-4 w-4" />
|
|
|
|
|
<span className="hidden md:inline text-sm">{buttonLabel}</span>
|
|
|
|
|
</Button>
|
|
|
|
|
</PopoverTrigger>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent>Share settings</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
2026-02-06 18:22:19 +05:30
|
|
|
<PopoverContent
|
2026-02-04 18:26:38 +02:00
|
|
|
className="w-[280px] md:w-[320px] p-0 rounded-lg shadow-lg border-border/60"
|
|
|
|
|
align="end"
|
|
|
|
|
sideOffset={8}
|
|
|
|
|
onCloseAutoFocus={(e) => e.preventDefault()}
|
|
|
|
|
>
|
2026-02-22 04:14:07 +05:30
|
|
|
<div className="p-1.5 space-y-1 select-none">
|
2026-02-04 18:26:38 +02:00
|
|
|
{/* Visibility Options */}
|
|
|
|
|
{visibilityOptions.map((option) => {
|
|
|
|
|
const isSelected = currentVisibility === option.value;
|
|
|
|
|
const Icon = option.icon;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
key={option.value}
|
|
|
|
|
onClick={() => handleVisibilityChange(option.value)}
|
2026-01-13 00:17:12 -08:00
|
|
|
className={cn(
|
2026-02-04 18:26:38 +02:00
|
|
|
"w-full flex items-center gap-2.5 px-2.5 py-2 rounded-md transition-all",
|
|
|
|
|
"hover:bg-accent/50 cursor-pointer",
|
|
|
|
|
"focus:outline-none",
|
|
|
|
|
isSelected && "bg-accent/80"
|
2026-01-13 00:17:12 -08:00
|
|
|
)}
|
|
|
|
|
>
|
2026-02-04 18:26:38 +02:00
|
|
|
<div
|
2026-01-13 00:17:12 -08:00
|
|
|
className={cn(
|
2026-02-04 18:26:38 +02:00
|
|
|
"size-7 rounded-md shrink-0 grid place-items-center",
|
|
|
|
|
isSelected ? "bg-primary/10" : "bg-muted"
|
2026-01-13 00:17:12 -08:00
|
|
|
)}
|
2026-02-04 18:26:38 +02:00
|
|
|
>
|
|
|
|
|
<Icon
|
|
|
|
|
className={cn(
|
|
|
|
|
"size-4 block",
|
|
|
|
|
isSelected ? "text-primary" : "text-muted-foreground"
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2026-01-13 00:17:12 -08:00
|
|
|
</div>
|
2026-02-04 18:26:38 +02:00
|
|
|
<div className="flex-1 text-left min-w-0">
|
|
|
|
|
<div className="flex items-center gap-1.5">
|
|
|
|
|
<span className={cn("text-sm font-medium", isSelected && "text-primary")}>
|
|
|
|
|
{option.label}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-xs text-muted-foreground mt-0.5 leading-snug">
|
|
|
|
|
{option.description}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
|
|
|
|
|
{canCreatePublicLink && (
|
|
|
|
|
<>
|
|
|
|
|
{/* Divider */}
|
|
|
|
|
<div className="border-t border-border my-1" />
|
|
|
|
|
|
|
|
|
|
{/* Public Link Option */}
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={handleCreatePublicLink}
|
|
|
|
|
disabled={isCreatingSnapshot}
|
|
|
|
|
className={cn(
|
|
|
|
|
"w-full flex items-center gap-2.5 px-2.5 py-2 rounded-md transition-all",
|
|
|
|
|
"hover:bg-accent/50 cursor-pointer",
|
|
|
|
|
"focus:outline-none",
|
|
|
|
|
"disabled:opacity-50 disabled:cursor-not-allowed"
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<div className="size-7 rounded-md shrink-0 grid place-items-center bg-muted">
|
2026-02-20 02:31:10 +05:30
|
|
|
<Earth className="size-4 block text-muted-foreground" />
|
2026-02-02 14:24:59 +02:00
|
|
|
</div>
|
2026-02-04 18:26:38 +02:00
|
|
|
<div className="flex-1 text-left min-w-0">
|
|
|
|
|
<div className="flex items-center gap-1.5">
|
|
|
|
|
<span className="text-sm font-medium">
|
|
|
|
|
{isCreatingSnapshot ? "Creating link..." : "Create public link"}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-xs text-muted-foreground mt-0.5 leading-snug">
|
|
|
|
|
Creates a shareable snapshot of this chat
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</PopoverContent>
|
|
|
|
|
</Popover>
|
|
|
|
|
</div>
|
2026-01-13 00:17:12 -08:00
|
|
|
);
|
|
|
|
|
}
|