Files
supabase/apps/studio/components/interfaces/ProjectHome/SnippetDropdown.tsx
Jordi EnricandClaude Opus 4.8 6770f1a148 feat(studio): share report snippets with the team when added to homepage report FE-3800 (#47629)
## Problem

Adding a private SQL snippet ('user' visibility) as a Home / project
overview report created a broken experience for other project members,
who saw "SQL snippet not found" because they had no access to the
snippet.

## Fix

Selecting a private snippet from the report block picker now shows a
confirmation step that makes the snippet public to the project before it
is added. Already-shared snippets are added directly, and snippets
created via drag-and-drop onto the report are now shared on creation.

## How to test

- Open a project's homepage and go to the Reports section
- Create a private SQL snippet if you do not have one
- Click "Add block" and select the private snippet
- Confirm a dialog appears explaining the snippet will become visible to
the team
- Confirm, and verify the block is added to the report
- Log in as another project member and confirm the report block renders
instead of "SQL snippet not found"
- Selecting an already-shared snippet should add it without the
confirmation dialog

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Added a “make snippet public” confirmation flow for user-owned report
blocks.
* Updated snippet selection to support a private-snippet share prompt
when appropriate.
* **Bug Fixes**
* Improved duplicate-block handling to prevent adding the same snippet
multiple times.
* **Refactor**
* Refactored SQL snippet upsert payload construction for more consistent
project visibility updates.
* **Tests**
  * Added unit tests covering snippet selection decision logic.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 13:04:36 +02:00

159 lines
5.0 KiB
TypeScript

import { keepPreviousData } from '@tanstack/react-query'
import { useDebounce, useIntersectionObserver } from '@uidotdev/usehooks'
import { Plus } from 'lucide-react'
import { ReactNode, useEffect, useMemo, useRef, useState } from 'react'
import {
Command,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
DropdownMenu,
DropdownMenuContent,
DropdownMenuTrigger,
ScrollArea,
} from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { useContentInfiniteQuery } from '@/data/content/content-infinite-query'
import type { Content } from '@/data/content/content-query'
import { SNIPPET_PAGE_LIMIT } from '@/data/content/sql-folders-query'
import { editorPanelState } from '@/state/editor-panel-state'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
type SnippetDropdownProps = {
projectRef?: string
trigger: ReactNode
side?: 'top' | 'bottom' | 'left' | 'right'
align?: 'start' | 'center' | 'end'
className?: string
autoFocus?: boolean
onSelect: (snippet: { id: string; name: string; visibility: Content['visibility'] }) => void
}
type SqlContentItem = Extract<Content, { type: 'sql' }>
export const SnippetDropdown = ({
projectRef,
trigger,
side = 'bottom',
align = 'end',
className,
autoFocus = false,
onSelect,
}: SnippetDropdownProps) => {
const { openSidebar } = useSidebarManagerSnapshot()
const scrollRootRef = useRef<HTMLDivElement | null>(null)
const [open, setOpen] = useState(false)
const [search, setSearch] = useState('')
const debouncedSearch = useDebounce(search, 500)
const {
data,
isPending: isLoading,
hasNextPage,
fetchNextPage,
isFetchingNextPage,
} = useContentInfiniteQuery(
{
projectRef,
type: 'sql',
limit: SNIPPET_PAGE_LIMIT,
name: search.length === 0 ? search : debouncedSearch,
},
{ placeholderData: keepPreviousData }
)
const snippets = useMemo(() => {
const items = data?.pages.flatMap((page) => page.content) ?? []
return items as SqlContentItem[]
}, [data?.pages])
const [sentinelRef, entry] = useIntersectionObserver({
root: scrollRootRef.current,
threshold: 0,
rootMargin: '0px',
})
useEffect(() => {
if (entry?.isIntersecting && hasNextPage && !isFetchingNextPage && !isLoading) {
fetchNextPage()
}
}, [entry?.isIntersecting, hasNextPage, isFetchingNextPage, isLoading, fetchNextPage])
return (
<DropdownMenu open={open} onOpenChange={setOpen}>
<DropdownMenuTrigger asChild>{trigger}</DropdownMenuTrigger>
<DropdownMenuContent
side={side}
align={align}
className={['w-80 p-0', className].filter(Boolean).join(' ')}
>
<Command shouldFilter={false}>
<CommandInput
showResetIcon
autoFocus={autoFocus}
placeholder="Search snippets..."
value={search}
onValueChange={setSearch}
handleReset={() => setSearch('')}
/>
<CommandList ref={scrollRootRef}>
{isLoading ? (
<p className="text-xs text-center text-foreground-lighter py-3">Loading...</p>
) : search.length > 0 && snippets.length === 0 ? (
<p className="text-xs text-center text-foreground-lighter py-3">No snippets found</p>
) : (
<CommandGroup>
<ScrollArea className={snippets.length > 7 ? 'h-[210px]' : ''}>
{snippets.map((snippet) => (
<CommandItem
key={snippet.id}
value={snippet.id}
onSelect={() =>
onSelect({
id: snippet.id,
name: snippet.name,
visibility: snippet.visibility,
})
}
>
{snippet.name}
</CommandItem>
))}
<div ref={sentinelRef} className="h-1 -mt-1" />
{hasNextPage && (
<div className="px-2 py-1">
<ShimmeringLoader className="py-2" />
</div>
)}
</ScrollArea>
</CommandGroup>
)}
<div className="h-px bg-border-overlay -mx-1" />
<CommandGroup>
<CommandItem
className="cursor-pointer w-full"
onSelect={() => {
setOpen(false)
editorPanelState.openAsNew()
openSidebar(SIDEBAR_KEYS.EDITOR_PANEL)
}}
>
<div className="w-full flex items-center gap-2">
<Plus size={14} strokeWidth={1.5} />
<p>Create snippet</p>
</div>
</CommandItem>
</CommandGroup>
</CommandList>
</Command>
</DropdownMenuContent>
</DropdownMenu>
)
}