refactor: improve error handling in ChatShareButton component by validating thread ID before API call, and update tooltip content for clarity, enhancing user experience and code robustness

This commit is contained in:
Anish Sarkar 2026-03-06 23:37:05 +05:30
parent c1ba3a9b6d
commit 74e8fb1cee

View file

@ -72,12 +72,15 @@ export function ChatShareButton({ thread, onVisibilityChange, className }: ChatS
// Query to check if thread has public snapshots // Query to check if thread has public snapshots
const { data: snapshotsData } = useQuery({ const { data: snapshotsData } = useQuery({
queryKey: ["thread-snapshots", thread?.id], queryKey: ["thread-snapshots", thread?.id],
queryFn: () => chatThreadsApiService.listPublicChatSnapshots({ thread_id: thread!.id }), queryFn: () => {
const id = thread?.id;
if (id == null) throw new Error("Missing thread id");
return chatThreadsApiService.listPublicChatSnapshots({ thread_id: id });
},
enabled: !!thread?.id, enabled: !!thread?.id,
staleTime: 30000, // Cache for 30 seconds staleTime: 30000, // Cache for 30 seconds
}); });
const hasPublicSnapshots = (snapshotsData?.snapshots?.length ?? 0) > 0; const hasPublicSnapshots = (snapshotsData?.snapshots?.length ?? 0) > 0;
const snapshotCount = snapshotsData?.snapshots?.length ?? 0;
// Use Jotai visibility if available (synced from chat page), otherwise fall back to thread prop // Use Jotai visibility if available (synced from chat page), otherwise fall back to thread prop
const currentVisibility = currentThreadState.visibility ?? thread?.visibility ?? "PRIVATE"; const currentVisibility = currentThreadState.visibility ?? thread?.visibility ?? "PRIVATE";
@ -152,11 +155,7 @@ export function ChatShareButton({ thread, onVisibilityChange, className }: ChatS
<Earth className="h-4 w-4 text-muted-foreground" /> <Earth className="h-4 w-4 text-muted-foreground" />
</button> </button>
</TooltipTrigger> </TooltipTrigger>
<TooltipContent> <TooltipContent>Manage public links</TooltipContent>
{snapshotCount === 1
? "This chat has a public link"
: `This chat has ${snapshotCount} public links`}
</TooltipContent>
</Tooltip> </Tooltip>
)} )}